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
31 import tango.sys.win32.UserGdi;
35 pthread_t pthread_self()
37 return cast(pthread_t) GetCurrentThreadId();
40 //version = GC_Use_Alloc_Win32;
44 import tango.stdc.posix.sys.mman;
45 import tango.stdc.stdlib;
47 //version = GC_Use_Alloc_MMap;
51 import tango.stdc.stdlib;
53 //version = GC_Use_Alloc_Malloc;
57 static if(is(typeof(VirtualAlloc)))
58 version = GC_Use_Alloc_Win32;
59 else static if (is(typeof(mmap)))
60 version = GC_Use_Alloc_MMap;
61 else static if (is(typeof(valloc)))
62 version = GC_Use_Alloc_Valloc;
63 else static if (is(typeof(malloc)))
64 version = GC_Use_Alloc_Malloc;
65 else static assert(false, "No supported allocation methods available.");
68 static if (is(typeof(VirtualAlloc))) // version (GC_Use_Alloc_Win32)
73 void *os_mem_map(size_t nbytes)
75 return VirtualAlloc(null, nbytes, MEM_RESERVE, PAGE_READWRITE);
85 int os_mem_commit(void *base, size_t offset, size_t nbytes)
88 p = VirtualAlloc(base + offset, nbytes, MEM_COMMIT, PAGE_READWRITE);
89 return cast(int)(p is null);
99 int os_mem_decommit(void *base, size_t offset, size_t nbytes)
101 return cast(int)(VirtualFree(base + offset, nbytes, MEM_DECOMMIT) == 0);
106 * Unmap memory allocated with os_mem_map().
107 * Memory must have already been decommitted.
112 int os_mem_unmap(void *base, size_t nbytes)
114 return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0);
117 else static if (is(typeof(mmap))) // else version (GC_Use_Alloc_MMap)
119 void *os_mem_map(size_t nbytes)
122 p = mmap(null, nbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
123 return (p == MAP_FAILED) ? null : p;
127 int os_mem_commit(void *base, size_t offset, size_t nbytes)
133 int os_mem_decommit(void *base, size_t offset, size_t nbytes)
139 int os_mem_unmap(void *base, size_t nbytes)
141 return munmap(base, nbytes);
144 else static if (is(typeof(valloc))) // else version (GC_Use_Alloc_Valloc)
146 void *os_mem_map(size_t nbytes)
148 return valloc(nbytes);
152 int os_mem_commit(void *base, size_t offset, size_t nbytes)
158 int os_mem_decommit(void *base, size_t offset, size_t nbytes)
164 int os_mem_unmap(void *base, size_t nbytes)
170 else static if (is(typeof(malloc))) // else version (GC_Use_Alloc_Malloc)
172 // NOTE: This assumes malloc granularity is at least (void*).sizeof. If
173 // (req_size + PAGESIZE) is allocated, and the pointer is rounded up
174 // to PAGESIZE alignment, there will be space for a void* at the end
175 // after PAGESIZE bytes used by the GC.
178 import gcx; // for PAGESIZE
181 const size_t PAGE_MASK = PAGESIZE - 1;
184 void *os_mem_map(size_t nbytes)
186 p = cast(byte *) malloc(nbytes + PAGESIZE);
187 q = p + ((PAGESIZE - ((cast(size_t) p & PAGE_MASK))) & PAGE_MASK);
188 * cast(void**)(q + nbytes) = p;
193 int os_mem_commit(void *base, size_t offset, size_t nbytes)
199 int os_mem_decommit(void *base, size_t offset, size_t nbytes)
205 int os_mem_unmap(void *base, size_t nbytes)
207 free( *cast(void**)( cast(byte*) base + nbytes ) );
213 static assert(false, "No supported allocation methods available.");