comparison druntime/src/gc/basic/gcalloc.d @ 1458:e0b2d67cfe7c

Added druntime (this should be removed once it works).
author Robert Clipsham <robert@octarineparrot.com>
date Tue, 02 Jun 2009 17:43:06 +0100
parents
children
comparison
equal deleted inserted replaced
1456:7b218ec1044f 1458:e0b2d67cfe7c
1 /**
2 * Contains OS-level allocation routines.
3 *
4 * Copyright: Copyright Digital Mars 2005 - 2009.
5 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>.
6 * Authors: Walter Bright, David Friedman, Sean Kelly
7 *
8 * Copyright Digital Mars 2005 - 2009.
9 * Distributed under the Boost Software License, Version 1.0.
10 * (See accompanying file LICENSE_1_0.txt or copy at
11 * http://www.boost.org/LICENSE_1_0.txt)
12 */
13 module gc.gcalloc;
14
15
16 version (Windows)
17 {
18 private import core.sys.windows.windows;
19
20 alias int pthread_t;
21
22 pthread_t pthread_self()
23 {
24 return cast(pthread_t) GetCurrentThreadId();
25 }
26
27 //version = GC_Use_Alloc_Win32;
28 }
29 else version (Posix)
30 {
31 private import core.sys.posix.sys.mman;
32 private import core.stdc.stdlib;
33
34 //version = GC_Use_Alloc_MMap;
35 }
36 else
37 {
38 private import core.stdc.stdlib;
39
40 //version = GC_Use_Alloc_Malloc;
41 }
42
43 /+
44 static if(is(typeof(VirtualAlloc)))
45 version = GC_Use_Alloc_Win32;
46 else static if (is(typeof(mmap)))
47 version = GC_Use_Alloc_MMap;
48 else static if (is(typeof(valloc)))
49 version = GC_Use_Alloc_Valloc;
50 else static if (is(typeof(malloc)))
51 version = GC_Use_Alloc_Malloc;
52 else static assert(false, "No supported allocation methods available.");
53 +/
54
55 static if (is(typeof(VirtualAlloc))) // version (GC_Use_Alloc_Win32)
56 {
57 /**
58 * Map memory.
59 */
60 void *os_mem_map(size_t nbytes)
61 {
62 return VirtualAlloc(null, nbytes, MEM_RESERVE, PAGE_READWRITE);
63 }
64
65
66 /**
67 * Commit memory.
68 * Returns:
69 * 0 success
70 * !=0 failure
71 */
72 int os_mem_commit(void *base, size_t offset, size_t nbytes)
73 { void *p;
74
75 p = VirtualAlloc(base + offset, nbytes, MEM_COMMIT, PAGE_READWRITE);
76 return cast(int)(p is null);
77 }
78
79
80 /**
81 * Decommit memory.
82 * Returns:
83 * 0 success
84 * !=0 failure
85 */
86 int os_mem_decommit(void *base, size_t offset, size_t nbytes)
87 {
88 return cast(int)(VirtualFree(base + offset, nbytes, MEM_DECOMMIT) == 0);
89 }
90
91
92 /**
93 * Unmap memory allocated with os_mem_map().
94 * Memory must have already been decommitted.
95 * Returns:
96 * 0 success
97 * !=0 failure
98 */
99 int os_mem_unmap(void *base, size_t nbytes)
100 {
101 return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0);
102 }
103 }
104 else static if (is(typeof(mmap))) // else version (GC_Use_Alloc_MMap)
105 {
106 void *os_mem_map(size_t nbytes)
107 { void *p;
108
109 p = mmap(null, nbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
110 return (p == MAP_FAILED) ? null : p;
111 }
112
113
114 int os_mem_commit(void *base, size_t offset, size_t nbytes)
115 {
116 return 0;
117 }
118
119
120 int os_mem_decommit(void *base, size_t offset, size_t nbytes)
121 {
122 return 0;
123 }
124
125
126 int os_mem_unmap(void *base, size_t nbytes)
127 {
128 return munmap(base, nbytes);
129 }
130 }
131 else static if (is(typeof(valloc))) // else version (GC_Use_Alloc_Valloc)
132 {
133 void *os_mem_map(size_t nbytes)
134 {
135 return valloc(nbytes);
136 }
137
138
139 int os_mem_commit(void *base, size_t offset, size_t nbytes)
140 {
141 return 0;
142 }
143
144
145 int os_mem_decommit(void *base, size_t offset, size_t nbytes)
146 {
147 return 0;
148 }
149
150
151 int os_mem_unmap(void *base, size_t nbytes)
152 {
153 free(base);
154 return 0;
155 }
156 }
157 else static if (is(typeof(malloc))) // else version (GC_Use_Alloc_Malloc)
158 {
159 // NOTE: This assumes malloc granularity is at least (void*).sizeof. If
160 // (req_size + PAGESIZE) is allocated, and the pointer is rounded up
161 // to PAGESIZE alignment, there will be space for a void* at the end
162 // after PAGESIZE bytes used by the GC.
163
164
165 private import gcx : PAGESIZE;
166
167
168 const size_t PAGE_MASK = PAGESIZE - 1;
169
170
171 void *os_mem_map(size_t nbytes)
172 { byte *p, q;
173 p = cast(byte *) malloc(nbytes + PAGESIZE);
174 q = p + ((PAGESIZE - ((cast(size_t) p & PAGE_MASK))) & PAGE_MASK);
175 * cast(void**)(q + nbytes) = p;
176 return q;
177 }
178
179
180 int os_mem_commit(void *base, size_t offset, size_t nbytes)
181 {
182 return 0;
183 }
184
185
186 int os_mem_decommit(void *base, size_t offset, size_t nbytes)
187 {
188 return 0;
189 }
190
191
192 int os_mem_unmap(void *base, size_t nbytes)
193 {
194 free( *cast(void**)( cast(byte*) base + nbytes ) );
195 return 0;
196 }
197 }
198 else
199 {
200 static assert(false, "No supported allocation methods available.");
201 }