Skip to content

Removed lib/system/allocators.nim. seqs_v2 and strs_v2 now uses allocShared0. #13190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2037,15 +2037,15 @@ proc genDestroy(p: BProc; n: PNode) =
of tyString:
var a: TLoc
initLocExpr(p, arg, a)
linefmt(p, cpsStmts, "if ($1.p && $1.p->allocator) {$n" &
" $1.p->allocator->dealloc($1.p->allocator, $1.p, $1.p->cap + 1 + sizeof(NI) + sizeof(void*));$n" &
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
" #deallocShared($1.p);$n" &
" $1.p = NIM_NIL; }$n",
[rdLoc(a)])
of tySequence:
var a: TLoc
initLocExpr(p, arg, a)
linefmt(p, cpsStmts, "if ($1.p && $1.p->allocator) {$n" &
" $1.p->allocator->dealloc($1.p->allocator, $1.p, ($1.p->cap * sizeof($2)) + sizeof(NI) + sizeof(void*));$n" &
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
" #deallocShared($1.p);$n" &
" $1.p = NIM_NIL; }$n",
[rdLoc(a), getTypeDesc(p.module, t.lastSon)])
else: discard "nothing to do"
Expand Down Expand Up @@ -2917,8 +2917,8 @@ proc genConstSeqV2(p: BProc, n: PNode, t: PType; isConst: bool): Rope =

appcg(p.module, cfsData,
"static $5 struct {$n" &
" NI cap; void* allocator; $1 data[$2];$n" &
"} $3 = {$2, NIM_NIL, $4};$n", [
" NI cap; $1 data[$2];$n" &
"} $3 = {$2 | NIM_STRLIT_FLAG, $4};$n", [
getTypeDesc(p.module, base), n.len, payload, data,
if isConst: "const" else: ""])
result = "{$1, ($2*)&$3}" % [rope(n.len), getSeqPayloadType(p.module, t), payload]
Expand Down
4 changes: 2 additions & 2 deletions compiler/ccgliterals.nim
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ proc genStringLiteralV1(m: BModule; n: PNode): Rope =

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

Expand Down
2 changes: 1 addition & 1 deletion compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ proc seqV2ContentType(m: BModule; t: PType; check: var IntSet) =
appcg(m, m.s[cfsTypes], """$N
$3ifndef $2_Content_PP
$3define $2_Content_PP
struct $2_Content { NI cap;#AllocatorObj* allocator;$1 data[SEQ_DECL_SIZE];};
struct $2_Content { NI cap; $1 data[SEQ_DECL_SIZE];};
$3endif$N
""", [getTypeDescAux(m, t.skipTypes(abstractInst)[0], check), result, rope"#"])

Expand Down
44 changes: 31 additions & 13 deletions lib/system/alloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -997,17 +1002,22 @@ 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 =
Expand All @@ -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) =
Expand Down
86 changes: 0 additions & 86 deletions lib/system/allocators.nim

This file was deleted.

2 changes: 2 additions & 0 deletions lib/system/ansi_c.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ proc c_sprintf*(buf, frmt: cstring): cint {.

proc c_malloc*(size: csize_t): pointer {.
importc: "malloc", header: "<stdlib.h>".}
proc c_calloc*(nmemb, size: csize_t): pointer {.
importc: "calloc", header: "<stdlib.h>".}
proc c_free*(p: pointer) {.
importc: "free", header: "<stdlib.h>".}
proc c_realloc*(p: pointer, newsize: csize_t): pointer {.
Expand Down
26 changes: 18 additions & 8 deletions lib/system/gc_regions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,21 @@ proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
deprecated: "old compiler compat".} = asgnRef(dest, src)

proc alloc(size: Natural): pointer =
proc allocImpl(size: Natural): pointer =
result = c_malloc(cast[csize_t](size))
if result == nil: raiseOutOfMem()
proc alloc0(size: Natural): pointer =
proc alloc0Impl(size: Natural): pointer =
result = alloc(size)
zeroMem(result, size)
proc realloc(p: pointer, newsize: Natural): pointer =
proc reallocImpl(p: pointer, newsize: Natural): pointer =
result = c_realloc(p, cast[csize_t](newsize))
if result == nil: raiseOutOfMem()
proc dealloc(p: pointer) = c_free(p)
proc realloc0Impl(p: pointer, oldsize, newsize: Natural): pointer =
result = c_realloc(p, cast[csize_t](newsize))
if result == nil: raiseOutOfMem()
if newsize > oldsize:
zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
proc deallocImpl(p: pointer) = c_free(p)

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

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

proc allocShared(size: Natural): pointer =
proc allocSharedImpl(size: Natural): pointer =
result = c_malloc(cast[csize_t](size))
if result == nil: raiseOutOfMem()
proc allocShared0(size: Natural): pointer =
proc allocShared0Impl(size: Natural): pointer =
result = alloc(size)
zeroMem(result, size)
proc reallocShared(p: pointer, newsize: Natural): pointer =
proc reallocSharedImpl(p: pointer, newsize: Natural): pointer =
result = c_realloc(p, cast[csize_t](newsize))
if result == nil: raiseOutOfMem()
proc reallocShared0Impl(p: pointer, oldsize, newsize: Natural): pointer =
result = c_realloc(p, cast[csize_t](newsize))
if result == nil: raiseOutOfMem()
proc deallocShared(p: pointer) = c_free(p)
if newsize > oldsize:
zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
proc deallocSharedImpl(p: pointer) = c_free(p)

when hasThreadSupport:
proc getFreeSharedMem(): int = 0
Expand Down
Loading