Skip to content

Commit

Permalink
Add missing functionality for RegionListAllocator.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Feb 15, 2024
1 parent fd72314 commit 4634d7d
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion source/vibe/container/internal/utilallocator.d
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ void ensureNotInGC(T)(string info = null) nothrow


final class RegionListAllocator(Allocator, bool leak = false) : IAllocator {
import vibe.internal.memory_legacy : AllocSize, alignedSize;
import std.algorithm.comparison : min, max;
import std.conv : emplace;

Expand Down Expand Up @@ -228,3 +227,38 @@ final class RegionListAllocator(Allocator, bool leak = false) : IAllocator {
return true;
}
}

unittest {
auto alloc = new RegionListAllocator!(shared(GCAllocator))(1024, GCAllocator.instance);
auto mem = alloc.allocate(8);
assert(mem.length == 8);
alloc.deallocateAll();
}

template AllocSize(T)
{
static if (is(T == class)) {
// workaround for a strange bug where AllocSize!SSLStream == 0: TODO: dustmite!
enum dummy = T.stringof ~ __traits(classInstanceSize, T).stringof;
enum AllocSize = __traits(classInstanceSize, T);
} else {
enum AllocSize = T.sizeof;
}
}

enum size_t alignment = 0x10;
enum size_t alignmentMask = alignment-1;

size_t alignedSize(size_t sz) nothrow
{
return ((sz + alignment - 1) / alignment) * alignment;
}

unittest {
foreach( i; 0 .. 20 ){
auto ia = alignedSize(i);
assert(ia >= i);
assert((ia & alignmentMask) == 0);
assert(ia < i+alignment);
}
}

0 comments on commit 4634d7d

Please sign in to comment.