Skip to content

Commit

Permalink
[GC] Use AdressRange to check if pointer is in a slab.
Browse files Browse the repository at this point in the history
  • Loading branch information
deadalnix committed Oct 17, 2024
1 parent ea3ea6b commit 93c01f1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
20 changes: 17 additions & 3 deletions sdlib/d/gc/range.d
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,22 @@ private:
size_t length;

public:
@property
void* ptr() {
return cast(void*) -base;
}

this(const void* ptr, size_t size) {
base = -(cast(ptrdiff_t) ptr);
length = size;
}

this(const void[] range) {
base = -(cast(ptrdiff_t) range.ptr);
length = range.length;
this(range.ptr, range.length);
}

this(const void* start, const void* stop) {
this(start[0 .. stop - start]);
this(start, stop - start);
}

bool contains(const void* ptr) const {
Expand Down Expand Up @@ -131,6 +140,7 @@ public:

unittest AddressRange {
auto r = AddressRange(cast(void*) AddressSpace, null);
assert(r.ptr is cast(void*) AddressSpace);

assert(!r.contains(null));
assert(!r.contains(cast(void*) 1));
Expand Down Expand Up @@ -188,6 +198,10 @@ unittest AddressRange {
auto r2 = AddressRange(cast(void*) 4160, cast(void*) 4240);
auto r3 = AddressRange(cast(void*) 4100, cast(void*) 4200);

assert(r1.ptr is cast(void*) 4000);
assert(r2.ptr is cast(void*) 4160);
assert(r3.ptr is cast(void*) 4100);

// Disjoint ranges.
auto r12 = AddressRange(cast(void*) 4000, cast(void*) 4240);
checkMerge(r1, r2, r12);
Expand Down
18 changes: 9 additions & 9 deletions sdlib/d/gc/scanner.d
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private:
* Scan the stack and TLS.
*
* It may seems counter intuitive that we do so for worker threads
* as well,b ut it turns out to be necessary. NPTL caches resources
* as well, but it turns out to be necessary. NPTL caches resources
* necessary to start a thread after a thread exits, to be able to
* restart new ones quickly and cheaply.
*
Expand Down Expand Up @@ -133,8 +133,8 @@ private:
* If there is, then we pick it up and start marking.
*
* Alternatively, if there is no work to do, and the number
* of active thread is 0, then we know no more work is comming
* and we shoudl stop.
* of active thread is 0, then we know no more work is coming
* and we should stop.
*/
static hasWork(Scanner* w) {
return w.cursor != 0 || w.activeThreads == 0;
Expand Down Expand Up @@ -244,7 +244,7 @@ public:
auto ms = scanner.managedAddressSpace;
auto cycle = scanner.gcCycle;

const(void*)[] lastDenseSlab;
AddressRange lastDenseSlab;
PageDescriptor lastDenseSlabPageDescriptor;

import d.gc.slab;
Expand All @@ -263,7 +263,7 @@ public:

if (lastDenseSlab.contains(ptr)) {
MarkDense:
auto base = cast(void*) lastDenseSlab.ptr;
auto base = lastDenseSlab.ptr;
auto offset = ptr - base;
auto index = lastDenseBin.computeIndex(offset);

Expand Down Expand Up @@ -296,9 +296,9 @@ public:
lastDenseSlabPageDescriptor = pd;
lastDenseBin = binInfos[ec.sizeClass];

auto base = cast(void**) (aptr - pd.index * PageSize);
lastDenseSlab =
base[0 .. lastDenseBin.npages * PointerInPage];
AddressRange(aptr - pd.index * PageSize,
lastDenseBin.npages * PointerInPage);

goto MarkDense;
}
Expand All @@ -311,7 +311,7 @@ public:
}

// In case we reached our limit, we bail. This ensures that
// we can scan iterratively.
// we can scan iteratively.
if (cursor == 0) {
return;
}
Expand All @@ -327,7 +327,7 @@ private:
/**
* /!\ This is not thread safe.
*
* In the context of concurent scans, slots might be
* In the context of concurrent scans, slots might be
* allocated/deallocated from the slab while we scan.
* It is unclear how to handle this at this time.
*/
Expand Down

0 comments on commit 93c01f1

Please sign in to comment.