summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2012-01-12 19:44:57 +0100
committerAraq <rumpf_a@web.de>2012-01-12 19:44:57 +0100
commite6b3f50c7f984fb9bd30db42582e04e6c3a2dd97 (patch)
tree254324ddcbc85fab57270d9b4fbf8b6b49a80d41 /lib
parent92e1a21b260996538465a1c32a09943f001adc34 (diff)
downloadNim-e6b3f50c7f984fb9bd30db42582e04e6c3a2dd97.tar.gz
more sysasserts for allocator/gc
Diffstat (limited to 'lib')
-rwxr-xr-xlib/pure/algorithm.nim17
-rwxr-xr-xlib/system.nim2
-rwxr-xr-xlib/system/alloc.nim4
-rwxr-xr-xlib/system/gc.nim9
4 files changed, 22 insertions, 10 deletions
diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim
index fa2482a3d..f283b434c 100755
--- a/lib/pure/algorithm.nim
+++ b/lib/pure/algorithm.nim
@@ -16,7 +16,8 @@ type
 proc `*`*(x: int, order: TSortOrder): int {.inline.} = 
   ## flips `x` if ``order == Descending``;
   ## if ``order == Ascending`` then `x` is returned.
-  ## `x` is supposed to be the result of a comparator.
+  ## `x` is supposed to be the result of a comparator, ie ``< 0`` for 
+  ## *less than*, ``== 0`` for *equal*, ``> 0`` for *greater than*.
   var y = order.ord - 1
   result = (x xor y) - y
 
@@ -80,14 +81,22 @@ proc merge[T](a, b: var openArray[T], lo, m, hi: int,
   else:
     if k < j: copyMem(addr(a[k]), addr(b[i]), sizeof(T)*(j-k))
 
-proc sort*[T](a: var openArray[T], 
-              cmp: proc (x, y: T): int = cmp,
+proc sort*[T](a: var openArray[T],
+              cmp: proc (x, y: T): int,
               order = TSortOrder.Ascending) =
   ## Default Nimrod sort. The sorting is guaranteed to be stable and 
   ## the worst case is guaranteed to be O(n log n).
   ## The current implementation uses an iterative
   ## mergesort to achieve this. It uses a temporary sequence of 
-  ## length ``a.len div 2``.
+  ## length ``a.len div 2``. Currently Nimrod does not support a
+  ## sensible default argument for ``cmp``, so you have to provide one
+  ## of your own. However, the ``system.cmp`` procs can be used:
+  ##
+  ## .. code-block:: nimrod
+  ##
+  ##    sort(myIntArray, system.cmp[int])
+  ##    sort(myStrArray, system.cmp)
+  ##
   var n = a.len
   var b: seq[T]
   newSeq(b, n div 2)
diff --git a/lib/system.nim b/lib/system.nim
index 59e95cf13..c073bff92 100755
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -833,7 +833,7 @@ proc quit*(errorcode: int = QuitSuccess) {.
 
 template sysAssert(cond, msg: expr) =
   # change this to activate system asserts
-  #if not cond: 
+  #if not cond:
   #  echo "[SYSASSERT] ", msg
   #  quit 1
   nil
diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim
index a7c504afc..6fd9efebd 100755
--- a/lib/system/alloc.nim
+++ b/lib/system/alloc.nim
@@ -478,6 +478,7 @@ proc getSmallChunk(a: var TMemRegion): PSmallChunk =
   result = cast[PSmallChunk](res)
 
 # -----------------------------------------------------------------------------
+proc isAllocatedPtr(a: TMemRegion, p: pointer): bool
 
 proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer =
   sysAssert(roundup(65, 8) == 72, "rawAlloc 1")
@@ -537,7 +538,8 @@ proc rawAlloc0(a: var TMemRegion, requestedSize: int): pointer =
   result = rawAlloc(a, requestedSize)
   zeroMem(result, requestedSize)
 
-proc rawDealloc(a: var TMemRegion, p: pointer) = 
+proc rawDealloc(a: var TMemRegion, p: pointer) =
+  sysAssert(isAllocatedPtr(a, p), "rawDealloc: no allocated pointer!")
   var c = pageAddr(p)
   if isSmallChunk(c):
     # `p` is within a small chunk:
diff --git a/lib/system/gc.nim b/lib/system/gc.nim
index 2f6b5d395..2ac61fca0 100755
--- a/lib/system/gc.nim
+++ b/lib/system/gc.nim
@@ -222,11 +222,8 @@ proc rtlAddZCT(c: PCell) {.rtl, inl.} =
     ReleaseSys(HeapLock)
 
 proc decRef(c: PCell) {.inline.} =
-  when stressGC:
-    if c.refcount <% rcIncrement:
-      writeCell("broken cell", c)
+  sysAssert(isAllocatedPtr(gch.region, c), "decRef: interiorPtr")
   sysAssert(c.refcount >=% rcIncrement, "decRef")
-  #if c.refcount <% rcIncrement: quit("leck mich")
   if --c.refcount:
     rtlAddZCT(c)
   elif canBeCycleRoot(c):
@@ -235,6 +232,7 @@ proc decRef(c: PCell) {.inline.} =
     rtlAddCycleRoot(c) 
 
 proc incRef(c: PCell) {.inline.} = 
+  sysAssert(isAllocatedPtr(gch.region, c), "incRef: interiorPtr")
   ++c.refcount
   if canBeCycleRoot(c):
     rtlAddCycleRoot(c)
@@ -500,6 +498,7 @@ proc doOperation(p: pointer, op: TWalkOp) =
   sysAssert(c != nil, "doOperation: 1")
   case op # faster than function pointers because of easy prediction
   of waZctDecRef:
+    sysAssert(isAllocatedPtr(gch.region, c), "decRef: waZctDecRef")
     sysAssert(c.refcount >=% rcIncrement, "doOperation 2")
     c.refcount = c.refcount -% rcIncrement
     when logGC: writeCell("decref (from doOperation)", c)
@@ -727,8 +726,10 @@ proc CollectZCT(gch: var TGcHeap) =
   var L = addr(gch.zct.len)
   while L[] > 0:
     var c = gch.zct.d[0]
+    sysAssert(isAllocatedPtr(gch.region, c), "CollectZCT: isAllocatedPtr")
     # remove from ZCT:
     sysAssert((c.refcount and rcZct) == rcZct, "collectZCT")
+    
     c.refcount = c.refcount and not colorMask
     gch.zct.d[0] = gch.zct.d[L[] - 1]
     dec(L[])