From c41f403f568d42ceaf4befd6aa915c298a9d6ec2 Mon Sep 17 00:00:00 2001 From: gbeauche <> Date: Wed, 22 Jun 2005 16:38:15 +0000 Subject: [PATCH] Rework sheepshaver_cpu object allocation and get rid of POSIX'ish functions. --- SheepShaver/src/Unix/configure.ac | 1 - SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp | 48 +++++++++++--------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/SheepShaver/src/Unix/configure.ac b/SheepShaver/src/Unix/configure.ac index 344a0ec1..db02e96b 100644 --- a/SheepShaver/src/Unix/configure.ac +++ b/SheepShaver/src/Unix/configure.ac @@ -346,7 +346,6 @@ AC_CHECK_FUNCS(nanosleep) AC_CHECK_FUNCS(sigaction signal) AC_CHECK_FUNCS(mmap mprotect munmap) 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(floorf roundf ceilf truncf floor round ceil trunc) diff --git a/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp b/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp index 4b1d6449..81ca17ed 100644 --- a/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp +++ b/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp @@ -183,36 +183,42 @@ public: 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 *p; + const int ALIGN = 16; -#if defined(HAVE_POSIX_MEMALIGN) - if (posix_memalign(&p, 16, size) != 0) + // Allocate enough space for sheepshaver_cpu data + signature + align pad + uint8 *ptr = (uint8 *)malloc(size + ALIGN * 2); + if (ptr == NULL) 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) { -#if defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC) -#if defined(__GLIBC__) - // this is known to work only with GNU libc - free(p); -#endif -#else - free(p); -#endif + uint32 *blk = (uint32 *)p; + assert(blk[-2] == FOURCC('S','C','P','U')); + void *ptr = (void *)(((uintptr)p) - blk[-1]); + free(ptr); } sheepshaver_cpu::sheepshaver_cpu()