2 * This module contains allocation functions for the garbage collector.
4 * Copyright: Copyright (C) 2005-2006 Digital Mars, www.digitalmars.com.
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, in both source and binary form, subject to the following
16 * o The origin of this software must not be misrepresented; you must not
17 * claim that you wrote the original software. If you use this software
18 * in a product, an acknowledgment in the product documentation would be
19 * appreciated but is not required.
20 * o Altered source versions must be plainly marked as such, and must not
21 * be misrepresented as being the original software.
22 * o This notice may not be removed or altered from any source
24 * Authors: Walter Bright, David Friedman, Sean Kelly
45 LPVOID VirtualAlloc(LPVOID, DWORD, DWORD, DWORD);
46 WINBOOL VirtualFree(LPVOID, DWORD, DWORD);
48 else version (Posix) {
49 version (linux) enum: bool { OPTIONAL_LARGEFILE_SUPPORT = true }
50 else version (solaris) enum: bool { OPTIONAL_LARGEFILE_SUPPORT = true }
51 else enum: bool { OPTIONAL_LARGEFILE_SUPPORT = false }
52 static if (OPTIONAL_LARGEFILE_SUPPORT)
53 enum: bool { USE_LARGEFILE64 = ((void*).sizeof == 4) }
55 enum: bool { USE_LARGEFILE64 = false }
56 static if (USE_LARGEFILE64 || (void*).sizeof > int.sizeof)
69 const MAP_FAILED = cast(void*) -1;
70 // Non-standard, but needed
71 version (linux) { enum: int { MAP_ANON = 0x20 } }
72 else version (darwin) { enum: int { MAP_ANON = 0x1000 } }
73 else version (freebsd) { enum: int { MAP_ANON = 0x1000 } }
74 else version (solaris) { enum: int { MAP_ANON = 0x100 } }
75 void* mmap(void*, size_t, int, int, int, off_t);
76 int munmap(void*, size_t);
92 void* os_mem_map(size_t nbytes);
100 int os_mem_commit(void* base, size_t offset, size_t nbytes);
108 int os_mem_decommit(void* base, size_t offset, size_t nbytes);
111 * Unmap memory allocated with os_mem_map().
112 * Memory must have already been decommitted.
117 int os_mem_unmap(void* base, size_t nbytes);
120 else static if (is(typeof(VirtualAlloc)))
122 void* os_mem_map(size_t nbytes)
124 return VirtualAlloc(null, nbytes, MEM_RESERVE, PAGE_READWRITE);
127 int os_mem_commit(void* base, size_t offset, size_t nbytes)
129 void* p = VirtualAlloc(base + offset, nbytes, MEM_COMMIT, PAGE_READWRITE);
130 return cast(int)(p is null);
133 int os_mem_decommit(void* base, size_t offset, size_t nbytes)
135 return cast(int)(VirtualFree(base + offset, nbytes, MEM_DECOMMIT) == 0);
138 int os_mem_unmap(void* base, size_t nbytes)
140 return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0);
143 else static if (is(typeof(mmap)) && is(typeof(MAP_ANON)))
145 void* os_mem_map(size_t nbytes)
147 void* p = mmap(null, nbytes,
148 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
149 return (p == MAP_FAILED) ? null : p;
152 int os_mem_commit(void* base, size_t offset, size_t nbytes)
157 int os_mem_decommit(void* base, size_t offset, size_t nbytes)
162 int os_mem_unmap(void* base, size_t nbytes)
164 return munmap(base, nbytes);
167 else static if (is(typeof(malloc)))
169 // NOTE: This assumes malloc granularity is at least (void*).sizeof. If
170 // (req_size + PAGESIZE) is allocated, and the pointer is rounded up
171 // to PAGESIZE alignment, there will be space for a void* at the end
172 // after PAGESIZE bytes used by the GC.
174 import gcx; // for PAGESIZE
176 const size_t PAGE_MASK = PAGESIZE - 1;
178 void* os_mem_map(size_t nbytes)
181 p = cast(byte* ) malloc(nbytes + PAGESIZE);
182 q = p + ((PAGESIZE - ((cast(size_t) p & PAGE_MASK))) & PAGE_MASK);
183 *cast(void**)(q + nbytes) = p;
187 int os_mem_commit(void* base, size_t offset, size_t nbytes)
192 int os_mem_decommit(void* base, size_t offset, size_t nbytes)
197 int os_mem_unmap(void* base, size_t nbytes)
199 free(*cast(void**)(cast(byte*) base + nbytes));
205 static assert(false, "No supported allocation methods available.");