malloc_usable_size.c 362 B

1234567891011121314
  1. /*
  2. malloc_usable_size - fully inspired by musl implementation
  3. */
  4. #include "malloc.h"
  5. /* for malloc_usable_size */
  6. #define OVERHEAD (2*sizeof(size_t))
  7. #define CHUNK_SIZE(c) ((c)->size & -2)
  8. #define MEM_TO_CHUNK(p) (struct malloc_chunk *)((char *)(p) - OVERHEAD)
  9. size_t malloc_usable_size(void *p) {
  10. return p ? CHUNK_SIZE(MEM_TO_CHUNK(p)) - OVERHEAD : 0;
  11. }