[project @ 1999-01-14 18:31:17 by sof]
[ghc-hetmet.git] / ghc / rts / MBlock.c
1 /* -----------------------------------------------------------------------------
2  * $Id: MBlock.c,v 1.4 1999/01/14 18:31:17 sof 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 lnat mblocks_allocated = 0;
66
67 void *
68 getMBlock(void)
69 {
70   return getMBlocks(1);
71 }
72
73 void *
74 getMBlocks(nat n)
75 {
76   static caddr_t next_request = (caddr_t)ASK_FOR_MEM_AT;
77   caddr_t ret;
78   lnat size = MBLOCK_SIZE * n;
79  
80 #ifdef solaris2_TARGET_OS
81   { 
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);
85       close(fd);
86   }
87 #else
88 # ifdef _WIN32
89   {
90     /* Note: on 95, the legal range for next_request is: [0x00400000, 0x7fffffff]
91              under NT it is: [0x00010000, 0x7fffffff]
92
93        We start allocating at 0x50000000, hopefully that's not conflicting with
94        others.. (ToDo: have the allocator try to gracefully rebase itself in
95        case our initial guess is conflicting with others.)
96     */
97     ret = VirtualAlloc(next_request, size, MEM_RESERVE | MEM_COMMIT , PAGE_READWRITE);
98     if (!ret) {
99 #  ifdef DEBUG
100          fprintf(stderr, "getMBlocks: VirtualAlloc failed with: %d\n", GetLastError());
101 #  endif
102          ret =(void*)-1;
103
104     }
105     return ret;
106   }
107 # else
108   ret = mmap(next_request, size, PROT_READ | PROT_WRITE, 
109              MAP_ANON | MAP_PRIVATE, -1, 0);
110 # endif
111 #endif
112   
113   if (ret == (void *)-1) {
114     if (errno == ENOMEM) {
115       barf("getMBlock: out of memory");
116     } else {
117       barf("GetMBlock: mmap failed");
118     }
119   }
120
121   if (((W_)ret & MBLOCK_MASK) != 0) {
122     barf("GetMBlock: misaligned block returned");
123   }
124
125   IF_DEBUG(gc,fprintf(stderr,"Allocated %d megablock(s) at %x\n",n,(nat)ret));
126
127   next_request += size;
128
129   mblocks_allocated += n;
130   
131   return ret;
132 }