blob: c6c7b1a8e29555cfca80c4604969a8a359e58c19 (
plain) (
blame)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#
#
# Nim's Runtime Library
# (c) Copyright 2019 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
# Cell seqs for cyclebreaker and cyclicrefs_v2.
type
CellTuple[T] = (T, PNimTypeV2)
CellArray[T] = ptr UncheckedArray[CellTuple[T]]
CellSeq[T] = object
len, cap: int
d: CellArray[T]
proc resize[T](s: var CellSeq[T]) =
s.cap = s.cap * 3 div 2
var newSize = s.cap * sizeof(CellTuple[T])
when compileOption("threads"):
s.d = cast[CellArray[T]](reallocShared(s.d, newSize))
else:
s.d = cast[CellArray[T]](realloc(s.d, newSize))
proc add[T](s: var CellSeq[T], c: T, t: PNimTypeV2) {.inline.} =
if s.len >= s.cap:
s.resize()
s.d[s.len] = (c, t)
inc(s.len)
proc init[T](s: var CellSeq[T], cap: int = 1024) =
s.len = 0
s.cap = cap
when compileOption("threads"):
s.d = cast[CellArray[T]](allocShared(uint(s.cap * sizeof(CellTuple[T]))))
else:
s.d = cast[CellArray[T]](alloc(s.cap * sizeof(CellTuple[T])))
proc deinit[T](s: var CellSeq[T]) =
if s.d != nil:
when compileOption("threads"):
deallocShared(s.d)
else:
dealloc(s.d)
s.d = nil
s.len = 0
s.cap = 0
proc pop[T](s: var CellSeq[T]): (T, PNimTypeV2) =
result = s.d[s.len-1]
dec s.len
|