summary refs log tree commit diff stats
path: root/lib/system
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2012-01-15 23:47:49 +0100
committerAraq <rumpf_a@web.de>2012-01-15 23:47:49 +0100
commit4de5b82fdca41204afe0e5405b27d325bb9cf164 (patch)
treebec9edee9bb7d4ddcf21005bf9bcfb0303abd255 /lib/system
parent9a2df340a78c10d092599cf5b0222eea5ecb67f2 (diff)
downloadNim-4de5b82fdca41204afe0e5405b27d325bb9cf164.tar.gz
better inferfacing to boehm GC
Diffstat (limited to 'lib/system')
-rwxr-xr-xlib/system/mmdisp.nim30
-rwxr-xr-xlib/system/sysstr.nim22
2 files changed, 39 insertions, 13 deletions
diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim
index 9dc0ca8ee..33df8e89a 100755
--- a/lib/system/mmdisp.nim
+++ b/lib/system/mmdisp.nim
@@ -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
diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim
index 4fcc7cd3e..52b15f698 100755
--- a/lib/system/sysstr.nim
+++ b/lib/system/sysstr.nim
@@ -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.} =