1 /* -----------------------------------------------------------------------------
2 * $Id: MBlock.c,v 1.3 1999/01/13 17:25:40 simonm Exp $
4 * MegaBlock Allocator Interface. This file contains all the dirty
5 * architecture-dependent hackery required to get a chunk of aligned
6 * memory from the operating system.
8 * ---------------------------------------------------------------------------*/
10 #define NON_POSIX_SOURCE
16 #include "BlockAlloc.h"
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
34 #if cygwin32_TARGET_OS
38 #if freebsd2_TARGET_OS || freebsd3_TARGET_OS
39 /* Executable is loaded from 0x0
40 * Shared libraries are loaded at 0x2000000
41 * Stack is at the top of the address space. The kernel probably owns
42 * 0x8000000 onwards, so we'll pick 0x5000000.
44 #define ASK_FOR_MEM_AT 0x50000000
49 #define ASK_FOR_MEM_AT 0x50000000
51 #elif cygwin32_TARGET_OS
54 #define ASK_FOR_MEM_AT 0x50000000
56 #elif solaris2_TARGET_OS
58 #define ASK_FOR_MEM_AT 0x50000000
61 #error Dont know where to get memory from on this architecture
62 /* ToDo: memory locations on other architectures */
65 lnat mblocks_allocated = 0;
76 static caddr_t next_request = (caddr_t)ASK_FOR_MEM_AT;
78 lnat size = MBLOCK_SIZE * n;
80 #ifdef solaris2_TARGET_OS
82 int fd = open("/dev/zero",O_RDONLY);
83 ret = mmap(next_request, size, PROT_READ | PROT_WRITE,
84 MAP_FIXED | MAP_PRIVATE, fd, 0);
90 /* avoid using cygwin32's mmap implementation, it's buggy and
91 it's just as easy to do what we want to do directly.
93 HANDLE hFile = (HANDLE)0xFFFFFFFF;
94 SECURITY_ATTRIBUTES sa;
97 sa.nLength = sizeof (SECURITY_ATTRIBUTES);
98 sa.bInheritHandle = TRUE;
99 sa.lpSecurityDescriptor = 0;
101 h = CreateFileMapping(hFile, &sa, PAGE_READWRITE, 0, size, NULL);
104 fprintf(stderr, "getMBlocks: CreateFileMapping failed with: %d\n", GetLastError());
108 ret = MapViewOfFileEx (h, FILE_MAP_WRITE, 0, 0, size, next_request);
109 if ( ret != next_request ) {
111 fprintf(stderr, "getMBlocks: MapViewOfFileEx failed with: %d\n", GetLastError());
118 ret = mmap(next_request, size, PROT_READ | PROT_WRITE,
119 MAP_ANON | MAP_PRIVATE, -1, 0);
123 if (ret == (void *)-1) {
124 if (errno == ENOMEM) {
125 barf("getMBlock: out of memory");
127 barf("GetMBlock: mmap failed");
131 if (((W_)ret & MBLOCK_MASK) != 0) {
132 barf("GetMBlock: misaligned block returned");
135 IF_DEBUG(gc,fprintf(stderr,"Allocated %d megablock(s) at %x\n",n,(nat)ret));
137 next_request += size;
139 mblocks_allocated += n;