Skip to content

Commit

Permalink
Set capacity to the target size when using realloc.
Browse files Browse the repository at this point in the history
  • Loading branch information
deadalnix committed Sep 2, 2023
1 parent 4616830 commit d8e9c0a
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions sdlib/d/gc/tcache.d
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,15 @@ public:
copySize = getSizeFromClass(oldSizeClass);
}
} else {
import d.gc.util;
copySize = min(size, pd.extent.usedCapacity);

auto esize = pd.extent.size;
if (samePointerness && alignUp(size, PageSize) == esize) {
pd.extent.setUsedCapacity(copySize);
pd.extent.setUsedCapacity(size);
return ptr;
}

// TODO: Try to extend/shrink in place.
import d.gc.util;
copySize = min(size, pd.extent.usedCapacity);
}

auto newPtr = alloc(size, containsPointers);
Expand All @@ -116,7 +115,7 @@ public:

if (isLargeSize(size)) {
auto npd = getPageDescriptor(newPtr);
npd.extent.setUsedCapacity(copySize);
npd.extent.setUsedCapacity(size);
}

memcpy(newPtr, ptr, copySize);
Expand Down Expand Up @@ -433,22 +432,21 @@ unittest getCapacity {
assert(threadCache.getCapacity(p2[0 .. 20000]) == 0);
assert(threadCache.getCapacity(p2[0 .. 20001]) == 0);

// Increasing the size of the allocation
// does not necesserly increase capacity.
// Increasing the size of the allocation increases capacity.
auto p3 = threadCache.realloc(p2, 20001, false);
assert(p3 is p2);

assert(threadCache.getCapacity(p3[0 .. 19999]) == 20480);
assert(threadCache.getCapacity(p3[0 .. 19999]) == 0);
assert(threadCache.getCapacity(p3[0 .. 20000]) == 0);
assert(threadCache.getCapacity(p3[0 .. 20001]) == 0);
assert(threadCache.getCapacity(p3[0 .. 20001]) == 20480);

auto p4 = threadCache.realloc(p3, 16000, false);
assert(p4 !is p3);
assert(threadCache.getCapacity(p4[0 .. 16000]) == 16384);

auto p5 = threadCache.realloc(p4, 20000, false);
assert(p5 !is p4);
assert(threadCache.getCapacity(p5[0 .. 16000]) == 20480);
assert(threadCache.getCapacity(p5[0 .. 20000]) == 20480);
}

unittest extend {
Expand Down

1 comment on commit d8e9c0a

@dsm9000
Copy link
Contributor

@dsm9000 dsm9000 commented on d8e9c0a Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you increase the used capacity of an alloc without going through extend() ?
Reallocing upwards should only change the available spare capacity, not the used capacity.
This change breaks the expected logic of the whole thing!

Please sign in to comment.