Changeset View
Changeset View
Standalone View
Standalone View
intern/guardedalloc/intern/mallocn_guarded_impl.c
| Show First 20 Lines • Show All 619 Lines • ▼ Show 20 Lines | void *MEM_guarded_mapallocN(size_t len, const char *str) | ||||
| /* on 64 bit, simply use calloc instead, as mmap does not support | /* on 64 bit, simply use calloc instead, as mmap does not support | ||||
| * allocating > 4 GB on Windows. the only reason mapalloc exists | * allocating > 4 GB on Windows. the only reason mapalloc exists | ||||
| * is to get around address space limitations in 32 bit OSes. */ | * is to get around address space limitations in 32 bit OSes. */ | ||||
| if (sizeof(void *) >= 8) | if (sizeof(void *) >= 8) | ||||
| return MEM_guarded_callocN(len, str); | return MEM_guarded_callocN(len, str); | ||||
| len = SIZET_ALIGN_4(len); | len = SIZET_ALIGN_4(len); | ||||
| #if defined(WIN32) | memh = MEM_mmap(NULL, len + sizeof(MemHead) + sizeof(MemTail), | ||||
| /* our windows mmap implementation is not thread safe */ | |||||
| mem_lock_thread(); | |||||
| #endif | |||||
| memh = mmap(NULL, len + sizeof(MemHead) + sizeof(MemTail), | |||||
| PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); | ||||
| #if defined(WIN32) | |||||
| mem_unlock_thread(); | |||||
| #endif | |||||
| if (memh != (MemHead *)-1) { | if (memh != (MemHead *)-1) { | ||||
| make_memhead_header(memh, len, str); | make_memhead_header(memh, len, str); | ||||
| memh->mmap = 1; | memh->mmap = 1; | ||||
| atomic_add_z(&mmap_in_use, len); | atomic_add_z(&mmap_in_use, len); | ||||
| mem_lock_thread(); | mem_lock_thread(); | ||||
| peak_mem = mmap_in_use > peak_mem ? mmap_in_use : peak_mem; | peak_mem = mmap_in_use > peak_mem ? mmap_in_use : peak_mem; | ||||
| mem_unlock_thread(); | mem_unlock_thread(); | ||||
| #ifdef DEBUG_MEMCOUNTER | #ifdef DEBUG_MEMCOUNTER | ||||
| if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL) | if (_mallocn_count == DEBUG_MEMCOUNTER_ERROR_VAL) | ||||
| memcount_raise(__func__); | memcount_raise(__func__); | ||||
| memh->_count = _mallocn_count++; | memh->_count = _mallocn_count++; | ||||
| #endif | #endif | ||||
| return (++memh); | return (++memh); | ||||
| } | } | ||||
| else { | else { | ||||
| print_error("Mapalloc returns null, fallback to regular malloc: " | print_error("Mapalloc returns null, fallback to regular malloc: " | ||||
| "len=" SIZET_FORMAT " in %s, total %u\n", | "len=" SIZET_FORMAT " in %s, total %u\n", | ||||
| SIZET_ARG(len), str, (unsigned int) mmap_in_use); | SIZET_ARG(len), str, (unsigned int) mmap_in_use); | ||||
| return MEM_guarded_callocN(len, str); | return MEM_guarded_callocN(len, str); | ||||
| } | } | ||||
| } | } | ||||
| #ifdef _WIN32 | |||||
| void *MEM_guarded_mmap(void *start, size_t len, int prot, int flags, int fd, off_t offset) | |||||
| { | |||||
| void *ret; | |||||
| mem_lock_thread(); | |||||
| ret = mmap(start, len, prot, flags, fd, offset); | |||||
| mem_unlock_thread(); | |||||
| return ret; | |||||
| } | |||||
| int MEM_guarded_munmap(void *ptr, size_t len) | |||||
| { | |||||
| int ret; | |||||
| mem_lock_thread(); | |||||
| ret = munmap(ptr, len); | |||||
| mem_unlock_thread(); | |||||
| return ret; | |||||
| } | |||||
| #endif | |||||
| /* Memory statistics print */ | /* Memory statistics print */ | ||||
| typedef struct MemPrintBlock { | typedef struct MemPrintBlock { | ||||
| const char *name; | const char *name; | ||||
| uintptr_t len; | uintptr_t len; | ||||
| int items; | int items; | ||||
| } MemPrintBlock; | } MemPrintBlock; | ||||
| static int compare_name(const void *p1, const void *p2) | static int compare_name(const void *p1, const void *p2) | ||||
| ▲ Show 20 Lines • Show All 344 Lines • ▼ Show 20 Lines | |||||
| #ifdef DEBUG_MEMDUPLINAME | #ifdef DEBUG_MEMDUPLINAME | ||||
| if (memh->need_free_name) | if (memh->need_free_name) | ||||
| free((char *) memh->name); | free((char *) memh->name); | ||||
| #endif | #endif | ||||
| if (memh->mmap) { | if (memh->mmap) { | ||||
| atomic_sub_z(&mmap_in_use, memh->len); | atomic_sub_z(&mmap_in_use, memh->len); | ||||
| #if defined(WIN32) | if (MEM_munmap(memh, memh->len + sizeof(MemHead) + sizeof(MemTail))) | ||||
| /* our windows mmap implementation is not thread safe */ | |||||
| mem_lock_thread(); | |||||
| #endif | |||||
| if (munmap(memh, memh->len + sizeof(MemHead) + sizeof(MemTail))) | |||||
| printf("Couldn't unmap memory %s\n", memh->name); | printf("Couldn't unmap memory %s\n", memh->name); | ||||
| #if defined(WIN32) | |||||
| mem_unlock_thread(); | |||||
| #endif | |||||
| } | } | ||||
| else { | else { | ||||
| if (UNLIKELY(malloc_debug_memset && memh->len)) | if (UNLIKELY(malloc_debug_memset && memh->len)) | ||||
| memset(memh + 1, 255, memh->len); | memset(memh + 1, 255, memh->len); | ||||
| if (LIKELY(memh->alignment == 0)) { | if (LIKELY(memh->alignment == 0)) { | ||||
| free(memh); | free(memh); | ||||
| } | } | ||||
| else { | else { | ||||
| ▲ Show 20 Lines • Show All 166 Lines • Show Last 20 Lines | |||||