From 5baa584e06b7cfbcdb9de30efe0092033adcd9e1 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sat, 16 Jan 2010 21:48:12 -0300 Subject: [PATCH] Make allocation functions that can fail return bool There is no point on returning int, since no error code is returned, just failure or success. --- gc/alloc.d | 42 +++++++++++++++++++++--------------------- gc/gc.d | 6 +++--- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/gc/alloc.d b/gc/alloc.d index c6a8bd1..575095b 100644 --- a/gc/alloc.d +++ b/gc/alloc.d @@ -97,7 +97,7 @@ version (D_Ddoc) * true success * false failure */ - int os_mem_commit(void* base, size_t offset, size_t nbytes); + bool os_mem_commit(void* base, size_t offset, size_t nbytes); /** * Decommit memory. @@ -105,7 +105,7 @@ version (D_Ddoc) * true success * false failure */ - int os_mem_decommit(void* base, size_t offset, size_t nbytes); + bool os_mem_decommit(void* base, size_t offset, size_t nbytes); /** * Unmap memory allocated with os_mem_map(). @@ -114,7 +114,7 @@ version (D_Ddoc) * true success * false failure */ - int os_mem_unmap(void* base, size_t nbytes); + bool os_mem_unmap(void* base, size_t nbytes); } // Implementations else static if (is(typeof(VirtualAlloc))) @@ -124,20 +124,20 @@ else static if (is(typeof(VirtualAlloc))) return VirtualAlloc(null, nbytes, MEM_RESERVE, PAGE_READWRITE); } - int os_mem_commit(void* base, size_t offset, size_t nbytes) + bool os_mem_commit(void* base, size_t offset, size_t nbytes) { void* p = VirtualAlloc(base + offset, nbytes, MEM_COMMIT, PAGE_READWRITE); - return cast(int)(p is null); + return p !is null; } - int os_mem_decommit(void* base, size_t offset, size_t nbytes) + bool os_mem_decommit(void* base, size_t offset, size_t nbytes) { - return cast(int)(VirtualFree(base + offset, nbytes, MEM_DECOMMIT) == 0); + return VirtualFree(base + offset, nbytes, MEM_DECOMMIT) != 0; } - int os_mem_unmap(void* base, size_t nbytes) + bool os_mem_unmap(void* base, size_t nbytes) { - return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0); + return VirtualFree(base, 0, MEM_RELEASE) != 0; } } else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) @@ -149,19 +149,19 @@ else static if (is(typeof(mmap)) && is(typeof(MAP_ANON))) return (p == MAP_FAILED) ? null : p; } - int os_mem_commit(void* base, size_t offset, size_t nbytes) + bool os_mem_commit(void* base, size_t offset, size_t nbytes) { - return 0; + return true; } - int os_mem_decommit(void* base, size_t offset, size_t nbytes) + bool os_mem_decommit(void* base, size_t offset, size_t nbytes) { - return 0; + return true; } - int os_mem_unmap(void* base, size_t nbytes) + bool os_mem_unmap(void* base, size_t nbytes) { - return munmap(base, nbytes); + return munmap(base, nbytes) == 0; } } else static if (is(typeof(malloc))) @@ -184,20 +184,20 @@ else static if (is(typeof(malloc))) return q; } - int os_mem_commit(void* base, size_t offset, size_t nbytes) + bool os_mem_commit(void* base, size_t offset, size_t nbytes) { - return 0; + return true; } - int os_mem_decommit(void* base, size_t offset, size_t nbytes) + bool os_mem_decommit(void* base, size_t offset, size_t nbytes) { - return 0; + return true; } - int os_mem_unmap(void* base, size_t nbytes) + bool os_mem_unmap(void* base, size_t nbytes) { free(*cast(void**)(cast(byte*) base + nbytes)); - return 0; + return true; } } else diff --git a/gc/gc.d b/gc/gc.d index 8cd5609..3dff4ea 100644 --- a/gc/gc.d +++ b/gc/gc.d @@ -2791,14 +2791,14 @@ struct Pool if (ncommitted) { result = os_mem_decommit(baseAddr, 0, ncommitted * PAGESIZE); - assert(result == 0); + assert(result); ncommitted = 0; } if (npages) { result = os_mem_unmap(baseAddr, npages * PAGESIZE); - assert(result == 0); + assert(result); npages = 0; } @@ -2885,7 +2885,7 @@ struct Pool tocommit = npages - ncommitted; //debug(PRINTF) printf("\tlooking to commit %d more pages\n", tocommit); //fflush(stdout); - if (os_mem_commit(baseAddr, ncommitted * PAGESIZE, tocommit * PAGESIZE) == 0) + if (os_mem_commit(baseAddr, ncommitted * PAGESIZE, tocommit * PAGESIZE)) { .memset(pagetable + ncommitted, B_FREE, tocommit); auto i = ncommitted; -- 2.43.0