Let memory allocation be aligned on 8-byte boundaries.

This commit is contained in:
Kees van Reeuwijk 2010-05-04 21:02:44 +00:00
parent 09958abda8
commit 51d46f8e46
3 changed files with 26 additions and 8 deletions

View file

@ -1,7 +1,16 @@
/* $Header$ */ /* $Header$ */
#include <stdlib.h> #include <stdlib.h>
#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 * void *
calloc(size_t nelem, size_t elsize) calloc(size_t nelem, size_t elsize)

View file

@ -1,6 +1,7 @@
/* $Header$ */ /* $Header$ */
/* replace undef by define */ /* replace undef by define */
#define ALIGN_EIGHT_BYTES /* Use 8-byte alignment. */
#define DEBUG /* check assertions */ #define DEBUG /* check assertions */
#undef SLOWDEBUG /* some extra test loops (requires DEBUG) */ #undef SLOWDEBUG /* some extra test loops (requires DEBUG) */
@ -24,7 +25,11 @@
#else #else
#define BRKSIZE 4096 #define BRKSIZE 4096
#endif #endif
#ifdef ALIGN_EIGHT_BYTES
#define PTRSIZE 8
#else
#define PTRSIZE ((int) sizeof(void *)) #define PTRSIZE ((int) sizeof(void *))
#endif
#define Align(x,a) (((x) + (a - 1)) & ~(a - 1)) #define Align(x,a) (((x) + (a - 1)) & ~(a - 1))
#define NextSlot(p) (* (void **) ((p) - PTRSIZE)) #define NextSlot(p) (* (void **) ((p) - PTRSIZE))
#define NextFree(p) (* (void **) (p)) #define NextFree(p) (* (void **) (p))
@ -41,6 +46,9 @@
* linked together by a pointer at the start of the * linked together by a pointer at the start of the
* user visable part, so just after the next-slot pointer. * user visable part, so just after the next-slot pointer.
* Free slots are merged together by free(). * 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); extern void *_sbrk(int);
@ -67,16 +75,17 @@ static int grow(size_t len)
} }
void * void *
malloc(size_t size) malloc(const size_t size)
{ {
register char *prev, *p, *next, *new; register char *prev, *p, *next, *new;
register unsigned len, ntries; unsigned ntries;
if (size == 0) if (size == 0)
return NULL; return NULL;
for (ntries = 0; ntries < 2; ntries++) { 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; errno = ENOMEM;
return NULL; return NULL;
} }

View file

@ -10,7 +10,7 @@ _alloca:
pop %ecx /* Return address */ pop %ecx /* Return address */
pop %eax /* Bytes to allocate */ pop %eax /* Bytes to allocate */
addl $2*4+3, %eax /* Add space for two saved register variables */ 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 */ movl %esp, %ebx /* Keep current esp */
subl %eax, %esp /* Lower stack */ subl %eax, %esp /* Lower stack */
movl %esp, %eax /* Return value */ movl %esp, %eax /* Return value */
@ -24,7 +24,7 @@ _alloca:
pop %ecx /* Return address */ pop %ecx /* Return address */
pop %eax /* Bytes to allocate */ pop %eax /* Bytes to allocate */
addl $3, %eax addl $3, %eax
andb $0xFC, %al /* Align */ andb $0xF8, %al /* Align */
subl %eax, %esp /* Lower stack */ subl %eax, %esp /* Lower stack */
movl %esp, %eax /* Return value */ movl %esp, %eax /* Return value */
push %eax /* Dummy argument */ push %eax /* Dummy argument */