seqs_v2.nim 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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) {.tags: [], raises: [].} =
  64. when nimvm:
  65. {.cast(tags: []).}:
  66. setLen(x, newLen)
  67. else:
  68. #sysAssert newLen <= x.len, "invalid newLen parameter for 'shrink'"
  69. when not supportsCopyMem(T):
  70. for i in countdown(x.len - 1, newLen):
  71. reset x[i]
  72. # XXX This is wrong for const seqs that were moved into 'x'!
  73. cast[ptr NimSeqV2[T]](addr x).len = newLen
  74. proc grow*[T](x: var seq[T]; newLen: Natural; value: T) =
  75. let oldLen = x.len
  76. #sysAssert newLen >= x.len, "invalid newLen parameter for 'grow'"
  77. if newLen <= oldLen: return
  78. var xu = cast[ptr NimSeqV2[T]](addr x)
  79. if xu.p == nil or xu.p.cap < newLen:
  80. xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newLen - oldLen, sizeof(T), alignof(T)))
  81. xu.len = newLen
  82. for i in oldLen .. newLen-1:
  83. xu.p.data[i] = value
  84. proc add*[T](x: var seq[T]; value: sink T) {.magic: "AppendSeqElem", noSideEffect, nodestroy.} =
  85. ## Generic proc for adding a data item `y` to a container `x`.
  86. ##
  87. ## For containers that have an order, `add` means *append*. New generic
  88. ## containers should also call their adding proc `add` for consistency.
  89. ## Generic code becomes much easier to write if the Nim naming scheme is
  90. ## respected.
  91. let oldLen = x.len
  92. var xu = cast[ptr NimSeqV2[T]](addr x)
  93. if xu.p == nil or xu.p.cap < oldLen+1:
  94. xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, 1, sizeof(T), alignof(T)))
  95. xu.len = oldLen+1
  96. # .nodestroy means `xu.p.data[oldLen] = value` is compiled into a
  97. # copyMem(). This is fine as know by construction that
  98. # in `xu.p.data[oldLen]` there is nothing to destroy.
  99. # We also save the `wasMoved + destroy` pair for the sink parameter.
  100. xu.p.data[oldLen] = value
  101. proc setLen[T](s: var seq[T], newlen: Natural) =
  102. {.noSideEffect.}:
  103. if newlen < s.len:
  104. shrink(s, newlen)
  105. else:
  106. let oldLen = s.len
  107. if newlen <= oldLen: return
  108. var xu = cast[ptr NimSeqV2[T]](addr s)
  109. if xu.p == nil or xu.p.cap < newlen:
  110. xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
  111. xu.len = newlen
  112. for i in oldLen..<newlen:
  113. xu.p.data[i] = default(T)
  114. proc newSeq[T](s: var seq[T], len: Natural) =
  115. shrink(s, 0)
  116. setLen(s, len)
  117. template capacityImpl(sek: NimSeqV2): int =
  118. if sek.p != nil: sek.p.cap else: 0
  119. func capacity*[T](self: seq[T]): int {.inline.} =
  120. ## Returns the current capacity of the seq.
  121. # See https://github.com/nim-lang/RFCs/issues/460
  122. runnableExamples:
  123. var lst = newSeqOfCap[string](cap = 42)
  124. lst.add "Nim"
  125. assert lst.capacity == 42
  126. {.cast(noSideEffect).}:
  127. let sek = unsafeAddr self
  128. result = capacityImpl(cast[ptr NimSeqV2](sek)[])