From f0d016bcde823ab65e29028dd4b82ad2de41baff Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 15 Jun 2017 10:27:12 +0200 Subject: [PATCH] Replace in and immutable with const in allocators --- source/tanya/memory/allocator.d | 7 +++--- source/tanya/memory/mallocator.d | 35 ++++++++++++++++++++------- source/tanya/memory/mmappool.d | 41 +++++++++++++++++++------------- source/tanya/memory/package.d | 29 +++++++++++++++++++++- 4 files changed, 83 insertions(+), 29 deletions(-) diff --git a/source/tanya/memory/allocator.d b/source/tanya/memory/allocator.d index 560d9a1..dca44d0 100644 --- a/source/tanya/memory/allocator.d +++ b/source/tanya/memory/allocator.d @@ -28,7 +28,7 @@ interface Allocator * * 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. @@ -49,7 +49,7 @@ interface Allocator * * 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 @@ -63,5 +63,6 @@ interface Allocator * * 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; } diff --git a/source/tanya/memory/mallocator.d b/source/tanya/memory/mallocator.d index 0e4ac7f..9e0f5fb 100644 --- a/source/tanya/memory/mallocator.d +++ b/source/tanya/memory/mallocator.d @@ -11,7 +11,6 @@ module tanya.memory.mallocator; import core.stdc.stdlib; -import std.algorithm.comparison; import tanya.memory.allocator; /** @@ -27,9 +26,9 @@ final class Mallocator : Allocator * * 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; } @@ -42,10 +41,11 @@ final class Mallocator : Allocator @nogc nothrow unittest { auto p = Mallocator.instance.allocate(20); - assert(p.length == 20); - Mallocator.instance.deallocate(p); + + p = Mallocator.instance.allocate(0); + assert(p.length == 0); } /** @@ -89,6 +89,13 @@ final class Mallocator : Allocator return false; } + /// + @nogc nothrow unittest + { + void[] p; + assert(!Mallocator.instance.reallocateInPlace(p, 8)); + } + /** * Increases or decreases the size of a memory block. * @@ -144,12 +151,24 @@ final class Mallocator : Allocator 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. */ @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) { - immutable size = __traits(classInstanceSize, Mallocator) + psize; + const size = __traits(classInstanceSize, Mallocator) + psize; void* p = malloc(size); if (p !is null) @@ -179,7 +198,7 @@ final class Mallocator : Allocator assert(instance is instance); } - private enum psize = 8; + private enum ushort psize = 8; private shared static Mallocator instance_; } diff --git a/source/tanya/memory/mmappool.d b/source/tanya/memory/mmappool.d index 5adbf10..4d64a0f 100644 --- a/source/tanya/memory/mmappool.d +++ b/source/tanya/memory/mmappool.d @@ -73,13 +73,13 @@ final class MmapPool : Allocator * * 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) { return null; } - immutable dataSize = addAlignment(size); + const dataSize = addAlignment(size); void* data = findBlock(dataSize); if (data is null) @@ -94,13 +94,14 @@ final class MmapPool : Allocator nothrow unittest { auto p = MmapPool.instance.allocate(20); - assert(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 * 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). */ - private void* findBlock(in ref size_t size) shared nothrow @nogc + private void* findBlock(const ref size_t size) shared nothrow @nogc { Block block1; 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. */ - 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) { @@ -262,8 +264,8 @@ final class MmapPool : Allocator p = p.ptr[0 .. size]; return true; } - immutable dataSize = addAlignment(size); - immutable delta = dataSize - addAlignment(p.length); + const dataSize = addAlignment(size); + const delta = dataSize - addAlignment(p.length); if (block1.next is null || !block1.next.free @@ -333,7 +335,7 @@ final class MmapPool : Allocator * * 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) { @@ -419,7 +421,8 @@ final class MmapPool : Allocator pageSize = si.dwPageSize; } - immutable instanceSize = addAlignment(__traits(classInstanceSize, MmapPool)); + const instanceSize = addAlignment(__traits(classInstanceSize, + MmapPool)); Region head; // Will become soon our region list head void* data = initializeRegion(instanceSize, head); @@ -451,7 +454,7 @@ final class MmapPool : Allocator private static void* initializeRegion(size_t size, ref Region head) nothrow @nogc { - immutable regionSize = calculateRegionSize(size); + const regionSize = calculateRegionSize(size); version (Posix) { @@ -524,8 +527,7 @@ final class MmapPool : Allocator * * Returns: Aligned size of $(D_PARAM x). */ - private static immutable(size_t) addAlignment(size_t x) - pure nothrow @safe @nogc + private static size_t addAlignment(size_t x) pure nothrow @safe @nogc out (result) { assert(result > 0); @@ -541,8 +543,7 @@ final class MmapPool : Allocator * * Returns: Minimum region size (a multiple of $(D_PSYMBOL pageSize)). */ - private static immutable(size_t) calculateRegionSize(size_t x) - nothrow @safe @nogc + private static size_t calculateRegionSize(size_t x) nothrow @safe @nogc out (result) { assert(result > 0); @@ -560,7 +561,13 @@ final class MmapPool : Allocator { 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 size_t pageSize; diff --git a/source/tanya/memory/package.d b/source/tanya/memory/package.d index 352476f..f48fbd1 100644 --- a/source/tanya/memory/package.d +++ b/source/tanya/memory/package.d @@ -15,6 +15,7 @@ import std.algorithm.iteration; public import std.experimental.allocator : make; import std.traits; public import tanya.memory.allocator; +import tanya.memory.mmappool; /** * The mixin generates common methods for classes and structs using @@ -88,7 +89,6 @@ shared Allocator allocator; shared static this() nothrow @trusted @nogc { - import tanya.memory.mmappool; allocator = MmapPool.instance; } @@ -112,6 +112,17 @@ body .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 * object of type $(D_PARAM T). @@ -307,3 +318,19 @@ private unittest 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); +}