Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih committed Dec 1, 2023
1 parent 0dd1c05 commit d25fef8
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions src/runtime/gc_custom_extalloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ var (
// of all allocations and to find references between them.
type allocEntry struct {
start, end uintptr // header + data + padding
next *allocEntry
marked bool
next *allocEntry // used by the scan queue
}

// Representation of a slice header (without the backing array).
Expand Down Expand Up @@ -417,18 +418,11 @@ func mark(addr uintptr) bool {

// Search the allocation list for the address and mark it if found
curAlloc := searchAllocations(addr)

if curAlloc != nil && curAlloc.next == nil {
if curAlloc != nil && !curAlloc.marked {
curAlloc.marked = true
// Push the allocation onto the scan queue
nextAlloc := scanQueue

if nextAlloc == nil {
// Insert a loop, so we can tell that this isn't marked
nextAlloc = curAlloc
}

scanQueue, curAlloc.next = curAlloc, nextAlloc

curAlloc.next = scanQueue
scanQueue = curAlloc
return true
}

Expand All @@ -443,15 +437,7 @@ func finishMarking() {
for scanQueue != nil {
// Pop a marked allocation off of the scan queue.
curAlloc := scanQueue
nextAlloc := curAlloc.next

// This is the last value on the queue.
if nextAlloc == curAlloc {
nextAlloc = nil
}

scanQueue = nextAlloc

scanQueue = curAlloc.next
// Scan and mark all allocations that are referenced
// by this allocation and adds them to the scan queue.
scan(curAlloc.start, curAlloc.end)
Expand All @@ -465,25 +451,23 @@ func sweep() {

for _, curAlloc := range allocations {
// This was never marked
if curAlloc.next == nil {
if !curAlloc.marked {
free(unsafe.Pointer(curAlloc.start))
continue
}
// Move this down in the list
// Move this down in the list to keep it compact
allocations[j] = curAlloc
j++

// Re-calculate used memory.
gcTotalAlloc += uint64(curAlloc.end - curAlloc.start)
}

// Remove the allocation from the list of allocations
allocations = allocations[:j]
}

// Unmarks all allocations in the allocations list.
func unmarkAllocations() {
for i := range allocations {
allocations[i].marked = false
allocations[i].next = nil
}
}
Expand Down

0 comments on commit d25fef8

Please sign in to comment.