Changeset View
Changeset View
Standalone View
Standalone View
intern/guardedalloc/intern/mallocn_lockfree_impl.c
| Show First 20 Lines • Show All 141 Lines • ▼ Show 20 Lines | #endif | ||||
| return; | return; | ||||
| } | } | ||||
| atomic_sub_u(&totblock, 1); | atomic_sub_u(&totblock, 1); | ||||
| atomic_sub_z(&mem_in_use, len); | atomic_sub_z(&mem_in_use, len); | ||||
| if (MEMHEAD_IS_MMAP(memh)) { | if (MEMHEAD_IS_MMAP(memh)) { | ||||
| atomic_sub_z(&mmap_in_use, len); | atomic_sub_z(&mmap_in_use, len); | ||||
| #if defined(WIN32) | if (MEM_munmap(memh, len + sizeof(MemHead))) | ||||
| /* our windows mmap implementation is not thread safe */ | |||||
| mem_lock_thread(); | |||||
| #endif | |||||
| if (munmap(memh, len + sizeof(MemHead))) | |||||
| printf("Couldn't unmap memory\n"); | printf("Couldn't unmap memory\n"); | ||||
| #if defined(WIN32) | |||||
| mem_unlock_thread(); | |||||
| #endif | |||||
| } | } | ||||
| else { | else { | ||||
| if (UNLIKELY(malloc_debug_memset && len)) { | if (UNLIKELY(malloc_debug_memset && len)) { | ||||
| memset(memh + 1, 255, len); | memset(memh + 1, 255, len); | ||||
| } | } | ||||
| if (UNLIKELY(MEMHEAD_IS_ALIGNED(memh))) { | if (UNLIKELY(MEMHEAD_IS_ALIGNED(memh))) { | ||||
| MemHeadAligned *memh_aligned = MEMHEAD_ALIGNED_FROM_PTR(vmemh); | MemHeadAligned *memh_aligned = MEMHEAD_ALIGNED_FROM_PTR(vmemh); | ||||
| aligned_free(MEMHEAD_REAL_PTR(memh_aligned)); | aligned_free(MEMHEAD_REAL_PTR(memh_aligned)); | ||||
| ▲ Show 20 Lines • Show All 212 Lines • ▼ Show 20 Lines | void *MEM_lockfree_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_lockfree_callocN(len, str); | return MEM_lockfree_callocN(len, str); | ||||
| len = SIZET_ALIGN_4(len); | len = SIZET_ALIGN_4(len); | ||||
| #if defined(WIN32) | memh = MEM_mmap(NULL, len + sizeof(MemHead), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); | ||||
| /* our windows mmap implementation is not thread safe */ | |||||
| mem_lock_thread(); | |||||
| #endif | |||||
| memh = mmap(NULL, len + sizeof(MemHead), | |||||
| 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) { | ||||
| memh->len = len | (size_t) MEMHEAD_MMAP_FLAG; | memh->len = len | (size_t) MEMHEAD_MMAP_FLAG; | ||||
| atomic_add_u(&totblock, 1); | atomic_add_u(&totblock, 1); | ||||
| atomic_add_z(&mem_in_use, len); | atomic_add_z(&mem_in_use, len); | ||||
| atomic_add_z(&mmap_in_use, len); | atomic_add_z(&mmap_in_use, len); | ||||
| update_maximum(&peak_mem, mem_in_use); | update_maximum(&peak_mem, mem_in_use); | ||||
| update_maximum(&peak_mem, mmap_in_use); | update_maximum(&peak_mem, mmap_in_use); | ||||
| return PTR_FROM_MEMHEAD(memh); | return PTR_FROM_MEMHEAD(memh); | ||||
| } | } | ||||
| 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_lockfree_callocN(len, str); | return MEM_lockfree_callocN(len, str); | ||||
| } | } | ||||
| #ifdef _WIN32 | |||||
| void *MEM_lockfree_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_lockfree_munmap(void *ptr, size_t len) | |||||
| { | |||||
| int ret; | |||||
| mem_lock_thread(); | |||||
| ret = munmap(ptr, len); | |||||
| mem_unlock_thread(); | |||||
| return ret; | |||||
| } | |||||
| #endif | |||||
| void MEM_lockfree_printmemlist_pydict(void) | void MEM_lockfree_printmemlist_pydict(void) | ||||
| { | { | ||||
| } | } | ||||
| void MEM_lockfree_printmemlist(void) | void MEM_lockfree_printmemlist(void) | ||||
| { | { | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 78 Lines • Show Last 20 Lines | |||||