1 #include <malloc.h> 2 #include "malloc_impl.h" 3 #include "malloc_config.h" 4 #ifdef MALLOC_RED_ZONE 5 #include "atomic.h" 6 #endif 7 8 #ifdef USE_JEMALLOC 9 extern size_t je_malloc_usable_size(void *p); 10 #endif 11 12 hidden void *(*const __realloc_dep)(void *, size_t) = realloc; 13 14 #ifdef HOOK_ENABLE __libc_malloc_usable_size(void * p)15size_t __libc_malloc_usable_size(void* p) 16 #else 17 size_t malloc_usable_size(void *p) 18 #endif 19 { 20 return internal_malloc_usable_size(p); 21 } 22 internal_malloc_usable_size(void * p)23size_t internal_malloc_usable_size(void* p) 24 { 25 #ifdef USE_JEMALLOC 26 return je_malloc_usable_size(p); 27 #endif 28 29 #ifndef MALLOC_RED_ZONE 30 return p ? CHUNK_SIZE(MEM_TO_CHUNK(p)) - OVERHEAD : 0; 31 #else 32 struct chunk *c; 33 34 if (!p) { 35 return 0; 36 } 37 38 c = MEM_TO_CHUNK(p); 39 if (chunk_checksum_check(c)) { 40 a_crash(); 41 } 42 if (!(c->state & M_STATE_USED)) { 43 return 0; 44 } 45 return c->usize; 46 #endif 47 } 48