tcustomtrace.nim 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. discard """
  2. outputsub: '''1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 89
  9. 90
  10. 90
  11. 0 0 1
  12. 0 1 2
  13. 0 2 3
  14. 1 0 4
  15. 1 1 5
  16. 1 2 6
  17. 1 3 7
  18. after 6 6
  19. MEM 0'''
  20. joinable: false
  21. cmd: "nim c --gc:orc -d:useMalloc $file"
  22. valgrind: "true"
  23. """
  24. import typetraits
  25. type
  26. myseq*[T] = object
  27. len, cap: int
  28. data: ptr UncheckedArray[T]
  29. # XXX make code memory safe for overflows in '*'
  30. var
  31. allocCount, deallocCount: int
  32. proc `=destroy`*[T](x: var myseq[T]) =
  33. if x.data != nil:
  34. when not supportsCopyMem(T):
  35. for i in 0..<x.len: `=destroy`(x[i])
  36. dealloc(x.data)
  37. inc deallocCount
  38. x.data = nil
  39. x.len = 0
  40. x.cap = 0
  41. proc `=copy`*[T](a: var myseq[T]; b: myseq[T]) =
  42. if a.data == b.data: return
  43. if a.data != nil:
  44. `=destroy`(a)
  45. #dealloc(a.data)
  46. #inc deallocCount
  47. #a.data = nil
  48. a.len = b.len
  49. a.cap = b.cap
  50. if b.data != nil:
  51. a.data = cast[type(a.data)](alloc(a.cap * sizeof(T)))
  52. inc allocCount
  53. when supportsCopyMem(T):
  54. copyMem(a.data, b.data, a.cap * sizeof(T))
  55. else:
  56. for i in 0..<a.len:
  57. a.data[i] = b.data[i]
  58. proc `=sink`*[T](a: var myseq[T]; b: myseq[T]) =
  59. if a.data != nil and a.data != b.data:
  60. dealloc(a.data)
  61. inc deallocCount
  62. a.len = b.len
  63. a.cap = b.cap
  64. a.data = b.data
  65. proc `=trace`*[T](x: var myseq[T]; env: pointer) =
  66. if x.data != nil:
  67. for i in 0..<x.len: `=trace`(x[i], env)
  68. proc resize[T](s: var myseq[T]) =
  69. let oldCap = s.cap
  70. if oldCap == 0: s.cap = 8
  71. else: s.cap = (s.cap * 3) shr 1
  72. if s.data == nil: inc allocCount
  73. s.data = cast[typeof(s.data)](realloc0(s.data, oldCap * sizeof(T), s.cap * sizeof(T)))
  74. proc reserveSlot[T](x: var myseq[T]): ptr T =
  75. if x.len >= x.cap: resize(x)
  76. result = addr(x.data[x.len])
  77. inc x.len
  78. template add*[T](x: var myseq[T]; y: T) =
  79. reserveSlot(x)[] = y
  80. proc shrink*[T](x: var myseq[T]; newLen: int) =
  81. assert newLen <= x.len
  82. assert newLen >= 0
  83. when not supportsCopyMem(T):
  84. for i in countdown(x.len - 1, newLen - 1):
  85. `=destroy`(x.data[i])
  86. x.len = newLen
  87. proc grow*[T](x: var myseq[T]; newLen: int; value: T) =
  88. if newLen <= x.len: return
  89. assert newLen >= 0
  90. let oldCap = x.cap
  91. if oldCap == 0: x.cap = newLen
  92. else: x.cap = max(newLen, (oldCap * 3) shr 1)
  93. if x.data == nil: inc allocCount
  94. x.data = cast[type(x.data)](realloc0(x.data, oldCap * sizeof(T), x.cap * sizeof(T)))
  95. for i in x.len..<newLen:
  96. x.data[i] = value
  97. x.len = newLen
  98. template default[T](t: typedesc[T]): T =
  99. var v: T
  100. v
  101. proc setLen*[T](x: var myseq[T]; newLen: int) {.deprecated.} =
  102. if newlen < x.len: shrink(x, newLen)
  103. else: grow(x, newLen, default(T))
  104. template `[]`*[T](x: myseq[T]; i: Natural): T =
  105. assert i < x.len
  106. x.data[i]
  107. template `[]=`*[T](x: myseq[T]; i: Natural; y: T) =
  108. assert i < x.len
  109. x.data[i] = y
  110. proc createSeq*[T](elems: varargs[T]): myseq[T] =
  111. result.cap = max(elems.len, 2)
  112. result.len = elems.len
  113. result.data = cast[type(result.data)](alloc0(result.cap * sizeof(T)))
  114. inc allocCount
  115. when supportsCopyMem(T):
  116. copyMem(result.data, addr(elems[0]), result.cap * sizeof(T))
  117. else:
  118. for i in 0..<result.len:
  119. result.data[i] = elems[i]
  120. proc len*[T](x: myseq[T]): int {.inline.} = x.len
  121. proc main =
  122. var s = createSeq(1, 2, 3, 4, 5, 6)
  123. s.add 89
  124. s.grow s.len + 2, 90
  125. for i in 0 ..< s.len:
  126. echo s[i]
  127. var nested = createSeq(createSeq(1, 2, 3), createSeq(4, 5, 6, 7))
  128. for i in 0 ..< nested.len:
  129. for j in 0 ..< nested[i].len:
  130. echo i, " ", j, " ", nested[i][j]
  131. main()
  132. echo "after ", allocCount, " ", deallocCount
  133. type
  134. Node = ref object
  135. name: char
  136. sccId: int
  137. kids: myseq[Node]
  138. rc: int
  139. proc edge(a, b: Node) =
  140. inc b.rc
  141. a.kids.add b
  142. proc createNode(name: char): Node =
  143. new result
  144. result.name = name
  145. result.kids = createSeq[Node]()
  146. proc use(x: Node) = discard
  147. proc buildComplexGraph: Node =
  148. # see https://en.wikipedia.org/wiki/Strongly_connected_component for the
  149. # graph:
  150. let a = createNode('a')
  151. let b = createNode('b')
  152. let c = createNode('c')
  153. let d = createNode('d')
  154. let e = createNode('e')
  155. a.edge c
  156. c.edge b
  157. c.edge e
  158. b.edge a
  159. d.edge c
  160. e.edge d
  161. let f = createNode('f')
  162. b.edge f
  163. e.edge f
  164. let g = createNode('g')
  165. let h = createNode('h')
  166. let i = createNode('i')
  167. f.edge g
  168. f.edge i
  169. g.edge h
  170. h.edge i
  171. i.edge g
  172. let j = createNode('j')
  173. h.edge j
  174. i.edge j
  175. let k = createNode('k')
  176. let l = createNode('l')
  177. f.edge k
  178. k.edge l
  179. l.edge k
  180. k.edge j
  181. let m = createNode('m')
  182. let n = createNode('n')
  183. let p = createNode('p')
  184. let q = createNode('q')
  185. m.edge n
  186. n.edge p
  187. n.edge q
  188. q.edge p
  189. p.edge m
  190. q.edge k
  191. d.edge m
  192. e.edge n
  193. result = a
  194. proc main2 =
  195. let g = buildComplexGraph()
  196. main2()
  197. GC_fullCollect()
  198. echo "MEM ", getOccupiedMem()