diff options
-rw-r--r-- | libc/stdlib/malloc/malloc.h | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/libc/stdlib/malloc/malloc.h b/libc/stdlib/malloc/malloc.h index f01484368..f08f4bf1b 100644 --- a/libc/stdlib/malloc/malloc.h +++ b/libc/stdlib/malloc/malloc.h @@ -7,7 +7,7 @@ * This file is subject to the terms and conditions of the GNU Lesser * General Public License. See the file COPYING.LIB in the main * directory of this archive for more details. - * + * * Written by Miles Bader <miles@gnu.org> */ @@ -42,6 +42,27 @@ #endif +/* The size of a malloc allocation is stored in a size_t word + MALLOC_ALIGNMENT bytes prior to the start address of the allocation: + + +--------+---------+-------------------+ + | SIZE |(unused) | allocation ... | + +--------+---------+-------------------+ + ^ BASE ^ ADDR + ^ ADDR - MALLOC_ALIGN +*/ + +/* Return base-address of a malloc allocation, given the user address. */ +#define MALLOC_BASE(addr) ((void *)((char *)addr - MALLOC_ALIGNMENT)) +/* Return the size of a malloc allocation, given the user address. */ +#define MALLOC_SIZE(addr) (*(size_t *)MALLOC_BASE(addr)) + +/* Return the user address of a malloc allocation, given the base address. */ +#define MALLOC_ADDR(base) ((void *)((char *)base + MALLOC_ALIGNMENT)) +/* Sets the size of a malloc allocation, given the base address. */ +#define MALLOC_SET_SIZE(base, size) (*(size_t *)(base) = (size)) + + #ifdef __UCLIBC_HAS_THREADS__ # include <pthread.h> |