Remove static constructor from the MmapPool

This commit is contained in:
Eugen Wissner 2017-01-05 07:35:29 +01:00
parent e27d0fe58c
commit 4271c8583e
2 changed files with 43 additions and 33 deletions

View File

@ -661,11 +661,19 @@ struct Vector(T)
body body
{ {
const T* end = vector + length_; const T* end = vector + length_;
for (T* a = r.begin, b = r.end; b != end; ++a, ++b) T* a = r.begin;
for (T* b = r.end; b != end; ++a, ++b)
{ {
*a = *b; *a = *b;
} }
length = length_ - r.length; for (; a != end; ++a)
{
static if (hasElaborateDestructor!T)
{
destroy(*a);
}
--length_;
}
return r; return r;
} }

View File

@ -13,6 +13,7 @@ module tanya.memory.mmappool;
import tanya.memory.allocator; import tanya.memory.allocator;
import core.atomic; import core.atomic;
import core.exception; import core.exception;
import core.stdc.string;
version (Posix) version (Posix)
{ {
@ -50,21 +51,6 @@ else version (Windows)
*/ */
final class MmapPool : Allocator final class MmapPool : Allocator
{ {
@nogc:
shared static this()
{
version (Posix)
{
pageSize = sysconf(_SC_PAGE_SIZE);
}
else version (Windows)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
pageSize = si.dwPageSize;
}
}
/** /**
* Allocates $(D_PARAM size) bytes of memory. * Allocates $(D_PARAM size) bytes of memory.
* *
@ -73,7 +59,7 @@ final class MmapPool : Allocator
* *
* Returns: Pointer to the new allocated memory. * Returns: Pointer to the new allocated memory.
*/ */
void[] allocate(size_t size) shared nothrow void[] allocate(size_t size) shared nothrow @nogc
{ {
if (!size) if (!size)
{ {
@ -109,7 +95,7 @@ final class MmapPool : Allocator
* *
* Returns: Data the block points to or $(D_KEYWORD null). * Returns: Data the block points to or $(D_KEYWORD null).
*/ */
private void* findBlock(size_t size) shared nothrow private void* findBlock(size_t size) shared nothrow @nogc
{ {
Block block1; Block block1;
RegionLoop: for (auto r = head; r !is null; r = r.next) RegionLoop: for (auto r = head; r !is null; r = r.next)
@ -167,7 +153,7 @@ final class MmapPool : Allocator
* *
* Returns: Whether the deallocation was successful. * Returns: Whether the deallocation was successful.
*/ */
bool deallocate(void[] p) shared nothrow bool deallocate(void[] p) shared nothrow @nogc
{ {
if (p is null) if (p is null)
{ {
@ -223,7 +209,7 @@ final class MmapPool : Allocator
* *
* Returns: Whether the reallocation was successful. * Returns: Whether the reallocation was successful.
*/ */
bool reallocate(ref void[] p, size_t size) shared nothrow bool reallocate(ref void[] p, size_t size) shared nothrow @nogc
{ {
void[] reallocP; void[] reallocP;
@ -291,17 +277,33 @@ final class MmapPool : Allocator
* *
* Returns: Global $(D_PSYMBOL MmapPool) instance. * Returns: Global $(D_PSYMBOL MmapPool) instance.
*/ */
static @property ref shared(MmapPool) instance() nothrow static @property ref shared(MmapPool) instance() nothrow @nogc
{ {
if (instance_ is null) if (instance_ is null)
{ {
// Get system dependend page size.
version (Posix)
{
pageSize = sysconf(_SC_PAGE_SIZE);
if (pageSize < 65536)
{
atomicOp!"*="(pageSize, 65536 / pageSize);
}
}
else version (Windows)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
pageSize = si.dwPageSize;
}
immutable instanceSize = addAlignment(__traits(classInstanceSize, MmapPool)); immutable instanceSize = addAlignment(__traits(classInstanceSize, MmapPool));
Region head; // Will become soon our region list head Region head; // Will become soon our region list head
void* data = initializeRegion(instanceSize, head); void* data = initializeRegion(instanceSize, head);
if (data !is null) if (data !is null)
{ {
data[0..instanceSize] = typeid(MmapPool).initializer[]; memcpy(data, typeid(MmapPool).initializer.ptr, instanceSize);
instance_ = cast(shared MmapPool) data; instance_ = cast(shared MmapPool) data;
instance_.head = head; instance_.head = head;
} }
@ -324,8 +326,8 @@ final class MmapPool : Allocator
* *
* Returns: A pointer to the data. * Returns: A pointer to the data.
*/ */
private static void* initializeRegion(size_t size, private static void* initializeRegion(size_t size, ref Region head)
ref Region head) nothrow nothrow @nogc
{ {
immutable regionSize = calculateRegionSize(size); immutable regionSize = calculateRegionSize(size);
@ -349,9 +351,9 @@ final class MmapPool : Allocator
else version (Windows) else version (Windows)
{ {
void* p = VirtualAlloc(null, void* p = VirtualAlloc(null,
regionSize, regionSize,
MEM_COMMIT, MEM_COMMIT,
PAGE_READWRITE); PAGE_READWRITE);
if (p is null) if (p is null)
{ {
if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY)
@ -398,7 +400,7 @@ final class MmapPool : Allocator
} }
/// Ditto. /// Ditto.
private void* initializeRegion(size_t size) shared nothrow private void* initializeRegion(size_t size) shared nothrow @nogc
{ {
return initializeRegion(size, head); return initializeRegion(size, head);
} }
@ -411,7 +413,7 @@ final class MmapPool : Allocator
*/ */
pragma(inline) pragma(inline)
private static immutable(size_t) addAlignment(size_t x) private static immutable(size_t) addAlignment(size_t x)
@safe pure nothrow pure nothrow @safe @nogc
out (result) out (result)
{ {
assert(result > 0); assert(result > 0);
@ -429,7 +431,7 @@ final class MmapPool : Allocator
*/ */
pragma(inline) pragma(inline)
private static immutable(size_t) calculateRegionSize(size_t x) private static immutable(size_t) calculateRegionSize(size_t x)
@safe pure nothrow nothrow @safe @nogc
out (result) out (result)
{ {
assert(result > 0); assert(result > 0);
@ -440,7 +442,7 @@ final class MmapPool : Allocator
return x / pageSize * pageSize + pageSize; return x / pageSize * pageSize + pageSize;
} }
@property uint alignment() shared const pure nothrow @safe @property uint alignment() shared const pure nothrow @safe @nogc
{ {
return alignment_; return alignment_;
} }
@ -448,7 +450,7 @@ final class MmapPool : Allocator
private static shared MmapPool instance_; private static shared MmapPool instance_;
private shared static immutable size_t pageSize; private shared static size_t pageSize;
private shared struct RegionEntry private shared struct RegionEntry
{ {