diff --git a/include/malloc.h b/include/malloc.h index e69de29b..35f8b19c 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -0,0 +1,25 @@ +#ifndef _MALLOC_H +#define _MALLOC_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t + +#include + +void *malloc (size_t); +void *calloc (size_t, size_t); +void *realloc (void *, size_t); +void free (void *); +void *valloc (size_t); +void *memalign(size_t, size_t); + +size_t malloc_usable_size(void *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/malloc/malloc_usable_size.c b/src/malloc/malloc_usable_size.c new file mode 100644 index 00000000..8cccd9d8 --- /dev/null +++ b/src/malloc/malloc_usable_size.c @@ -0,0 +1,17 @@ +#include + +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 CHUNK_SIZE(MEM_TO_CHUNK(p)) - OVERHEAD; +}