1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-1999
5 * MegaBlock Allocator Interface. This file contains all the dirty
6 * architecture-dependent hackery required to get a chunk of aligned
7 * memory from the operating system.
9 * ---------------------------------------------------------------------------*/
11 /* This is non-posix compliant. */
12 /* #include "PosixSource.h" */
18 #include "BlockAlloc.h"
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
33 #ifndef mingw32_HOST_OS
34 # ifdef HAVE_SYS_MMAN_H
35 # include <sys/mman.h>
45 #include <mach/vm_map.h>
50 lnat mblocks_allocated = 0;
52 /* -----------------------------------------------------------------------------
53 The MBlock Map: provides our implementation of HEAP_ALLOCED()
54 -------------------------------------------------------------------------- */
56 #if SIZEOF_VOID_P == 4
57 StgWord8 mblock_map[MBLOCK_MAP_SIZE]; // initially all zeros
58 #elif SIZEOF_VOID_P == 8
59 static MBlockMap dummy_mblock_map;
60 MBlockMap *mblock_cache = &dummy_mblock_map;
61 int mblock_map_count = 0;
62 MBlockMap **mblock_maps = NULL;
65 findMBlockMap(void *p)
68 StgWord32 hi = (StgWord32) (((StgWord)p) >> 32);
69 for( i = 0; i < mblock_map_count; i++ )
71 if(mblock_maps[i]->addrHigh32 == hi)
73 return mblock_maps[i];
80 slowIsHeapAlloced(void *p)
82 MBlockMap *map = findMBlockMap(p);
86 return map->mblocks[MBLOCK_MAP_ENTRY(p)];
94 markHeapAlloced(void *p)
96 #if SIZEOF_VOID_P == 4
97 mblock_map[MBLOCK_MAP_ENTRY(p)] = 1;
98 #elif SIZEOF_VOID_P == 8
99 MBlockMap *map = findMBlockMap(p);
103 mblock_maps = realloc(mblock_maps,
104 sizeof(MBlockMap*) * mblock_map_count);
105 map = mblock_maps[mblock_map_count-1] = calloc(1,sizeof(MBlockMap));
106 map->addrHigh32 = (StgWord32) (((StgWord)p) >> 32);
108 map->mblocks[MBLOCK_MAP_ENTRY(p)] = 1;
113 /* -----------------------------------------------------------------------------
114 Allocate new mblock(s)
115 -------------------------------------------------------------------------- */
120 return getMBlocks(1);
123 /* -----------------------------------------------------------------------------
126 On Unix-like systems, we use mmap() to allocate our memory. We
127 want memory in chunks of MBLOCK_SIZE, and aligned on an MBLOCK_SIZE
128 boundary. The mmap() interface doesn't give us this level of
129 control, so we have to use some heuristics.
131 In the general case, if we want a block of n megablocks, then we
132 allocate n+1 and trim off the slop from either side (using
133 munmap()) to get an aligned chunk of size n. However, the next
134 time we'll try to allocate directly after the previously allocated
135 chunk, on the grounds that this is aligned and likely to be free.
136 If it turns out that we were wrong, we have to munmap() and try
137 again using the general method.
139 Note on posix_memalign(): this interface is available on recent
140 systems and appears to provide exactly what we want. However, it
141 turns out not to be as good as our mmap() implementation, because
142 it wastes extra space (using double the address space, in a test on
143 x86_64/Linux). The problem seems to be that posix_memalign()
144 returns memory that can be free()'d, so the library must store
145 extra information along with the allocated block, thus messing up
146 the alignment. Hence, we don't use posix_memalign() for now.
148 -------------------------------------------------------------------------- */
150 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS)
152 // A wrapper around mmap(), to abstract away from OS differences in
153 // the mmap() interface.
156 my_mmap (void *addr, lnat size)
160 #if defined(solaris2_HOST_OS) || defined(irix_HOST_OS)
162 int fd = open("/dev/zero",O_RDONLY);
163 ret = mmap(addr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
167 ret = mmap(addr, size, PROT_READ | PROT_WRITE,
168 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
170 // Without MAP_FIXED, Apple's mmap ignores addr.
171 // With MAP_FIXED, it overwrites already mapped regions, whic
172 // mmap(0, ... MAP_FIXED ...) is worst of all: It unmaps the program text
173 // and replaces it with zeroes, causing instant death.
174 // This behaviour seems to be conformant with IEEE Std 1003.1-2001.
175 // Let's just use the underlying Mach Microkernel calls directly,
176 // they're much nicer.
180 if(addr) // try to allocate at adress
181 err = vm_allocate(mach_task_self(),(vm_address_t*) &ret, size, FALSE);
182 if(!addr || err) // try to allocate anywhere
183 err = vm_allocate(mach_task_self(),(vm_address_t*) &ret, size, TRUE);
186 // don't know what the error codes mean exactly, assume it's
187 // not our problem though.
188 errorBelch("memory allocation failed (requested %lu bytes)", size);
189 stg_exit(EXIT_FAILURE);
191 vm_protect(mach_task_self(),ret,size,FALSE,VM_PROT_READ|VM_PROT_WRITE);
194 ret = mmap(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC,
195 MAP_ANON | MAP_PRIVATE, -1, 0);
198 if (ret == (void *)-1) {
199 if (errno == ENOMEM ||
200 (errno == EINVAL && sizeof(void*)==4 && size >= 0xc0000000)) {
201 // If we request more than 3Gig, then we get EINVAL
202 // instead of ENOMEM (at least on Linux).
203 errorBelch("out of memory (requested %lu bytes)", size);
204 stg_exit(EXIT_FAILURE);
206 barf("getMBlock: mmap: %s", strerror(errno));
213 // Implements the general case: allocate a chunk of memory of 'size'
217 gen_map_mblocks (lnat size)
222 // Try to map a larger block, and take the aligned portion from
223 // it (unmap the rest).
225 ret = my_mmap(0, size);
227 // unmap the slop bits around the chunk we allocated
228 slop = (W_)ret & MBLOCK_MASK;
230 if (munmap(ret, MBLOCK_SIZE - slop) == -1) {
231 barf("gen_map_mblocks: munmap failed");
233 if (slop > 0 && munmap(ret+size-slop, slop) == -1) {
234 barf("gen_map_mblocks: munmap failed");
237 // ToDo: if we happened to get an aligned block, then don't
238 // unmap the excess, just use it. For this to work, you
239 // need to keep in mind the following:
240 // * Calling my_mmap() with an 'addr' arg pointing to
241 // already my_mmap()ed space is OK and won't fail.
242 // * If my_mmap() can't satisfy the request at the
243 // given 'next_request' address in getMBlocks(), that
244 // you unmap the extra mblock mmap()ed here (or simply
245 // satisfy yourself that the slop introduced isn't worth
249 // next time, try after the block we just got.
250 ret += MBLOCK_SIZE - slop;
255 // The external interface: allocate 'n' mblocks, and return the
261 static caddr_t next_request = (caddr_t)HEAP_BASE;
263 lnat size = MBLOCK_SIZE * n;
266 if (next_request == 0) {
267 // use gen_map_mblocks the first time.
268 ret = gen_map_mblocks(size);
270 ret = my_mmap(next_request, size);
272 if (((W_)ret & MBLOCK_MASK) != 0) {
274 #if 0 // defined(DEBUG)
275 errorBelch("warning: getMBlock: misaligned block %p returned when allocating %d megablock(s) at %p", ret, n, next_request);
278 // unmap this block...
279 if (munmap(ret, size) == -1) {
280 barf("getMBlock: munmap failed");
282 // and do it the hard way
283 ret = gen_map_mblocks(size);
287 // Next time, we'll try to allocate right after the block we just got.
288 // ToDo: check that we haven't already grabbed the memory at next_request
289 next_request = ret + size;
291 debugTrace(DEBUG_gc, "allocated %d megablock(s) at %p",n,ret);
294 for (i = 0; i < n; i++) {
295 markHeapAlloced( ret + i * MBLOCK_SIZE );
298 mblocks_allocated += n;
306 /* XXX Do something here */
309 #else /* defined(mingw32_HOST_OS) || defined(cygwin32_HOST_OS) */
312 On Win32 platforms we make use of the two-phased virtual memory API
313 to allocate mega blocks. We proceed as follows:
315 Reserve a large chunk of VM (256M at the time, or what the user asked
316 for via the -M option), but don't supply a base address that's aligned on
317 a MB boundary. Instead we round up to the nearest mblock from the chunk of
318 VM we're handed back from the OS (at the moment we just leave the 'slop' at
319 the beginning of the reserved chunk unused - ToDo: reuse it .)
321 Reserving memory doesn't allocate physical storage (not even in the
322 page file), this is done later on by committing pages (or mega-blocks in
326 static char* base_non_committed = (char*)0;
327 static char* end_non_committed = (char*)0;
329 static void *membase;
331 /* Default is to reserve 256M of VM to minimise the slop cost. */
332 #define SIZE_RESERVED_POOL ( 256 * 1024 * 1024 )
334 /* Number of bytes reserved */
335 static unsigned long size_reserved_pool = SIZE_RESERVED_POOL;
340 static char* base_mblocks = (char*)0;
341 static char* next_request = (char*)0;
342 void* ret = (void*)0;
345 lnat size = MBLOCK_SIZE * n;
347 if ( (base_non_committed == 0) || (next_request + size > end_non_committed) ) {
348 if (base_non_committed) {
349 /* Tacky, but if no user-provided -M option is in effect,
350 * set it to the default (==256M) in time for the heap overflow PSA.
352 if (RtsFlags.GcFlags.maxHeapSize == 0) {
353 RtsFlags.GcFlags.maxHeapSize = size_reserved_pool / BLOCK_SIZE;
357 if (RtsFlags.GcFlags.maxHeapSize != 0) {
358 size_reserved_pool = BLOCK_SIZE * RtsFlags.GcFlags.maxHeapSize;
359 if (size_reserved_pool < MBLOCK_SIZE) {
360 size_reserved_pool = 2*MBLOCK_SIZE;
363 base_non_committed = VirtualAlloc ( NULL
368 membase = base_non_committed;
369 if ( base_non_committed == 0 ) {
370 errorBelch("getMBlocks: VirtualAlloc MEM_RESERVE %lu failed with: %ld\n", size_reserved_pool, GetLastError());
373 end_non_committed = (char*)base_non_committed + (unsigned long)size_reserved_pool;
374 /* The returned pointer is not aligned on a mega-block boundary. Make it. */
375 base_mblocks = (char*)((unsigned long)base_non_committed & (unsigned long)~MBLOCK_MASK) + MBLOCK_SIZE;
377 debugBelch("getMBlocks: Dropping %d bytes off of 256M chunk\n",
378 (unsigned)base_mblocks - (unsigned)base_non_committed);
381 if ( ((char*)base_mblocks + size) > end_non_committed ) {
382 debugBelch("getMBlocks: oops, committed too small a region to start with.");
385 next_request = base_mblocks;
389 /* Commit the mega block(s) to phys mem */
390 if ( ret != (void*)-1 ) {
391 ret = VirtualAlloc(next_request, size, MEM_COMMIT, PAGE_READWRITE);
393 debugBelch("getMBlocks: VirtualAlloc MEM_COMMIT %lu failed with: %ld\n", size, GetLastError());
398 if (ret == (void*)-1) {
399 barf("getMBlocks: unknown memory allocation failure on Win32.");
402 if (((W_)ret & MBLOCK_MASK) != 0) {
403 barf("getMBlocks: misaligned block returned");
406 debugTrace(DEBUG_gc, "allocated %d megablock(s) at 0x%x",n,(nat)ret);
407 next_request = (char*)next_request + size;
409 mblocks_allocated += n;
412 for (i = 0; i < n; i++) {
413 markHeapAlloced( ret + i * MBLOCK_SIZE );
424 rc = VirtualFree(membase, 0, MEM_RELEASE);
427 debugBelch("freeAllMBlocks: VirtualFree failed with: %ld\n", GetLastError());
431 /* Hand back the physical memory that is allocated to a mega-block.
432 ToDo: chain the released mega block onto some list so that
433 getMBlocks() can get at it.
439 freeMBlock(void* p, nat n)
443 rc = VirtualFree(p, n * MBLOCK_SIZE , MEM_DECOMMIT );
447 debugBelch("freeMBlocks: VirtualFree failed with: %d\n", GetLastError());