From 2d4b4cafe62b43815806fa7145e647f9daca0d38 Mon Sep 17 00:00:00 2001 From: Nico Huber Date: Fri, 14 Jun 2013 15:26:49 +0200 Subject: [PATCH] libpayload: Make heap code independent of its header size Signed-off-by: Patrick Georgi Change-Id: Ie69ceb343494b7dd309847b7d606cb47925f68b6 Reviewed-on: http://review.coreboot.org/3888 Tested-by: build bot (Jenkins) Reviewed-by: Ronald G. Minnich Reviewed-by: Paul Menzel --- payloads/libpayload/libc/malloc.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/payloads/libpayload/libc/malloc.c b/payloads/libpayload/libc/malloc.c index 7756a3bc49..c03cc737fa 100644 --- a/payloads/libpayload/libc/malloc.c +++ b/payloads/libpayload/libc/malloc.c @@ -51,11 +51,12 @@ static void *hstart = (void *)&_heap; static void *hend = (void *)&_eheap; typedef unsigned int hdrtype_t; +#define HDRSIZE (sizeof(hdrtype_t)) -#define MAGIC (0x2a << 26) -#define FLAG_FREE (1 << 25) -#define SIZE_BITS 25 -#define MAX_SIZE ((1 << SIZE_BITS) - 1) +#define SIZE_BITS ((HDRSIZE << 3) - 7) +#define MAGIC (((hdrtype_t)0x2a) << (SIZE_BITS + 1)) +#define FLAG_FREE (((hdrtype_t)0x01) << (SIZE_BITS + 0)) +#define MAX_SIZE ((((hdrtype_t)0x01) << SIZE_BITS) - 1) #define SIZE(_h) ((_h) & MAX_SIZE) @@ -64,8 +65,6 @@ typedef unsigned int hdrtype_t; #define FREE_BLOCK(_s) _HEADER(_s, FLAG_FREE) #define USED_BLOCK(_s) _HEADER(_s, 0) -#define HDRSIZE (sizeof(hdrtype_t)) - #define IS_FREE(_h) (((_h) & (MAGIC | FLAG_FREE)) == (MAGIC | FLAG_FREE)) #define HAS_MAGIC(_h) (((_h) & MAGIC) == MAGIC) @@ -93,7 +92,7 @@ static void *alloc(int len) hdrtype_t volatile *ptr = (hdrtype_t volatile *) hstart; /* Align the size. */ - len = (len + 3) & ~3; + len = (len + HDRSIZE - 1) & ~(HDRSIZE - 1); if (!len || len > MAX_SIZE) return (void *)NULL;