-
Notifications
You must be signed in to change notification settings - Fork 2
/
native.clj
323 lines (159 loc) · 4.85 KB
/
native.clj
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at https://mozilla.org/MPL/2.0/.
(ns helins.binf.native
"Contains [[view]] for creating a view over native memory (as opposed to the JVM heap)
as well as utilities for handling raw pointers.
The main utility is [[view]] which returns a `DirectByteBuffer`. Allocating native memory
can lead to performance increase and simplifies things when using tools such as JNI.
Other utilities are related to raw pointers and are DANGEROUS.
Manipulating raw pointers and managing memory is just as error prone as in native programming.
Those utilities are for users knowing both what they want and what it implies. Otherwise,
segfaults are to be expected.
<!> Pointer utilities are still experimental, might change in the future <!>"
{:author "Adam Helinski"}
(:require [helins.binf.int :as binf.int])
(:import (java.nio ByteBuffer
DirectByteBuffer)
sun.misc.Unsafe))
;;;;;;;;;; Access to the Unsafe API
(def ^:no-doc ^Unsafe -unsafe
(let [field (doto (.getDeclaredField Unsafe
"theUnsafe")
(.setAccessible true))]
(.get field
nil)))
;;;;;;;;;; Creating native views
(defn view
"Allocates `n-byte` bytes in native memory, outside of the JVM heap, and returns a view over it.
Very useful for performance increase in some situations or for interacting with native functions.
The returned view is actually a `DirectByteBuffer` commonly understood by many tools (such as JNI)."
^DirectByteBuffer
[n-byte]
(ByteBuffer/allocateDirect n-byte))
;;;;;;;;;; Handling raw pointers
(defn alloc
"Returns a raw pointer after allocating `n-byte` bytes in native memory."
[n-byte]
(.allocateMemory -unsafe
n-byte))
(defn copy
"Copies `n-byte` bytes from a raw pointer to another raw pointer."
[ptr-dest ptr-src n-byte]
(.copyMemory -unsafe
ptr-src
ptr-dest
n-byte))
(defn free
"De-allocates a raw pointer."
[ptr]
(.freeMemory -unsafe
ptr)
nil)
(defn realloc
"Re-allocates a raw pointer."
[ptr n-byte]
(.reallocateMemory -unsafe
ptr
n-byte))
;;;;;;;;;; Reading and writing values using raw pointers
(defn r-i8
"Dereferences a raw pointer to a signed 8-bit integer."
[ptr]
(.getByte -unsafe
ptr))
(defn r-u8
"Dereferences a raw pointer to an unsigned 8-bit integer."
[ptr]
(binf.int/u8 (r-i8 ptr)))
(defn w-b8
"Writes an 8-bit integer at the given raw pointer."
[ptr b8]
(.putByte -unsafe
ptr
b8))
;;;;;
(defn r-i16
"Dereferences a raw pointer to a signed 16-bit integer."
[ptr]
(.getShort -unsafe
ptr))
(defn r-u16
"Dereferences a raw pointer to an unsigned 16-bit integer."
[ptr]
(binf.int/u16 (r-i16 ptr)))
(defn w-b16
"Writes a 16-bit integer at the given raw pointer."
[ptr b16]
(.putShort -unsafe
ptr
b16))
;;;;;
(defn r-i32
"Dereferences a raw pointer to a signed 32-bit integer."
[ptr]
(.getInt -unsafe
ptr))
(defn r-u32
"Dereferences a raw pointer to an unsigned 32-bit integer."
[ptr]
(binf.int/u32 (r-i32 ptr)))
(defn w-b32
"Writes a 32-bit integer at the given raw pointer."
[ptr b32]
(.putInt -unsafe
ptr
b32))
;;;;;
(defn r-b64
"Dereferences a raw pointer to a 64-bit integer.
Meant to be used with the `helins.binf.int64` namespace."
[ptr]
(.getLong -unsafe
ptr))
(defn w-b64
"Writes a 64-bit integer at the given raw pointer."
[ptr b64]
(.putLong -unsafe
ptr
b64))
;;;;;
(defn r-f32
"Dereferences a raw pointer to a 32-bit float"
[ptr]
(.getFloat -unsafe
ptr))
(defn w-f32
"Writes a 32-bit float at the given raw pointer."
[ptr f32]
(.putFloat -unsafe
ptr
f32))
;;;;;
(defn r-f64
"Dereferences a raw pointer to a 64-bit float"
[ptr]
(.getDouble -unsafe
ptr))
(defn w-f64
"Writes a 64-bit float at the given raw pointer."
[ptr f64]
(.putDouble -unsafe
ptr
f64))
;;;;;;;;;; Reading and writing pointers
(def n-byte-ptr
"Number of bytes in a pointer on this machine.
Should be 4 on a 32-bit machine, 8 on a 64-bit machine."
(.addressSize -unsafe))
(defn r-ptr
"Dereference a raw pointer to a pointer."
[ptr]
(.getAddress -unsafe
ptr))
(defn w-ptr
"Writes a pointer at the given raw pointer."
[ptr ptr-value]
(.putAddress -unsafe
ptr
(unchecked-long ptr-value)))