void *MEM_mallocN(size_t len, const char *str) { MemHead *memh; mem_lock_thread(); len = (len + 3 ) & ~3; /* allocate in units of 4 */ memh= (MemHead *)malloc(len+sizeof(MemHead)+sizeof(MemTail)); if(memh) { make_memhead_header(memh, len, str); mem_unlock_thread(); if(malloc_debug_memset && len) memset(memh+1, 255, len); #ifdef DEBUG_MEMCOUNTER if(_mallocn_count==DEBUG_MEMCOUNTER_ERROR_VAL) memcount_raise("MEM_mallocN"); memh->_count= _mallocn_count++; #endif return (++memh); } mem_unlock_thread(); print_error("Malloc returns null: len=" SIZET_FORMAT " in %s, total %u\n", SIZET_ARG(len), str, mem_in_use); return NULL; }
/* note; mmap returns zero'd memory */ void *MEM_mapallocN(size_t len, const char *str) { MemHead *memh; mem_lock_thread(); len = (len + 3 ) & ~3; /* allocate in units of 4 */ memh= mmap(NULL, len+sizeof(MemHead)+sizeof(MemTail), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0); if(memh!=(MemHead *)-1) { make_memhead_header(memh, len, str); memh->mmap= 1; mmap_in_use += len; peak_mem = mmap_in_use > peak_mem ? mmap_in_use : peak_mem; mem_unlock_thread(); #ifdef DEBUG_MEMCOUNTER if(_mallocn_count==DEBUG_MEMCOUNTER_ERROR_VAL) memcount_raise("MEM_mapallocN"); memh->_count= _mallocn_count++; #endif return (++memh); } else { mem_unlock_thread(); print_error("Mapalloc returns null, fallback to regular malloc: len=" SIZET_FORMAT " in %s, total %u\n", SIZET_ARG(len), str, mmap_in_use); return MEM_callocN(len, str); } }