From ae96781efd37c63dda16175258480d9ad5919418 Mon Sep 17 00:00:00 2001 From: Miles Bader Date: Tue, 30 Jul 2002 09:20:31 +0000 Subject: Add macros to abstract the malloc header format a bit. --- libc/stdlib/malloc/malloc.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'libc') 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 */ @@ -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 -- cgit v1.2.3