Skip to content

Commit 3685382

Browse files
authored
fix pools destructors (#95)
* fix pools destructors * minor
1 parent 8cf7c3d commit 3685382

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/fusion/pools.nim

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,26 @@ type
3131
last: ptr Chunk[T]
3232
lastCap: int
3333

34+
proc `=destroy`*[T](p: var Pool[T]) =
35+
var it = p.last
36+
while it != nil:
37+
when not supportsCopyMem(T):
38+
for i in 0..<it.len:
39+
`=destroy`(it.elems[i])
40+
let next = it.next
41+
deallocShared(it)
42+
it = next
43+
44+
proc `=copy`*[T](dest: var Pool[T]; src: Pool[T]) {.error.}
45+
3446
proc newNode*[T](p: var Pool[T]): ptr T =
3547
if p.len >= p.lastCap:
3648
if p.lastCap == 0: p.lastCap = 4
3749
elif p.lastCap < 65_000: p.lastCap *= 2
3850
when not supportsCopyMem(T):
39-
var n = cast[ptr Chunk[T]](allocShared0(sizeof(Chunk[T]) + p.lastCap * sizeof(T)))
51+
let n = cast[ptr Chunk[T]](allocShared0(sizeof(Chunk[T]) + p.lastCap * sizeof(T)))
4052
else:
41-
var n = cast[ptr Chunk[T]](allocShared(sizeof(Chunk[T]) + p.lastCap * sizeof(T)))
53+
let n = cast[ptr Chunk[T]](allocShared(sizeof(Chunk[T]) + p.lastCap * sizeof(T)))
4254
n.next = nil
4355
n.next = p.last
4456
p.last = n
@@ -47,21 +59,6 @@ proc newNode*[T](p: var Pool[T]): ptr T =
4759
inc p.len
4860
inc p.last.len
4961

50-
proc `=`[T](dest: var Pool[T]; src: Pool[T]) {.error.}
51-
52-
proc `=destroy`[T](p: var Pool[T]) =
53-
var it = p.last
54-
while it != nil:
55-
when not supportsCopyMem(T):
56-
for i in 0..<it.len:
57-
`=destroy`(it.elems[i])
58-
let next = it.next
59-
deallocShared(it)
60-
it = next
61-
p.len = 0
62-
p.lastCap = 0
63-
p.last = nil
64-
6562
when isMainModule:
6663
const withNonTrivialDestructor = false
6764
include prelude

0 commit comments

Comments
 (0)