Removed lib/system/allocators.nim. seqs_v2 and strs_v2 now uses allocShared0. (#13190)
* Cleanup, remove lib/system/allocators.nim. seqs_v2 and strs_v2 now use allocShared0 by default. * Fixed -d:useMalloc allocShared / reallocShared / deallocShared. These now use the alloc/dealloc/realloc implementation that also takes care of zeroing memory at realloc. * Removed debug printfs * Removed unpairedEnvAllocs() from tests/destructor/tnewruntime_misc * More mmdisp cleanups. The shared allocators do not need to zero memory or throw since the regular ones already do that * Introduced realloc0 and reallocShared0, these procs are now used by strs_v2 and seqs_v2. This also allowed the -d:useMalloc allocator to drop the extra header with allocation length. * Moved strs_v2/seqs_v2 'allocated' flag into 'cap' field * Added 'getAllocStats()' to get low level alloc/dealloc counters. Enable with -d:allocStats * *allocShared implementations for boehm and go allocators now depend on the proper *allocImpl procs
This commit is contained in:
parent
f500895efe
commit
b68eb1cad0
21 changed files with 275 additions and 312 deletions
|
|
@ -9,15 +9,18 @@
|
|||
|
||||
|
||||
# import typetraits
|
||||
# strs already imported allocators for us.
|
||||
# strs already imported allocateds for us.
|
||||
|
||||
proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
|
||||
|
||||
## Default seq implementation used by Nim's core.
|
||||
type
|
||||
|
||||
NimSeqPayloadBase = object
|
||||
cap: int
|
||||
|
||||
NimSeqPayload[T] = object
|
||||
cap: int
|
||||
allocator: Allocator
|
||||
data: UncheckedArray[T]
|
||||
|
||||
NimSeqV2*[T] = object
|
||||
|
|
@ -26,22 +29,13 @@ type
|
|||
|
||||
const nimSeqVersion {.core.} = 2
|
||||
|
||||
template payloadSize(cap): int = cap * sizeof(T) + sizeof(int) + sizeof(Allocator)
|
||||
|
||||
# XXX make code memory safe for overflows in '*'
|
||||
|
||||
type
|
||||
PayloadBase = object
|
||||
cap: int
|
||||
allocator: Allocator
|
||||
|
||||
proc newSeqPayload(cap, elemSize: int): pointer {.compilerRtl, raises: [].} =
|
||||
# we have to use type erasure here as Nim does not support generic
|
||||
# compilerProcs. Oh well, this will all be inlined anyway.
|
||||
if cap > 0:
|
||||
let allocator = getLocalAllocator()
|
||||
var p = cast[ptr PayloadBase](allocator.alloc(allocator, cap * elemSize + sizeof(int) + sizeof(Allocator)))
|
||||
p.allocator = allocator
|
||||
var p = cast[ptr NimSeqPayloadBase](allocShared0(cap * elemSize + sizeof(NimSeqPayloadBase)))
|
||||
p.cap = cap
|
||||
result = p
|
||||
else:
|
||||
|
|
@ -53,7 +47,7 @@ proc prepareSeqAdd(len: int; p: pointer; addlen, elemSize: int): pointer {.
|
|||
template `+!`(p: pointer, s: int): pointer =
|
||||
cast[pointer](cast[int](p) +% s)
|
||||
|
||||
const headerSize = sizeof(int) + sizeof(Allocator)
|
||||
const headerSize = sizeof(NimSeqPayloadBase)
|
||||
if addlen <= 0:
|
||||
result = p
|
||||
elif p == nil:
|
||||
|
|
@ -61,23 +55,19 @@ proc prepareSeqAdd(len: int; p: pointer; addlen, elemSize: int): pointer {.
|
|||
else:
|
||||
# Note: this means we cannot support things that have internal pointers as
|
||||
# they get reallocated here. This needs to be documented clearly.
|
||||
var p = cast[ptr PayloadBase](p)
|
||||
let cap = max(resize(p.cap), len+addlen)
|
||||
if p.allocator == nil:
|
||||
let allocator = getLocalAllocator()
|
||||
var q = cast[ptr PayloadBase](allocator.alloc(allocator,
|
||||
headerSize + elemSize * cap))
|
||||
var p = cast[ptr NimSeqPayloadBase](p)
|
||||
let oldCap = p.cap and not strlitFlag
|
||||
let newCap = max(resize(oldCap), len+addlen)
|
||||
if (p.cap and strlitFlag) == strlitFlag:
|
||||
var q = cast[ptr NimSeqPayloadBase](allocShared0(headerSize + elemSize * newCap))
|
||||
copyMem(q +! headerSize, p +! headerSize, len * elemSize)
|
||||
q.allocator = allocator
|
||||
q.cap = cap
|
||||
q.cap = newCap
|
||||
result = q
|
||||
else:
|
||||
let allocator = p.allocator
|
||||
var q = cast[ptr PayloadBase](allocator.realloc(allocator, p,
|
||||
headerSize + elemSize * p.cap,
|
||||
headerSize + elemSize * cap))
|
||||
q.allocator = allocator
|
||||
q.cap = cap
|
||||
let oldSize = headerSize + elemSize * oldCap
|
||||
let newSize = headerSize + elemSize * newCap
|
||||
var q = cast[ptr NimSeqPayloadBase](reallocShared0(p, oldSize, newSize))
|
||||
q.cap = newCap
|
||||
result = q
|
||||
|
||||
proc shrink*[T](x: var seq[T]; newLen: Natural) =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue