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,
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,
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
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 :