static void free_check (void *mem, const void *caller) { mchunkptr p; if (!mem) return; (void) mutex_lock (&main_arena.mutex); p = mem2chunk_check (mem, NULL); if (!p) { (void) mutex_unlock (&main_arena.mutex); malloc_printerr (check_action, "free(): invalid pointer", mem); return; } if (chunk_is_mmapped (p)) { (void) mutex_unlock (&main_arena.mutex); munmap_chunk (p); return; } _int_free (&main_arena, p, 1); (void) mutex_unlock (&main_arena.mutex); }
static void* realloc_check(void* oldmem, size_t bytes, const void *caller) { INTERNAL_SIZE_T nb; void* newmem = 0; unsigned char *magic_p; if (bytes+1 == 0) { __set_errno (ENOMEM); return NULL; } if (oldmem == 0) return malloc_check(bytes, NULL); if (bytes == 0) { free_check (oldmem, NULL); return NULL; } (void)mutex_lock(&main_arena.mutex); const mchunkptr oldp = mem2chunk_check(oldmem, &magic_p); (void)mutex_unlock(&main_arena.mutex); if(!oldp) { malloc_printerr(check_action, "realloc(): invalid pointer", oldmem); return malloc_check(bytes, NULL); } const INTERNAL_SIZE_T oldsize = chunksize(oldp); checked_request2size(bytes+1, nb); (void)mutex_lock(&main_arena.mutex); if (chunk_is_mmapped(oldp)) { #if HAVE_MREMAP mchunkptr newp = mremap_chunk(oldp, nb); if(newp) newmem = chunk2mem(newp); else #endif { /* Note the extra SIZE_SZ overhead. */ if(oldsize - SIZE_SZ >= nb) newmem = oldmem; /* do nothing */ else { /* Must alloc, copy, free. */ if (top_check() >= 0) newmem = _int_malloc(&main_arena, bytes+1); if (newmem) { MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ); munmap_chunk(oldp); } } } } else { if (top_check() >= 0) { INTERNAL_SIZE_T nb; checked_request2size(bytes + 1, nb); newmem = _int_realloc(&main_arena, oldp, oldsize, nb); } } /* mem2chunk_check changed the magic byte in the old chunk. If newmem is NULL, then the old chunk will still be used though, so we need to invert that change here. */ if (newmem == NULL) *magic_p ^= 0xFF; (void)mutex_unlock(&main_arena.mutex); return mem2mem_check(newmem, bytes); }