sysstr.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # string & sequence handling procedures needed by the code generator
  10. # strings are dynamically resized, have a length field
  11. # and are zero-terminated, so they can be casted to C
  12. # strings easily
  13. # we don't use refcounts because that's a behaviour
  14. # the programmer may not want
  15. proc dataPointer(a: PGenericSeq, elemAlign: int): pointer =
  16. cast[pointer](cast[int](a) +% align(GenericSeqSize, elemAlign))
  17. proc dataPointer(a: PGenericSeq, elemAlign, elemSize, index: int): pointer =
  18. cast[pointer](cast[int](a) +% align(GenericSeqSize, elemAlign) +% (index*%elemSize))
  19. proc resize(old: int): int {.inline.} =
  20. if old <= 0: result = 4
  21. elif old < 65536: result = old * 2
  22. else: result = old * 3 div 2 # for large arrays * 3/2 is better
  23. when declared(allocAtomic):
  24. template allocStr(size: untyped): untyped =
  25. cast[NimString](allocAtomic(size))
  26. template allocStrNoInit(size: untyped): untyped =
  27. cast[NimString](boehmAllocAtomic(size))
  28. elif defined(gcRegions):
  29. template allocStr(size: untyped): untyped =
  30. cast[NimString](newStr(addr(strDesc), size, true))
  31. template allocStrNoInit(size: untyped): untyped =
  32. cast[NimString](newStr(addr(strDesc), size, false))
  33. else:
  34. template allocStr(size: untyped): untyped =
  35. cast[NimString](newObj(addr(strDesc), size))
  36. template allocStrNoInit(size: untyped): untyped =
  37. cast[NimString](newObjNoInit(addr(strDesc), size))
  38. proc rawNewStringNoInit(space: int): NimString {.compilerproc.} =
  39. var s = space
  40. if s < 7: s = 7
  41. result = allocStrNoInit(sizeof(TGenericSeq) + s + 1)
  42. result.reserved = s
  43. result.len = 0
  44. when defined(gogc):
  45. result.elemSize = 1
  46. proc rawNewString(space: int): NimString {.compilerproc.} =
  47. var s = space
  48. if s < 7: s = 7
  49. result = allocStr(sizeof(TGenericSeq) + s + 1)
  50. result.reserved = s
  51. result.len = 0
  52. when defined(gogc):
  53. result.elemSize = 1
  54. proc mnewString(len: int): NimString {.compilerproc.} =
  55. result = rawNewString(len)
  56. result.len = len
  57. proc copyStrLast(s: NimString, start, last: int): NimString {.compilerproc.} =
  58. # This is not used by most recent versions of the compiler anymore, but
  59. # required for bootstrapping purposes.
  60. let start = max(start, 0)
  61. if s == nil: return nil
  62. let len = min(last, s.len-1) - start + 1
  63. if len > 0:
  64. result = rawNewStringNoInit(len)
  65. result.len = len
  66. copyMem(addr(result.data), addr(s.data[start]), len)
  67. result.data[len] = '\0'
  68. else:
  69. result = rawNewString(len)
  70. proc copyStr(s: NimString, start: int): NimString {.compilerproc.} =
  71. # This is not used by most recent versions of the compiler anymore, but
  72. # required for bootstrapping purposes.
  73. if s == nil: return nil
  74. result = copyStrLast(s, start, s.len-1)
  75. proc nimToCStringConv(s: NimString): cstring {.compilerproc, nonReloadable, inline.} =
  76. if s == nil or s.len == 0: result = cstring""
  77. else: result = cast[cstring](addr s.data)
  78. proc toNimStr(str: cstring, len: int): NimString {.compilerproc.} =
  79. result = rawNewStringNoInit(len)
  80. result.len = len
  81. copyMem(addr(result.data), str, len + 1)
  82. proc cstrToNimstr(str: cstring): NimString {.compilerRtl.} =
  83. if str == nil: NimString(nil)
  84. else: toNimStr(str, str.len)
  85. proc copyString(src: NimString): NimString {.compilerRtl.} =
  86. if src != nil:
  87. if (src.reserved and seqShallowFlag) != 0:
  88. result = src
  89. else:
  90. result = rawNewStringNoInit(src.len)
  91. result.len = src.len
  92. copyMem(addr(result.data), addr(src.data), src.len + 1)
  93. sysAssert((seqShallowFlag and result.reserved) == 0, "copyString")
  94. when defined(nimShallowStrings):
  95. if (src.reserved and strlitFlag) != 0:
  96. result.reserved = (result.reserved and not strlitFlag) or seqShallowFlag
  97. proc newOwnedString(src: NimString; n: int): NimString =
  98. result = rawNewStringNoInit(n)
  99. result.len = n
  100. copyMem(addr(result.data), addr(src.data), n)
  101. result.data[n] = '\0'
  102. proc copyStringRC1(src: NimString): NimString {.compilerRtl.} =
  103. if src != nil:
  104. if (src.reserved and seqShallowFlag) != 0:
  105. result = src
  106. when declared(incRef):
  107. incRef(usrToCell(result))
  108. else:
  109. when declared(newObjRC1) and not defined(gcRegions):
  110. var s = src.len
  111. if s < 7: s = 7
  112. result = cast[NimString](newObjRC1(addr(strDesc), sizeof(TGenericSeq) +
  113. s+1))
  114. result.reserved = s
  115. when defined(gogc):
  116. result.elemSize = 1
  117. else:
  118. result = rawNewStringNoInit(src.len)
  119. result.len = src.len
  120. copyMem(addr(result.data), addr(src.data), src.len + 1)
  121. sysAssert((seqShallowFlag and result.reserved) == 0, "copyStringRC1")
  122. when defined(nimShallowStrings):
  123. if (src.reserved and strlitFlag) != 0:
  124. result.reserved = (result.reserved and not strlitFlag) or seqShallowFlag
  125. proc copyDeepString(src: NimString): NimString {.inline.} =
  126. if src != nil:
  127. result = rawNewStringNoInit(src.len)
  128. result.len = src.len
  129. copyMem(addr(result.data), addr(src.data), src.len + 1)
  130. proc addChar(s: NimString, c: char): NimString =
  131. # is compilerproc!
  132. if s == nil:
  133. result = rawNewStringNoInit(1)
  134. result.len = 0
  135. else:
  136. result = s
  137. if result.len >= result.space:
  138. let r = resize(result.space)
  139. result = rawNewStringNoInit(r)
  140. result.len = s.len
  141. copyMem(addr result.data[0], unsafeAddr(s.data[0]), s.len+1)
  142. result.reserved = r
  143. result.data[result.len] = c
  144. result.data[result.len+1] = '\0'
  145. inc(result.len)
  146. # These routines should be used like following:
  147. # <Nim code>
  148. # s &= "Hello " & name & ", how do you feel?"
  149. #
  150. # <generated C code>
  151. # {
  152. # s = resizeString(s, 6 + name->len + 17);
  153. # appendString(s, strLit1);
  154. # appendString(s, strLit2);
  155. # appendString(s, strLit3);
  156. # }
  157. #
  158. # <Nim code>
  159. # s = "Hello " & name & ", how do you feel?"
  160. #
  161. # <generated C code>
  162. # {
  163. # string tmp0;
  164. # tmp0 = rawNewString(6 + name->len + 17);
  165. # appendString(s, strLit1);
  166. # appendString(s, strLit2);
  167. # appendString(s, strLit3);
  168. # s = tmp0;
  169. # }
  170. #
  171. # <Nim code>
  172. # s = ""
  173. #
  174. # <generated C code>
  175. # s = rawNewString(0);
  176. proc resizeString(dest: NimString, addlen: int): NimString {.compilerRtl.} =
  177. if dest == nil:
  178. result = rawNewStringNoInit(addlen)
  179. elif dest.len + addlen <= dest.space:
  180. result = dest
  181. else: # slow path:
  182. let sp = max(resize(dest.space), dest.len + addlen)
  183. result = rawNewStringNoInit(sp)
  184. result.len = dest.len
  185. copyMem(addr result.data[0], unsafeAddr(dest.data[0]), dest.len+1)
  186. result.reserved = sp
  187. #result = rawNewString(sp)
  188. #copyMem(result, dest, dest.len + sizeof(TGenericSeq))
  189. # DO NOT UPDATE LEN YET: dest.len = newLen
  190. proc appendString(dest, src: NimString) {.compilerproc, inline.} =
  191. if src != nil:
  192. copyMem(addr(dest.data[dest.len]), addr(src.data), src.len + 1)
  193. inc(dest.len, src.len)
  194. proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} =
  195. dest.data[dest.len] = c
  196. dest.data[dest.len+1] = '\0'
  197. inc(dest.len)
  198. proc setLengthStr(s: NimString, newLen: int): NimString {.compilerRtl.} =
  199. let n = max(newLen, 0)
  200. if s == nil:
  201. result = mnewString(newLen)
  202. elif n <= s.space:
  203. result = s
  204. else:
  205. let sp = max(resize(s.space), newLen)
  206. result = rawNewStringNoInit(sp)
  207. result.len = s.len
  208. copyMem(addr result.data[0], unsafeAddr(s.data[0]), s.len+1)
  209. zeroMem(addr result.data[s.len], newLen - s.len)
  210. result.reserved = sp
  211. result.len = n
  212. result.data[n] = '\0'
  213. # ----------------- sequences ----------------------------------------------
  214. proc incrSeq(seq: PGenericSeq, elemSize, elemAlign: int): PGenericSeq {.compilerproc.} =
  215. # increments the length by one:
  216. # this is needed for supporting ``add``;
  217. #
  218. # add(seq, x) generates:
  219. # seq = incrSeq(seq, sizeof(x));
  220. # seq[seq->len-1] = x;
  221. result = seq
  222. if result.len >= result.space:
  223. let r = resize(result.space)
  224. result = cast[PGenericSeq](growObj(result, align(GenericSeqSize, elemAlign) + elemSize * r))
  225. result.reserved = r
  226. inc(result.len)
  227. proc incrSeqV2(seq: PGenericSeq, elemSize, elemAlign: int): PGenericSeq {.compilerproc.} =
  228. # incrSeq version 2
  229. result = seq
  230. if result.len >= result.space:
  231. let r = resize(result.space)
  232. result = cast[PGenericSeq](growObj(result, align(GenericSeqSize, elemAlign) + elemSize * r))
  233. result.reserved = r
  234. proc incrSeqV3(s: PGenericSeq, typ: PNimType): PGenericSeq {.compilerproc.} =
  235. if s == nil:
  236. result = cast[PGenericSeq](newSeq(typ, 1))
  237. result.len = 0
  238. else:
  239. result = s
  240. if result.len >= result.space:
  241. let r = resize(result.space)
  242. result = cast[PGenericSeq](newSeq(typ, r))
  243. result.len = s.len
  244. copyMem(dataPointer(result, typ.base.align), dataPointer(s, typ.base.align), s.len * typ.base.size)
  245. # since we steal the content from 's', it's crucial to set s's len to 0.
  246. s.len = 0
  247. proc setLengthSeq(seq: PGenericSeq, elemSize, elemAlign, newLen: int): PGenericSeq {.
  248. compilerRtl, inl.} =
  249. result = seq
  250. if result.space < newLen:
  251. let r = max(resize(result.space), newLen)
  252. result = cast[PGenericSeq](growObj(result, align(GenericSeqSize, elemAlign) + elemSize * r))
  253. result.reserved = r
  254. elif newLen < result.len:
  255. # we need to decref here, otherwise the GC leaks!
  256. when not defined(boehmGC) and not defined(nogc) and
  257. not defined(gcMarkAndSweep) and not defined(gogc) and
  258. not defined(gcRegions):
  259. if ntfNoRefs notin extGetCellType(result).base.flags:
  260. for i in newLen..result.len-1:
  261. forAllChildrenAux(dataPointer(result, elemAlign, elemSize, i),
  262. extGetCellType(result).base, waZctDecRef)
  263. # XXX: zeroing out the memory can still result in crashes if a wiped-out
  264. # cell is aliased by another pointer (ie proc parameter or a let variable).
  265. # This is a tough problem, because even if we don't zeroMem here, in the
  266. # presence of user defined destructors, the user will expect the cell to be
  267. # "destroyed" thus creating the same problem. We can destroy the cell in the
  268. # finalizer of the sequence, but this makes destruction non-deterministic.
  269. zeroMem(dataPointer(result, elemAlign, elemSize, newLen), (result.len-%newLen) *% elemSize)
  270. result.len = newLen
  271. proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
  272. compilerRtl.} =
  273. sysAssert typ.kind == tySequence, "setLengthSeqV2: type is not a seq"
  274. if s == nil:
  275. result = cast[PGenericSeq](newSeq(typ, newLen))
  276. else:
  277. let elemSize = typ.base.size
  278. let elemAlign = typ.base.align
  279. if s.space < newLen:
  280. let r = max(resize(s.space), newLen)
  281. result = cast[PGenericSeq](newSeq(typ, r))
  282. copyMem(dataPointer(result, elemAlign), dataPointer(s, elemAlign), s.len * elemSize)
  283. # since we steal the content from 's', it's crucial to set s's len to 0.
  284. s.len = 0
  285. elif newLen < s.len:
  286. result = s
  287. # we need to decref here, otherwise the GC leaks!
  288. when not defined(boehmGC) and not defined(nogc) and
  289. not defined(gcMarkAndSweep) and not defined(gogc) and
  290. not defined(gcRegions):
  291. if ntfNoRefs notin typ.base.flags:
  292. for i in newLen..result.len-1:
  293. forAllChildrenAux(dataPointer(result, elemAlign, elemSize, i),
  294. extGetCellType(result).base, waZctDecRef)
  295. # XXX: zeroing out the memory can still result in crashes if a wiped-out
  296. # cell is aliased by another pointer (ie proc parameter or a let variable).
  297. # This is a tough problem, because even if we don't zeroMem here, in the
  298. # presence of user defined destructors, the user will expect the cell to be
  299. # "destroyed" thus creating the same problem. We can destroy the cell in the
  300. # finalizer of the sequence, but this makes destruction non-deterministic.
  301. zeroMem(dataPointer(result, elemAlign, elemSize, newLen), (result.len-%newLen) *% elemSize)
  302. else:
  303. result = s
  304. zeroMem(dataPointer(result, elemAlign, elemSize, result.len), (newLen-%result.len) *% elemSize)
  305. result.len = newLen