From: Leandro Lucarella Date: Thu, 19 Aug 2010 23:17:49 +0000 (-0300) Subject: alloc: Use tango to access OS-API X-Git-Url: https://git.llucax.com/software/dgc/cdgc.git/commitdiff_plain/0849555bf578d233e054052f87c55c2cdf46d575 alloc: Use tango to access OS-API --- diff --git a/rt/gc/cdgc/alloc.d b/rt/gc/cdgc/alloc.d index cc03862..845fccb 100644 --- a/rt/gc/cdgc/alloc.d +++ b/rt/gc/cdgc/alloc.d @@ -26,82 +26,35 @@ module rt.gc.cdgc.alloc; +version (Win32) + import tango.sys.win32.UserGdi; +else version (Posix) + import tango.stdc.posix.sys.mman; +else + import tango.stdc.stdlib; -// C OS-specific API - -private extern (C) { - version (Win32) { - alias void* POINTER; - alias POINTER LPVOID; - alias uint DWORD; - alias int WINBOOL; - enum: DWORD { - PAGE_READWRITE = 4, - MEM_RESERVE = 8192, - MEM_COMMIT = 4096, - MEM_DECOMMIT = 16384, - MEM_RELEASE = 32768, - } - LPVOID VirtualAlloc(LPVOID, DWORD, DWORD, DWORD); - WINBOOL VirtualFree(LPVOID, DWORD, DWORD); - } - else version (Posix) { - version (linux) enum: bool { OPTIONAL_LARGEFILE_SUPPORT = true } - else version (solaris) enum: bool { OPTIONAL_LARGEFILE_SUPPORT = true } - else enum: bool { OPTIONAL_LARGEFILE_SUPPORT = false } - static if (OPTIONAL_LARGEFILE_SUPPORT) - enum: bool { USE_LARGEFILE64 = ((void*).sizeof == 4) } - else - enum: bool { USE_LARGEFILE64 = false } - static if (USE_LARGEFILE64 || (void*).sizeof > int.sizeof) - alias long off_t; - else - alias int off_t; - enum: int { - PROT_NONE = 0x0, - PROT_READ = 0x1, - PROT_WRITE = 0x2, - PROT_EXEC = 0x4, - MAP_SHARED = 0x01, - MAP_PRIVATE = 0x02, - MAP_FIXED = 0x10, - } - const MAP_FAILED = cast(void*) -1; - // Non-standard, but needed - version (linux) { enum: int { MAP_ANON = 0x20 } } - else version (darwin) { enum: int { MAP_ANON = 0x1000 } } - else version (freebsd) { enum: int { MAP_ANON = 0x1000 } } - else version (solaris) { enum: int { MAP_ANON = 0x100 } } - void* mmap(void*, size_t, int, int, int, off_t); - int munmap(void*, size_t); - } - else { - // Standard C library - import gc.libc; - } -} +// Public interface/Documentation + +version (D_Ddoc) { -// Public interface +/** + * Map memory. + */ +void* os_mem_map(size_t nbytes); -version (D_Ddoc) -{ - /** - * Map memory. - */ - void* os_mem_map(size_t nbytes); +/** + * Unmap memory allocated with os_mem_map(). + * Returns: + * true success + * false failure + */ +bool os_mem_unmap(void* base, size_t nbytes); - /** - * Unmap memory allocated with os_mem_map(). - * Returns: - * true success - * false failure - */ - bool os_mem_unmap(void* base, size_t nbytes); } + // Implementations -else static if (is(typeof(VirtualAlloc))) -{ +else static if (is(typeof(VirtualAlloc))) { void* os_mem_map(size_t nbytes) { return VirtualAlloc(null, nbytes, MEM_RESERVE | MEM_COMMIT, @@ -113,8 +66,8 @@ else static if (is(typeof(VirtualAlloc))) return VirtualFree(base, 0, MEM_RELEASE) != 0; } } -else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) -{ + +else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) { void* os_mem_map(size_t nbytes) { void* p = mmap(null, nbytes, @@ -127,8 +80,8 @@ else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) return munmap(base, nbytes) == 0; } } -else static if (is(typeof(malloc))) -{ + +else static if (is(typeof(malloc))) { // NOTE: This assumes malloc granularity is at least (void*).sizeof. If // (req_size + PAGESIZE) is allocated, and the pointer is rounded up // to PAGESIZE alignment, there will be space for a void* at the end @@ -153,10 +106,8 @@ else static if (is(typeof(malloc))) return true; } } -else -{ - static assert(false, "No supported allocation methods available."); -} + +else static assert(false, "No supported allocation methods available."); // vim: set et sw=4 sts=4 :