16-byte aligned memory allocator will try the following functions in-order

(determined at compile-time): posix_memalign, memalign, valloc, malloc.
This commit is contained in:
gbeauche 2004-02-24 14:09:12 +00:00
parent ae93ea2f16
commit 47348e8120
2 changed files with 18 additions and 1 deletions

View File

@ -223,6 +223,7 @@ AC_CHECK_FUNCS(nanosleep)
AC_CHECK_FUNCS(sigaction signal) AC_CHECK_FUNCS(sigaction signal)
AC_CHECK_FUNCS(mmap mprotect munmap) AC_CHECK_FUNCS(mmap mprotect munmap)
AC_CHECK_FUNCS(vm_allocate vm_deallocate vm_protect) AC_CHECK_FUNCS(vm_allocate vm_deallocate vm_protect)
AC_CHECK_FUNCS(posix_memalign memalign valloc)
dnl Darwin seems to define mach_task_self() instead of task_self(). dnl Darwin seems to define mach_task_self() instead of task_self().
AC_CHECK_FUNCS(mach_task_self task_self) AC_CHECK_FUNCS(mach_task_self task_self)

View File

@ -40,6 +40,7 @@
#include "ether.h" #include "ether.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#if ENABLE_MON #if ENABLE_MON
#include "mon.h" #include "mon.h"
@ -167,16 +168,31 @@ void *operator new(size_t size)
{ {
void *p; void *p;
/* XXX: try different approaches */ #if defined(HAVE_POSIX_MEMALIGN)
if (posix_memalign(&p, 16, size) != 0) if (posix_memalign(&p, 16, size) != 0)
throw std::bad_alloc(); throw std::bad_alloc();
#elif defined(HAVE_MEMALIGN)
p = memalign(16, size);
#elif defined(HAVE_VALLOC)
p = valloc(size); // page-aligned!
#else
/* XXX: handle padding ourselves */
p = malloc(size);
#endif
return p; return p;
} }
void operator delete(void *p) void operator delete(void *p)
{ {
#if defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC)
#if defined(__GLIBC__)
// this is known to work only with GNU libc
free(p); free(p);
#endif
#else
free(p);
#endif
} }
sheepshaver_cpu::sheepshaver_cpu() sheepshaver_cpu::sheepshaver_cpu()