From 51d46f8e4690456942e014c8313f89de2fb1260f Mon Sep 17 00:00:00 2001 From: Kees van Reeuwijk Date: Tue, 4 May 2010 21:02:44 +0000 Subject: [PATCH] Let memory allocation be aligned on 8-byte boundaries. --- lib/libc/ansi/calloc.c | 11 ++++++++++- lib/libc/ansi/malloc.c | 19 ++++++++++++++----- lib/libc/arch/i386/misc/alloca.S | 4 ++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/libc/ansi/calloc.c b/lib/libc/ansi/calloc.c index cccd79ab7..0c4323902 100644 --- a/lib/libc/ansi/calloc.c +++ b/lib/libc/ansi/calloc.c @@ -1,7 +1,16 @@ /* $Header$ */ #include -#define ALIGN(x) (((x) + (sizeof(size_t) - 1)) & ~(sizeof(size_t) - 1)) +/* replace undef by define */ +#define ALIGN_EIGHT_BYTES /* Use 8-byte alignment. */ + +#ifdef ALIGN_EIGHT_BYTES +#define ALIGN_SIZE 8 +#else +#define ALIGN_SIZE sizeof(size_t) +#endif + +#define ALIGN(x) (((x) + (ALIGN_SIZE - 1)) & ~(ALIGN_SIZE - 1)) void * calloc(size_t nelem, size_t elsize) diff --git a/lib/libc/ansi/malloc.c b/lib/libc/ansi/malloc.c index d6990fef8..f4644f1c2 100644 --- a/lib/libc/ansi/malloc.c +++ b/lib/libc/ansi/malloc.c @@ -1,8 +1,9 @@ /* $Header$ */ /* replace undef by define */ -#define DEBUG /* check assertions */ -#undef SLOWDEBUG /* some extra test loops (requires DEBUG) */ +#define ALIGN_EIGHT_BYTES /* Use 8-byte alignment. */ +#define DEBUG /* check assertions */ +#undef SLOWDEBUG /* some extra test loops (requires DEBUG) */ #ifndef DEBUG #define NDEBUG @@ -24,7 +25,11 @@ #else #define BRKSIZE 4096 #endif +#ifdef ALIGN_EIGHT_BYTES +#define PTRSIZE 8 +#else #define PTRSIZE ((int) sizeof(void *)) +#endif #define Align(x,a) (((x) + (a - 1)) & ~(a - 1)) #define NextSlot(p) (* (void **) ((p) - PTRSIZE)) #define NextFree(p) (* (void **) (p)) @@ -41,6 +46,9 @@ * linked together by a pointer at the start of the * user visable part, so just after the next-slot pointer. * Free slots are merged together by free(). + * + * Since modern processors prefer 8-byte alignment, we now pretend + * our pointers are 8 bytes wide. */ extern void *_sbrk(int); @@ -67,16 +75,17 @@ static int grow(size_t len) } void * -malloc(size_t size) +malloc(const size_t size) { register char *prev, *p, *next, *new; - register unsigned len, ntries; + unsigned ntries; if (size == 0) return NULL; for (ntries = 0; ntries < 2; ntries++) { - if ((len = Align(size, PTRSIZE) + PTRSIZE) < 2 * PTRSIZE) { + unsigned len = Align(size, PTRSIZE) + PTRSIZE; + if (len < 2 * PTRSIZE) { errno = ENOMEM; return NULL; } diff --git a/lib/libc/arch/i386/misc/alloca.S b/lib/libc/arch/i386/misc/alloca.S index 24de081fb..0c9b598fe 100644 --- a/lib/libc/arch/i386/misc/alloca.S +++ b/lib/libc/arch/i386/misc/alloca.S @@ -10,7 +10,7 @@ _alloca: pop %ecx /* Return address */ pop %eax /* Bytes to allocate */ addl $2*4+3, %eax /* Add space for two saved register variables */ - andb $0xFC, %al /* Align */ + andb $0xF8, %al /* Align */ movl %esp, %ebx /* Keep current esp */ subl %eax, %esp /* Lower stack */ movl %esp, %eax /* Return value */ @@ -24,7 +24,7 @@ _alloca: pop %ecx /* Return address */ pop %eax /* Bytes to allocate */ addl $3, %eax - andb $0xFC, %al /* Align */ + andb $0xF8, %al /* Align */ subl %eax, %esp /* Lower stack */ movl %esp, %eax /* Return value */ push %eax /* Dummy argument */