Skip to content

Commit

Permalink
make @safe
Browse files Browse the repository at this point in the history
Signed-off-by: denisa.ciobanu1208 <[email protected]>
  • Loading branch information
denisa.ciobanu1208 committed Dec 13, 2024
1 parent b925c16 commit 10fca7e
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions std/experimental/allocator/building_blocks/kernighan_ritchie.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
Source: $(PHOBOSSRC std/experimental/allocator/building_blocks/kernighan_ritchie.d)
*/
module std.experimental.allocator.building_blocks.kernighan_ritchie;
import std.experimental.allocator.building_blocks.null_allocator :
NullAllocator;
import std.experimental.allocator.building_blocks.null_allocator;

//debug = KRRegion;
debug(KRRegion) import std.stdio;
Expand Down Expand Up @@ -362,7 +361,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
/// Ditto
static if (!is(ParentAllocator == NullAllocator)
&& hasMember!(ParentAllocator, "deallocate"))
~this()
@trusted ~this()
{
parent.deallocate(payload);
}
Expand Down Expand Up @@ -652,7 +651,7 @@ fronting the GC allocator.
import std.experimental.allocator.gc_allocator : GCAllocator;
import std.typecons : Ternary;
// KRRegion fronting a general-purpose allocator
align(KRRegion!().alignment) ubyte[1024 * 128] buf;
ubyte[1024 * 128] buf;
auto alloc = fallbackAllocator(KRRegion!()(buf), GCAllocator.instance);
auto b = alloc.allocate(100);
assert(b.length == 100);
Expand All @@ -674,6 +673,7 @@ it actually returns memory to the operating system when possible.
import std.algorithm.comparison : max;
import std.experimental.allocator.building_blocks.allocator_list
: AllocatorList;
import std.experimental.allocator.gc_allocator : GCAllocator;
import std.experimental.allocator.mmap_allocator : MmapAllocator;
AllocatorList!(n => KRRegion!MmapAllocator(max(n * 16, 1024 * 1024))) alloc;
}
Expand All @@ -683,6 +683,7 @@ it actually returns memory to the operating system when possible.
import std.algorithm.comparison : max;
import std.experimental.allocator.building_blocks.allocator_list
: AllocatorList;
import std.experimental.allocator.gc_allocator : GCAllocator;
import std.experimental.allocator.mallocator : Mallocator;
import std.typecons : Ternary;
/*
Expand Down Expand Up @@ -715,6 +716,7 @@ it actually returns memory to the operating system when possible.
import std.algorithm.comparison : max;
import std.experimental.allocator.building_blocks.allocator_list
: AllocatorList;
import std.experimental.allocator.gc_allocator : GCAllocator;
import std.experimental.allocator.mmap_allocator : MmapAllocator;
import std.typecons : Ternary;
/*
Expand Down Expand Up @@ -747,7 +749,6 @@ it actually returns memory to the operating system when possible.
}
}

version (StdUnittest)
@system unittest
{
import std.algorithm.comparison : max;
Expand All @@ -759,12 +760,13 @@ version (StdUnittest)
n => KRRegion!GCAllocator(max(n * 16, 1024 * 1024)))());
}

@safe unittest
@trusted unittest
{
import std.experimental.allocator.gc_allocator : GCAllocator;

auto alloc = KRRegion!GCAllocator(1024 * 1024);



void[][] array;
foreach (i; 1 .. 4)
{
Expand All @@ -785,9 +787,9 @@ version (StdUnittest)
cast(ubyte[])(GCAllocator.instance.allocate(1024 * 1024)));
const store = (() pure nothrow @safe @nogc => alloc.allocate(KRRegion!().sizeof))();
auto p = cast(KRRegion!()* ) store.ptr;
import core.lifetime : emplace;
import core.stdc.string : memcpy;
import std.conv : text;
import std.algorithm.mutation : move;
import std.conv : text, emplace;

memcpy(p, &alloc, alloc.sizeof);
emplace(&alloc);
Expand Down Expand Up @@ -825,14 +827,16 @@ version (StdUnittest)
assert(p.length == 1024 * 1024);
}


@safe unittest
{
import std.random : randomCover;
import std.experimental.allocator.building_blocks;
import std.random;
import std.typecons : Ternary;

// Both sequences must work on either system

// A sequence of allocs which generates the error described in https://issues.dlang.org/show_bug.cgi?id=16564
// A sequence of allocs which generates the error described in issue 16564
// that is a gap at the end of buf from the perspective of the allocator

// for 64 bit systems (leftover balance = 8 bytes < 16)
Expand All @@ -842,10 +846,10 @@ version (StdUnittest)
int[] sizes32 = [81412, 107068, 49892, 23768];


void test(int[] sizes)
@system void test(int[] sizes)
{
align(size_t.sizeof) ubyte[256 * 1024] buf;
auto a = KRRegion!()(buf);
auto a = (() @trusted => createAllocator(buf))();

void[][] bufs;

Expand All @@ -862,12 +866,22 @@ version (StdUnittest)
assert((() pure nothrow @safe @nogc => a.empty)() == Ternary.yes);
}

test(sizes64);
test(sizes32);
() @trusted {
test(sizes64);
test(sizes32);
}();
}

@safe KRRegion!NullAllocator createAllocator(ubyte[] buf)
{
return KRRegion!NullAllocator(buf);
}


@safe unittest
{
import std.experimental.allocator.building_blocks;
import std.random;
import std.typecons : Ternary;

// For 64 bits, we allocate in multiples of 8, but the minimum alloc size is 16.
Expand Down Expand Up @@ -921,7 +935,7 @@ version (StdUnittest)
@system unittest
{ import std.typecons : Ternary;

align(KRRegion!().alignment) ubyte[1024] b;
ubyte[1024] b;
auto alloc = KRRegion!()(b);

auto k = alloc.allocate(128);
Expand All @@ -941,4 +955,4 @@ version (StdUnittest)
assert(alloc.empty == Ternary.no);
assert(alloc.deallocate(k));
assert(alloc.empty == Ternary.yes);
}
}

0 comments on commit 10fca7e

Please sign in to comment.