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:
Ico Doornekamp 2020-01-23 14:25:22 +01:00 • committed by Andreas Rumpf
commit b68eb1cad0
21 changed files with 275 additions and 312 deletions

View file

@ -948,13 +948,18 @@ proc dealloc(allocator: var MemRegion, p: pointer) =
proc realloc(allocator: var MemRegion, p: pointer, newsize: Natural): pointer =
if newsize > 0:
result = alloc0(allocator, newsize)
result = alloc(allocator, newsize)
if p != nil:
copyMem(result, p, min(ptrSize(p), newsize))
dealloc(allocator, p)
elif p != nil:
dealloc(allocator, p)
proc realloc0(allocator: var MemRegion, p: pointer, oldsize, newsize: Natural): pointer =
result = realloc(allocator, p, newsize)
if newsize > oldsize:
zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
proc deallocOsPages(a: var MemRegion) =
# we free every 'ordinarily' allocated page by iterating over the page bits:
var it = addr(a.heapLinks)
@ -997,18 +1002,23 @@ template instantiateForRegion(allocator: untyped) {.dirty.} =
proc deallocOsPages = deallocOsPages(allocator)
proc alloc(size: Natural): pointer =
proc allocImpl(size: Natural): pointer =
result = alloc(allocator, size)
proc alloc0(size: Natural): pointer =
proc alloc0Impl(size: Natural): pointer =
result = alloc0(allocator, size)
proc dealloc(p: pointer) =
proc deallocImpl(p: pointer) =
dealloc(allocator, p)
proc realloc(p: pointer, newSize: Natural): pointer =
proc reallocImpl(p: pointer, newSize: Natural): pointer =
result = realloc(allocator, p, newSize)
proc realloc0Impl(p: pointer, oldSize, newSize: Natural): pointer =
result = realloc(allocator, p, newSize)
if newSize > oldSize:
zeroMem(cast[pointer](cast[int](result) + oldSize), newSize - oldSize)
when false:
proc countFreeMem(): int =
# only used for assertions
@ -1034,33 +1044,41 @@ template instantiateForRegion(allocator: untyped) {.dirty.} =
var heapLock: SysLock
initSysLock(heapLock)
proc allocShared(size: Natural): pointer =
proc allocSharedImpl(size: Natural): pointer =
when hasThreadSupport:
acquireSys(heapLock)
result = alloc(sharedHeap, size)
releaseSys(heapLock)
else:
result = alloc(size)
result = allocImpl(size)
proc allocShared0(size: Natural): pointer =
result = allocShared(size)
proc allocShared0Impl(size: Natural): pointer =
result = allocSharedImpl(size)
zeroMem(result, size)
proc deallocShared(p: pointer) =
proc deallocSharedImpl(p: pointer) =
when hasThreadSupport:
acquireSys(heapLock)
dealloc(sharedHeap, p)
releaseSys(heapLock)
else:
dealloc(p)
deallocImpl(p)
proc reallocShared(p: pointer, newSize: Natural): pointer =
proc reallocSharedImpl(p: pointer, newSize: Natural): pointer =
when hasThreadSupport:
acquireSys(heapLock)
result = realloc(sharedHeap, p, newSize)
releaseSys(heapLock)
else:
result = realloc(p, newSize)
result = reallocImpl(p, newSize)
proc reallocShared0Impl(p: pointer, oldSize, newSize: Natural): pointer =
when hasThreadSupport:
acquireSys(heapLock)
result = realloc0(sharedHeap, p, oldSize, newSize)
releaseSys(heapLock)
else:
result = realloc0Impl(p, oldSize, newSize)
when hasThreadSupport:
template sharedMemStatsShared(v: int) =