forked from uncomplicate/clojure-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclojure_cpp.clj
More file actions
2215 lines (1957 loc) · 82.9 KB
/
Copy pathclojure_cpp.clj
File metadata and controls
2215 lines (1957 loc) · 82.9 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Copyright (c) Dragan Djuric. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) or later
;; which can be found in the file LICENSE at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns uncomplicate.clojure-cpp
"ClojureCPP is a Clojure library for integrating C/C++ based libraries available through JavaCPP.
It is much more than a wrapper around JavaCPP that hides Java interop. It enables you to use JavaCPP's
Pointer-based infrastucture the Clojure way; exposing what is absolutely necessary, automating whatever
boilerplate that can be automated under the hood, and protecting yourself from shooting yourself
in the foot with memory leaks and segmentation faults as much as possible (95%? 89%? who knows, but not 100%).
You still have to be careful, because you're stepping outside the JVM, but you'll write a lot less code,
your code will fit nicely with the rest of Clojure code in your program, and you will do the wrong
thing less often.
The center piece of this library is JavaCPP's `Pointer` class. Almost all JavaCPP interop methods
accept subclasses of `Pointer` as arguments (along with good old Java primitives such as `int` or `double`).
Very often, these pointers are pointers to off-heap primitive memory blocks that are not managed by
your JVM, which are managed by classes such as `DoublePointer` or `FloatPointer`. That is because
many C libraries use the respective arrays. Some libraries define their own structs; these are
typically represented as specialized `Pointer` sublasses by JavaCPP. Typical examples would be
CUDA or MKL wrappers. You can explore ClojureCUDA and Neanderthal code for real-world examples
of how to deal with these if you need to create a new wrapper for a new library. Fortunately,
if someone else has already done the integration, you might even write code without thinking
much outside of Clojure. Depending of your grasp of the basics of C and/or C++, it still might be
a good idea to read some [introduction to JavaCPP](https://github.com/bytedeco/javacpp).
Note that ClojureCPP is well integrated into Clojure and Uncomplicate family of libraries,
which means that lots of functionality is integrated into general functions such as
`release`, `size`, `bytesize`, `sizeof` from `uncomplicate.commons`, or `fmap`, `fmap!`,
`fold`, `op` from `uncomplicate.fluokitten`, or similar polymorphic functions. Ideally,
you'll prefer these over fiddling with lower-level ClojureCPP-specific code (when possible).
Here's a loose grouping of ClojureCPP functions:
- System functions give you some insight int o the overall system, memory, and allocations:
[[physical-bytes]], [[available-physical-bytes]], [[max-physical-bytes]], [[total-physical-bytes]],
[[tracked-bytes]], [[max-tracked-bytes]] and [[pointers-count]].
- Constructors. The pointers that you'll use throughout your code are explicitly created by these
constructors. Alternatively, you'll work with pointers returned by custom functions from many of
C libraries available through JavaCPP (such as CUDA etc.). You'll use these functions a lot.
[[pointer]], [[pointer-pointer]], [[byte-pointer]], [[keyword-pointer]], [[string-pointer]],
[[bool-pointer]], [[bool-pointer]], [[clong-pointer]], [[size-t-pointer]], [[char-pointer]],
[[short-pointer]], [[int-pointer]], [[long-pointer]], [[float-pointer]], [[double-pointer]],
and [[function-pointer]].
- Memory allocation functions with standard C counterparts (you typically don't have to use these
unless you're writing low-level stuff):
[[malloc!]], [[calloc!]], [[realloc!]], [[free!]], [[memcmp]], [[memcpy!]], [[memmove!]],
[[memset!]], and [[zero!]].
- General pointer features:
[[pointer-type]], [[pointer-class]], and [[type-pointer]] are convenience mappings between
keywords representing primitive types, built-in pointer classes, and pointer constructor functions,
so you can, for example, use `:float` or `Float` instead if importing `FloatPointer`. That also helps in
integration with other systems without coupling with JavaCPP types.
The following functions give you an insight into the properties of the pointer at hand. Most of
the time, these properties are set by other functions to their right values, but sometimes you
want to see details or even set something yourself (but be careful of papercuts!).
[[address]], [[null?]], [[capacity]], [[capacity!]], [[limit]], [[limit!]], [[position]], [[position!]],
- This group of functions do some pointer arithmetic, type casts, or type conversions:
[[get-pointer]], [[safe]], [[safe2]], [[ptr*]], [[ptr]], [[ptr2]],
[[float-ptr*]], [[float-ptr]], [[float-ptr2]], [[double-ptr*]], [[double-ptr]], [[double-ptr2]],
[[long-ptr*]], [[long-ptr]], [[long-ptr2]], [[int-ptr*]], [[int-ptr]], [[int-ptr2]],
[[short-ptr*]], [[short-ptr]], [[short-ptr2]], [[short-ptr*]], [[short-ptr]], [[short-ptr2]],
[[byte-ptr*]], [[byte-ptr]], [[byte-ptr2]], [[size-t-ptr*]], [[size-t-ptr]], [[size-t-ptr2]],
[[clong-ptr*]], [[clong-ptr]], [[clong-ptr2]], [[bool-ptr*]], [[bool-ptr]], [[bool-ptr2]],
[[char-ptr*]], [[char-ptr]], and [[char-ptr2]].
- Java and Clojure conversions to Java buffers or Clojure vectors and sequences:
[[as-byte-buffer]], [[as-buffer]], [[pointer-vec]], [[pointer-seq]],
- Polymorphic access to data in memory blocks managed by a pointer:
[[get!]], [[put!]], [[get-entry!]], [[put-entry!]], and [[fill!]]
- Raw access to bytes from a `BytePointer`:
[[get-keyword]], [[put-keyword!]], [[get-string]], [[put-string!]],
[[get-pointer-value]], [[put-pointer-value]], [[get-unsigned]], [[put-unsigned!]], [[get-bool]],
[[put-bool!]], [[get-char]], [[put-char!]], [[get-int]], [[put-int!]], [[get-short]], [[put-short!]],
[[get-long]], [[put-long!]], [[get-byte]], [[put-byte!]], [[get-short]], [[put-short!]],
[[get-double]], [[put-double!]], [[get-float]], [[put-float!]], and [[get-string-bytes]].
Please read [JavaCPP javadoc](http://bytedeco.org/javacpp/apidocs/overview-summary.html) for more internal details when necessary.
Also, getting familiar with common C library functions can be very helpful.
Please check out `uncomplicate.clojure-cpp-test` for examples of how to use these functions!"
(:require [clojure.string :refer [split]]
[uncomplicate.commons
[core :refer [Releaseable let-release Info info Bytes Entries bytesize size]]
[utils :refer [dragan-says-ex]]]
[uncomplicate.fluokitten
[core :as fluokitten]
[protocols :refer [Functor PseudoFunctor Magma Foldable foldmap fold Comonad]]])
(:import [java.nio Buffer ByteBuffer CharBuffer ShortBuffer IntBuffer LongBuffer FloatBuffer
DoubleBuffer]
java.nio.charset.Charset
[clojure.lang Seqable Keyword IFn$LLL IFn$LDD IFn$LOO]
[org.bytedeco.javacpp Pointer BytePointer CharPointer BoolPointer ShortPointer
IntPointer LongPointer FloatPointer DoublePointer CLongPointer FunctionPointer
PointerPointer SizeTPointer]
[uncomplicate.clojure_cpp StringPointer KeywordPointer]))
;; ================= System =================================
(defn physical-bytes
"Amount of non-shared physical memory currently used by the process.
If provided with `max-size`, may return an approximate value, between real physical bytes and `max-size`,
saving some processing time. Returns `0` if this amount can't be determined."
(^long []
(Pointer/physicalBytes))
(^long [^long max-size]
(Pointer/physicalBytesInaccurate max-size)))
(defn available-physical-bytes
"Amount of physical memory that is available (free) from the operating system.
Returns `0` if this amount can't be determined."
^long []
(Pointer/availablePhysicalBytes))
(defn max-physical-bytes
"The maximum amount of physical memory that should (could?) be used."
^long []
(Pointer/maxPhysicalBytes))
(defn total-physical-bytes
"The total amount of memory physically installed in the machine.
The amount of `physical-bytes` can't be larger than this value.
Returns `0` if the amount can't be determined."
^long []
(Pointer/totalPhysicalBytes))
(defn tracked-bytes
"The amount of memory currently tracked by JavaCPP deallocators."
^long []
(Pointer/totalBytes))
(defn max-tracked-bytes
"The maximum amount of memory allowed to be tracked by JavaCPP deallocators."
^long []
(Pointer/maxBytes))
(defn pointers-count
"Number of pointers currently tracked by JavaCPP deallocators."
^long []
(Pointer/totalCount))
(defn safe
"If pointer `p` is neither `nil` nor `NULL`, returns `p`. Otherwise, throws an `IllegalArgumentexception`."
^Pointer [^Pointer p]
(if-not (Pointer/isNull p)
p
(throw (IllegalArgumentException. "Neither nil nor NULL pointer is allowed in this part of code. Please do not use non-initialized pointers here."))))
(defn safe2
"If pointer `p` is not `NULL`, returns `p`. Otherwise, throws an `IllegalArgumentexception`."
^Pointer [^Pointer p]
(if (or (nil? p) (< 0 (.address p)))
p
(throw (IllegalArgumentException. "NULL pointer is not allowed in this part of code. Please do not use non-initialized pointers here."))))
;; ================= A set of standard C functions =================================
(defn malloc!
"Allocates the `byte-size` bytes of memory and returns a `Pointer` that manages it.
The memory block allocated by `malloc!` has to be explicitly freed by `free!`.
Calling `release` has no effect because no deallocator has been attached.
It is very important to keep in mind that [[malloc!]] does NOT initialize the memory block.
The danger is that often values could be `0`, and this may trick you into believing that
they typically will be initialized! In general, the memory block contains garbage, and must be
explicitly initialized if your program relies on the values being `0` after allocation.
If called with a negative number, returns `nil`.
Prefer creating a pointer with (int-pointer 8) instead of with (int-pointer (malloc! 8)),
unless you have a specific reason to do otherwise.
This returns a fully configured, but still uninitialized, pointer that has a deallocator.
"
[^long byte-size]
(when-let [p (Pointer/malloc byte-size)]
(.capacity p byte-size)))
(defn calloc!
"Allocated a memory block of `n` elements each taking `element-size` bytes, and initializes
it to `0`. An alternative to [[malloc!]].
(int-pointer (calloc! 2 8))
=> {:address \"0x7fd5b87596a0\", :type :int, :position 0, :limit 2, :capacity 2, :entries (0 0)}
"
[^long n ^long element-size]
(when-let [p (Pointer/calloc n element-size)]
(.capacity p (* n element-size))))
(defn realloc!
"Attempts to resize a memory block previously created by `malloc!` or `calloc!`.
"
[^Pointer p ^long byte-size]
(when-let [p (Pointer/realloc p byte-size)]
(.capacity p byte-size)))
(defn free!
"Deallocates the memory block that was allocated by [[calloc!]], [[malloc!]], or [[realloc!]].
Although typically attempting to free a wrong block may hard crash your program,
this function has protections against most of common errors, such as trying to free
an already de-allocated block, or calling free after a careless pointer arithmetic.
But there's always a way to shoot oneself in the foot, so please be careful with this.
Returns a NULL pointer (not `nil`!).
"
[^Pointer p]
(when-not (Pointer/isNull p)
(Pointer/free (.position p 0))
(.deallocate p)
(.setNull p))
p)
(defn memcmp
"Compares the first `byte-size` bytes of `p1` and `p2`. Returns a `long` integer, not a boolean!
The result is as follows:
zero: `byte-size` bytes are equal
negative: `p1` is less than `p2`
positive: `p2` is less than `p1`
If `byte-size` is not within bounds of `p1` and `p2`, throws `IndexOutOfBoundsException`.
(memcmp (byte-pointer [1 2 3]) (byte-pointer [1 1 4]) 3) => 1
"
(^long [^Pointer p1 ^Pointer p2 ^long byte-size]
(if (and (not (Pointer/isNull p1) (not (Pointer/isNull p2)))
(<= 0 byte-size (min (bytesize p1) (bytesize p2))))
(Pointer/memcmp p1 p2 byte-size)
(throw (IndexOutOfBoundsException.
(format "You're trying to compare data outside the bounds of %s memory block."
(if (< (bytesize p1) (bytesize p2)) "p1" "p2"))))))
(^long [^Pointer p1 ^Pointer p2]
(Pointer/memcmp p1 p2 (min (bytesize p1) (bytesize p2)))))
(defn memcpy!
"Copies `byte-size` bytes from `src` to `dst`, nad returns `dst`. If `byte-size` is not within
bounds of `src` and `dst`, throws `IndexOutOfBoundsException`.
"
([^Pointer src ^Pointer dst ^long byte-size]
(if (and (not (Pointer/isNull src)) (not (Pointer/isNull dst))
(<= 0 byte-size (min (bytesize src) (bytesize dst))))
(Pointer/memcpy dst src byte-size)
(throw (IndexOutOfBoundsException.
(format "You're trying to copy outside the bounds of %s memory block."
(if (< (bytesize src) (bytesize dst)) "source" "destination"))))))
([^Pointer src ^Pointer dst]
(Pointer/memcpy dst src (min (bytesize src) (bytesize dst)))))
(defn memmove!
"Copies `byte-size` bytes from `src` to `dst`, and returns `dst`. A safer alternative
to [[memcpy!]] in cases when `src` and `dst` contain overlapping memory blocks.
If `byte-size` is not within bounds of `src` and `dst`, throws `IndexOutOfBoundsException`.
"
([^Pointer src ^Pointer dst ^long byte-size]
(if (and (not (Pointer/isNull src)) (not (Pointer/isNull dst))
(<= 0 byte-size (min (bytesize src) (bytesize dst))))
(Pointer/memmove dst src byte-size)
(throw (IndexOutOfBoundsException.
(format "You're trying to move data outside the bounds of %s memory block."
(if (< (bytesize src) (bytesize dst)) "source" "destination"))))))
([^Pointer src ^Pointer dst]
(Pointer/memmove dst src (min (bytesize src) (bytesize dst)))))
(defn memset!
"Sets `byte-size` bytes of `dst` to `value`, and returns `dst`.
If `byte-size` is not within bounds of `src` and `dst`, throws `IndexOutOfBoundsException`.
"
[^Pointer dst ^long value ^long byte-size]
(if (and (not (Pointer/isNull dst)) (<= 0 byte-size (bytesize dst)))
(Pointer/memset dst value byte-size)
(throw (IndexOutOfBoundsException. "You're trying to set data outside the bounds of the destination memory block."))))
(defn zero!
"Initializes all elements in the memory block managed by `p` to zero."
[^Pointer p]
(if-not (Pointer/isNull p)
(.zero p)
p))
;; ================= Pointer =================================
(def ^:const pointer-type
"A mapping of JavaCPP pointer types to appropriate keywords.
(pointer-type FloatPointer) => :float
"
{DoublePointer :double
FloatPointer :float
LongPointer :long
IntPointer :int
ShortPointer :short
BytePointer :byte
CharPointer :char
CLongPointer :clong
SizeTPointer :size-t
BoolPointer :bool
FunctionPointer :function
PointerPointer :pointer
Pointer :default})
(def ^:const pointer-class
"A mapping of Java number types and related keywords to appropriate JavaCPP pointer types.
(pointer-class :float) => FloatPointer
"
{:double DoublePointer
:float FloatPointer
:long LongPointer
:int IntPointer
:short ShortPointer
:byte BytePointer
:char CharPointer
:clong CLongPointer
:size-t SizeTPointer
:bool BoolPointer
:function FunctionPointer
:fn FunctionPointer
:pointer PointerPointer
:default Pointer
Double/TYPE DoublePointer
Float/TYPE FloatPointer
Long/TYPE LongPointer
Integer/TYPE IntPointer
Short/TYPE ShortPointer
Byte/TYPE BytePointer
Character/TYPE CharPointer
Boolean/TYPE BoolPointer})
(defn address
"Returns the address of pointer `p`"
^long [^Pointer p]
(.address p))
(defn null?
"Checks whether `p` points is `nil` or a NULL pointer. Typically, the NULL pointer
is a pointer that has not yet been allocated (by [[malloc!]], [[calloc!]], or pointer constructors)
or has been deallocated or freed.
"
[^Pointer p]
(Pointer/isNull p))
(defn capacity
"Returns the capacity of `p`, as number of bytes if `p` is just a `Pointer`, or in number
of elements, if `p` is one of typed pointers such as `DoublePointer` or `IntPointer`.
"
^long [^Pointer p]
(.capacity p))
(defn capacity! ;;TODO think about removing this if Neanderthal and DD can use limit!
"Sets the capacity of `p`, as number of bytes if `p` is just a `Pointer`, or in number
of elements, if `p` is one of typed pointers such as `DoublePointer` or `IntPointer`.
If `n` is negative, sets the capacity to `0`. Be warned that setting capacity to
an arbitrarily large number can crash your program, or, worse, Heisenbugs.
"
[^Pointer p ^long n]
(.capacity p (max 0 n)))
(defn limit
"Returns the limit of `p`, as number of bytes if `p` is just a `Pointer`, or in number
of elements, if `p` is one of typed pointers such as `DoublePointer` or `IntPointer`.
"
^long [^Pointer p]
(.limit p))
(defn limit!
"Sets the limit of `p`, as number of bytes if `p` is just a `Pointer`, or in number
of elements, if `p` is one of typed pointers such as `DoublePointer` or `IntPointer`.
If `n` is negative, or larger than the available capacity, throws `IllegalArgumentexception`.
"
[^Pointer p ^long n]
(if (<= 0 n (.capacity p))
(.limit p n)
(throw (IndexOutOfBoundsException. (format "The requested position %d is larger than capacity %d."
n (.capacity p))))))
(defn position
"Returns the position where `p` begins, as number of bytes if `p` is just a `Pointer`,
or in number of elements, if `p` is one of typed pointers such as `DoublePointer` or `IntPointer`.
"
^long [^Pointer p]
(.position p))
(defn position!
"Sets the position where `p` begins, as number of bytes if `p` is just a `Pointer`,
or in number of elements, if `p` is one of typed pointers such as `DoublePointer` or `IntPointer`.
If `n` is negative, or larger than the available capacity, throws `IllegalArgumentexception`.
"
[^Pointer p ^long n]
(if (<= 0 n (.capacity p))
(.position p n)
(throw (IndexOutOfBoundsException. (format "The requested position %d is larger than capacity %d."
n (.capacity p))))))
(defn get-pointer
"Returns a new pointer that manages the memory block managed by `p`, starting from element `i`,
bounded by capacity `(capacity p)`. If provided with pointer's type, coerces the pointer to it
(please see [[pointer-class]] for available types).
This is useful when you need to change some of pointer's properties in parts of code, but leave
the original pointer unchanged.
Please be careful: although negative values of `i` can be used normally as you go back and forth
through a memory block, it is your responsibility to make sure that you do not overstep into the
territory outside the originally allocated array. ClojureCPP doesn't have a way to do this for you.
"
(^Pointer [^Pointer p]
(.getPointer p))
(^Pointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p))))
(^Pointer [^Pointer p type ^long i]
(.getPointer p (get pointer-class type type) i)))
;; ================= Buffer =================================
(defn as-byte-buffer
"Returns a `ByteBuffer` representation of a pointer."
^ByteBuffer [^Pointer p]
(.asByteBuffer p))
(defn as-buffer ^Buffer [^Pointer p]
(.asBuffer p))
(defprotocol ^:no-doc PointerCreator
(pointer* [this] [this i] "Coerces a type into the appropriate `Pointer`."))
(defprotocol ^:no-doc RawPointerCreator
(raw* [this] "Creates a new instance of this pointer's type with the same size."))
(defprotocol TypedPointerCreator
(^PointerPointer pointer-pointer [this] [this charset] "Converts an object into `PointerPointer`.")
(^BytePointer byte-pointer [this] [this charset] "Converts an object into `BytePointer`.")
(^KeywordPointer keyword-pointer [this] [this charset] "Converts an object into `KeywordPointer`.")
(^StringPointer string-pointer [this] [this charset] "Converts an object into `StringPointer`.")
(^BoolPointer bool-pointer [this] "Converts an object into `BoolPointer`.")
(^CLongPointer clong-pointer [this] "Converts an object into `CLongPointer`.")
(^SizeTPointer size-t-pointer [this] "Converts an object into `SizeTPointer`.")
(^CharPointer char-pointer [this] "Converts an object into `CharPointer`.")
(^ShortPointer short-pointer [this] "Converts an object into `ShortPointer`.")
(^IntPointer int-pointer [this] "Converts an object into `IntPointer`.")
(^LongPointer long-pointer [this] "Converts an object into `LongPointer`.")
(^FloatPointer float-pointer [this] "Converts an object into `FloatPointer`.")
(^DoublePointer double-pointer [this] "Converts an object into `DoublePointer`.")
(^FunctionPointer function-pointer [this] "Converts an object into `FunctionPointer`."))
(defprotocol PointerVec
(pointer-vec [this]
"Returns a vector representation of elements in pointer's memory block,
taking into account pointer's type"))
(defn pointer
"Coerces `x` to appropriate `Pointer` subclass, with position indicated by index `i`.
The exact behavior is polymorphic per pointer type. If the argument is already a pointer,
it just gets returned without change.
Most common Clojure and Java collections are supported out of the box, with following
characteristics regarding the new pointer's memory block:
- The contents of a Java array is copied into new pointer's memory block,
with no further connection between the two.
- The contents of a `java.nio.Buffer` is copied into new pointer's memory block,
with no further connection between the two.
- The contents of a direct `java.nio.Buffer` is referenced by the pointer's memory,
and each change is reflected in both.
- The contents of a clojure sequence is copied into new pointer's memory block,
with no further connection between the two.
Custom classes are free to define they own way of converting to pointers by
implementing the [[pointer*]] methods of the [[PointerCreator]] protocol.
"
(^Pointer [x]
(pointer* x))
(^Pointer [x i]
(pointer* x i)))
(defn ptr*
"Casts pointer `p` into `Pointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. If provided with index `i`,
behaves like [[get-pointer]].
"
(^Pointer [^Pointer p]
p)
(^Pointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `Pointer`.
Useful when metadata for avoiding reflection is not easily added in code,
such as in macros. If provided with index `i`, behaves like [[get-pointer]].
Please be careful: although negative values of `i` can be used normally as you go back and forth
through a memory block, it is your responsibility to make sure that you do not overstep into the
territory outside the originally allocated array. ClojureCPP doesn't have a way to do this for you.
"
(^Pointer [^Pointer p]
(safe p))
(^Pointer [^Pointer p ^long i]
(.getPointer (safe p) (min i (.capacity p)))))
(defn ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `Pointer`.
Useful when metadata for avoiding reflection is not easily added in code,
such as in macros. If provided with index `i`, behaves like [[get-pointer]].
Please be careful: although negative values of `i` can be used normally as you go back and forth
through a memory block, it is your responsibility to make sure that you do not overstep into the
territory outside the originally allocated array. ClojureCPP doesn't have a way to do this for you.
"
(^Pointer [^Pointer p]
(safe2 p))
(^Pointer [^Pointer p ^long i]
(.getPointer (safe2 p) (min i (.capacity p)))))
(defn float-ptr*
"Casts pointer `p` into `FloatPointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `FloatPointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[float-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^FloatPointer [p]
p)
(^FloatPointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn float-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `FloatPointer`.
Does not actually convert `p` into `FloatPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually require `p`
to be a `FloatPointer`!.
Prefer this method to [[float-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[float-ptr2]] in places when `nil` is not allowed.
"
(^FloatPointer [^FloatPointer p]
(cast FloatPointer (safe p)))
(^FloatPointer [^FloatPointer p ^long i]
(.getPointer ^FloatPointer (safe p) (min i (.capacity p)))))
(defn float-ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `FloatPointer`.
Does not actually convert `p` into `FloatPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually require `p`
to be a `FloatPointer`!.
Prefer this method to [[float-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[float-ptr]] in places when `nil` is acceptable.
"
(^FloatPointer [^FloatPointer p]
(cast FloatPointer (safe2 p)))
(^FloatPointer [^FloatPointer p ^long i]
(.getPointer ^FloatPointer (safe2 p) (min i (.capacity p)))))
(defn double-ptr*
"Casts pointer `p` into `DoublePointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `DoublePointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[double-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^DoublePointer [^DoublePointer p]
p)
(^DoublePointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn double-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `DoublePointer`.
Does not actually convert `p` into `DoublePointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually require `p`
to be a `DoublePointer`!.
Prefer this method to [[double-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[double-ptr2]] in places when `nil` is not allowed.
"
(^DoublePointer [^DoublePointer p]
(cast DoublePointer (safe p)))
(^DoublePointer [^DoublePointer p ^long i]
(.getPointer ^DoublePointer (safe p) (min i (.capacity p)))))
(defn double-ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `DoublePointer`.
Does not actually convert `p` into `DoublePointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually require `p`
to be a `DoublePointer`!.
Prefer this method to [[double-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[double-ptr]] in places when `nil` is acceptable.
"
(^DoublePointer [^DoublePointer p]
(cast DoublePointer (safe2 p)))
(^DoublePointer [^DoublePointer p ^long i]
(.getPointer ^DoublePointer (safe2 p) (min i (.capacity p)))))
(defn long-ptr*
"Casts pointer `p` into `LongPointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `LongPointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[long-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^LongPointer [^LongPointer p]
p)
(^LongPointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn long-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `LongPointer`.
Does not actually convert `p` into `LongPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually require `p`
to be a `LongPointer`!.
Prefer this method to [[long-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[long-ptr2]] in places when `nil` is not allowed.
"
(^LongPointer [^LongPointer p]
(cast LongPointer (safe p)))
(^LongPointer [^LongPointer p ^long i]
(.getPointer ^LongPointer (safe p) (min i (.capacity p)))))
(defn long-ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `LongPointer`.
Does not actually convert `p` into `LongPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually require `p`
to be a `LongPointer`!.
Prefer this method to [[long-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[long-ptr]] in places when `nil` is acceptable.
"
(^LongPointer [^LongPointer p]
(cast LongPointer (safe2 p)))
(^LongPointer [^LongPointer p ^long i]
(.getPointer ^LongPointer (safe2 p) (min i (.capacity p)))))
(defn int-ptr*
"Casts pointer `p` into `IntPointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `IntPointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[int-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^IntPointer [^IntPointer p]
p)
(^IntPointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn int-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `IntPointer`.
Does not actually convert `p` into `IntPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually convert `p`
into `IntPointer`!.
Prefer this method to [[int-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[int-ptr2]] in places when `nil` is not allowed.
"
(^IntPointer [^IntPointer p]
(cast IntPointer (safe p)))
(^IntPointer [^IntPointer p ^long i]
(.getPointer ^IntPointer (safe p) (min i (.capacity p)))))
(defn int-ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `IntPointer`.
Does not actually convert `p` into `IntPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually convert `p`
into `IntPointer`!.
Prefer this method to [[int-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[int-ptr]] in places when `nil` is acceptable.
"
(^IntPointer [^IntPointer p]
(cast IntPointer (safe2 p)))
(^IntPointer [^IntPointer p ^long i]
(.getPointer ^IntPointer (safe2 p) (min i (.capacity p)))))
(defn short-ptr*
"Casts pointer `p` into `ShortPointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `ShortPointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[short-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^ShortPointer [^ShortPointer p]
p)
(^ShortPointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn short-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `ShortPointer`.
Does not actually convert `p` into `ShortPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually convert `p`
into `ShortPointer`!.
Prefer this method to [[short-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[short-ptr2]] in places when `nil` is not allowed.
"
(^ShortPointer [^ShortPointer p]
(cast ShortPointer (safe p)))
(^ShortPointer [^ShortPointer p ^long i]
(.getPointer ^ShortPointer (safe p) (min i (.capacity p)))))
(defn short-ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `ShortPointer`.
Does not actually convert `p` into `ShortPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually convert `p`
into `ShortPointer`!.
Prefer this method to [[short-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[short-ptr]] in places when `nil` is acceptable.
"
(^ShortPointer [^ShortPointer p]
(cast ShortPointer (safe2 p)))
(^ShortPointer [^ShortPointer p ^long i]
(.getPointer ^ShortPointer (safe2 p) (min i (.capacity p)))))
(defn byte-ptr*
"Casts pointer `p` into `BytePointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `BytePointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[byte-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^BytePointer [^BytePointer p]
p)
(^BytePointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn byte-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `BytePointer`.
Does not actually convert `p` into `BytePointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually convert `p`
into `BytePointer`!.
Prefer this method to [[byte-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[byte-ptr2]] in places when `nil` is not allowed.
"
(^BytePointer [^BytePointer p]
(cast BytePointer (safe p)))
(^BytePointer [^BytePointer p ^long i]
(.getPointer ^BytePointer (safe p) (min i (.capacity p)))))
(defn byte-ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `BytePointer`.
Does not actually convert `p` into `BytePointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually convert `p`
into `BytePointer`!.
Prefer this method to [[byte-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[byte-ptr]] in places when `nil` is acceptable.
"
(^BytePointer [^BytePointer p]
(cast BytePointer (safe2 p)))
(^BytePointer [^BytePointer p ^long i]
(.getPointer ^BytePointer (safe2 p) (min i (.capacity p)))))
(defn clong-ptr*
"Casts pointer `p` into `CLongPointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `CLongPointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[clong-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^CLongPointer [^CLongPointer p]
p)
(^CLongPointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn clong-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `CLongPointer`.
Does not actually convert `p` into `CLongPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually convert `p`
into `CLongPointer`!.
Prefer this method to [[clong-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[clong-ptr2]] in places when `nil` is not allowed.
"
(^CLongPointer [^CLongPointer p]
(cast CLongPointer (safe p)))
(^CLongPointer [^CLongPointer p ^long i]
(.getPointer ^CLongPointer (safe p) (min i (.capacity p)))))
(defn clong-ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `CLongPointer`.
Does not actually convert `p` into `CLongPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually convert `p`
into `CLongPointer`!.
Prefer this method to [[clong-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[clong-ptr]] in places when `nil` is acceptable.
"
(^CLongPointer [^CLongPointer p]
(cast CLongPointer (safe2 p)))
(^CLongPointer [^CLongPointer p ^long i]
(.getPointer ^CLongPointer (safe2 p) (min i (.capacity p)))))
(defn size-t-ptr*
"Casts pointer `p` into `SizeTPointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `SizeTPointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[size-t-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^SizeTPointer [^SizeTPointer p]
p)
(^SizeTPointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn size-t-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `SizeTPointer`.
Does not actually convert `p` into `SizeTPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually convert `p`
into `SizeTPointer`!.
Prefer this method to [[size-t-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[size-t-ptr2]] in places when `nil` is not allowed.
"
(^SizeTPointer [^SizeTPointer p]
(cast SizeTPointer (safe p)))
(^SizeTPointer [^SizeTPointer p ^long i]
(.getPointer ^SizeTPointer (safe p) (min i (.capacity p)))))
(defn size-t-ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `SizeTPointer`.
Does not actually convert `p` into `SizeTPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually require `p`
to be a `SizeTPointer`!.
Prefer this method to [[size-t-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[size-t-ptr]] in places when `nil` is acceptable.
"
(^SizeTPointer [^SizeTPointer p]
(cast SizeTPointer (safe2 p)))
(^SizeTPointer [^SizeTPointer p ^long i]
(.getPointer ^SizeTPointer (safe2 p) (min i (.capacity p)))))
(defn bool-ptr*
"Casts pointer `p` into `BoolPointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `BoolPointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[bool-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^BoolPointer [^BoolPointer p]
p)
(^BoolPointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn bool-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `BoolPointer`.
Does not actually convert `p` into `BoolPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually require `p`
to be a `BoolPointer`!.
Prefer this method to [[bool-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[bool-ptr2]] in places when `nil` is not allowed.
"
(^BoolPointer [^BoolPointer p]
(cast BoolPointer (safe p)))
(^BoolPointer [^BoolPointer p ^long i]
(.getPointer ^BoolPointer (safe p) (min i (.capacity p)))))
(defn bool-ptr2
"Checks pointer `p` for safety with [[safe2]] and casts it into `BoolPointer`.
Does not actually convert `p` into `BoolPointer`!. It just does the type cast to satisfy
Clojure's compiler. Useful when metadata for avoiding reflection is not easily added in code,
such as in macros.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block. It DOES actually require `p`
to be a `BoolPointer`!.
Prefer this method to [[bool-ptr*]] in places when NULL pointer can cause harm.
Prefer this method to [[bool-ptr]] in places when `nil` is acceptable.
"
(^BoolPointer [^BoolPointer p]
(cast BoolPointer (safe2 p)))
(^BoolPointer [^BoolPointer p ^long i]
(.getPointer ^BoolPointer (safe2 p) (min i (.capacity p)))))
(defn char-ptr*
"Casts pointer `p` into `CharPointer`. Useful when metadata for avoiding reflection
is not easily added in code, such as in macros. Does not actually convert `p` into `CharPointer`!.
It just does the type cast to satisfy Clojure's compiler.
If provided with index `i`, behaves like [[get-pointer]]. Negative `i` is allowed, but bee careful
to stay within the original memory block.
Prefer this method to [[char-ptr]] in places when you only care about satisfying the compiler,
and don't care about NULL pointers.
"
(^CharPointer [^CharPointer p]
p)
(^CharPointer [^Pointer p ^long i]
(.getPointer p (min i (.capacity p)))))
(defn char-ptr
"Checks pointer `p` for safety with [[safe]] and casts it into `CharPointer`.