aboutsummaryrefslogtreecommitdiff
path: root/GC.md
blob: c7aac96e792825f14c383d9b363dfb89fe3da978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Region-based garbage collection
Memory that is allocated should have metadata to which block (scope) it belongs to.
When data is moved, the metadata should be updated to which block it belongs to.
When a function is called and the function accesses a member owned by the callee, then the owner region of the member should
be temporary changed to that function and then at the end of the scope, it should be reverted to the callee so the member is not
freed. This would be a runtime borrow mechanism. However if the variable is passed to the function and not used after that, then it should be freed in
the called function, or at the end of the parent scope (which could be a if/for scope). This would be a runtime move mechanism.

```
create_linked_list = fn() {
    # Circular linked list!
    a = { next: null }
    b = { next: a }
    c = { next: b }
    a.next = c
    return c
}

main = fn() {
    linked_list = create_linked_list()
    while(true) {
        # ...
    }
}
```