cellseqs_v2.nim 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2019 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Cell seqs for cyclebreaker and cyclicrefs_v2.
  10. type
  11. CellTuple[T] = (T, PNimTypeV2)
  12. CellArray[T] = ptr UncheckedArray[CellTuple[T]]
  13. CellSeq[T] = object
  14. len, cap: int
  15. d: CellArray[T]
  16. proc add[T](s: var CellSeq[T], c: T; t: PNimTypeV2) {.inline.} =
  17. if s.len >= s.cap:
  18. s.cap = s.cap * 3 div 2
  19. when compileOption("threads"):
  20. var d = cast[CellArray[T]](allocShared(uint(s.cap * sizeof(CellTuple[T]))))
  21. else:
  22. var d = cast[CellArray[T]](alloc(s.cap * sizeof(CellTuple[T])))
  23. copyMem(d, s.d, s.len * sizeof(CellTuple[T]))
  24. when compileOption("threads"):
  25. deallocShared(s.d)
  26. else:
  27. dealloc(s.d)
  28. s.d = d
  29. # XXX: realloc?
  30. s.d[s.len] = (c, t)
  31. inc(s.len)
  32. proc init[T](s: var CellSeq[T], cap: int = 1024) =
  33. s.len = 0
  34. s.cap = cap
  35. when compileOption("threads"):
  36. s.d = cast[CellArray[T]](allocShared(uint(s.cap * sizeof(CellTuple[T]))))
  37. else:
  38. s.d = cast[CellArray[T]](alloc(s.cap * sizeof(CellTuple[T])))
  39. proc deinit[T](s: var CellSeq[T]) =
  40. if s.d != nil:
  41. when compileOption("threads"):
  42. deallocShared(s.d)
  43. else:
  44. dealloc(s.d)
  45. s.d = nil
  46. s.len = 0
  47. s.cap = 0
  48. proc pop[T](s: var CellSeq[T]): (T, PNimTypeV2) =
  49. result = s.d[s.len-1]
  50. dec s.len