Skip to content

Commit b68eb1c

Browse files
zevvAraq
authored andcommitted
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
1 parent f500895 commit b68eb1c

21 files changed

+274
-311
lines changed

compiler/ccgexprs.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,15 +2037,15 @@ proc genDestroy(p: BProc; n: PNode) =
20372037
of tyString:
20382038
var a: TLoc
20392039
initLocExpr(p, arg, a)
2040-
linefmt(p, cpsStmts, "if ($1.p && $1.p->allocator) {$n" &
2041-
" $1.p->allocator->dealloc($1.p->allocator, $1.p, $1.p->cap + 1 + sizeof(NI) + sizeof(void*));$n" &
2040+
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
2041+
" #deallocShared($1.p);$n" &
20422042
" $1.p = NIM_NIL; }$n",
20432043
[rdLoc(a)])
20442044
of tySequence:
20452045
var a: TLoc
20462046
initLocExpr(p, arg, a)
2047-
linefmt(p, cpsStmts, "if ($1.p && $1.p->allocator) {$n" &
2048-
" $1.p->allocator->dealloc($1.p->allocator, $1.p, ($1.p->cap * sizeof($2)) + sizeof(NI) + sizeof(void*));$n" &
2047+
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
2048+
" #deallocShared($1.p);$n" &
20492049
" $1.p = NIM_NIL; }$n",
20502050
[rdLoc(a), getTypeDesc(p.module, t.lastSon)])
20512051
else: discard "nothing to do"
@@ -2917,8 +2917,8 @@ proc genConstSeqV2(p: BProc, n: PNode, t: PType; isConst: bool): Rope =
29172917

29182918
appcg(p.module, cfsData,
29192919
"static $5 struct {$n" &
2920-
" NI cap; void* allocator; $1 data[$2];$n" &
2921-
"} $3 = {$2, NIM_NIL, $4};$n", [
2920+
" NI cap; $1 data[$2];$n" &
2921+
"} $3 = {$2 | NIM_STRLIT_FLAG, $4};$n", [
29222922
getTypeDesc(p.module, base), n.len, payload, data,
29232923
if isConst: "const" else: ""])
29242924
result = "{$1, ($2*)&$3}" % [rope(n.len), getSeqPayloadType(p.module, t), payload]

compiler/ccgliterals.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ proc genStringLiteralV1(m: BModule; n: PNode): Rope =
5555

5656
proc genStringLiteralDataOnlyV2(m: BModule, s: string; result: Rope; isConst: bool) =
5757
m.s[cfsData].addf("static $4 struct {$n" &
58-
" NI cap; void* allocator; NIM_CHAR data[$2+1];$n" &
59-
"} $1 = { $2, NIM_NIL, $3 };$n",
58+
" NI cap; NIM_CHAR data[$2+1];$n" &
59+
"} $1 = { $2 | NIM_STRLIT_FLAG, $3 };$n",
6060
[result, rope(s.len), makeCString(s),
6161
rope(if isConst: "const" else: "")])
6262

compiler/ccgtypes.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ proc seqV2ContentType(m: BModule; t: PType; check: var IntSet) =
432432
appcg(m, m.s[cfsTypes], """$N
433433
$3ifndef $2_Content_PP
434434
$3define $2_Content_PP
435-
struct $2_Content { NI cap;#AllocatorObj* allocator;$1 data[SEQ_DECL_SIZE];};
435+
struct $2_Content { NI cap; $1 data[SEQ_DECL_SIZE];};
436436
$3endif$N
437437
""", [getTypeDescAux(m, t.skipTypes(abstractInst)[0], check), result, rope"#"])
438438

lib/system/alloc.nim

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -948,13 +948,18 @@ proc dealloc(allocator: var MemRegion, p: pointer) =
948948

949949
proc realloc(allocator: var MemRegion, p: pointer, newsize: Natural): pointer =
950950
if newsize > 0:
951-
result = alloc0(allocator, newsize)
951+
result = alloc(allocator, newsize)
952952
if p != nil:
953953
copyMem(result, p, min(ptrSize(p), newsize))
954954
dealloc(allocator, p)
955955
elif p != nil:
956956
dealloc(allocator, p)
957957

958+
proc realloc0(allocator: var MemRegion, p: pointer, oldsize, newsize: Natural): pointer =
959+
result = realloc(allocator, p, newsize)
960+
if newsize > oldsize:
961+
zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
962+
958963
proc deallocOsPages(a: var MemRegion) =
959964
# we free every 'ordinarily' allocated page by iterating over the page bits:
960965
var it = addr(a.heapLinks)
@@ -997,17 +1002,22 @@ template instantiateForRegion(allocator: untyped) {.dirty.} =
9971002

9981003
proc deallocOsPages = deallocOsPages(allocator)
9991004

1000-
proc alloc(size: Natural): pointer =
1005+
proc allocImpl(size: Natural): pointer =
10011006
result = alloc(allocator, size)
10021007

1003-
proc alloc0(size: Natural): pointer =
1008+
proc alloc0Impl(size: Natural): pointer =
10041009
result = alloc0(allocator, size)
10051010

1006-
proc dealloc(p: pointer) =
1011+
proc deallocImpl(p: pointer) =
10071012
dealloc(allocator, p)
10081013

1009-
proc realloc(p: pointer, newSize: Natural): pointer =
1014+
proc reallocImpl(p: pointer, newSize: Natural): pointer =
1015+
result = realloc(allocator, p, newSize)
1016+
1017+
proc realloc0Impl(p: pointer, oldSize, newSize: Natural): pointer =
10101018
result = realloc(allocator, p, newSize)
1019+
if newSize > oldSize:
1020+
zeroMem(cast[pointer](cast[int](result) + oldSize), newSize - oldSize)
10111021

10121022
when false:
10131023
proc countFreeMem(): int =
@@ -1034,33 +1044,41 @@ template instantiateForRegion(allocator: untyped) {.dirty.} =
10341044
var heapLock: SysLock
10351045
initSysLock(heapLock)
10361046

1037-
proc allocShared(size: Natural): pointer =
1047+
proc allocSharedImpl(size: Natural): pointer =
10381048
when hasThreadSupport:
10391049
acquireSys(heapLock)
10401050
result = alloc(sharedHeap, size)
10411051
releaseSys(heapLock)
10421052
else:
1043-
result = alloc(size)
1053+
result = allocImpl(size)
10441054

1045-
proc allocShared0(size: Natural): pointer =
1046-
result = allocShared(size)
1055+
proc allocShared0Impl(size: Natural): pointer =
1056+
result = allocSharedImpl(size)
10471057
zeroMem(result, size)
10481058

1049-
proc deallocShared(p: pointer) =
1059+
proc deallocSharedImpl(p: pointer) =
10501060
when hasThreadSupport:
10511061
acquireSys(heapLock)
10521062
dealloc(sharedHeap, p)
10531063
releaseSys(heapLock)
10541064
else:
1055-
dealloc(p)
1065+
deallocImpl(p)
10561066

1057-
proc reallocShared(p: pointer, newSize: Natural): pointer =
1067+
proc reallocSharedImpl(p: pointer, newSize: Natural): pointer =
10581068
when hasThreadSupport:
10591069
acquireSys(heapLock)
10601070
result = realloc(sharedHeap, p, newSize)
10611071
releaseSys(heapLock)
10621072
else:
1063-
result = realloc(p, newSize)
1073+
result = reallocImpl(p, newSize)
1074+
1075+
proc reallocShared0Impl(p: pointer, oldSize, newSize: Natural): pointer =
1076+
when hasThreadSupport:
1077+
acquireSys(heapLock)
1078+
result = realloc0(sharedHeap, p, oldSize, newSize)
1079+
releaseSys(heapLock)
1080+
else:
1081+
result = realloc0Impl(p, oldSize, newSize)
10641082

10651083
when hasThreadSupport:
10661084
template sharedMemStatsShared(v: int) =

lib/system/allocators.nim

Lines changed: 0 additions & 86 deletions
This file was deleted.

lib/system/ansi_c.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ proc c_sprintf*(buf, frmt: cstring): cint {.
140140

141141
proc c_malloc*(size: csize_t): pointer {.
142142
importc: "malloc", header: "<stdlib.h>".}
143+
proc c_calloc*(nmemb, size: csize_t): pointer {.
144+
importc: "calloc", header: "<stdlib.h>".}
143145
proc c_free*(p: pointer) {.
144146
importc: "free", header: "<stdlib.h>".}
145147
proc c_realloc*(p: pointer, newsize: csize_t): pointer {.

lib/system/gc_regions.nim

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,21 @@ proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
377377
proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
378378
deprecated: "old compiler compat".} = asgnRef(dest, src)
379379

380-
proc alloc(size: Natural): pointer =
380+
proc allocImpl(size: Natural): pointer =
381381
result = c_malloc(cast[csize_t](size))
382382
if result == nil: raiseOutOfMem()
383-
proc alloc0(size: Natural): pointer =
383+
proc alloc0Impl(size: Natural): pointer =
384384
result = alloc(size)
385385
zeroMem(result, size)
386-
proc realloc(p: pointer, newsize: Natural): pointer =
386+
proc reallocImpl(p: pointer, newsize: Natural): pointer =
387387
result = c_realloc(p, cast[csize_t](newsize))
388388
if result == nil: raiseOutOfMem()
389-
proc dealloc(p: pointer) = c_free(p)
389+
proc realloc0Impl(p: pointer, oldsize, newsize: Natural): pointer =
390+
result = c_realloc(p, cast[csize_t](newsize))
391+
if result == nil: raiseOutOfMem()
392+
if newsize > oldsize:
393+
zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
394+
proc deallocImpl(p: pointer) = c_free(p)
390395

391396
proc alloc0(r: var MemRegion; size: Natural): pointer =
392397
# ignore the region. That is correct for the channels module
@@ -400,16 +405,21 @@ proc alloc(r: var MemRegion; size: Natural): pointer =
400405

401406
proc dealloc(r: var MemRegion; p: pointer) = dealloc(p)
402407

403-
proc allocShared(size: Natural): pointer =
408+
proc allocSharedImpl(size: Natural): pointer =
404409
result = c_malloc(cast[csize_t](size))
405410
if result == nil: raiseOutOfMem()
406-
proc allocShared0(size: Natural): pointer =
411+
proc allocShared0Impl(size: Natural): pointer =
407412
result = alloc(size)
408413
zeroMem(result, size)
409-
proc reallocShared(p: pointer, newsize: Natural): pointer =
414+
proc reallocSharedImpl(p: pointer, newsize: Natural): pointer =
415+
result = c_realloc(p, cast[csize_t](newsize))
416+
if result == nil: raiseOutOfMem()
417+
proc reallocShared0Impl(p: pointer, oldsize, newsize: Natural): pointer =
410418
result = c_realloc(p, cast[csize_t](newsize))
411419
if result == nil: raiseOutOfMem()
412-
proc deallocShared(p: pointer) = c_free(p)
420+
if newsize > oldsize:
421+
zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
422+
proc deallocSharedImpl(p: pointer) = c_free(p)
413423

414424
when hasThreadSupport:
415425
proc getFreeSharedMem(): int = 0

0 commit comments

Comments
 (0)