Replace in and immutable with const in allocators
This commit is contained in:
parent
70e96c62b3
commit
f0d016bcde
@ -28,7 +28,7 @@ interface Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Pointer to the new allocated memory.
|
* Returns: Pointer to the new allocated memory.
|
||||||
*/
|
*/
|
||||||
void[] allocate(in size_t size) shared nothrow @nogc;
|
void[] allocate(const size_t size) shared nothrow @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deallocates a memory block.
|
* Deallocates a memory block.
|
||||||
@ -49,7 +49,7 @@ interface Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Pointer to the allocated memory.
|
* Returns: Pointer to the allocated memory.
|
||||||
*/
|
*/
|
||||||
bool reallocate(ref void[] p, in size_t size) shared nothrow @nogc;
|
bool reallocate(ref void[] p, const size_t size) shared nothrow @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reallocates a memory block in place if possible or returns
|
* Reallocates a memory block in place if possible or returns
|
||||||
@ -63,5 +63,6 @@ interface Allocator
|
|||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
||||||
*/
|
*/
|
||||||
bool reallocateInPlace(ref void[] p, in size_t size) shared nothrow @nogc;
|
bool reallocateInPlace(ref void[] p, const size_t size)
|
||||||
|
shared nothrow @nogc;
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
module tanya.memory.mallocator;
|
module tanya.memory.mallocator;
|
||||||
|
|
||||||
import core.stdc.stdlib;
|
import core.stdc.stdlib;
|
||||||
import std.algorithm.comparison;
|
|
||||||
import tanya.memory.allocator;
|
import tanya.memory.allocator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,9 +26,9 @@ final class Mallocator : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: The pointer to the new allocated memory.
|
* Returns: The pointer to the new allocated memory.
|
||||||
*/
|
*/
|
||||||
void[] allocate(in size_t size) shared nothrow @nogc
|
void[] allocate(const size_t size) shared nothrow @nogc
|
||||||
{
|
{
|
||||||
if (!size)
|
if (size == 0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -42,10 +41,11 @@ final class Mallocator : Allocator
|
|||||||
@nogc nothrow unittest
|
@nogc nothrow unittest
|
||||||
{
|
{
|
||||||
auto p = Mallocator.instance.allocate(20);
|
auto p = Mallocator.instance.allocate(20);
|
||||||
|
|
||||||
assert(p.length == 20);
|
assert(p.length == 20);
|
||||||
|
|
||||||
Mallocator.instance.deallocate(p);
|
Mallocator.instance.deallocate(p);
|
||||||
|
|
||||||
|
p = Mallocator.instance.allocate(0);
|
||||||
|
assert(p.length == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -89,6 +89,13 @@ final class Mallocator : Allocator
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@nogc nothrow unittest
|
||||||
|
{
|
||||||
|
void[] p;
|
||||||
|
assert(!Mallocator.instance.reallocateInPlace(p, 8));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increases or decreases the size of a memory block.
|
* Increases or decreases the size of a memory block.
|
||||||
*
|
*
|
||||||
@ -144,12 +151,24 @@ final class Mallocator : Allocator
|
|||||||
assert(p is null);
|
assert(p is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fails with false.
|
||||||
|
private @nogc nothrow unittest
|
||||||
|
{
|
||||||
|
void[] p;
|
||||||
|
assert(!Mallocator.instance.reallocate(p, size_t.max - Mallocator.psize * 2));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: The alignment offered.
|
* Returns: The alignment offered.
|
||||||
*/
|
*/
|
||||||
@property uint alignment() shared const pure nothrow @safe @nogc
|
@property uint alignment() shared const pure nothrow @safe @nogc
|
||||||
{
|
{
|
||||||
return cast(uint) max(double.alignof, real.alignof);
|
return (void*).alignof;
|
||||||
|
}
|
||||||
|
|
||||||
|
private nothrow @nogc unittest
|
||||||
|
{
|
||||||
|
assert(Mallocator.instance.alignment == (void*).alignof);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -161,7 +180,7 @@ final class Mallocator : Allocator
|
|||||||
{
|
{
|
||||||
if (instance_ is null)
|
if (instance_ is null)
|
||||||
{
|
{
|
||||||
immutable size = __traits(classInstanceSize, Mallocator) + psize;
|
const size = __traits(classInstanceSize, Mallocator) + psize;
|
||||||
void* p = malloc(size);
|
void* p = malloc(size);
|
||||||
|
|
||||||
if (p !is null)
|
if (p !is null)
|
||||||
@ -179,7 +198,7 @@ final class Mallocator : Allocator
|
|||||||
assert(instance is instance);
|
assert(instance is instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum psize = 8;
|
private enum ushort psize = 8;
|
||||||
|
|
||||||
private shared static Mallocator instance_;
|
private shared static Mallocator instance_;
|
||||||
}
|
}
|
||||||
|
@ -73,13 +73,13 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Pointer to the new allocated memory.
|
* Returns: Pointer to the new allocated memory.
|
||||||
*/
|
*/
|
||||||
void[] allocate(in size_t size) shared nothrow @nogc
|
void[] allocate(const size_t size) shared nothrow @nogc
|
||||||
{
|
{
|
||||||
if (!size)
|
if (!size)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
immutable dataSize = addAlignment(size);
|
const dataSize = addAlignment(size);
|
||||||
|
|
||||||
void* data = findBlock(dataSize);
|
void* data = findBlock(dataSize);
|
||||||
if (data is null)
|
if (data is null)
|
||||||
@ -94,13 +94,14 @@ final class MmapPool : Allocator
|
|||||||
nothrow unittest
|
nothrow unittest
|
||||||
{
|
{
|
||||||
auto p = MmapPool.instance.allocate(20);
|
auto p = MmapPool.instance.allocate(20);
|
||||||
|
|
||||||
assert(p);
|
assert(p);
|
||||||
|
|
||||||
MmapPool.instance.deallocate(p);
|
MmapPool.instance.deallocate(p);
|
||||||
|
|
||||||
|
p = MmapPool.instance.allocate(0);
|
||||||
|
assert(p.length == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Search for a block large enough to keep $(D_PARAM size) and split it
|
* Search for a block large enough to keep $(D_PARAM size) and split it
|
||||||
* into two blocks if the block is too large.
|
* into two blocks if the block is too large.
|
||||||
*
|
*
|
||||||
@ -109,7 +110,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(in ref size_t size) shared nothrow @nogc
|
private void* findBlock(const ref 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)
|
||||||
@ -242,7 +243,8 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
||||||
*/
|
*/
|
||||||
bool reallocateInPlace(ref void[] p, in size_t size) shared nothrow @nogc
|
bool reallocateInPlace(ref void[] p, const size_t size)
|
||||||
|
shared nothrow @nogc
|
||||||
{
|
{
|
||||||
if (p is null || size == 0)
|
if (p is null || size == 0)
|
||||||
{
|
{
|
||||||
@ -262,8 +264,8 @@ final class MmapPool : Allocator
|
|||||||
p = p.ptr[0 .. size];
|
p = p.ptr[0 .. size];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
immutable dataSize = addAlignment(size);
|
const dataSize = addAlignment(size);
|
||||||
immutable delta = dataSize - addAlignment(p.length);
|
const delta = dataSize - addAlignment(p.length);
|
||||||
|
|
||||||
if (block1.next is null
|
if (block1.next is null
|
||||||
|| !block1.next.free
|
|| !block1.next.free
|
||||||
@ -333,7 +335,7 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Whether the reallocation was successful.
|
* Returns: Whether the reallocation was successful.
|
||||||
*/
|
*/
|
||||||
bool reallocate(ref void[] p, in size_t size) shared nothrow @nogc
|
bool reallocate(ref void[] p, const size_t size) shared nothrow @nogc
|
||||||
{
|
{
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
{
|
{
|
||||||
@ -419,7 +421,8 @@ final class MmapPool : Allocator
|
|||||||
pageSize = si.dwPageSize;
|
pageSize = si.dwPageSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
immutable instanceSize = addAlignment(__traits(classInstanceSize, MmapPool));
|
const 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);
|
||||||
@ -451,7 +454,7 @@ final class MmapPool : Allocator
|
|||||||
private static void* initializeRegion(size_t size, ref Region head)
|
private static void* initializeRegion(size_t size, ref Region head)
|
||||||
nothrow @nogc
|
nothrow @nogc
|
||||||
{
|
{
|
||||||
immutable regionSize = calculateRegionSize(size);
|
const regionSize = calculateRegionSize(size);
|
||||||
|
|
||||||
version (Posix)
|
version (Posix)
|
||||||
{
|
{
|
||||||
@ -524,8 +527,7 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Aligned size of $(D_PARAM x).
|
* Returns: Aligned size of $(D_PARAM x).
|
||||||
*/
|
*/
|
||||||
private static immutable(size_t) addAlignment(size_t x)
|
private static size_t addAlignment(size_t x) pure nothrow @safe @nogc
|
||||||
pure nothrow @safe @nogc
|
|
||||||
out (result)
|
out (result)
|
||||||
{
|
{
|
||||||
assert(result > 0);
|
assert(result > 0);
|
||||||
@ -541,8 +543,7 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Minimum region size (a multiple of $(D_PSYMBOL pageSize)).
|
* Returns: Minimum region size (a multiple of $(D_PSYMBOL pageSize)).
|
||||||
*/
|
*/
|
||||||
private static immutable(size_t) calculateRegionSize(size_t x)
|
private static size_t calculateRegionSize(size_t x) nothrow @safe @nogc
|
||||||
nothrow @safe @nogc
|
|
||||||
out (result)
|
out (result)
|
||||||
{
|
{
|
||||||
assert(result > 0);
|
assert(result > 0);
|
||||||
@ -560,7 +561,13 @@ final class MmapPool : Allocator
|
|||||||
{
|
{
|
||||||
return alignment_;
|
return alignment_;
|
||||||
}
|
}
|
||||||
private enum alignment_ = 8;
|
|
||||||
|
private nothrow @nogc unittest
|
||||||
|
{
|
||||||
|
assert(MmapPool.instance.alignment == MmapPool.alignment_);
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum uint alignment_ = 8;
|
||||||
|
|
||||||
private shared static MmapPool instance_;
|
private shared static MmapPool instance_;
|
||||||
private shared static size_t pageSize;
|
private shared static size_t pageSize;
|
||||||
|
@ -15,6 +15,7 @@ import std.algorithm.iteration;
|
|||||||
public import std.experimental.allocator : make;
|
public import std.experimental.allocator : make;
|
||||||
import std.traits;
|
import std.traits;
|
||||||
public import tanya.memory.allocator;
|
public import tanya.memory.allocator;
|
||||||
|
import tanya.memory.mmappool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The mixin generates common methods for classes and structs using
|
* The mixin generates common methods for classes and structs using
|
||||||
@ -88,7 +89,6 @@ shared Allocator allocator;
|
|||||||
|
|
||||||
shared static this() nothrow @trusted @nogc
|
shared static this() nothrow @trusted @nogc
|
||||||
{
|
{
|
||||||
import tanya.memory.mmappool;
|
|
||||||
allocator = MmapPool.instance;
|
allocator = MmapPool.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,6 +112,17 @@ body
|
|||||||
.allocator = allocator;
|
.allocator = allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private nothrow @nogc unittest
|
||||||
|
{
|
||||||
|
import tanya.memory.mallocator;
|
||||||
|
|
||||||
|
auto oldAllocator = defaultAllocator;
|
||||||
|
defaultAllocator = Mallocator.instance;
|
||||||
|
assert(defaultAllocator is Mallocator.instance);
|
||||||
|
|
||||||
|
defaultAllocator = oldAllocator;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the size in bytes of the state that needs to be allocated to hold an
|
* Returns the size in bytes of the state that needs to be allocated to hold an
|
||||||
* object of type $(D_PARAM T).
|
* object of type $(D_PARAM T).
|
||||||
@ -307,3 +318,19 @@ private unittest
|
|||||||
|
|
||||||
defaultAllocator.dispose(p);
|
defaultAllocator.dispose(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Works with interfaces.
|
||||||
|
private unittest
|
||||||
|
{
|
||||||
|
interface I
|
||||||
|
{
|
||||||
|
}
|
||||||
|
class C : I
|
||||||
|
{
|
||||||
|
}
|
||||||
|
auto c = defaultAllocator.make!C();
|
||||||
|
I i = c;
|
||||||
|
|
||||||
|
defaultAllocator.dispose(i);
|
||||||
|
defaultAllocator.dispose(i);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user