seqs_v2.nim 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. # Some optimizations here may be not to empty-seq-initialize some symbols, then StrictNotNil complains.
  12. {.push warning[StrictNotNil]: off.} # See https://github.com/nim-lang/Nim/issues/21401
  13. proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
  14. ## Default seq implementation used by Nim's core.
  15. type
  16. NimSeqPayloadBase = object
  17. cap: int
  18. NimSeqPayload[T] = object
  19. cap: int
  20. data: UncheckedArray[T]
  21. NimSeqV2*[T] = object # \
  22. # if you change this implementation, also change seqs_v2_reimpl.nim!
  23. len: int
  24. p: ptr NimSeqPayload[T]
  25. const nimSeqVersion {.core.} = 2
  26. # XXX make code memory safe for overflows in '*'
  27. proc newSeqPayload(cap, elemSize, elemAlign: int): pointer {.compilerRtl, raises: [].} =
  28. # we have to use type erasure here as Nim does not support generic
  29. # compilerProcs. Oh well, this will all be inlined anyway.
  30. if cap > 0:
  31. var p = cast[ptr NimSeqPayloadBase](alignedAlloc0(align(sizeof(NimSeqPayloadBase), elemAlign) + cap * elemSize, elemAlign))
  32. p.cap = cap
  33. result = p
  34. else:
  35. result = nil
  36. template `+!`(p: pointer, s: int): pointer =
  37. cast[pointer](cast[int](p) +% s)
  38. template `-!`(p: pointer, s: int): pointer =
  39. cast[pointer](cast[int](p) -% s)
  40. proc prepareSeqAdd(len: int; p: pointer; addlen, elemSize, elemAlign: int): pointer {.
  41. noSideEffect, tags: [], raises: [], compilerRtl.} =
  42. {.noSideEffect.}:
  43. let headerSize = align(sizeof(NimSeqPayloadBase), elemAlign)
  44. if addlen <= 0:
  45. result = p
  46. elif p == nil:
  47. result = newSeqPayload(len+addlen, elemSize, elemAlign)
  48. else:
  49. # Note: this means we cannot support things that have internal pointers as
  50. # they get reallocated here. This needs to be documented clearly.
  51. var p = cast[ptr NimSeqPayloadBase](p)
  52. let oldCap = p.cap and not strlitFlag
  53. let newCap = max(resize(oldCap), len+addlen)
  54. if (p.cap and strlitFlag) == strlitFlag:
  55. var q = cast[ptr NimSeqPayloadBase](alignedAlloc0(headerSize + elemSize * newCap, elemAlign))
  56. copyMem(q +! headerSize, p +! headerSize, len * elemSize)
  57. q.cap = newCap
  58. result = q
  59. else:
  60. let oldSize = headerSize + elemSize * oldCap
  61. let newSize = headerSize + elemSize * newCap
  62. var q = cast[ptr NimSeqPayloadBase](alignedRealloc0(p, oldSize, newSize, elemAlign))
  63. q.cap = newCap
  64. result = q
  65. proc shrink*[T](x: var seq[T]; newLen: Natural) {.tags: [], raises: [].} =
  66. when nimvm:
  67. {.cast(tags: []).}:
  68. setLen(x, newLen)
  69. else:
  70. #sysAssert newLen <= x.len, "invalid newLen parameter for 'shrink'"
  71. when not supportsCopyMem(T):
  72. for i in countdown(x.len - 1, newLen):
  73. reset x[i]
  74. # XXX This is wrong for const seqs that were moved into 'x'!
  75. {.noSideEffect.}:
  76. cast[ptr NimSeqV2[T]](addr x).len = newLen
  77. proc grow*[T](x: var seq[T]; newLen: Natural; value: T) =
  78. let oldLen = x.len
  79. #sysAssert newLen >= x.len, "invalid newLen parameter for 'grow'"
  80. if newLen <= oldLen: return
  81. var xu = cast[ptr NimSeqV2[T]](addr x)
  82. if xu.p == nil or (xu.p.cap and not strlitFlag) < newLen:
  83. xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newLen - oldLen, sizeof(T), alignof(T)))
  84. xu.len = newLen
  85. for i in oldLen .. newLen-1:
  86. xu.p.data[i] = value
  87. proc add*[T](x: var seq[T]; y: sink T) {.magic: "AppendSeqElem", noSideEffect, nodestroy.} =
  88. ## Generic proc for adding a data item `y` to a container `x`.
  89. ##
  90. ## For containers that have an order, `add` means *append*. New generic
  91. ## containers should also call their adding proc `add` for consistency.
  92. ## Generic code becomes much easier to write if the Nim naming scheme is
  93. ## respected.
  94. {.cast(noSideEffect).}:
  95. let oldLen = x.len
  96. var xu = cast[ptr NimSeqV2[T]](addr x)
  97. if xu.p == nil or (xu.p.cap and not strlitFlag) < oldLen+1:
  98. xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, 1, sizeof(T), alignof(T)))
  99. xu.len = oldLen+1
  100. # .nodestroy means `xu.p.data[oldLen] = value` is compiled into a
  101. # copyMem(). This is fine as know by construction that
  102. # in `xu.p.data[oldLen]` there is nothing to destroy.
  103. # We also save the `wasMoved + destroy` pair for the sink parameter.
  104. xu.p.data[oldLen] = y
  105. proc setLen[T](s: var seq[T], newlen: Natural) =
  106. {.noSideEffect.}:
  107. if newlen < s.len:
  108. shrink(s, newlen)
  109. else:
  110. let oldLen = s.len
  111. if newlen <= oldLen: return
  112. var xu = cast[ptr NimSeqV2[T]](addr s)
  113. if xu.p == nil or (xu.p.cap and not strlitFlag) < newlen:
  114. xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
  115. xu.len = newlen
  116. for i in oldLen..<newlen:
  117. xu.p.data[i] = default(T)
  118. proc newSeq[T](s: var seq[T], len: Natural) =
  119. shrink(s, 0)
  120. setLen(s, len)
  121. proc sameSeqPayload*[T](x: seq[T], y: seq[T]): bool {.magic: "SameSeqPayload", inline.} =
  122. result = cast[NimSeqV2[T]](x).p == cast[NimSeqV2[T]](y).p
  123. template capacityImpl(sek: NimSeqV2): int =
  124. if sek.p != nil: (sek.p.cap and not strlitFlag) else: 0
  125. func capacity*[T](self: seq[T]): int {.inline.} =
  126. ## Returns the current capacity of the seq.
  127. # See https://github.com/nim-lang/RFCs/issues/460
  128. runnableExamples:
  129. var lst = newSeqOfCap[string](cap = 42)
  130. lst.add "Nim"
  131. assert lst.capacity == 42
  132. {.cast(noSideEffect).}:
  133. let sek = unsafeAddr self
  134. result = capacityImpl(cast[ptr NimSeqV2[T]](sek)[])
  135. {.pop.} # See https://github.com/nim-lang/Nim/issues/21401