Skip to content

Commit

Permalink
finalizer as optional param to allocAppendable
Browse files Browse the repository at this point in the history
  • Loading branch information
dsm9000 committed Oct 12, 2023
1 parent 3faa385 commit 2507f37
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions sdlib/d/gc/tcache.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public:
: arena.allocLarge(emap, size, false);
}

void* allocAppendable(size_t size, bool containsPointers) {
void* allocAppendable(size_t size, bool containsPointers,
void* finalizer = null) {
auto asize = getAllocSize(alignUp(size, 2 * Quantum));
assert(isAppendableSizeClass(getSizeClass(asize)),
"allocAppendable got non-appendable size class!");
Expand All @@ -42,12 +43,15 @@ public:
auto ptr = arena.allocSmall(emap, asize);
auto pd = getPageDescriptor(ptr);
setSmallUsedCapacity(pd, ptr, size);
assert(finalizer is null, "finalizer not yet supported for slab!");
return ptr;
}

auto ptr = arena.allocLarge(emap, asize, false);
auto pd = getPageDescriptor(ptr);
pd.extent.setUsedCapacity(size);
auto e = pd.extent;
e.setUsedCapacity(size);
e.setFinalizer(finalizer);
return ptr;
}

Expand Down Expand Up @@ -104,22 +108,6 @@ public:
freeImpl(pd, ptr);
}

bool setFinalizer(void* ptr, void* finalizer) {
auto pd = maybeGetPageDescriptor(ptr);
auto e = pd.extent;
if (e is null) {
return false;
}

if (pd.isSlab()) {
// Slab is not yet supported
return false;
}

e.setFinalizer(finalizer);
return true;
}

void* realloc(void* ptr, size_t size, bool containsPointers) {
if (size == 0) {
free(ptr);
Expand Down Expand Up @@ -866,8 +854,7 @@ unittest finalization {
}

// Finalizers for large allocs:
auto s0 = threadCache.allocAppendable(16384, false);
threadCache.setFinalizer(s0, &destruct);
auto s0 = threadCache.allocAppendable(16384, false, &destruct);
threadCache.destroy(s0);
assert(lastKilledAddress == s0);
assert(lastKilledUsedCapacity == 16384);
Expand Down

0 comments on commit 2507f37

Please sign in to comment.