Take MmapPool from the standard builds

This commit is contained in:
Eugen Wissner 2018-03-29 16:54:56 +02:00
parent 792d289541
commit af45de842e
6 changed files with 49 additions and 88 deletions

View File

@ -8,7 +8,7 @@
* Allocators are classes encapsulating memory allocation strategy. This allows * Allocators are classes encapsulating memory allocation strategy. This allows
* to decouple memory management from the algorithms and the data. * 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/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)

View File

@ -5,7 +5,7 @@
/** /**
* Allocator based on $(D_PSYMBOL malloc), $(D_PSYMBOL realloc) and $(D_PSYMBOL free). * 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/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)

View File

@ -3,9 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * 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/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
@ -18,68 +18,41 @@ import std.algorithm.comparison;
import tanya.memory.allocator; import tanya.memory.allocator;
import tanya.memory.op; 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, void* p = mmap(null,
MAP_FAILED, len,
MAP_PRIVATE, PROT_READ | PROT_WRITE,
PROT_READ, MAP_PRIVATE | MAP_ANON,
PROT_WRITE; -1,
import core.sys.posix.unistd; 0);
return p is MAP_FAILED ? null : p;
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;
}
} }
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; return munmap(cast(void*) addr, len) == 0;
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;
}
} }
/* /*
@ -106,7 +79,6 @@ else version (Windows)
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* </pre> * </pre>
*/ */
deprecated("Use tanya.memory.mallocator instead")
final class MmapPool : Allocator final class MmapPool : Allocator
{ {
version (none) version (none)
@ -156,7 +128,7 @@ final class MmapPool : Allocator
return data is null ? null : data[0 .. size]; return data is null ? null : data[0 .. size];
} }
version (TanyaNative) @nogc nothrow pure unittest @nogc nothrow pure unittest
{ {
auto p = MmapPool.instance.allocate(20); auto p = MmapPool.instance.allocate(20);
assert(p); assert(p);
@ -167,7 +139,7 @@ final class MmapPool : Allocator
} }
// Issue 245: https://issues.caraus.io/issues/245. // Issue 245: https://issues.caraus.io/issues/245.
version (TanyaNative) @nogc nothrow pure unittest @nogc nothrow pure unittest
{ {
// allocate() check. // allocate() check.
size_t tooMuchMemory = size_t.max size_t tooMuchMemory = size_t.max
@ -299,7 +271,7 @@ final class MmapPool : Allocator
return true; return true;
} }
version (TanyaNative) @nogc nothrow pure unittest @nogc nothrow pure unittest
{ {
auto p = MmapPool.instance.allocate(20); auto p = MmapPool.instance.allocate(20);
@ -382,7 +354,7 @@ final class MmapPool : Allocator
return true; return true;
} }
version (TanyaNative) @nogc nothrow pure unittest @nogc nothrow pure unittest
{ {
void[] p; void[] p;
assert(!MmapPool.instance.reallocateInPlace(p, 5)); assert(!MmapPool.instance.reallocateInPlace(p, 5));
@ -447,7 +419,7 @@ final class MmapPool : Allocator
return true; return true;
} }
version (TanyaNative) @nogc nothrow pure unittest @nogc nothrow pure unittest
{ {
void[] p; void[] p;
MmapPool.instance.reallocate(p, 10 * int.sizeof); MmapPool.instance.reallocate(p, 10 * int.sizeof);
@ -480,19 +452,10 @@ final class MmapPool : Allocator
if (instance_ is null) if (instance_ is null)
{ {
// Get system dependend page size. // Get system dependend page size.
version (Posix) size_t pageSize = sysconf(_SC_PAGE_SIZE);
if (pageSize < 65536)
{ {
size_t pageSize = sysconf(_SC_PAGE_SIZE); pageSize = pageSize * 65536 / pageSize;
if (pageSize < 65536)
{
pageSize = pageSize * 65536 / pageSize;
}
}
else version (Windows)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
size_t pageSize = si.dwPageSize;
} }
const instanceSize = addAlignment(__traits(classInstanceSize, const instanceSize = addAlignment(__traits(classInstanceSize,
@ -521,7 +484,7 @@ final class MmapPool : Allocator
return (cast(GetPureInstance!MmapPool) &instantiate)(); return (cast(GetPureInstance!MmapPool) &instantiate)();
} }
version (TanyaNative) @nogc nothrow pure unittest @nogc nothrow pure unittest
{ {
assert(instance is instance); assert(instance is instance);
} }
@ -626,7 +589,7 @@ final class MmapPool : Allocator
return alignment_; return alignment_;
} }
version (TanyaNative) @nogc nothrow pure unittest @nogc nothrow pure unittest
{ {
assert(MmapPool.instance.alignment == MmapPool.alignment_); assert(MmapPool.instance.alignment == MmapPool.alignment_);
} }
@ -657,8 +620,6 @@ final class MmapPool : Allocator
private alias Block = shared BlockEntry*; private alias Block = shared BlockEntry*;
} }
version (TanyaNative):
// A lot of allocations/deallocations, but it is the minimum caused a // A lot of allocations/deallocations, but it is the minimum caused a
// segmentation fault because MmapPool reallocateInPlace moves a block wrong. // segmentation fault because MmapPool reallocateInPlace moves a block wrong.
@nogc nothrow pure unittest @nogc nothrow pure unittest

View File

@ -5,7 +5,7 @@
/** /**
* Set of operations on memory blocks. * 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/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)

View File

@ -5,7 +5,7 @@
/** /**
* Dynamic memory management. * Dynamic memory management.
* *
* Copyright: Eugene Wissner 2016-2017. * Copyright: Eugene Wissner 2016-2018.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)

View File

@ -14,7 +14,7 @@
* $(LI Unique ownership) * $(LI Unique ownership)
* ) * )
* *
* Copyright: Eugene Wissner 2016-2017. * Copyright: Eugene Wissner 2016-2018.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)