mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-18 13:30:33 +00:00
Make newSeqOfCap not initialize memory. (#21842)
It's used in `newSeqUninitialized`.
---------
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
(cherry picked from commit 8853fb0775)
This commit is contained in:
committed by
narimiran
parent
a12cb273b3
commit
12f2c36aca
@@ -1496,7 +1496,7 @@ proc genNewSeqOfCap(p: BProc; e: PNode; d: var TLoc) =
|
||||
initLocExpr(p, e[1], a)
|
||||
if optSeqDestructors in p.config.globalOptions:
|
||||
if d.k == locNone: getTemp(p, e.typ, d, needsInit=false)
|
||||
linefmt(p, cpsStmts, "$1.len = 0; $1.p = ($4*) #newSeqPayload($2, sizeof($3), NIM_ALIGNOF($3));$n",
|
||||
linefmt(p, cpsStmts, "$1.len = 0; $1.p = ($4*) #newSeqPayloadUninit($2, sizeof($3), NIM_ALIGNOF($3));$n",
|
||||
[d.rdLoc, a.rdLoc, getTypeDesc(p.module, seqtype.lastSon),
|
||||
getSeqPayloadType(p.module, seqtype),
|
||||
])
|
||||
|
||||
@@ -941,7 +941,7 @@ proc reset*[T](obj: var T) {.noSideEffect.} =
|
||||
obj = default(typeof(obj))
|
||||
|
||||
proc setLen*[T](s: var seq[T], newlen: Natural) {.
|
||||
magic: "SetLengthSeq", noSideEffect.}
|
||||
magic: "SetLengthSeq", noSideEffect, nodestroy.}
|
||||
## Sets the length of seq `s` to `newlen`. `T` may be any sequence type.
|
||||
##
|
||||
## If the current length is greater than the new length,
|
||||
|
||||
@@ -109,7 +109,7 @@ proc newSeqRC1(typ: PNimType, len: int): pointer {.compilerRtl.} =
|
||||
writebarrierptr(addr(result), newSeq(typ, len))
|
||||
|
||||
proc nimNewSeqOfCap(typ: PNimType, cap: int): pointer {.compilerproc.} =
|
||||
result = newObj(typ, align(GenericSeqSize, typ.base.align) + cap * typ.base.size)
|
||||
result = newObjNoInit(typ, align(GenericSeqSize, typ.base.align) + cap * typ.base.size)
|
||||
cast[PGenericSeq](result).len = 0
|
||||
cast[PGenericSeq](result).reserved = cap
|
||||
cast[PGenericSeq](result).elemSize = typ.base.size
|
||||
|
||||
@@ -50,6 +50,15 @@ proc newSeqPayload(cap, elemSize, elemAlign: int): pointer {.compilerRtl, raises
|
||||
else:
|
||||
result = nil
|
||||
|
||||
proc newSeqPayloadUninit(cap, elemSize, elemAlign: int): pointer {.compilerRtl, raises: [].} =
|
||||
# Used in `newSeqOfCap()`.
|
||||
if cap > 0:
|
||||
var p = cast[ptr NimSeqPayloadBase](alignedAlloc(align(sizeof(NimSeqPayloadBase), elemAlign) + cap * elemSize, elemAlign))
|
||||
p.cap = cap
|
||||
result = p
|
||||
else:
|
||||
result = nil
|
||||
|
||||
template `+!`(p: pointer, s: int): pointer =
|
||||
cast[pointer](cast[int](p) +% s)
|
||||
|
||||
@@ -125,7 +134,7 @@ proc add*[T](x: var seq[T]; y: sink T) {.magic: "AppendSeqElem", noSideEffect, n
|
||||
# We also save the `wasMoved + destroy` pair for the sink parameter.
|
||||
xu.p.data[oldLen] = y
|
||||
|
||||
proc setLen[T](s: var seq[T], newlen: Natural) =
|
||||
proc setLen[T](s: var seq[T], newlen: Natural) {.nodestroy.} =
|
||||
{.noSideEffect.}:
|
||||
if newlen < s.len:
|
||||
shrink(s, newlen)
|
||||
|
||||
Reference in New Issue
Block a user