summaryrefslogblamecommitdiff
path: root/src/malloc/malloc_usable_size.c
blob: 6743ea7752a0c44305b009c80ad7b23d153a5ac0 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                                
                                                              
 
#include <malloc.h>

void *(*const __realloc_dep)(void *, size_t) = realloc;

struct chunk {
	size_t psize, csize;
	struct chunk *next, *prev;
};

#define OVERHEAD (2*sizeof(size_t))
#define CHUNK_SIZE(c) ((c)->csize & -2)
#define MEM_TO_CHUNK(p) (struct chunk *)((char *)(p) - OVERHEAD)

size_t malloc_usable_size(void *p)
{
	return p ? CHUNK_SIZE(MEM_TO_CHUNK(p)) - OVERHEAD : 0;
}