forked from enlightenment/efl
efl mem - every mmap anon - allow envv ar to turn it off to be malloc
helps with memory debug to use libc mallocs/callocs etc. set EFL_NO_MMAP_ANON env var to anything to stop anon mmaps
This commit is contained in:
parent
ab02b4fa46
commit
14946e9cf4
|
@ -54,6 +54,8 @@ static int chunk2_num = 0;
|
|||
static int chunk3_size = 0;
|
||||
static int chunk3_num = 0;
|
||||
|
||||
static int no_anon = -1;
|
||||
|
||||
// get a new chunk of "anonymous mmaped memory"
|
||||
static void *
|
||||
_eina_debug_chunk_need(int size)
|
||||
|
@ -64,11 +66,20 @@ _eina_debug_chunk_need(int size)
|
|||
if (RUNNING_ON_VALGRIND) ptr = malloc(size);
|
||||
else
|
||||
#endif
|
||||
{
|
||||
if (no_anon == -1)
|
||||
{
|
||||
if (getenv("EFL_NO_MMAP_ANON")) no_anon = 1;
|
||||
else no_anon = 0;
|
||||
}
|
||||
if (no_anon == 1) ptr = malloc(size);
|
||||
else
|
||||
{
|
||||
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANON, -1, 0);
|
||||
if (ptr == MAP_FAILED) return NULL;
|
||||
}
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
@ -80,7 +91,10 @@ _eina_debug_chunk_noneed(void *ptr, int size)
|
|||
if (RUNNING_ON_VALGRIND) free(ptr);
|
||||
else
|
||||
#endif
|
||||
munmap(ptr, size);
|
||||
{
|
||||
if (no_anon == 1) free(ptr);
|
||||
else munmap(ptr, size);
|
||||
}
|
||||
}
|
||||
|
||||
// push a new bit of mem on our growing stack of mem - given our workload,
|
||||
|
|
|
@ -94,6 +94,8 @@ get_time(void)
|
|||
#endif
|
||||
}
|
||||
|
||||
static int no_anon = -1;
|
||||
|
||||
static void
|
||||
alloc_buf(Eina_Evlog_Buf *b, unsigned int size)
|
||||
{
|
||||
|
@ -105,11 +107,20 @@ alloc_buf(Eina_Evlog_Buf *b, unsigned int size)
|
|||
if (RUNNING_ON_VALGRIND) b->buf = malloc(size);
|
||||
else
|
||||
# endif
|
||||
{
|
||||
if (no_anon == -1)
|
||||
{
|
||||
if (getenv("EFL_NO_MMAP_ANON")) no_anon = 1;
|
||||
else no_anon = 0;
|
||||
}
|
||||
if (no_anon == 1) b->buf = malloc(size);
|
||||
else
|
||||
{
|
||||
b->buf = mmap(NULL, size, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANON, -1, 0);
|
||||
if (b->buf == MAP_FAILED) b->buf = NULL;
|
||||
}
|
||||
}
|
||||
#else
|
||||
b->buf = malloc(size);
|
||||
#endif
|
||||
|
@ -125,7 +136,10 @@ free_buf(Eina_Evlog_Buf *b)
|
|||
if (RUNNING_ON_VALGRIND) free(b->buf);
|
||||
else
|
||||
# endif
|
||||
munmap(b->buf, b->size);
|
||||
{
|
||||
if (no_anon == 1) free(b->buf);
|
||||
else munmap(b->buf, b->size);
|
||||
}
|
||||
#else
|
||||
free(b->buf);
|
||||
#endif
|
||||
|
|
|
@ -72,6 +72,8 @@ static Eina_Spinlock sl;
|
|||
#define MEM_PAGE_SIZE 4096
|
||||
#define SAFEPOINTER_MAGIC 0x7DEADC03
|
||||
|
||||
static int no_anon = -1;
|
||||
|
||||
static void *
|
||||
_eina_safepointer_calloc(int number, size_t size)
|
||||
{
|
||||
|
@ -89,6 +91,14 @@ _eina_safepointer_calloc(int number, size_t size)
|
|||
(size % MEM_PAGE_SIZE ? 1 : 0))
|
||||
* MEM_PAGE_SIZE;
|
||||
|
||||
if (no_anon == -1)
|
||||
{
|
||||
if (getenv("EFL_NO_MMAP_ANON")) no_anon = 1;
|
||||
else no_anon = 0;
|
||||
}
|
||||
if (no_anon == 1) header = calloc(number, size);
|
||||
else
|
||||
{
|
||||
header = mmap(NULL, newsize, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANON, -1, 0);
|
||||
if (header == MAP_FAILED)
|
||||
|
@ -96,6 +106,7 @@ _eina_safepointer_calloc(int number, size_t size)
|
|||
ERR("mmap of Eina_Safepointer table region failed.");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
header->size = newsize;
|
||||
EINA_MAGIC_SET(header, SAFEPOINTER_MAGIC);
|
||||
|
@ -120,13 +131,16 @@ _eina_safepointer_free(void *pointer)
|
|||
|
||||
if (!pointer) return;
|
||||
|
||||
if (no_anon == 1) free((void *)((uintptr_t) pointer & ~0x3));
|
||||
else
|
||||
{
|
||||
header = (Eina_Memory_Header*)(pointer) - 1;
|
||||
if (!EINA_MAGIC_CHECK(header, SAFEPOINTER_MAGIC))
|
||||
EINA_MAGIC_FAIL(header, SAFEPOINTER_MAGIC);
|
||||
|
||||
EINA_MAGIC_SET(header, 0);
|
||||
munmap(header, header->size);
|
||||
}
|
||||
}
|
||||
#else
|
||||
free((void *)((uintptr_t) pointer & ~0x3));
|
||||
#endif
|
||||
|
|
|
@ -1488,6 +1488,8 @@ eo_class_free(_Efl_Class *klass)
|
|||
eina_freeq_ptr_main_add(klass, free, 0);
|
||||
}
|
||||
|
||||
int _eo_no_anon = -1;
|
||||
|
||||
static inline void
|
||||
_eo_classes_release(void)
|
||||
{
|
||||
|
@ -1496,12 +1498,16 @@ _eo_classes_release(void)
|
|||
if (RUNNING_ON_VALGRIND) free(_eo_classes);
|
||||
else
|
||||
# endif
|
||||
{
|
||||
if (_eo_no_anon == 1) free(_eo_classes);
|
||||
else
|
||||
{
|
||||
size_t size;
|
||||
|
||||
size = _eo_classes_alloc * sizeof(_Efl_Class *);
|
||||
if (_eo_classes) munmap(_eo_classes, size);
|
||||
}
|
||||
}
|
||||
#else
|
||||
free(_eo_classes);
|
||||
#endif
|
||||
|
@ -1534,6 +1540,24 @@ _eo_classes_expand(void)
|
|||
}
|
||||
else
|
||||
# endif
|
||||
{
|
||||
if (_eo_no_anon == -1)
|
||||
{
|
||||
if (getenv("EFL_NO_MMAP_ANON")) _eo_no_anon = 1;
|
||||
else _eo_no_anon = 0;
|
||||
}
|
||||
if (_eo_no_anon == 1)
|
||||
{
|
||||
_eo_classes_alloc += 128;
|
||||
newsize = _eo_classes_alloc * sizeof(_Efl_Class *);
|
||||
ptr = realloc(_eo_classes, newsize);
|
||||
if (!ptr)
|
||||
{
|
||||
ERR("realloc of eo class table region faile!!");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_eo_classes_alloc += (MEM_PAGE_SIZE / sizeof(_Efl_Class *));
|
||||
newsize = _eo_classes_alloc * sizeof(_Efl_Class *);
|
||||
|
@ -1547,6 +1571,7 @@ _eo_classes_expand(void)
|
|||
if (psize > 0) memcpy(ptr, _eo_classes, psize);
|
||||
if (_eo_classes) munmap(_eo_classes, psize);
|
||||
}
|
||||
}
|
||||
#else
|
||||
_eo_classes_alloc += 128;
|
||||
newsize = _eo_classes_alloc * sizeof(_Efl_Class *);
|
||||
|
|
|
@ -41,6 +41,14 @@ _eo_call_stack_mem_alloc(size_t size)
|
|||
if (RUNNING_ON_VALGRIND) return calloc(1, size);
|
||||
else
|
||||
# endif
|
||||
{
|
||||
if (_eo_no_anon == -1)
|
||||
{
|
||||
if (getenv("EFL_NO_MMAP_ANON")) _eo_no_anon = 1;
|
||||
else _eo_no_anon = 0;
|
||||
}
|
||||
if (_eo_no_anon == 1) return calloc(1, size);
|
||||
else
|
||||
{
|
||||
// allocate eo call stack via mmped anon segment if on linux - more
|
||||
// secure and safe. also gives page aligned memory allowing madvise
|
||||
|
@ -57,6 +65,7 @@ _eo_call_stack_mem_alloc(size_t size)
|
|||
}
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
#else
|
||||
//in regular cases just use malloc
|
||||
return calloc(1, size);
|
||||
|
@ -71,7 +80,10 @@ _eo_call_stack_mem_free(void *ptr, size_t size)
|
|||
if (RUNNING_ON_VALGRIND) free(ptr);
|
||||
else
|
||||
# endif
|
||||
munmap(ptr, size);
|
||||
{
|
||||
if (_eo_no_anon == 1) free(ptr);
|
||||
else munmap(ptr, size);
|
||||
}
|
||||
#else
|
||||
(void) size;
|
||||
free(ptr);
|
||||
|
|
|
@ -153,6 +153,8 @@ typedef struct _Mem_Header
|
|||
size_t magic;
|
||||
} Mem_Header;
|
||||
|
||||
extern int _eo_no_anon;
|
||||
|
||||
static void *
|
||||
_eo_id_mem_alloc(size_t size)
|
||||
{
|
||||
|
@ -161,6 +163,14 @@ _eo_id_mem_alloc(size_t size)
|
|||
if (RUNNING_ON_VALGRIND) return malloc(size);
|
||||
else
|
||||
# endif
|
||||
{
|
||||
if (_eo_no_anon == -1)
|
||||
{
|
||||
if (getenv("EFL_NO_MMAP_ANON")) _eo_no_anon = 1;
|
||||
else _eo_no_anon = 0;
|
||||
}
|
||||
if (_eo_no_anon == 1) return malloc(size);
|
||||
else
|
||||
{
|
||||
void *ptr;
|
||||
Mem_Header *hdr;
|
||||
|
@ -180,6 +190,7 @@ _eo_id_mem_alloc(size_t size)
|
|||
/* DBG("asked:%lu allocated:%lu wasted:%lu bytes", size, newsize, (newsize - size)); */
|
||||
return (void *)(((unsigned char *)ptr) + MEM_HEADER_SIZE);
|
||||
}
|
||||
}
|
||||
#else
|
||||
return malloc(size);
|
||||
#endif
|
||||
|
@ -202,6 +213,9 @@ _eo_id_mem_free(void *ptr)
|
|||
if (RUNNING_ON_VALGRIND) free(ptr);
|
||||
else
|
||||
# endif
|
||||
{
|
||||
if (_eo_no_anon == 1) free(ptr);
|
||||
else
|
||||
{
|
||||
Mem_Header *hdr;
|
||||
if (!ptr) return;
|
||||
|
@ -213,6 +227,7 @@ _eo_id_mem_free(void *ptr)
|
|||
}
|
||||
munmap(hdr, hdr->size);
|
||||
}
|
||||
}
|
||||
#else
|
||||
free(ptr);
|
||||
#endif
|
||||
|
|
|
@ -102,12 +102,16 @@ _evas_common_rgba_image_surface_size(unsigned int w, unsigned int h,
|
|||
#endif
|
||||
|
||||
if (EINA_UNLIKELY(evas_image_no_mmap == -1))
|
||||
{
|
||||
if (getenv("EFL_NO_MMAP_ANON")) evas_image_no_mmap = 1;
|
||||
else
|
||||
{
|
||||
const char *s = getenv("EVAS_IMAGE_NO_MMAP");
|
||||
evas_image_no_mmap = s && (atoi(s));
|
||||
if (evas_image_no_mmap)
|
||||
WRN("EVAS_IMAGE_NO_MMAP is set, use this only for debugging!");
|
||||
}
|
||||
}
|
||||
|
||||
switch (cspace)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue