93 lines
2.9 KiB
Nim
Raw Normal View History

2023-09-12 18:33:38 -07:00
# Nim's Runtime Library
# (c) Copyright 2021 Nim contributors
import std/atomics
import std/isolation
import std/typetraits
import std/strutils
export isolation
proc raiseNilAccess() {.noinline.} =
raise newException(NilAccessDefect, "dereferencing nil smart pointer")
template checkNotNil(p: typed) =
when compileOption("boundChecks"):
{.line.}:
if p.isNil:
raiseNilAccess()
type
SharedPtr*[T] = object
## Shared ownership reference counting pointer.
cnt: ptr int
2023-09-12 18:50:38 -07:00
val*: ptr T
2023-09-12 18:33:38 -07:00
proc incr*[T](a: SharedPtr[T]) =
let res = atomicAddFetch(a.cnt, 1, ATOMIC_RELAXED)
2023-09-12 18:50:38 -07:00
echo "SharedPtr: manual incr: ", res
2023-09-12 18:33:38 -07:00
proc decr*[T](x: SharedPtr[T]) =
2023-09-12 18:50:38 -07:00
if x.val != nil and x.cnt != nil:
2023-09-12 18:33:38 -07:00
let res = atomicSubFetch(x.cnt, 1, ATOMIC_ACQUIRE)
if res == 0:
2023-09-12 18:50:38 -07:00
echo "SharedPtr: FREE: ", repr x.val.pointer, " ", x.cnt[]
deallocShared(x.val)
2023-09-12 18:33:38 -07:00
deallocShared(x.cnt)
else:
2023-09-12 18:50:38 -07:00
echo "SharedPtr: decr: ", repr x.val.pointer, " ", x.cnt[]
2023-09-12 18:33:38 -07:00
proc `=destroy`*[T](x: var SharedPtr[T]) =
2023-09-12 18:50:38 -07:00
echo "SharedPtr: destroy: ", repr x.val.pointer, " ", x.cnt.repr
# echo "SharedPtr: destroy:st: ", ($getStackTrace()).split("\n").join(";")
2023-09-12 18:33:38 -07:00
decr(x)
proc `=dup`*[T](src: SharedPtr[T]): SharedPtr[T] =
if src.val != nil:
2023-09-12 18:50:38 -07:00
discard atomicAddFetch(src.cnt, 1, ATOMIC_RELAXED)
2023-09-12 18:33:38 -07:00
result.val = src.val
proc `=copy`*[T](dest: var SharedPtr[T], src: SharedPtr[T]) =
if src.val != nil:
# echo "SharedPtr: copy: ", src.val.pointer.repr
2023-09-12 18:50:38 -07:00
discard atomicAddFetch(src.cnt, 1, ATOMIC_RELAXED)
2023-09-12 18:33:38 -07:00
`=destroy`(dest)
dest.val = src.val
proc newSharedPtr*[T](val: sink Isolated[T]): SharedPtr[T] {.nodestroy.} =
2023-09-12 18:50:38 -07:00
## Returns a shared pointer which shares,
2023-09-12 18:33:38 -07:00
## ownership of the object by reference counting.
2023-09-12 18:50:38 -07:00
result.cnt = cast[ptr int](allocShared0(sizeof(result.cnt)))
2023-09-12 18:33:38 -07:00
result.val = cast[typeof(result.val)](allocShared(sizeof(result.val[])))
int(result.val.counter) = 1
result.val.value = extract val
echo "SharedPtr: alloc: ", result.val.pointer.repr, " tp: ", $(typeof(T))
template newSharedPtr*[T](val: T): SharedPtr[T] =
newSharedPtr(isolate(val))
proc newSharedPtr*[T](t: typedesc[T]): SharedPtr[T] =
## Returns a shared pointer. It is not initialized,
## so reading from it before writing to it is undefined behaviour!
2023-09-12 18:50:38 -07:00
result.cnt = cast[ptr int](allocShared0(sizeof(result.cnt)))
2023-09-12 18:33:38 -07:00
result.val = cast[typeof(result.val)](allocShared0(sizeof(result.val[])))
2023-09-12 18:50:38 -07:00
result.cnt[] = 1
2023-09-12 18:33:38 -07:00
echo "SharedPtr: allocT: ", result.val.pointer.repr, " tp: ", $(typeof(T))
proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
p.val == nil
proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} =
checkNotNil(p)
2023-09-12 18:50:38 -07:00
p.val[]
2023-09-12 18:33:38 -07:00
proc `[]=`*[T](p: SharedPtr[T], val: sink Isolated[T]) {.inline.} =
checkNotNil(p)
2023-09-12 18:50:38 -07:00
p.val[] = extract val
2023-09-12 18:33:38 -07:00
template `[]=`*[T](p: SharedPtr[T]; val: T) =
`[]=`(p, isolate(val))
proc `$`*[T](p: SharedPtr[T]): string {.inline.} =
if p.val == nil: "nil"
else: "(val: " & $p.val.value & ")"