better inferfacing to boehm GC

This commit is contained in:
Araq
2012-01-15 23:47:49 +01:00
parent 9a2df340a7
commit 4de5b82fdc
2 changed files with 39 additions and 13 deletions

View File

@@ -79,6 +79,23 @@ when defined(boehmgc):
importc: "GC_realloc", dynlib: boehmLib.}
proc boehmDealloc(p: pointer) {.importc: "GC_free", dynlib: boehmLib.}
proc boehmGetHeapSize: int {.importc: "GC_get_heap_size", dynlib: boehmLib.}
## Return the number of bytes in the heap. Excludes collector private
## data structures. Includes empty blocks and fragmentation loss.
## Includes some pages that were allocated but never written.
proc boehmGetFreeBytes: int {.importc: "GC_get_free_bytes", dynlib: boehmLib.}
## Return a lower bound on the number of free bytes in the heap.
proc boehmGetBytesSinceGC: int {.importc: "GC_get_bytes_since_gc",
dynlib: boehmLib.}
## Return the number of bytes allocated since the last collection.
proc boehmGetTotalBytes: int {.importc: "GC_get_total_bytes",
dynlib: boehmLib.}
## Return the total number of bytes allocated in this process.
## Never decreases.
when not defined(useNimRtl):
proc alloc(size: int): pointer =
@@ -91,6 +108,10 @@ when defined(boehmgc):
result = boehmRealloc(p, newsize)
if result == nil: raiseOutOfMem()
proc dealloc(p: Pointer) = boehmDealloc(p)
proc allocAtomic(size: int): pointer =
result = boehmAllocAtomic(size)
zeroMem(result, size)
proc allocShared(size: int): pointer =
result = boehmAlloc(size)
@@ -113,9 +134,9 @@ when defined(boehmgc):
proc GC_disableMarkAndSweep() = nil
proc GC_getStatistics(): string = return ""
proc getOccupiedMem(): int = return -1
proc getFreeMem(): int = return -1
proc getTotalMem(): int = return -1
proc getOccupiedMem(): int = return boehmGetHeapSize()-boehmGetFreeBytes()
proc getFreeMem(): int = return boehmGetFreeBytes()
proc getTotalMem(): int = return boehmGetHeapSize()
proc setStackBottom(theStackBottom: pointer) = nil
@@ -123,7 +144,8 @@ when defined(boehmgc):
when defined(macosx): boehmGCinit()
proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
result = alloc(size)
if ntfNoRefs in typ.flags: result = allocAtomic(size)
else: result = alloc(size)
proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
cast[PGenericSeq](result).len = len

View File

@@ -32,11 +32,17 @@ proc eqStrings(a, b: NimString): bool {.inline, compilerProc.} =
return a.len == b.len and
c_memcmp(a.data, b.data, a.len * sizeof(char)) == 0'i32
when defined(allocAtomic):
template allocStr(size: expr): expr =
cast[NimString](allocAtomic(size))
else:
template allocStr(size: expr): expr =
cast[NimString](newObj(addr(strDesc), size))
proc rawNewString(space: int): NimString {.compilerProc.} =
var s = space
if s < 8: s = 7
result = cast[NimString](newObj(addr(strDesc), sizeof(TGenericSeq) +
(s+1) * sizeof(char)))
result = allocStr(sizeof(TGenericSeq) + s + 1)
result.space = s
proc mnewString(len: int): NimString {.compilerProc.} =
@@ -78,13 +84,12 @@ proc copyStringRC1(src: NimString): NimString {.compilerProc.} =
if s < 8: s = 7
when defined(newObjRC1):
result = cast[NimString](newObjRC1(addr(strDesc), sizeof(TGenericSeq) +
(s+1) * sizeof(char)))
s+1))
else:
result = cast[NimString](newObj(addr(strDesc), sizeof(TGenericSeq) +
(s+1) * sizeof(char)))
result = allocStr(sizeof(TGenericSeq) + s + 1)
result.space = s
result.len = src.len
c_memcpy(result.data, src.data, (src.len + 1) * sizeof(Char))
c_memcpy(result.data, src.data, src.len + 1)
proc hashString(s: string): int {.compilerproc.} =
# the compiler needs exactly the same hash function!
@@ -150,15 +155,14 @@ proc resizeString(dest: NimString, addlen: int): NimString {.compilerproc.} =
result = dest
else: # slow path:
var sp = max(resize(dest.space), dest.len + addLen)
result = cast[NimString](growObj(dest, sizeof(TGenericSeq) +
(sp+1) * sizeof(Char)))
result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1))
result.space = sp
#result = rawNewString(sp)
#copyMem(result, dest, dest.len * sizeof(char) + sizeof(TGenericSeq))
# DO NOT UPDATE LEN YET: dest.len = newLen
proc appendString(dest, src: NimString) {.compilerproc, inline.} =
c_memcpy(addr(dest.data[dest.len]), src.data, (src.len + 1) * sizeof(Char))
c_memcpy(addr(dest.data[dest.len]), src.data, src.len + 1)
inc(dest.len, src.len)
proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} =