-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpointers.nim
167 lines (148 loc) · 4.61 KB
/
pointers.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
## :Author: Kaushal Modi
## :License: MIT
##
## Introduction
## ============
## This module implements basic Pointer Arithmetic functions.
##
## Source
## ======
## `Repo link <https://github.com/kaushalmodi/ptr_math>`_
##
## The code in this module is mostly from `this code snippet <https://forum.nim-lang.org/t/1188#7366>`_ on Nim Forum.
template ptrMath*(body: untyped) =
## Enclosure to contain the pointer arithmetic expressions.
runnableExamples:
var
a: array[0 .. 3, int]
p = addr(a[0]) # p is pointing to a[0]
for i, _ in a:
a[i] += i
ptrMath:
p += 1 # p is now pointing to a[1]
p[] = 100 # p[] is accessing the contents of a[1]
doAssert a[1] == 100
p[0] = 200 # .. so does p[0]
doAssert a[1] == 200
p[1] -= 2 # p[1] is accessing the contents of a[2]
doAssert a[2] == 0
p[2] += 50 # p[2] is accessing the contents of a[3]
doAssert a[3] == 53
p += 2 # p is now pointing to a[3]
p[-1] += 77 # p[-1] is accessing the contents of a[2]
doAssert a[2] == 77
doAssert a == [0, 200, 77, 53]
##
template `+`[T](p: ptr T, offset: int): ptr T =
## Increments pointer `p` by `offset` that jumps memory in increments of
## the size of `T`.
## This can be used only within the `ptrMath:` template.
runnableExamples:
type
MyObject = object
i: int
f: float
b: bool
var
a = [MyObject(i: 100, f: 2.3, b: true),
MyObject(i: 300, f: 4.5, b: false),
MyObject(i: 500, f: 6.7, b: true)]
p = addr(a[0])
ptrMath:
var
p2 = p + 2
doAssert p2[0].i == 500
doAssert p2[-1].f == 4.5
##
cast[ptr T](cast[ByteAddress](p) +% (offset * sizeof(T)))
# `+%` treats x and y inputs as unsigned
# and adds them: https://nim-lang.github.io/Nim/system.html#%2B%25%2Cint%2Cint
template `-`[T](p: ptr T, offset: int): ptr T =
## Decrements pointer `p` by `offset` that jumps memory in increments of
## the size of `T`.
## This can be used only within the `ptrMath:` template.
runnableExamples:
type
MyObject = object
i: int
f: float
b: bool
var
a = [MyObject(i: 100, f: 2.3, b: true),
MyObject(i: 300, f: 4.5, b: false),
MyObject(i: 500, f: 6.7, b: true)]
p = addr(a[2])
ptrMath:
var
p1 = p - 1
doAssert p1[0].i == 300
doAssert p1[-1].b == true
doAssert p1[1].f == 6.7
##
cast[ptr T](cast[ByteAddress](p) -% (offset * sizeof(T)))
template `[]`[T](p: ptr T, offset: int): T =
## Retrieves the value from `p[offset]`.
## This can be used only within the `ptrMath:` template.
runnableExamples:
var
a = [1, 3, 5, 7]
p = addr(a[0])
ptrMath:
doAssert p[] == a[0]
doAssert p[0] == a[0]
doAssert p[2] == a[2]
##
(p + offset)[]
template `[]=`[T](p: ptr T, offset: int, val: T) =
## Assigns the value at memory location pointed by `p[offset]`.
## This can be used only within the `ptrMath:` template.
runnableExamples:
var
a = [1.3, -9.5, 100.0]
p = addr(a[1])
ptrMath:
p[0] = 123.456
doAssert a[1] == 123.456
##
(p + offset)[] = val
template `+=`[T](p: ptr T, offset: int) =
## Increments pointer `p` *in place* by `offset` that jumps memory
## in increments of the size of `T`.
## This can be used only within the `ptrMath:` template.
runnableExamples:
type
MyObject = object
i: int
f: float
b: bool
var
a = [MyObject(i: 100, f: 2.3, b: true),
MyObject(i: 300, f: 4.5, b: false),
MyObject(i: 500, f: 6.7, b: true)]
p = addr(a[0])
ptrMath:
p += 1
doAssert p[].i == 300
##
p = p + offset
template `-=`[T](p: ptr T, offset: int) =
## Decrements pointer `p` *in place* by `offset` that jumps memory
## in increments of the size of `T`.
## This can be used only within the `ptrMath:` template.
runnableExamples:
type
MyObject = object
i: int
f: float
b: bool
var
a = [MyObject(i: 100, f: 2.3, b: true),
MyObject(i: 300, f: 4.5, b: false),
MyObject(i: 500, f: 6.7, b: true)]
p = addr(a[2])
ptrMath:
p -= 2
doAssert p[].f == 2.3
##
p = p - offset
body