seqs_v2.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2017 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # import typetraits
  10. # strs already imported allocateds for us.
  11. proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
  12. ## Default seq implementation used by Nim's core.
  13. type
  14. NimSeqPayloadBase = object
  15. cap: int
  16. NimSeqPayload[T] = object
  17. cap: int
  18. data: UncheckedArray[T]
  19. NimSeqV2*[T] = object # \
  20. # if you change this implementation, also change seqs_v2_reimpl.nim!
  21. len: int
  22. p: ptr NimSeqPayload[T]
  23. const nimSeqVersion {.core.} = 2
  24. # XXX make code memory safe for overflows in '*'
  25. proc newSeqPayload(cap, elemSize, elemAlign: int): pointer {.compilerRtl, raises: [].} =
  26. # we have to use type erasure here as Nim does not support generic
  27. # compilerProcs. Oh well, this will all be inlined anyway.
  28. if cap > 0:
  29. var p = cast[ptr NimSeqPayloadBase](alignedAlloc0(align(sizeof(NimSeqPayloadBase), elemAlign) + cap * elemSize, elemAlign))
  30. p.cap = cap
  31. result = p
  32. else:
  33. result = nil
  34. template `+!`(p: pointer, s: int): pointer =
  35. cast[pointer](cast[int](p) +% s)
  36. template `-!`(p: pointer, s: int): pointer =
  37. cast[pointer](cast[int](p) -% s)
  38. proc prepareSeqAdd(len: int; p: pointer; addlen, elemSize, elemAlign: int): pointer {.
  39. noSideEffect, raises: [], compilerRtl.} =
  40. {.noSideEffect.}:
  41. let headerSize = align(sizeof(NimSeqPayloadBase), elemAlign)
  42. if addlen <= 0:
  43. result = p
  44. elif p == nil:
  45. result = newSeqPayload(len+addlen, elemSize, elemAlign)
  46. else:
  47. # Note: this means we cannot support things that have internal pointers as
  48. # they get reallocated here. This needs to be documented clearly.
  49. var p = cast[ptr NimSeqPayloadBase](p)
  50. let oldCap = p.cap and not strlitFlag
  51. let newCap = max(resize(oldCap), len+addlen)
  52. if (p.cap and strlitFlag) == strlitFlag:
  53. var q = cast[ptr NimSeqPayloadBase](alignedAlloc0(headerSize + elemSize * newCap, elemAlign))
  54. copyMem(q +! headerSize, p +! headerSize, len * elemSize)
  55. q.cap = newCap
  56. result = q
  57. else:
  58. let oldSize = headerSize + elemSize * oldCap
  59. let newSize = headerSize + elemSize * newCap
  60. var q = cast[ptr NimSeqPayloadBase](alignedRealloc0(p, oldSize, newSize, elemAlign))
  61. q.cap = newCap
  62. result = q
  63. proc shrink*[T](x: var seq[T]; newLen: Natural) =
  64. when nimvm:
  65. setLen(x, newLen)
  66. else:
  67. #sysAssert newLen <= x.len, "invalid newLen parameter for 'shrink'"
  68. when not supportsCopyMem(T):
  69. for i in countdown(x.len - 1, newLen):
  70. reset x[i]
  71. # XXX This is wrong for const seqs that were moved into 'x'!
  72. cast[ptr NimSeqV2[T]](addr x).len = newLen
  73. proc grow*[T](x: var seq[T]; newLen: Natural; value: T) =
  74. let oldLen = x.len
  75. #sysAssert newLen >= x.len, "invalid newLen parameter for 'grow'"
  76. if newLen <= oldLen: return
  77. var xu = cast[ptr NimSeqV2[T]](addr x)
  78. if xu.p == nil or xu.p.cap < newLen:
  79. xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newLen - oldLen, sizeof(T), alignof(T)))
  80. xu.len = newLen
  81. for i in oldLen .. newLen-1:
  82. xu.p.data[i] = value
  83. proc add*[T](x: var seq[T]; value: sink T) {.magic: "AppendSeqElem", noSideEffect, nodestroy.} =
  84. ## Generic proc for adding a data item `y` to a container `x`.
  85. ##
  86. ## For containers that have an order, `add` means *append*. New generic
  87. ## containers should also call their adding proc `add` for consistency.
  88. ## Generic code becomes much easier to write if the Nim naming scheme is
  89. ## respected.
  90. let oldLen = x.len
  91. var xu = cast[ptr NimSeqV2[T]](addr x)
  92. if xu.p == nil or xu.p.cap < oldLen+1:
  93. xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, 1, sizeof(T), alignof(T)))
  94. xu.len = oldLen+1
  95. # .nodestroy means `xu.p.data[oldLen] = value` is compiled into a
  96. # copyMem(). This is fine as know by construction that
  97. # in `xu.p.data[oldLen]` there is nothing to destroy.
  98. # We also save the `wasMoved + destroy` pair for the sink parameter.
  99. xu.p.data[oldLen] = value
  100. proc setLen[T](s: var seq[T], newlen: Natural) =
  101. {.noSideEffect.}:
  102. if newlen < s.len:
  103. shrink(s, newlen)
  104. else:
  105. let oldLen = s.len
  106. if newlen <= oldLen: return
  107. var xu = cast[ptr NimSeqV2[T]](addr s)
  108. if xu.p == nil or xu.p.cap < newlen:
  109. xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
  110. xu.len = newlen