/
smartptrs.nim
179 lines (146 loc) · 5.33 KB
/
smartptrs.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#
#
# Nim's Runtime Library
# (c) Copyright 2020 Nim contributors
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
## C++11 like Smart pointers. It always uses the shared allocator.
# TODO: Add the "isolation" checks that are currently implicitly assumed
# for memory safety.
type
UniquePtr*[T] = object
## Non copyable pointer to object T, exclusive ownership of the object is assumed.
val: ptr T
proc `=destroy`*[T](p: var UniquePtr[T]) =
mixin `=destroy`
if p.val != nil:
`=destroy`(p.val[])
when compileOption("threads"):
deallocShared(p.val)
else:
dealloc(p.val)
proc `=`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.error.}
proc newUniquePtr*[T](val: sink T): UniquePtr[T] {.nodestroy.} =
when compileOption("threads"):
result.val = cast[ptr T](allocShared(sizeof(T)))
else:
result.val = cast[ptr T](alloc(sizeof(T)))
# thanks to '.nodestroy' we don't have to use allocShared0 here.
# This is compiled into a copyMem operation, no need for a sink
# here either.
result.val[] = val
# no destructor call for 'val: sink T' here either.
converter convertUniquePtrToObj*[T](p: UniquePtr[T]): var T {.inline.} =
when compileOption("boundChecks"):
assert(p.val != nil, "deferencing nil unique pointer")
p.val[]
proc isNil*[T](p: UniquePtr[T]): bool {.inline.} =
p.val == nil
proc `[]`*[T](p: UniquePtr[T]): var T {.inline.} =
when compileOption("boundChecks"):
assert(p.val != nil, "deferencing nil unique pointer")
p.val[]
proc `[]=`*[T](p:UniquePtr[T], v:T) {.inline.} = (p[]) = v
proc `$`*[T](p: UniquePtr[T]): string {.inline.} =
if p.val == nil: "UniquePtr[" & $T & "](nil)"
else: "UniquePtr[" & $T & "](" & $p.val[] & ")"
#------------------------------------------------------------------------------
type
SharedPtr*[T] = object
## Shared ownership reference counting pointer
val: ptr tuple[value: T, atomicCounter: int]
proc `=destroy`*[T](p: var SharedPtr[T]) =
mixin `=destroy`
if p.val != nil:
if (when compileOption("threads"):
atomicLoadN(addr p.val[].atomicCounter, ATOMIC_CONSUME) == 0 else:
p.val[].atomicCounter == 0):
`=destroy`(p.val[])
when compileOption("threads"):
deallocShared(p.val)
else:
dealloc(p.val)
else:
when compileOption("threads"):
discard atomicDec(p.val[].atomicCounter)
else:
dec(p.val[].atomicCounter)
proc `=`*[T](dest: var SharedPtr[T], src: SharedPtr[T]) =
if src.val != nil:
when compileOption("threads"):
discard atomicInc(src.val[].atomicCounter)
else:
inc(src.val[].atomicCounter)
if dest.val != nil:
`=destroy`(dest)
dest.val = src.val
proc newSharedPtr*[T](val: sink T): SharedPtr[T] {.nodestroy.} =
when compileOption("threads"):
result.val = cast[typeof(result.val)](allocShared(sizeof(result.val[])))
else:
result.val = cast[typeof(result.val)](alloc(sizeof(result.val[])))
result.val.atomicCounter = 0
result.val.value = val
converter convertSharedPtrToObj*[T](p: SharedPtr[T]): var T {.inline.} =
when compileOption("boundChecks"):
doAssert(p.val != nil, "deferencing nil shared pointer")
p.val.value
proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
p.val == nil
proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} =
when compileOption("boundChecks"):
doAssert(p.val != nil, "deferencing nil shared pointer")
p.val.value
proc `[]=`*[T](p:SharedPtr[T], v:T) {.inline.} = (p[]) = v
proc `$`*[T](p: SharedPtr[T]): string {.inline.} =
if p.val == nil: "SharedPtr[" & $T & "](nil)"
else: "SharedPtr[" & $T & "](" & $p.val.value & ")"
#------------------------------------------------------------------------------
type
ConstPtr*[T] = distinct SharedPtr[T]
## Distinct version of referencing counting smart pointer SharedPtr[T],
## which doesn't allow mutating underlying object.
proc newConstPtr*[T](val: sink T): ConstPtr[T] =
ConstPtr[T](newSharedPtr(val))
converter convertConstPtrToObj*[T](p: ConstPtr[T]): lent T {.inline.} =
SharedPtr[T](p).val.value
proc isNil*[T](p: ConstPtr[T]): bool {.inline.} =
SharedPtr[T](p).val == nil
proc `[]`*[T](p: ConstPtr[T]): lent T {.inline.} =
when compileOption("boundChecks"):
doAssert(SharedPtr[T](p).val != nil, "deferencing nil const pointer")
SharedPtr[T](p).val.value
template `[]=`*[T](p:ConstPtr[T], v:T) = {.error: "'" & p.astToStr & "[]' cannot be assigned to (ConstPtr)".}
proc `$`*[T](p: ConstPtr[T]): string {.inline.} =
if SharedPtr[T](p).val == nil: "ConstPtr[" & $T & "](nil)"
else: "ConstPtr[" & $T & "](" & $SharedPtr[T](p).val.value & ")"
when isMainModule:
import unittest
test "UniquePtr[T] test":
var a1: UniquePtr[float]
let a2 = newUniquePtr(0)
check:
$a1 == "UniquePtr[float](nil)"
a1.isNil == true
$a2 == "UniquePtr[int](0)"
a2.isNil == false
a2[] == 0
test "SharedPtr[T] test":
var a1: SharedPtr[float]
let a2 = newSharedPtr(0)
check:
$a1 == "SharedPtr[float](nil)"
a1.isNil == true
$a2 == "SharedPtr[int](0)"
a2.isNil == false
a2[] == 0
test "ConstPtr[T] test":
var a1: ConstPtr[float]
let a2 = newConstPtr(0)
check:
$a1 == "ConstPtr[float](nil)"
a1.isNil == true
$a2 == "ConstPtr[int](0)"
a2.isNil == false
a2[] == 0