diff --git a/src/backend/vm.nim b/src/backend/vm.nim index 2a5eb54..5f13622 100644 --- a/src/backend/vm.nim +++ b/src/backend/vm.nim @@ -161,9 +161,15 @@ template free*(self: var PeonVM, kind: typedesc, p: pointer): untyped = discard reallocate(self, p, sizeof(kind), 0) +template setKind*[T, K](t: var T, kind: untyped, target: K) = + ## Thanks to https://forum.nim-lang.org/t/8312 + cast[ptr K](cast[int](addr t) + offsetOf(typeof(t), kind))[] = target + + proc allocate*(self: var PeonVM, kind: ObjectKind, size: typedesc, count: int): ptr HeapObject {.inline.} = ## Allocates an object on the heap result = cast[ptr HeapObject](self.reallocate(nil, 0, sizeof(HeapObject))) + setkind(result[], kind, kind) result.marked = false self.gc.bytesAllocated.total += sizeof(HeapObject) self.gc.bytesAllocated.current += sizeof(HeapObject) diff --git a/tests/gc.pn b/tests/gc.pn index 9f0b330..23dc5be 100644 --- a/tests/gc.pn +++ b/tests/gc.pn @@ -1,7 +1,7 @@ import std; -var x = 10000; +var x = 1000000; var y = "just a test"; print(y); print("Starting GC torture test");