From d8944d1a3011a7b9b2ffcebf41fdb5d6d3b03467 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 19 Aug 2010 22:48:57 -0300 Subject: [PATCH] Allow testing for fork() availability The concurrent GC will fork() to run the collection, so it need to know if the underlying OS supports it. This patch renames the alloc module to os to group all needed OS abstractions in one module. --- rt/gc/cdgc/bits.d | 18 +++++------ rt/gc/cdgc/gc.d | 6 ++-- rt/gc/cdgc/{alloc.d => os.d} | 61 +++++++++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 23 deletions(-) rename rt/gc/cdgc/{alloc.d => os.d} (70%) diff --git a/rt/gc/cdgc/bits.d b/rt/gc/cdgc/bits.d index 9a8d1b4..3dd8f34 100644 --- a/rt/gc/cdgc/bits.d +++ b/rt/gc/cdgc/bits.d @@ -26,7 +26,7 @@ module rt.gc.cdgc.bits; -import rt.gc.cdgc.alloc: os_mem_map, os_mem_unmap, Vis; +import os = rt.gc.cdgc.os; import cstring = tango.stdc.string; @@ -67,15 +67,15 @@ struct GCBits return (nwords + 2) * uint.sizeof; // +2 for sentinels } - void Dtor(Vis vis = Vis.PRIV) + void Dtor(os.Vis vis = os.Vis.PRIV) { - // Even when os_mem_unmap() can be called with a null pointer, the - // extra call might be significant. On hard GC benchmarks making the - // test for null here (i.e. not making the call) can reduce the GC time - // by almost ~5%. + // Even when os.dealloc() can be called with a null pointer, the extra + // call might be significant. On hard GC benchmarks making the test for + // null here (i.e. not making the call) can reduce the GC time by + // almost ~5%. if (data) { - os_mem_unmap(data, data_size, vis); + os.dealloc(data, data_size, vis); data = null; } } @@ -88,11 +88,11 @@ struct GCBits } } - void alloc(size_t nbits, Vis vis = Vis.PRIV) + void alloc(size_t nbits, os.Vis vis = os.Vis.PRIV) { this.nbits = nbits; this.nwords = (nbits + (BITS_PER_WORD - 1)) >> BITS_SHIFT; - this.data = cast(uint*) os_mem_map(data_size, vis); + this.data = cast(uint*) os.alloc(data_size, vis); if (!data) onOutOfMemoryError(); } diff --git a/rt/gc/cdgc/gc.d b/rt/gc/cdgc/gc.d index cadda73..0084481 100644 --- a/rt/gc/cdgc/gc.d +++ b/rt/gc/cdgc/gc.d @@ -45,7 +45,7 @@ version = STACKGROWSDOWN; // growing the stack means subtracting from the import rt.gc.cdgc.bits: GCBits; import rt.gc.cdgc.stats: GCStats, Stats; import dynarray = rt.gc.cdgc.dynarray; -import alloc = rt.gc.cdgc.alloc; +import os = rt.gc.cdgc.os; import opts = rt.gc.cdgc.opts; import cstdlib = tango.stdc.stdlib; @@ -1846,7 +1846,7 @@ struct Pool { size_t poolsize = npages * PAGESIZE; assert(poolsize >= POOLSIZE); - baseAddr = cast(byte *) alloc.os_mem_map(poolsize); + baseAddr = cast(byte *) os.alloc(poolsize); // Some of the code depends on page alignment of memory pools assert((cast(size_t)baseAddr & (PAGESIZE - 1)) == 0); @@ -1881,7 +1881,7 @@ struct Pool if (npages) { - result = alloc.os_mem_unmap(baseAddr, npages * PAGESIZE); + result = os.dealloc(baseAddr, npages * PAGESIZE); assert(result); npages = 0; } diff --git a/rt/gc/cdgc/alloc.d b/rt/gc/cdgc/os.d similarity index 70% rename from rt/gc/cdgc/alloc.d rename to rt/gc/cdgc/os.d index eeeab73..81939d2 100644 --- a/rt/gc/cdgc/alloc.d +++ b/rt/gc/cdgc/os.d @@ -21,10 +21,49 @@ * be misrepresented as being the original software. * o This notice may not be removed or altered from any source * distribution. - * Authors: Walter Bright, David Friedman, Sean Kelly + * Authors: Walter Bright, David Friedman, Sean Kelly, Leandro Lucarella */ -module rt.gc.cdgc.alloc; +module rt.gc.cdgc.os; + + +// Fork +//////////////////////////////////////////////////////////////////////// + +// Public interface/Documentation + +version (D_Ddoc) { + +/** + * Indicates if an implementation support fork(). + * + * The value shown here is just demostrative, the real value is defined based + * on the OS it's being compiled in. + */ +const HAVE_FORK = true; + +public import tango.stdc.posix.unistd: pid_t, fork; +public import tango.stdc.posix.sys.wait: waitpid; + +} + +// Implementations +else version (Posix) { + enum { HAVE_FORK = true } + public import tango.stdc.posix.unistd: pid_t, fork; + public import tango.stdc.posix.sys.wait: waitpid; +} + +else { + enum { HAVE_FORK = false } + alias int pid_t; + pid_t fork() { assert (false); return -1; } + pid_t waitpid(pid_t, int*, int) { assert (false); return -1; } +} + + +// Allocation +//////////////////////////////////////////////////////////////////////// version (Win32) import tango.sys.win32.UserGdi; @@ -58,15 +97,15 @@ const HAVE_SHARED = false; /** * Map memory. */ -void* os_mem_map(size_t nbytes, Vis vis = Vis.PRIV); +void* alloc(size_t nbytes, Vis vis = Vis.PRIV); /** - * Unmap memory allocated with os_mem_map(). + * Unmap memory allocated with alloc(). * Returns: * true success * false failure */ -bool os_mem_unmap(void* base, size_t nbytes, Vis vis = Vis.PRIV); +bool dealloc(void* base, size_t nbytes, Vis vis = Vis.PRIV); } @@ -74,14 +113,14 @@ bool os_mem_unmap(void* base, size_t nbytes, Vis vis = Vis.PRIV); else static if (is(typeof(VirtualAlloc))) { enum { HAVE_SHARED = false } - void* os_mem_map(size_t nbytes, Vis vis = Vis.PRIV) + void* alloc(size_t nbytes, Vis vis = Vis.PRIV) { assert (vis == Vis.PRIV); return VirtualAlloc(null, nbytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); } - bool os_mem_unmap(void* base, size_t nbytes, Vis vis = Vis.PRIV) + bool dealloc(void* base, size_t nbytes, Vis vis = Vis.PRIV) { assert (vis == Vis.PRIV); return VirtualFree(base, 0, MEM_RELEASE) != 0; @@ -92,7 +131,7 @@ else static if (is(typeof(VirtualAlloc))) { else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) { enum { HAVE_SHARED = true } - void* os_mem_map(size_t nbytes, Vis vis = Vis.PRIV) + void* alloc(size_t nbytes, Vis vis = Vis.PRIV) { auto flags = MAP_ANON; if (vis == Vis.SHARED) @@ -103,7 +142,7 @@ else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) { return (p == MAP_FAILED) ? null : p; } - bool os_mem_unmap(void* base, size_t nbytes, Vis vis = Vis.PRIV) + bool dealloc(void* base, size_t nbytes, Vis vis = Vis.PRIV) { // vis is not necessary to unmap return munmap(base, nbytes) == 0; @@ -122,7 +161,7 @@ else static if (is(typeof(malloc))) { const size_t PAGE_MASK = PAGESIZE - 1; - void* os_mem_map(size_t nbytes, Vis vis = Vis.PRIV) + void* alloc(size_t nbytes, Vis vis = Vis.PRIV) { assert (vis == Vis.PRIV); byte* p, q; @@ -132,7 +171,7 @@ else static if (is(typeof(malloc))) { return q; } - bool os_mem_unmap(void* base, size_t nbytes, Vis vis = Vis.PRIV) + bool dealloc(void* base, size_t nbytes, Vis vis = Vis.PRIV) { assert (vis == Vis.PRIV); free(*cast(void**)(cast(byte*) base + nbytes)); -- 2.43.0