fixing up tests - print pretty

This commit is contained in:
Jaremy Creechley 2023-09-13 13:55:11 -07:00
parent 5810901bc0
commit f5b0d24f82
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
2 changed files with 21 additions and 10 deletions

View File

@ -27,7 +27,7 @@ proc `=destroy`*(x: var DataBufferHolder) =
## copy pointer implementation
if x.buf != nil:
# when isMainModule or true:
echo "databuffer: FREE: ", repr x.buf.pointer
echoed "databuffer: FREE: ", repr x.buf.pointer
deallocShared(x.buf)
proc len*(a: DataBuffer): int = a[].size
@ -48,8 +48,8 @@ proc new*(tp: typedesc[DataBuffer], size: int = 0): DataBuffer =
buf: cast[typeof(result[].buf)](allocShared0(size)),
size: size,
))
echo "DataBuffer:new: ", result.unsafeRawPtr().repr,
" @ ", result[].buf.pointer.repr & " thr: " & $getThreadId(),
echoed "DataBuffer:new: ", result.unsafeRawPtr().repr,
" @ ", result[].buf.pointer.repr,
" -> ", result.toString().repr
proc new*[T: byte | char](tp: typedesc[DataBuffer], data: openArray[T]): DataBuffer =

View File

@ -18,14 +18,25 @@ template checkNotNil(p: typed) =
raiseNilAccess()
import std/terminal
import std/locks
var elock: Lock
elock.initLock()
proc echoed*(vals: varargs[string, `$`]) =
proc printThread(): ForegroundColor =
let tclr = [fgRed, fgGreen, fgBlue, fgMagenta, fgCyan]
let tid = getThreadId()
let color = tclr[(tid mod (tclr.len() - 1))]
stdout.styledWrite(styleBright, fgWhite, "(thr: ", color, $tid, fgWhite, ") ")
color
try:
let color = printThread()
var i = 0
if vals.len() mod 2 != 0:
stdout.styledWrite(fgBlue, vals[i])
i.inc()
while i + 1 < vals.len():
stdout.styledWrite(fgBlue, vals[i], fgYellow, vals[i+1])
stdout.styledWrite(color, vals[i], fgDefault, vals[i+1])
i.inc(2)
stdout.styledWrite("\n")
except:
@ -41,20 +52,20 @@ type
proc incr*[T](a: var SharedPtr[T]) =
if a.container != nil and a.cnt != nil:
let res = atomicAddFetch(a.cnt, 1, ATOMIC_RELAXED)
echoed "SharedPtr: manual incr: ", res, " thr: ", $getThreadId()
echoed "SharedPtr: manual incr: ", res
proc decr*[T](x: var SharedPtr[T]) =
if x.container != nil:
let res = atomicSubFetch(addr x.container.cnt, 1, ATOMIC_ACQUIRE)
if res == 0:
echoed "SharedPtr: FREE: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T)), " thr: ", $getThreadId()
echoed "SharedPtr: FREE: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T))
when compiles(`=destroy`(x[])):
echoed "SharedPtr:call:child:destructor: ", $(typeof(x[]))
`=destroy`(x[])
deallocShared(x.container)
x.container = nil
else:
echoed "SharedPtr: decr: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T)), " thr: ", $getThreadId()
echoed "SharedPtr: decr: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T))
proc release*[T](x: var SharedPtr[T]) =
echoed "SharedPtr: release: ", $(typeof(T))
@ -63,7 +74,7 @@ proc release*[T](x: var SharedPtr[T]) =
proc `=destroy`*[T](x: var SharedPtr[T]) =
if x.container != nil:
echoed "SharedPtr: destroy: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T)), " thr: ", $getThreadId()
echoed "SharedPtr: destroy: ", x.container.pointer.repr, " cnt: ", x.container.cnt, " tp: ", $(typeof(T))
decr(x)
proc `=dup`*[T](src: SharedPtr[T]): SharedPtr[T] =
@ -85,7 +96,7 @@ proc newSharedPtr*[T](val: sink Isolated[T]): SharedPtr[T] {.nodestroy.} =
result.container = cast[typeof(result.container)](allocShared(sizeof(result.container[])))
result.container.cnt = 1
result.container.value = extract val
echoed "SharedPtr: alloc: ", result.container.pointer.repr, " cnt: ", result.container.cnt, " tp: ", $(typeof(T)), " thr: ", $getThreadId()
echoed "SharedPtr: alloc: ", result.container.pointer.repr, " cnt: ", result.container.cnt, " tp: ", $(typeof(T))
template newSharedPtr*[T](val: T): SharedPtr[T] =
newSharedPtr(isolate(val))
@ -94,7 +105,7 @@ proc newSharedPtr*[T](t: typedesc[T]): SharedPtr[T] =
## Returns a shared pointer. It is not initialized,
result.container = cast[typeof(result.container)](allocShared0(sizeof(result.container[])))
result.container.cnt = 1
echoed "SharedPtr: alloc: ", result.container.pointer.repr, " cnt: ", result.container.cnt, " tp: ", $(typeof(T)), " thr: ", $getThreadId()
echoed "SharedPtr: alloc: ", result.container.pointer.repr, " cnt: ", result.container.cnt, " tp: ", $(typeof(T))
proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
p.container == nil