/* -----------------------------------------------------------------------------
- * $Id: MBlock.c,v 1.30 2002/10/21 11:38:53 simonmar Exp $
+ * $Id: MBlock.c,v 1.36 2002/11/05 09:31:37 simonmar Exp $
*
* (c) The GHC Team 1998-1999
*
#if HAVE_WINDOWS_H
#include <windows.h>
#endif
+#if darwin_TARGET_OS
+#include <mach/vm_map.h>
+#endif
#include <errno.h>
#ifdef solaris2_TARGET_OS
{
int fd = open("/dev/zero",O_RDONLY);
- ret = mmap(addr, size, PROT_READ | PROT_WRITE,
- MAP_FIXED | MAP_PRIVATE, fd, 0);
+ ret = mmap(addr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
close(fd);
}
#elif hpux_TARGET_OS
ret = mmap(addr, size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
#elif darwin_TARGET_OS
- ret = mmap(addr, size, PROT_READ | PROT_WRITE,
- MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
+ // Without MAP_FIXED, Apple's mmap ignores addr.
+ // With MAP_FIXED, it overwrites already mapped regions, whic
+ // mmap(0, ... MAP_FIXED ...) is worst of all: It unmaps the program text
+ // and replaces it with zeroes, causing instant death.
+ // This behaviour seems to be conformant with IEEE Std 1003.1-2001.
+ // Let's just use the underlying Mach Microkernel calls directly,
+ // they're much nicer.
+
+ kern_return_t err;
+ ret = addr;
+ if(addr) // try to allocate at adress
+ err = vm_allocate(mach_task_self(),(vm_address_t*) &ret, size, FALSE);
+ if(!addr || err) // try to allocate anywhere
+ err = vm_allocate(mach_task_self(),(vm_address_t*) &ret, size, TRUE);
+
+ if(err)
+ ret = (void*) -1;
+ else
+ vm_protect(mach_task_self(),ret,size,FALSE,VM_PROT_READ|VM_PROT_WRITE);
#else
ret = mmap(addr, size, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0);
#endif
+ if (ret == (void *)-1) {
+ if (errno == ENOMEM) {
+ barf("out of memory (requested %d bytes)", size);
+ } else {
+ barf("getMBlock: mmap failed");
+ }
+ }
+
return ret;
-}
+}
// Implements the general case: allocate a chunk of memory of 'size'
// mblocks.
// it (unmap the rest).
size += MBLOCK_SIZE;
ret = my_mmap(0, size);
- if (ret == (void *)-1) {
- barf("gen_map_mblocks: mmap failed");
- }
// unmap the slop bits around the chunk we allocated
slop = (W_)ret & MBLOCK_MASK;
barf("gen_map_mblocks: munmap failed");
}
+ // ToDo: if we happened to get an aligned block, then don't
+ // unmap the excess, just use it.
+
// next time, try after the block we just got.
ret += MBLOCK_SIZE - slop;
return ret;
ret = gen_map_mblocks(size);
} else {
ret = my_mmap(next_request, size);
-
- if (ret == (void *)-1) {
- if (errno == ENOMEM) {
- belch("out of memory (requested %d bytes)", n * BLOCK_SIZE);
- stg_exit(EXIT_FAILURE);
- } else {
- barf("getMBlock: mmap failed");
- }
- }
if (((W_)ret & MBLOCK_MASK) != 0) {
// misaligned block!
}
// Next time, we'll try to allocate right after the block we just got.
+ // ToDo: check that we haven't already grabbed the memory at next_request
next_request = ret + size;
IF_DEBUG(gc,fprintf(stderr,"Allocated %d megablock(s) at %p\n",n,ret));
static char* base_mblocks = (char*)0;
static char* next_request = (char*)0;
void* ret = (void*)0;
+ int i;
lnat size = MBLOCK_SIZE * n;
} else {
end_non_committed = (char*)base_non_committed + (unsigned long)size_reserved_pool;
/* The returned pointer is not aligned on a mega-block boundary. Make it. */
- base_mblocks = (char*)((unsigned long)base_non_committed & (unsigned long)0xfff00000) + MBLOCK_SIZE;
+ base_mblocks = (char*)((unsigned long)base_non_committed & (unsigned long)~MBLOCK_MASK) + MBLOCK_SIZE;
# if 0
fprintf(stderr, "getMBlocks: Dropping %d bytes off of 256M chunk\n",
(unsigned)base_mblocks - (unsigned)base_non_committed);