mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 22:43:40 +00:00
compiler,runtime: implement a portable conservative GC
This commit is contained in:
@@ -282,8 +282,8 @@ func GC() {
|
||||
}
|
||||
|
||||
// Mark phase: mark all reachable objects, recursively.
|
||||
markRoots(globalsStart, globalsEnd)
|
||||
markRoots(getCurrentStackPointer(), stackTop) // assume a descending stack
|
||||
markGlobals()
|
||||
markStack()
|
||||
|
||||
// Sweep phase: free all non-marked objects and unmark marked objects for
|
||||
// the next collection cycle.
|
||||
@@ -311,18 +311,22 @@ func markRoots(start, end uintptr) {
|
||||
|
||||
for addr := start; addr != end; addr += unsafe.Sizeof(addr) {
|
||||
root := *(*uintptr)(unsafe.Pointer(addr))
|
||||
if looksLikePointer(root) {
|
||||
block := blockFromAddr(root)
|
||||
head := block.findHead()
|
||||
if head.state() != blockStateMark {
|
||||
if gcDebug {
|
||||
println("found unmarked pointer", root, "at address", addr)
|
||||
}
|
||||
head.setState(blockStateMark)
|
||||
next := block.findNext()
|
||||
// TODO: avoid recursion as much as possible
|
||||
markRoots(head.address(), next.address())
|
||||
markRoot(addr, root)
|
||||
}
|
||||
}
|
||||
|
||||
func markRoot(addr, root uintptr) {
|
||||
if looksLikePointer(root) {
|
||||
block := blockFromAddr(root)
|
||||
head := block.findHead()
|
||||
if head.state() != blockStateMark {
|
||||
if gcDebug {
|
||||
println("found unmarked pointer", root, "at address", addr)
|
||||
}
|
||||
head.setState(blockStateMark)
|
||||
next := block.findNext()
|
||||
// TODO: avoid recursion as much as possible
|
||||
markRoots(head.address(), next.address())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user