[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / rts / MBlock.c
1 /* -----------------------------------------------------------------------------
2  * $Id: MBlock.c,v 1.2 1998/12/02 13:28:28 simonm Exp $
3  *
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.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #define NON_POSIX_SOURCE
11
12 #include "Rts.h"
13 #include "RtsUtils.h"
14 #include "RtsFlags.h"
15 #include "MBlock.h"
16 #include "BlockAlloc.h"
17
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29
30 #ifdef HAVE_FCNTL_H
31 #include <fcntl.h>
32 #endif
33
34 #if cygwin32_TARGET_OS
35 #include <windows.h>
36 #endif
37
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.
43  */
44 #define ASK_FOR_MEM_AT 0x50000000
45
46 #elif linux_TARGET_OS
47 /* Any ideas?
48  */
49 #define ASK_FOR_MEM_AT 0x50000000
50
51 #elif cygwin32_TARGET_OS
52 /* Any ideas?
53  */
54 #define ASK_FOR_MEM_AT 0x50000000
55
56 #elif solaris2_TARGET_OS
57 /* guess */
58 #define ASK_FOR_MEM_AT 0x50000000
59
60 #else
61 #error Dont know where to get memory from on this architecture
62 /* ToDo: memory locations on other architectures */
63 #endif
64
65 void *
66 getMBlock(void)
67 {
68   return getMBlocks(1);
69 }
70
71 void *
72 getMBlocks(nat n)
73 {
74   static caddr_t next_request = (caddr_t)ASK_FOR_MEM_AT;
75   caddr_t ret;
76   lnat size = MBLOCK_SIZE * n;
77  
78 #ifdef solaris2_TARGET_OS
79   { 
80       int fd = open("/dev/zero",O_RDONLY);
81       ret = mmap(next_request, size, PROT_READ | PROT_WRITE, 
82                  MAP_FIXED | MAP_PRIVATE, fd, 0);
83       close(fd);
84   }
85 #else
86 # ifdef _WIN32
87   {
88     /* avoid using cygwin32's mmap implementation, it's buggy and
89        it's just as easy to do what we want to do directly.
90     */
91    HANDLE hFile = (HANDLE)0xFFFFFFFF;
92    SECURITY_ATTRIBUTES sa;
93    HANDLE h;
94
95    sa.nLength = sizeof (SECURITY_ATTRIBUTES);
96    sa.bInheritHandle = TRUE;
97    sa.lpSecurityDescriptor = 0;
98
99    h = CreateFileMapping(hFile, &sa, PAGE_READWRITE, 0, size, NULL);
100    if ( h == 0 ) {
101 #  ifdef DEBUG
102       fprintf(stderr, "getMBlocks: CreateFileMapping failed with: %d\n", GetLastError());
103 #  endif
104       ret=(void*)-1;
105    } else {
106       ret = MapViewOfFileEx (h, FILE_MAP_WRITE, 0, 0, size, next_request);
107       if ( ret != next_request ) {
108 #  ifdef DEBUG
109          fprintf(stderr, "getMBlocks: MapViewOfFileEx failed with: %d\n", GetLastError());
110 #  endif
111          ret =(void*)-1;
112       }
113    }
114   }
115 # else
116   ret = mmap(next_request, size, PROT_READ | PROT_WRITE, 
117              MAP_ANON | MAP_PRIVATE, -1, 0);
118 # endif
119 #endif
120   
121   if (ret == (void *)-1) {
122     if (errno == ENOMEM) {
123       barf("getMBlock: out of memory");
124     } else {
125       barf("GetMBlock: mmap failed");
126     }
127   }
128
129   if (((W_)ret & MBLOCK_MASK) != 0) {
130     barf("GetMBlock: misaligned block returned");
131   }
132
133   IF_DEBUG(gc,fprintf(stderr,"Allocated %d megablock(s) at %x\n",n,(nat)ret));
134
135   next_request += size;
136
137   return ret;
138 }