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, Leandro Lucarella
31 ////////////////////////////////////////////////////////////////////////
33 // Public interface/Documentation
38 * Indicates if an implementation support fork().
40 * The value shown here is just demostrative, the real value is defined based
41 * on the OS it's being compiled in.
43 const HAVE_FORK = true;
45 public import tango.stdc.posix.unistd: pid_t, fork;
46 public import tango.stdc.posix.sys.wait: waitpid;
51 else version (Posix) {
52 enum { HAVE_FORK = true }
53 public import tango.stdc.posix.unistd: pid_t, fork;
54 public import tango.stdc.posix.sys.wait: waitpid;
58 enum { HAVE_FORK = false }
60 pid_t fork() { assert (false); return -1; }
61 pid_t waitpid(pid_t, int*, int) { assert (false); return -1; }
66 ////////////////////////////////////////////////////////////////////////
69 import tango.sys.win32.UserGdi;
71 import tango.stdc.posix.sys.mman;
73 import tango.stdc.stdlib;
76 // Public interface/Documentation
79 * Visibility of the mapped memory.
83 PRIV, /// Private to this process
84 SHARED, /// Shared across fork()ed processes (only when HAVE_SHARED)
90 * Indicates if an implementation support mapping shared memory.
92 * The value shown here is just demostrative, the real value is defined based
93 * on the OS it's being compiled in.
95 const HAVE_SHARED = false;
100 void* alloc(size_t nbytes, Vis vis = Vis.PRIV);
103 * Unmap memory allocated with alloc().
108 bool dealloc(void* base, size_t nbytes, Vis vis = Vis.PRIV);
113 else static if (is(typeof(VirtualAlloc))) {
114 enum { HAVE_SHARED = false }
116 void* alloc(size_t nbytes, Vis vis = Vis.PRIV)
118 assert (vis == Vis.PRIV);
119 return VirtualAlloc(null, nbytes, MEM_RESERVE | MEM_COMMIT,
123 bool dealloc(void* base, size_t nbytes, Vis vis = Vis.PRIV)
125 assert (vis == Vis.PRIV);
126 return VirtualFree(base, 0, MEM_RELEASE) != 0;
131 else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) {
132 enum { HAVE_SHARED = true }
134 void* alloc(size_t nbytes, Vis vis = Vis.PRIV)
136 auto flags = MAP_ANON;
137 if (vis == Vis.SHARED)
140 flags |= MAP_PRIVATE;
141 void* p = mmap(null, nbytes, PROT_READ | PROT_WRITE, flags, -1, 0);
142 return (p == MAP_FAILED) ? null : p;
145 bool dealloc(void* base, size_t nbytes, Vis vis = Vis.PRIV)
147 // vis is not necessary to unmap
148 return munmap(base, nbytes) == 0;
152 else static if (is(typeof(malloc))) {
153 enum { HAVE_SHARED = false }
155 // NOTE: This assumes malloc granularity is at least (void*).sizeof. If
156 // (req_size + PAGESIZE) is allocated, and the pointer is rounded up
157 // to PAGESIZE alignment, there will be space for a void* at the end
158 // after PAGESIZE bytes used by the GC.
160 import rt.gc.cdgc.gc: PAGESIZE;
162 const size_t PAGE_MASK = PAGESIZE - 1;
164 void* alloc(size_t nbytes, Vis vis = Vis.PRIV)
166 assert (vis == Vis.PRIV);
168 p = cast(byte* ) malloc(nbytes + PAGESIZE);
169 q = p + ((PAGESIZE - ((cast(size_t) p & PAGE_MASK))) & PAGE_MASK);
170 *cast(void**)(q + nbytes) = p;
174 bool dealloc(void* base, size_t nbytes, Vis vis = Vis.PRIV)
176 assert (vis == Vis.PRIV);
177 free(*cast(void**)(cast(byte*) base + nbytes));
182 else static assert(false, "No supported allocation methods available.");
185 // vim: set et sw=4 sts=4 :