--newruntime: progress
This commit is contained in:
parent
f1a945b05f
commit
e263702688
15 changed files with 111 additions and 109 deletions
|
|
@ -12,7 +12,7 @@ type
|
|||
ThreadLocal ## the allocator is thread local only.
|
||||
ZerosMem ## the allocator always zeros the memory on an allocation
|
||||
Allocator* = ptr AllocatorObj
|
||||
AllocatorObj* {.inheritable.} = object
|
||||
AllocatorObj* {.inheritable, compilerproc.} = object
|
||||
alloc*: proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall, raises: [], tags: [].}
|
||||
dealloc*: proc (a: Allocator; p: pointer; size: int) {.nimcall, raises: [], tags: [].}
|
||||
realloc*: proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall, raises: [], tags: [].}
|
||||
|
|
|
|||
|
|
@ -38,15 +38,6 @@ type
|
|||
# we could remove it in non-debug builds but this seems
|
||||
# unwise.
|
||||
|
||||
proc isObj(obj: PNimType, subclass: cstring): bool {.compilerproc.} =
|
||||
proc strstr(s, sub: cstring): cstring {.header: "<string.h>", importc.}
|
||||
|
||||
result = strstr(obj.name, subclass) != nil
|
||||
|
||||
proc chckObj(obj: PNimType, subclass: cstring) {.compilerproc.} =
|
||||
# checks if obj is of type subclass:
|
||||
if not isObj(obj, subclass): sysFatal(ObjectConversionError, "invalid object conversion")
|
||||
|
||||
template `+!`(p: pointer, s: int): pointer =
|
||||
cast[pointer](cast[int](p) +% s)
|
||||
|
||||
|
|
@ -76,3 +67,12 @@ proc nimDestroyAndDispose(p: pointer) {.compilerRtl.} =
|
|||
let d = cast[ptr PNimType](p)[].destructor
|
||||
if d != nil: d(p)
|
||||
nimRawDispose(p)
|
||||
|
||||
proc isObj(obj: PNimType, subclass: cstring): bool {.compilerproc.} =
|
||||
proc strstr(s, sub: cstring): cstring {.header: "<string.h>", importc.}
|
||||
|
||||
result = strstr(obj.name, subclass) != nil
|
||||
|
||||
proc chckObj(obj: PNimType, subclass: cstring) {.compilerproc.} =
|
||||
# checks if obj is of type subclass:
|
||||
if not isObj(obj, subclass): sysFatal(ObjectConversionError, "invalid object conversion")
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
|
|||
type
|
||||
NimSeqPayload[T] = object
|
||||
cap: int
|
||||
region: Allocator
|
||||
allocator: Allocator
|
||||
data: UncheckedArray[T]
|
||||
|
||||
NimSeqV2*[T] = object
|
||||
|
|
@ -52,8 +52,8 @@ when not defined(nimV2):
|
|||
mixin `=destroy`
|
||||
when not supportsCopyMem(T):
|
||||
for i in 0..<x.len: `=destroy`(p.data[i])
|
||||
if p.region != nil:
|
||||
p.region.dealloc(p.region, p, payloadSize(p.cap))
|
||||
if p.allocator != nil:
|
||||
p.allocator.dealloc(p.allocator, p, payloadSize(p.cap))
|
||||
x.p = nil
|
||||
x.len = 0
|
||||
|
||||
|
|
@ -87,15 +87,15 @@ when not defined(nimV2):
|
|||
type
|
||||
PayloadBase = object
|
||||
cap: int
|
||||
region: Allocator
|
||||
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 region = getLocalAllocator()
|
||||
var p = cast[ptr PayloadBase](region.alloc(region, cap * elemSize + sizeof(int) + sizeof(Allocator)))
|
||||
p.region = region
|
||||
let allocator = getLocalAllocator()
|
||||
var p = cast[ptr PayloadBase](allocator.alloc(allocator, cap * elemSize + sizeof(int) + sizeof(Allocator)))
|
||||
p.allocator = allocator
|
||||
p.cap = cap
|
||||
result = p
|
||||
else:
|
||||
|
|
@ -112,12 +112,12 @@ proc prepareSeqAdd(len: int; p: pointer; addlen, elemSize: int): pointer {.
|
|||
# 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 region = if p.region == nil: getLocalAllocator() else: p.region
|
||||
let allocator = if p.allocator == nil: getLocalAllocator() else: p.allocator
|
||||
let cap = max(resize(p.cap), len+addlen)
|
||||
var q = cast[ptr PayloadBase](region.realloc(region, p,
|
||||
var q = cast[ptr PayloadBase](allocator.realloc(allocator, p,
|
||||
sizeof(int) + sizeof(Allocator) + elemSize * p.cap,
|
||||
sizeof(int) + sizeof(Allocator) + elemSize * cap))
|
||||
q.region = region
|
||||
q.allocator = allocator
|
||||
q.cap = cap
|
||||
result = q
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ import allocators
|
|||
type
|
||||
NimStrPayload {.core.} = object
|
||||
cap: int
|
||||
region: Allocator
|
||||
allocator: Allocator
|
||||
data: UncheckedArray[char]
|
||||
|
||||
NimStringV2 {.core.} = object
|
||||
|
|
@ -37,7 +37,7 @@ type
|
|||
|
||||
const nimStrVersion {.core.} = 2
|
||||
|
||||
template isLiteral(s): bool = s.p == nil or s.p.region == nil
|
||||
template isLiteral(s): bool = s.p == nil or s.p.allocator == nil
|
||||
|
||||
template contentSize(cap): int = cap + 1 + sizeof(int) + sizeof(Allocator)
|
||||
|
||||
|
|
@ -45,7 +45,7 @@ when not defined(nimV2):
|
|||
|
||||
template frees(s) =
|
||||
if not isLiteral(s):
|
||||
s.p.region.dealloc(s.p.region, s.p, contentSize(s.p.cap))
|
||||
s.p.allocator.dealloc(s.p.allocator, s.p, contentSize(s.p.cap))
|
||||
|
||||
proc `=destroy`(s: var string) =
|
||||
var a = cast[ptr NimStringV2](addr s)
|
||||
|
|
@ -72,12 +72,12 @@ when not defined(nimV2):
|
|||
# we can shallow copy literals:
|
||||
a.p = b.p
|
||||
else:
|
||||
let region = if a.p != nil and a.p.region != nil: a.p.region else: getLocalAllocator()
|
||||
let allocator = if a.p != nil and a.p.allocator != nil: a.p.allocator else: getLocalAllocator()
|
||||
# we have to allocate the 'cap' here, consider
|
||||
# 'let y = newStringOfCap(); var x = y'
|
||||
# on the other hand... These get turned into moves now.
|
||||
a.p = cast[ptr NimStrPayload](region.alloc(region, contentSize(b.len)))
|
||||
a.p.region = region
|
||||
a.p = cast[ptr NimStrPayload](allocator.alloc(allocator, contentSize(b.len)))
|
||||
a.p.allocator = allocator
|
||||
a.p.cap = b.len
|
||||
copyMem(unsafeAddr a.p.data[0], unsafeAddr b.p.data[0], b.len+1)
|
||||
|
||||
|
|
@ -90,16 +90,16 @@ proc prepareAdd(s: var NimStringV2; addlen: int) {.compilerRtl.} =
|
|||
if isLiteral(s):
|
||||
let oldP = s.p
|
||||
# can't mutate a literal, so we need a fresh copy here:
|
||||
let region = getLocalAllocator()
|
||||
s.p = cast[ptr NimStrPayload](region.alloc(region, contentSize(s.len + addlen)))
|
||||
s.p.region = region
|
||||
let allocator = getLocalAllocator()
|
||||
s.p = cast[ptr NimStrPayload](allocator.alloc(allocator, contentSize(s.len + addlen)))
|
||||
s.p.allocator = allocator
|
||||
s.p.cap = s.len + addlen
|
||||
if s.len > 0:
|
||||
# we are about to append, so there is no need to copy the \0 terminator:
|
||||
copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len)
|
||||
elif s.len + addlen > s.p.cap:
|
||||
let cap = max(s.len + addlen, resize(s.p.cap))
|
||||
s.p = cast[ptr NimStrPayload](s.p.region.realloc(s.p.region, s.p,
|
||||
s.p = cast[ptr NimStrPayload](s.p.allocator.realloc(s.p.allocator, s.p,
|
||||
oldSize = contentSize(s.p.cap),
|
||||
newSize = contentSize(cap)))
|
||||
s.p.cap = cap
|
||||
|
|
@ -114,9 +114,9 @@ proc toNimStr(str: cstring, len: int): NimStringV2 {.compilerProc.} =
|
|||
if len <= 0:
|
||||
result = NimStringV2(len: 0, p: nil)
|
||||
else:
|
||||
let region = getLocalAllocator()
|
||||
var p = cast[ptr NimStrPayload](region.alloc(region, contentSize(len)))
|
||||
p.region = region
|
||||
let allocator = getLocalAllocator()
|
||||
var p = cast[ptr NimStrPayload](allocator.alloc(allocator, contentSize(len)))
|
||||
p.allocator = allocator
|
||||
p.cap = len
|
||||
if len > 0:
|
||||
# we are about to append, so there is no need to copy the \0 terminator:
|
||||
|
|
@ -147,9 +147,9 @@ proc rawNewString(space: int): NimStringV2 {.compilerProc.} =
|
|||
if space <= 0:
|
||||
result = NimStringV2(len: 0, p: nil)
|
||||
else:
|
||||
let region = getLocalAllocator()
|
||||
var p = cast[ptr NimStrPayload](region.alloc(region, contentSize(space)))
|
||||
p.region = region
|
||||
let allocator = getLocalAllocator()
|
||||
var p = cast[ptr NimStrPayload](allocator.alloc(allocator, contentSize(space)))
|
||||
p.allocator = allocator
|
||||
p.cap = space
|
||||
result = NimStringV2(len: 0, p: p)
|
||||
|
||||
|
|
@ -157,9 +157,9 @@ proc mnewString(len: int): NimStringV2 {.compilerProc.} =
|
|||
if len <= 0:
|
||||
result = NimStringV2(len: 0, p: nil)
|
||||
else:
|
||||
let region = getLocalAllocator()
|
||||
var p = cast[ptr NimStrPayload](region.alloc(region, contentSize(len)))
|
||||
p.region = region
|
||||
let allocator = getLocalAllocator()
|
||||
var p = cast[ptr NimStrPayload](allocator.alloc(allocator, contentSize(len)))
|
||||
p.allocator = allocator
|
||||
p.cap = len
|
||||
result = NimStringV2(len: len, p: p)
|
||||
|
||||
|
|
|
|||
|
|
@ -679,7 +679,8 @@ when not defined(JS) and not defined(nimscript):
|
|||
when not defined(gcDestructors):
|
||||
template space(s: PGenericSeq): int {.dirty.} =
|
||||
s.reserved and not (seqShallowFlag or strlitFlag)
|
||||
include "system/hti"
|
||||
when not defined(nimV2):
|
||||
include "system/hti"
|
||||
|
||||
type
|
||||
byte* = uint8 ## This is an alias for ``uint8``, that is an unsigned
|
||||
|
|
@ -2925,6 +2926,14 @@ proc compiles*(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime.
|
|||
## echo "'+' for integers is available"
|
||||
discard
|
||||
|
||||
when not defined(js) and not defined(nimscript):
|
||||
include "system/ansi_c"
|
||||
|
||||
when not declared(sysFatal):
|
||||
include "system/fatal"
|
||||
|
||||
when defined(nimV2) and not defined(nimscript):
|
||||
include core/runtime_v2
|
||||
|
||||
import system/assertions
|
||||
export assertions
|
||||
|
|
@ -2995,7 +3004,7 @@ when not defined(nimscript) and hasAlloc:
|
|||
gcOptimizeTime, ## optimize for speed
|
||||
gcOptimizeSpace ## optimize for memory footprint
|
||||
|
||||
when not defined(JS):
|
||||
when not defined(JS) and not defined(nimV2):
|
||||
proc GC_disable*() {.rtl, inl, benign.}
|
||||
## Disables the GC. If called `n` times, `n` calls to `GC_enable`
|
||||
## are needed to reactivate the GC.
|
||||
|
|
@ -3242,12 +3251,6 @@ when hostOS == "standalone":
|
|||
if s == nil or s.len == 0: result = cstring""
|
||||
else: result = cstring(addr s.data)
|
||||
|
||||
when not defined(js) and not defined(nimscript):
|
||||
include "system/ansi_c"
|
||||
|
||||
when not declared(sysFatal):
|
||||
include "system/fatal"
|
||||
|
||||
proc getTypeInfo*[T](x: T): pointer {.magic: "GetTypeInfo", benign.}
|
||||
## Get type information for `x`.
|
||||
##
|
||||
|
|
@ -3364,11 +3367,8 @@ when not defined(JS): #and not defined(nimscript):
|
|||
{.push stack_trace: off, profiler:off.}
|
||||
|
||||
when hasAlloc:
|
||||
when not defined(gcRegions):
|
||||
when not defined(gcRegions) and not defined(nimV2):
|
||||
proc initGC() {.gcsafe.}
|
||||
when not defined(boehmgc) and not defined(useMalloc) and
|
||||
not defined(gogc) and not defined(gcRegions):
|
||||
proc initAllocator() {.inline.}
|
||||
|
||||
proc initStackBottom() {.inline, compilerproc.} =
|
||||
# WARNING: This is very fragile! An array size of 8 does not work on my
|
||||
|
|
@ -3547,26 +3547,27 @@ when not defined(JS): #and not defined(nimscript):
|
|||
else:
|
||||
const GenericSeqSize = (2 * sizeof(int))
|
||||
|
||||
proc getDiscriminant(aa: pointer, n: ptr TNimNode): int =
|
||||
sysAssert(n.kind == nkCase, "getDiscriminant: node != nkCase")
|
||||
var d: int
|
||||
var a = cast[ByteAddress](aa)
|
||||
case n.typ.size
|
||||
of 1: d = ze(cast[ptr int8](a +% n.offset)[])
|
||||
of 2: d = ze(cast[ptr int16](a +% n.offset)[])
|
||||
of 4: d = int(cast[ptr int32](a +% n.offset)[])
|
||||
of 8: d = int(cast[ptr int64](a +% n.offset)[])
|
||||
else: sysAssert(false, "getDiscriminant: invalid n.typ.size")
|
||||
return d
|
||||
when not defined(nimV2):
|
||||
proc getDiscriminant(aa: pointer, n: ptr TNimNode): int =
|
||||
sysAssert(n.kind == nkCase, "getDiscriminant: node != nkCase")
|
||||
var d: int
|
||||
var a = cast[ByteAddress](aa)
|
||||
case n.typ.size
|
||||
of 1: d = ze(cast[ptr int8](a +% n.offset)[])
|
||||
of 2: d = ze(cast[ptr int16](a +% n.offset)[])
|
||||
of 4: d = int(cast[ptr int32](a +% n.offset)[])
|
||||
of 8: d = int(cast[ptr int64](a +% n.offset)[])
|
||||
else: sysAssert(false, "getDiscriminant: invalid n.typ.size")
|
||||
return d
|
||||
|
||||
proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode =
|
||||
var discr = getDiscriminant(aa, n)
|
||||
if discr <% n.len:
|
||||
result = n.sons[discr]
|
||||
if result == nil: result = n.sons[n.len]
|
||||
# n.sons[n.len] contains the ``else`` part (but may be nil)
|
||||
else:
|
||||
result = n.sons[n.len]
|
||||
proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode =
|
||||
var discr = getDiscriminant(aa, n)
|
||||
if discr <% n.len:
|
||||
result = n.sons[discr]
|
||||
if result == nil: result = n.sons[n.len]
|
||||
# n.sons[n.len] contains the ``else`` part (but may be nil)
|
||||
else:
|
||||
result = n.sons[n.len]
|
||||
|
||||
{.push profiler:off.}
|
||||
when hasAlloc: include "system/mmdisp"
|
||||
|
|
@ -3584,7 +3585,8 @@ when not defined(JS): #and not defined(nimscript):
|
|||
when not defined(nimscript) and hasAlloc:
|
||||
when not defined(gcDestructors):
|
||||
include "system/assign"
|
||||
include "system/repr"
|
||||
when not defined(nimV2):
|
||||
include "system/repr"
|
||||
|
||||
when hostOS != "standalone" and not defined(nimscript):
|
||||
proc getCurrentException*(): ref Exception {.compilerRtl, inl, benign.} =
|
||||
|
|
|
|||
|
|
@ -224,10 +224,6 @@ proc addChunkToMatrix(a: var MemRegion; b: PBigChunk) =
|
|||
setBit(sl, a.slBitmap[fl])
|
||||
setBit(fl, a.flBitmap)
|
||||
|
||||
{.push stack_trace: off.}
|
||||
proc initAllocator() = discard "nothing to do anymore"
|
||||
{.pop.}
|
||||
|
||||
proc incCurrMem(a: var MemRegion, bytes: int) {.inline.} =
|
||||
inc(a.currMem, bytes)
|
||||
|
||||
|
|
|
|||
|
|
@ -139,4 +139,11 @@ proc c_free(p: pointer) {.
|
|||
proc c_realloc(p: pointer, newsize: csize): pointer {.
|
||||
importc: "realloc", header: "<stdlib.h>".}
|
||||
|
||||
proc c_fwrite(buf: pointer, size, n: csize, f: CFilePtr): cint {.
|
||||
importc: "fwrite", header: "<stdio.h>".}
|
||||
|
||||
proc rawWrite(f: CFilePtr, s: cstring) {.compilerproc, nonreloadable, inline.} =
|
||||
# we cannot throw an exception here!
|
||||
discard c_fwrite(s, 1, s.len, f)
|
||||
|
||||
{.pop}
|
||||
|
|
|
|||
|
|
@ -17,13 +17,6 @@ var
|
|||
## instead of `stdmsg.write` when printing stacktrace.
|
||||
## Unstable API.
|
||||
|
||||
proc c_fwrite(buf: pointer, size, n: csize, f: CFilePtr): cint {.
|
||||
importc: "fwrite", header: "<stdio.h>".}
|
||||
|
||||
proc rawWrite(f: CFilePtr, s: cstring) {.compilerproc, nonreloadable, hcrInline.} =
|
||||
# we cannot throw an exception here!
|
||||
discard c_fwrite(s, 1, s.len, f)
|
||||
|
||||
when not defined(windows) or not defined(guiapp):
|
||||
proc writeToStdErr(msg: cstring) = rawWrite(cstderr, msg)
|
||||
|
||||
|
|
|
|||
|
|
@ -167,7 +167,6 @@ when declared(threadType):
|
|||
## This function is available only when ``--threads:on`` and ``--tlsEmulation:off``
|
||||
## switches are used
|
||||
if threadType == ThreadType.None:
|
||||
initAllocator()
|
||||
var stackTop {.volatile.}: pointer
|
||||
nimGC_setStackBottom(addr(stackTop))
|
||||
initGC()
|
||||
|
|
|
|||
|
|
@ -497,7 +497,8 @@ else:
|
|||
# XXX due to bootstrapping reasons, we cannot use compileOption("gc", "stack") here
|
||||
include "system/gc_regions"
|
||||
elif defined(nimV2):
|
||||
include "core/runtime_v2"
|
||||
var allocator {.rtlThreadVar.}: MemRegion
|
||||
instantiateForRegion(allocator)
|
||||
elif defined(gcMarkAndSweep) or defined(gcDestructors):
|
||||
# XXX use 'compileOption' here
|
||||
include "system/gc_ms"
|
||||
|
|
|
|||
|
|
@ -456,8 +456,6 @@ template threadProcWrapperBody(closure: untyped): untyped =
|
|||
var thrd = cast[ptr Thread[TArg]](closure)
|
||||
var core = thrd.core
|
||||
when declared(globalsSlot): threadVarSetValue(globalsSlot, thrd.core)
|
||||
when declared(initAllocator):
|
||||
initAllocator()
|
||||
threadProcWrapStackFrame(thrd)
|
||||
# Since an unhandled exception terminates the whole process (!), there is
|
||||
# no need for a ``try finally`` here, nor would it be correct: The current
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue