Rework sheepshaver_cpu object allocation and get rid of POSIX'ish functions.

This commit is contained in:
gbeauche 2005-06-22 16:38:15 +00:00
parent c4a7b10650
commit c41f403f56
2 changed files with 27 additions and 22 deletions

View File

@ -346,7 +346,6 @@ 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)
AC_CHECK_FUNCS(exp2f log2f exp2 log2) AC_CHECK_FUNCS(exp2f log2f exp2 log2)
AC_CHECK_FUNCS(floorf roundf ceilf truncf floor round ceil trunc) AC_CHECK_FUNCS(floorf roundf ceilf truncf floor round ceil trunc)

View File

@ -183,36 +183,42 @@ public:
void operator delete(void *p); void operator delete(void *p);
}; };
// Memory allocator returning areas aligned on 16-byte boundaries // Memory allocator returning sheepshaver_cpu objects aligned on 16-byte boundaries
// FORMAT: [ alignment ] magic identifier, offset to malloc'ed data, sheepshaver_cpu data
void *sheepshaver_cpu::operator new(size_t size) void *sheepshaver_cpu::operator new(size_t size)
{ {
void *p; const int ALIGN = 16;
#if defined(HAVE_POSIX_MEMALIGN) // Allocate enough space for sheepshaver_cpu data + signature + align pad
if (posix_memalign(&p, 16, size) != 0) uint8 *ptr = (uint8 *)malloc(size + ALIGN * 2);
if (ptr == NULL)
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; // Align memory
int ofs = 0;
while ((((uintptr)ptr) % ALIGN) != 0)
ofs++, ptr++;
// Insert signature and offset
struct aligned_block_t {
uint32 pad[(ALIGN - 8) / 4];
uint32 signature;
uint32 offset;
uint8 data[sizeof(sheepshaver_cpu)];
};
aligned_block_t *blk = (aligned_block_t *)ptr;
blk->signature = FOURCC('S','C','P','U');
blk->offset = ofs + (&blk->data[0] - (uint8 *)blk);
assert((((uintptr)&blk->data) % ALIGN) == 0);
return &blk->data[0];
} }
void sheepshaver_cpu::operator delete(void *p) void sheepshaver_cpu::operator delete(void *p)
{ {
#if defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC) uint32 *blk = (uint32 *)p;
#if defined(__GLIBC__) assert(blk[-2] == FOURCC('S','C','P','U'));
// this is known to work only with GNU libc void *ptr = (void *)(((uintptr)p) - blk[-1]);
free(p); free(ptr);
#endif
#else
free(p);
#endif
} }
sheepshaver_cpu::sheepshaver_cpu() sheepshaver_cpu::sheepshaver_cpu()