X-Git-Url: https://git.llucax.com/software/dgc/cdgc.git/blobdiff_plain/ec73552782f845e080e7c46324201351f7df30f2..fdc67622a72cf5863d7faa6667f5e526b524568c:/gc/alloc.d diff --git a/gc/alloc.d b/gc/alloc.d index 9c6d249..c6a8bd1 100644 --- a/gc/alloc.d +++ b/gc/alloc.d @@ -26,7 +26,9 @@ module gc.alloc; + // C OS-specific API + private extern (C) { version (Win32) { alias void* POINTER; @@ -79,106 +81,87 @@ private extern (C) { } } -static if (is(typeof(VirtualAlloc))) + +// Public interface + +version (D_Ddoc) { /** * Map memory. */ - void *os_mem_map(size_t nbytes) - { - return VirtualAlloc(null, nbytes, MEM_RESERVE, PAGE_READWRITE); - } - + void* os_mem_map(size_t nbytes); /** * Commit memory. * Returns: - * 0 success - * !=0 failure + * true success + * false failure */ - int os_mem_commit(void *base, size_t offset, size_t nbytes) - { void *p; - - p = VirtualAlloc(base + offset, nbytes, MEM_COMMIT, PAGE_READWRITE); - return cast(int)(p is null); - } - + int os_mem_commit(void* base, size_t offset, size_t nbytes); /** * Decommit memory. * Returns: - * 0 success - * !=0 failure + * true success + * false failure */ - int os_mem_decommit(void *base, size_t offset, size_t nbytes) - { - return cast(int)(VirtualFree(base + offset, nbytes, MEM_DECOMMIT) == 0); - } - + int os_mem_decommit(void* base, size_t offset, size_t nbytes); /** * Unmap memory allocated with os_mem_map(). * Memory must have already been decommitted. * Returns: - * 0 success - * !=0 failure + * true success + * false failure */ - int os_mem_unmap(void *base, size_t nbytes) - { - return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0); - } + int os_mem_unmap(void* base, size_t nbytes); } -else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) +// Implementations +else static if (is(typeof(VirtualAlloc))) { - void *os_mem_map(size_t nbytes) - { void *p; - - p = mmap(null, nbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); - return (p == MAP_FAILED) ? null : p; + void* os_mem_map(size_t nbytes) + { + return VirtualAlloc(null, nbytes, MEM_RESERVE, PAGE_READWRITE); } - - int os_mem_commit(void *base, size_t offset, size_t nbytes) + int os_mem_commit(void* base, size_t offset, size_t nbytes) { - return 0; + void* p = VirtualAlloc(base + offset, nbytes, MEM_COMMIT, PAGE_READWRITE); + return cast(int)(p is null); } - - int os_mem_decommit(void *base, size_t offset, size_t nbytes) + int os_mem_decommit(void* base, size_t offset, size_t nbytes) { - return 0; + return cast(int)(VirtualFree(base + offset, nbytes, MEM_DECOMMIT) == 0); } - - int os_mem_unmap(void *base, size_t nbytes) + int os_mem_unmap(void* base, size_t nbytes) { - return munmap(base, nbytes); + return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0); } } -else static if (is(typeof(valloc))) +else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) { - void *os_mem_map(size_t nbytes) + void* os_mem_map(size_t nbytes) { - return valloc(nbytes); + void* p = mmap(null, nbytes, + PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + return (p == MAP_FAILED) ? null : p; } - - int os_mem_commit(void *base, size_t offset, size_t nbytes) + int os_mem_commit(void* base, size_t offset, size_t nbytes) { return 0; } - - int os_mem_decommit(void *base, size_t offset, size_t nbytes) + int os_mem_decommit(void* base, size_t offset, size_t nbytes) { return 0; } - - int os_mem_unmap(void *base, size_t nbytes) + int os_mem_unmap(void* base, size_t nbytes) { - free(base); - return 0; + return munmap(base, nbytes); } } else static if (is(typeof(malloc))) @@ -188,37 +171,32 @@ else static if (is(typeof(malloc))) // to PAGESIZE alignment, there will be space for a void* at the end // after PAGESIZE bytes used by the GC. - import gcx; // for PAGESIZE - const size_t PAGE_MASK = PAGESIZE - 1; - - void *os_mem_map(size_t nbytes) - { byte *p, q; - p = cast(byte *) malloc(nbytes + PAGESIZE); + void* os_mem_map(size_t nbytes) + { + byte* p, q; + p = cast(byte* ) malloc(nbytes + PAGESIZE); q = p + ((PAGESIZE - ((cast(size_t) p & PAGE_MASK))) & PAGE_MASK); - * cast(void**)(q + nbytes) = p; + *cast(void**)(q + nbytes) = p; return q; } - - int os_mem_commit(void *base, size_t offset, size_t nbytes) + int os_mem_commit(void* base, size_t offset, size_t nbytes) { return 0; } - - int os_mem_decommit(void *base, size_t offset, size_t nbytes) + int os_mem_decommit(void* base, size_t offset, size_t nbytes) { return 0; } - - int os_mem_unmap(void *base, size_t nbytes) + int os_mem_unmap(void* base, size_t nbytes) { - free( *cast(void**)( cast(byte*) base + nbytes ) ); + free(*cast(void**)(cast(byte*) base + nbytes)); return 0; } }