From af45de842e3d69a4d07788b5153902d21601d91c Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 29 Mar 2018 16:54:56 +0200 Subject: [PATCH] Take MmapPool from the standard builds --- source/tanya/memory/allocator.d | 2 +- source/tanya/memory/mallocator.d | 2 +- source/tanya/memory/mmappool.d | 127 +++++++++++-------------------- source/tanya/memory/op.d | 2 +- source/tanya/memory/package.d | 2 +- source/tanya/memory/smartref.d | 2 +- 6 files changed, 49 insertions(+), 88 deletions(-) diff --git a/source/tanya/memory/allocator.d b/source/tanya/memory/allocator.d index 881a790..0eaff65 100644 --- a/source/tanya/memory/allocator.d +++ b/source/tanya/memory/allocator.d @@ -8,7 +8,7 @@ * Allocators are classes encapsulating memory allocation strategy. This allows * to decouple memory management from the algorithms and the data. * - * Copyright: Eugene Wissner 2016-2017. + * Copyright: Eugene Wissner 2016-2018. * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * Mozilla Public License, v. 2.0). * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) diff --git a/source/tanya/memory/mallocator.d b/source/tanya/memory/mallocator.d index 4e37a05..ce868b8 100644 --- a/source/tanya/memory/mallocator.d +++ b/source/tanya/memory/mallocator.d @@ -5,7 +5,7 @@ /** * Allocator based on $(D_PSYMBOL malloc), $(D_PSYMBOL realloc) and $(D_PSYMBOL free). * - * Copyright: Eugene Wissner 2017. + * Copyright: Eugene Wissner 2017-2018. * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * Mozilla Public License, v. 2.0). * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) diff --git a/source/tanya/memory/mmappool.d b/source/tanya/memory/mmappool.d index b0f68a9..b555580 100644 --- a/source/tanya/memory/mmappool.d +++ b/source/tanya/memory/mmappool.d @@ -3,9 +3,9 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* - * Native allocator for Posix and Windows. + * Native allocator. * - * Copyright: Eugene Wissner 2016-2017. + * Copyright: Eugene Wissner 2016-2018. * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * Mozilla Public License, v. 2.0). * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) @@ -18,68 +18,41 @@ import std.algorithm.comparison; import tanya.memory.allocator; import tanya.memory.op; -version (Posix) +version (TanyaNative): + +import core.sys.posix.sys.mman : MAP_ANON, + MAP_FAILED, + MAP_PRIVATE, + PROT_READ, + PROT_WRITE; +import core.sys.posix.unistd; + +extern(C) +private void* mmap(void* addr, + size_t len, + int prot, + int flags, + int fd, + off_t offset) pure nothrow @system @nogc; + +extern(C) +private int munmap(void* addr, size_t len) pure nothrow @system @nogc; + +private void* mapMemory(const size_t len) pure nothrow @system @nogc { - import core.sys.posix.sys.mman : MAP_ANON, - MAP_FAILED, - MAP_PRIVATE, - PROT_READ, - PROT_WRITE; - import core.sys.posix.unistd; - - extern(C) - private void* mmap(void* addr, - size_t len, - int prot, - int flags, - int fd, - off_t offset) pure nothrow @system @nogc; - - extern(C) - private int munmap(void* addr, size_t len) pure nothrow @system @nogc; - - private void* mapMemory(const size_t len) pure nothrow @system @nogc - { - void* p = mmap(null, - len, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON, - -1, - 0); - return p is MAP_FAILED ? null : p; - } - - private bool unmapMemory(shared void* addr, const size_t len) - pure nothrow @system @nogc - { - return munmap(cast(void*) addr, len) == 0; - } + void* p = mmap(null, + len, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON, + -1, + 0); + return p is MAP_FAILED ? null : p; } -else version (Windows) + +private bool unmapMemory(shared void* addr, const size_t len) +pure nothrow @system @nogc { - import core.sys.windows.winbase : GetSystemInfo, SYSTEM_INFO; - - extern(Windows) - private void* VirtualAlloc(void*, size_t, uint, uint) - pure nothrow @system @nogc; - - extern(Windows) - private int VirtualFree(void* addr, size_t len, uint) - pure nothrow @system @nogc; - - private void* mapMemory(const size_t len) pure nothrow @system @nogc - { - return VirtualAlloc(null, - len, - 0x00001000, // MEM_COMMIT - 0x04); // PAGE_READWRITE - } - - private bool unmapMemory(shared void* addr, const size_t len) - pure nothrow @system @nogc - { - return VirtualFree(cast(void*) addr, 0, 0x8000) == 0; - } + return munmap(cast(void*) addr, len) == 0; } /* @@ -106,7 +79,6 @@ else version (Windows) * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * */ -deprecated("Use tanya.memory.mallocator instead") final class MmapPool : Allocator { version (none) @@ -156,7 +128,7 @@ final class MmapPool : Allocator return data is null ? null : data[0 .. size]; } - version (TanyaNative) @nogc nothrow pure unittest + @nogc nothrow pure unittest { auto p = MmapPool.instance.allocate(20); assert(p); @@ -167,7 +139,7 @@ final class MmapPool : Allocator } // Issue 245: https://issues.caraus.io/issues/245. - version (TanyaNative) @nogc nothrow pure unittest + @nogc nothrow pure unittest { // allocate() check. size_t tooMuchMemory = size_t.max @@ -299,7 +271,7 @@ final class MmapPool : Allocator return true; } - version (TanyaNative) @nogc nothrow pure unittest + @nogc nothrow pure unittest { auto p = MmapPool.instance.allocate(20); @@ -382,7 +354,7 @@ final class MmapPool : Allocator return true; } - version (TanyaNative) @nogc nothrow pure unittest + @nogc nothrow pure unittest { void[] p; assert(!MmapPool.instance.reallocateInPlace(p, 5)); @@ -447,7 +419,7 @@ final class MmapPool : Allocator return true; } - version (TanyaNative) @nogc nothrow pure unittest + @nogc nothrow pure unittest { void[] p; MmapPool.instance.reallocate(p, 10 * int.sizeof); @@ -480,19 +452,10 @@ final class MmapPool : Allocator if (instance_ is null) { // Get system dependend page size. - version (Posix) + size_t pageSize = sysconf(_SC_PAGE_SIZE); + if (pageSize < 65536) { - size_t pageSize = sysconf(_SC_PAGE_SIZE); - if (pageSize < 65536) - { - pageSize = pageSize * 65536 / pageSize; - } - } - else version (Windows) - { - SYSTEM_INFO si; - GetSystemInfo(&si); - size_t pageSize = si.dwPageSize; + pageSize = pageSize * 65536 / pageSize; } const instanceSize = addAlignment(__traits(classInstanceSize, @@ -521,7 +484,7 @@ final class MmapPool : Allocator return (cast(GetPureInstance!MmapPool) &instantiate)(); } - version (TanyaNative) @nogc nothrow pure unittest + @nogc nothrow pure unittest { assert(instance is instance); } @@ -626,7 +589,7 @@ final class MmapPool : Allocator return alignment_; } - version (TanyaNative) @nogc nothrow pure unittest + @nogc nothrow pure unittest { assert(MmapPool.instance.alignment == MmapPool.alignment_); } @@ -657,8 +620,6 @@ final class MmapPool : Allocator private alias Block = shared BlockEntry*; } -version (TanyaNative): - // A lot of allocations/deallocations, but it is the minimum caused a // segmentation fault because MmapPool reallocateInPlace moves a block wrong. @nogc nothrow pure unittest diff --git a/source/tanya/memory/op.d b/source/tanya/memory/op.d index e113ba0..0a73020 100644 --- a/source/tanya/memory/op.d +++ b/source/tanya/memory/op.d @@ -5,7 +5,7 @@ /** * Set of operations on memory blocks. * - * Copyright: Eugene Wissner 2017. + * Copyright: Eugene Wissner 2017-2018. * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * Mozilla Public License, v. 2.0). * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) diff --git a/source/tanya/memory/package.d b/source/tanya/memory/package.d index 565a8bb..a0ccfb5 100644 --- a/source/tanya/memory/package.d +++ b/source/tanya/memory/package.d @@ -5,7 +5,7 @@ /** * Dynamic memory management. * - * Copyright: Eugene Wissner 2016-2017. + * Copyright: Eugene Wissner 2016-2018. * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * Mozilla Public License, v. 2.0). * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) diff --git a/source/tanya/memory/smartref.d b/source/tanya/memory/smartref.d index b877ce6..5a97ebe 100644 --- a/source/tanya/memory/smartref.d +++ b/source/tanya/memory/smartref.d @@ -14,7 +14,7 @@ * $(LI Unique ownership) * ) * - * Copyright: Eugene Wissner 2016-2017. + * Copyright: Eugene Wissner 2016-2018. * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * Mozilla Public License, v. 2.0). * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)