Take MmapPool from the standard builds
This commit is contained in:
parent
792d289541
commit
af45de842e
@ -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)
|
||||||
|
@ -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)
|
||||||
|
@ -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,8 +18,8 @@ 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,
|
import core.sys.posix.sys.mman : MAP_ANON,
|
||||||
MAP_FAILED,
|
MAP_FAILED,
|
||||||
MAP_PRIVATE,
|
MAP_PRIVATE,
|
||||||
@ -54,33 +54,6 @@ version (Posix)
|
|||||||
{
|
{
|
||||||
return munmap(cast(void*) addr, len) == 0;
|
return munmap(cast(void*) addr, len) == 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else version (Windows)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This allocator allocates memory in regions (multiple of 64 KB for example).
|
* This allocator allocates memory in regions (multiple of 64 KB for example).
|
||||||
@ -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,20 +452,11 @@ 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);
|
size_t pageSize = sysconf(_SC_PAGE_SIZE);
|
||||||
if (pageSize < 65536)
|
if (pageSize < 65536)
|
||||||
{
|
{
|
||||||
pageSize = pageSize * 65536 / pageSize;
|
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,
|
||||||
MmapPool));
|
MmapPool));
|
||||||
@ -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
|
||||||
|
@ -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)
|
||||||
|
@ -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)
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user