-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstj.hpp
More file actions
1063 lines (892 loc) · 31.2 KB
/
stj.hpp
File metadata and controls
1063 lines (892 loc) · 31.2 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
#pragma once
// TODO: remove
#include <cstdint>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <type_traits>
#include <limits>
#include <algorithm>
#include <utility>
// TODO: clean the namespace
namespace __stj_basic_impl {
thread_local bool error = false;
}
// panic ===
#define PANIC(msg) do { \
std::fprintf(stderr, "Panic at %s:%d: %s\n", __FILE__, __LINE__, msg); \
std::abort(); \
} while(0)
// === panic
// SafeInt ====
template <typename T>
class SafeInt {
private:
static_assert(std::is_integral_v<T>, "SafeInt can only wrap integral types");
T value;
static bool add_overflow(T a, T b, T& result) {
if constexpr (std::is_unsigned_v<T>) {
result = a + b;
return result < a;
} else {
if (b > 0 && a > std::numeric_limits<T>::max() - b) return true;
if (b < 0 && a < std::numeric_limits<T>::min() - b) return true;
result = a + b;
return false;
}
}
static bool sub_overflow(T a, T b, T& result) {
if constexpr (std::is_unsigned_v<T>) {
result = a - b;
return a < b;
} else {
if (b < 0 && a > std::numeric_limits<T>::max() + b) return true;
if (b > 0 && a < std::numeric_limits<T>::min() + b) return true;
result = a - b;
return false;
}
}
static bool mul_overflow(T a, T b, T& result) {
if constexpr (std::is_unsigned_v<T>) {
if (a == 0 || b == 0) {
result = 0;
return false;
}
result = a * b;
return result / a != b;
} else {
if (a == 0 || b == 0) {
result = 0;
return false;
}
if (a == -1 && b == std::numeric_limits<T>::min()) return true;
if (b == -1 && a == std::numeric_limits<T>::min()) return true;
T max = std::numeric_limits<T>::max();
T min = std::numeric_limits<T>::min();
if (a > 0 && b > 0 && a > max / b) return true;
if (a < 0 && b < 0 && a < max / b) return true;
if (a > 0 && b < 0 && b < min / a) return true;
if (a < 0 && b > 0 && a < min / b) return true;
result = a * b;
return false;
}
}
public:
SafeInt() : value(0) {}
SafeInt(T val) : value(val) {}
template <typename U, typename = std::enable_if_t<std::is_integral_v<U>>>
SafeInt(U val) : value(static_cast<T>(val)) {
if (static_cast<U>(static_cast<T>(val)) != val) {
PANIC("Integer conversion overflow");
}
}
template <typename U>
SafeInt(const SafeInt<U>& other) : value(static_cast<T>(other.raw())) {
if (static_cast<U>(static_cast<T>(other.raw())) != other.raw()) {
PANIC("Integer conversion overflow");
}
}
operator T() const { return value; }
T raw() const { return value; }
SafeInt operator+(const SafeInt& other) const {
T result;
if (add_overflow(value, other.value, result)) {
PANIC("Integer overflow in addition");
}
return SafeInt(result);
}
SafeInt operator-(const SafeInt& other) const {
T result;
if (sub_overflow(value, other.value, result)) {
PANIC("Integer underflow in subtraction");
}
return SafeInt(result);
}
SafeInt operator*(const SafeInt& other) const {
T result;
if (mul_overflow(value, other.value, result)) {
PANIC("Integer overflow in multiplication");
}
return SafeInt(result);
}
SafeInt operator/(const SafeInt& other) const {
if (other.value == 0) {
PANIC("Division by zero");
}
if constexpr (std::is_signed_v<T>) {
if (value == std::numeric_limits<T>::min() && other.value == -1) {
PANIC("Integer overflow in division");
}
}
return SafeInt(value / other.value);
}
SafeInt operator%(const SafeInt& other) const {
if (other.value == 0) {
PANIC("Modulo by zero");
}
if constexpr (std::is_signed_v<T>) {
if (value == std::numeric_limits<T>::min() && other.value == -1) {
return SafeInt(0);
}
}
return SafeInt(value % other.value);
}
template <typename U>
auto operator+(const U& other) const -> SafeInt<decltype(value + U{})> {
using ResultType = decltype(value + U{});
return SafeInt<ResultType>(value) + SafeInt<ResultType>(other);
}
template <typename U>
auto operator-(const U& other) const -> SafeInt<decltype(value - U{})> {
using ResultType = decltype(value - U{});
return SafeInt<ResultType>(value) - SafeInt<ResultType>(other);
}
template <typename U>
auto operator*(const U& other) const -> SafeInt<decltype(value * U{})> {
using ResultType = decltype(value * U{});
return SafeInt<ResultType>(value) * SafeInt<ResultType>(other);
}
template <typename U>
auto operator/(const U& other) const -> SafeInt<decltype(value / U{})> {
using ResultType = decltype(value / U{});
return SafeInt<ResultType>(value) / SafeInt<ResultType>(other);
}
template <typename U>
auto operator%(const U& other) const -> SafeInt<decltype(value % U{})> {
using ResultType = decltype(value % U{});
return SafeInt<ResultType>(value) % SafeInt<ResultType>(other);
}
SafeInt operator-() const {
if constexpr (std::is_signed_v<T>) {
if (value == std::numeric_limits<T>::min()) {
PANIC("Integer overflow in negation");
}
return SafeInt(-value);
} else {
PANIC("Cannot negate unsigned integer");
}
}
SafeInt& operator+=(const SafeInt& other) {
T result;
if (add_overflow(value, other.value, result)) {
PANIC("Integer overflow in addition");
}
value = result;
return *this;
}
SafeInt& operator-=(const SafeInt& other) {
T result;
if (sub_overflow(value, other.value, result)) {
PANIC("Integer underflow in subtraction");
}
value = result;
return *this;
}
SafeInt& operator*=(const SafeInt& other) {
T result;
if (mul_overflow(value, other.value, result)) {
PANIC("Integer overflow in multiplication");
}
value = result;
return *this;
}
SafeInt& operator/=(const SafeInt& other) {
*this = *this / other;
return *this;
}
SafeInt& operator%=(const SafeInt& other) {
*this = *this % other;
return *this;
}
template <typename U>
SafeInt& operator+=(const U& other) {
*this = SafeInt(*this + other);
return *this;
}
template <typename U>
SafeInt& operator-=(const U& other) {
*this = SafeInt(*this - other);
return *this;
}
template <typename U>
SafeInt& operator*=(const U& other) {
*this = SafeInt(*this * other);
return *this;
}
template <typename U>
SafeInt& operator/=(const U& other) {
*this = SafeInt(*this / other);
return *this;
}
template <typename U>
SafeInt& operator%=(const U& other) {
*this = SafeInt(*this % other);
return *this;
}
template <typename U>
SafeInt& operator=(const U& other) {
*this = SafeInt(other);
return *this;
}
SafeInt& operator++() {
T result;
if (add_overflow(value, T(1), result)) {
PANIC("Integer overflow in increment");
}
value = result;
return *this;
}
SafeInt operator++(int) {
SafeInt old = *this;
++(*this);
return old;
}
SafeInt& operator--() {
T result;
if (sub_overflow(value, T(1), result)) {
PANIC("Integer underflow in decrement");
}
value = result;
return *this;
}
SafeInt operator--(int) {
SafeInt old = *this;
--(*this);
return old;
}
bool operator==(const SafeInt& other) const { return value == other.value; }
bool operator!=(const SafeInt& other) const { return value != other.value; }
bool operator<(const SafeInt& other) const { return value < other.value; }
bool operator<=(const SafeInt& other) const { return value <= other.value; }
bool operator>(const SafeInt& other) const { return value > other.value; }
bool operator>=(const SafeInt& other) const { return value >= other.value; }
template <typename U>
bool operator==(const U& other) const { return value == other; }
template <typename U>
bool operator!=(const U& other) const { return value != other; }
template <typename U>
bool operator<(const U& other) const { return value < other; }
template <typename U>
bool operator<=(const U& other) const { return value <= other; }
template <typename U>
bool operator>(const U& other) const { return value > other; }
template <typename U>
bool operator>=(const U& other) const { return value >= other; }
SafeInt operator&(const SafeInt& other) const { return SafeInt(value & other.value); }
SafeInt operator|(const SafeInt& other) const { return SafeInt(value | other.value); }
SafeInt operator^(const SafeInt& other) const { return SafeInt(value ^ other.value); }
SafeInt operator~() const { return SafeInt(~value); }
SafeInt operator<<(const SafeInt& shift) const {
if (shift.value < 0 || shift.value >= static_cast<T>(sizeof(T) * 8)) {
PANIC("Invalid shift amount");
}
if (shift.value > 0) {
T max_shift = static_cast<T>(sizeof(T) * 8 - 1);
if constexpr (std::is_unsigned_v<T>) {
if (value > (std::numeric_limits<T>::max() >> shift.value)) {
PANIC("Integer overflow in left shift");
}
} else {
if (value > 0 && value > (std::numeric_limits<T>::max() >> shift.value)) {
PANIC("Integer overflow in left shift");
}
if (value < 0 && value < (std::numeric_limits<T>::min() >> shift.value)) {
PANIC("Integer underflow in left shift");
}
}
}
return SafeInt(value << shift.value);
}
SafeInt operator>>(const SafeInt& shift) const {
if (shift.value < 0 || shift.value >= static_cast<T>(sizeof(T) * 8)) {
PANIC("Invalid shift amount");
}
return SafeInt(value >> shift.value);
}
};
using u8 = SafeInt<std::uint8_t>;
using i8 = SafeInt<std::int8_t>;
using u16 = SafeInt<std::uint16_t>;
using i16 = SafeInt<std::int16_t>;
using u32 = SafeInt<std::uint32_t>;
using i32 = SafeInt<std::int32_t>;
using u64 = SafeInt<std::uint64_t>;
using i64 = SafeInt<std::int64_t>;
using usize = SafeInt<std::size_t>;
using isize = SafeInt<std::ptrdiff_t>;
// ==== SafeInt
// defer ====
template <typename F>
struct privDefer {
F f;
privDefer(F f) : f(f) {}
~privDefer() { f(); }
};
template <typename F>
privDefer<F> defer_func(F f) {
return privDefer<F>(f);
}
#define DEFER_1(x, y) x##y
#define DEFER_2(x, y) DEFER_1(x, y)
#define DEFER_3(x) DEFER_2(x, __COUNTER__)
#define defer(code) auto DEFER_3(_defer_) = defer_func([&](){code;})
// ==== defer
// Error ====
template <typename... Enums>
class Error {
private:
template <typename D, typename... Ts>
struct contains_type {
static constexpr bool value = (std::is_same_v<D, Ts> || ...);
};
template <typename D, typename... Ts>
struct index_of;
template <typename D, typename First, typename... Rest>
struct index_of<D, First, Rest...> {
static constexpr std::size_t value = std::is_same_v<D, First> ? 0 : 1 + index_of<D, Rest...>::value;
};
template <typename D>
struct index_of<D> {
static constexpr std::size_t value = 0;
};
using TagType = std::uint8_t;
static constexpr TagType INVALID_TAG = 0;
static constexpr TagType VALUE_TAG = 1;
union Storage {
char errorStorage[std::max({sizeof(Enums)...})];
} storage;
TagType tag;
template <typename E>
static constexpr TagType getTagForType() {
static_assert(
contains_type<E, Enums...>::value,
"Type not in the list of supported enum types"
);
return static_cast<TagType>(index_of<E, Enums...>::value + 2);
}
template <typename E, typename... Os>
void tryConvertEnum(const Error<Os...>& other, bool& converted) {
if (!converted && other.template is<E>()) {
E error = other.template get<E>();
tag = getTagForType<E>();
*reinterpret_cast<E*>(storage.errorStorage) = error;
converted = true;
}
}
public:
Error() : tag(INVALID_TAG) {}
template <typename E, typename = std::enable_if_t<contains_type<E, Enums...>::value>>
Error(E error) : tag(getTagForType<E>()) {
*reinterpret_cast<E*>(storage.errorStorage) = error;
}
template <typename... OtherEnums>
Error(const Error<OtherEnums...>& other) : tag(INVALID_TAG) {
static_assert(
(contains_type<OtherEnums, Enums...>::value && ...),
"Target Error type must include all enum types from source Error"
);
if (other.isEmpty()) {
return;
}
bool converted = false;
(tryConvertEnum<OtherEnums>(other, converted), ...);
}
template <typename E, typename = std::enable_if_t<contains_type<E, Enums...>::value>>
bool is() const {
return tag == getTagForType<E>();
}
bool hasError() const {
return tag != INVALID_TAG;
}
template <typename E, typename = std::enable_if_t<contains_type<E, Enums...>::value>>
E get() const {
if (tag != getTagForType<E>()) {
PANIC("Error does not contain this error type");
}
return *reinterpret_cast<const E*>(storage.errorStorage);
}
bool isEmpty() const {
return tag == INVALID_TAG;
}
};
// ==== Error
// Result ====
template <typename T, typename... Enums>
class Result {
private:
template <typename D, typename... Ts>
struct contains_type {
static constexpr bool value = (std::is_same_v<D, Ts> || ...);
};
template <typename D, typename... Ts>
struct index_of;
template <typename D, typename First, typename... Rest>
struct index_of<D, First, Rest...> {
static constexpr std::size_t value = std::is_same_v<D, First> ? 0 : 1 + index_of<D, Rest...>::value;
};
template <typename D>
struct index_of<D> {
static constexpr std::size_t value = 0;
};
using TagType = std::uint8_t;
static constexpr TagType INVALID_TAG = 0;
static constexpr TagType VALUE_TAG = 1;
union Storage {
alignas(T) char valueStorage[sizeof(T)];
char errorStorage[std::max({sizeof(Enums)...})];
Storage() {}
~Storage() {}
} storage;
TagType tag;
template <typename E>
static constexpr TagType getTagForType() {
static_assert(
contains_type<E, Enums...>::value,
"Type not in the list of supported enum types"
);
return static_cast<TagType>(index_of<E, Enums...>::value + 2);
}
template <typename E, typename U, typename... Os>
void tryConvertEnum(const Result<U, Os...>& other, bool& converted) {
if (!converted && other.template hasError<E>()) {
E error = other.template error<E>();
tag = getTagForType<E>();
*reinterpret_cast<E*>(storage.errorStorage) = error;
converted = true;
}
}
void cleanup() {
if (tag == VALUE_TAG) {
reinterpret_cast<T*>(storage.valueStorage)->~T();
}
}
void copyFrom(const Result& other) {
tag = other.tag;
if (tag == VALUE_TAG) {
new (storage.valueStorage) T(other.value());
} else if (tag > VALUE_TAG) {
std::memcpy(storage.errorStorage, other.storage.errorStorage, sizeof(storage.errorStorage));
}
}
void moveFrom(Result&& other) {
tag = other.tag;
if (tag == VALUE_TAG) {
new (storage.valueStorage) T(std::move(other.value()));
} else if (tag > VALUE_TAG) {
std::memcpy(storage.errorStorage, other.storage.errorStorage, sizeof(storage.errorStorage));
}
other.tag = INVALID_TAG;
}
public:
Result() : tag(INVALID_TAG) {}
Result(const T& value) : tag(VALUE_TAG) {
new (storage.valueStorage) T(value);
}
Result(T&& value) : tag(VALUE_TAG) {
new (storage.valueStorage) T(std::move(value));
}
template <typename U, typename = std::enable_if_t<
std::is_convertible_v<U, T> &&
!std::is_same_v<std::decay_t<U>, T> &&
!contains_type<std::decay_t<U>, Enums...>::value
>>
Result(U&& value) : tag(VALUE_TAG) {
new (storage.valueStorage) T(std::forward<U>(value));
}
template <typename E, typename = std::enable_if_t<contains_type<E, Enums...>::value>>
Result(E error) : tag(getTagForType<E>()) {
*reinterpret_cast<E*>(storage.errorStorage) = error;
}
template <typename U, typename... OtherEnums, typename = std::enable_if_t<std::is_convertible_v<U, T>>>
Result(const Result<U, OtherEnums...>& other) : tag(INVALID_TAG) {
static_assert(
(contains_type<OtherEnums, Enums...>::value && ...),
"Target Result type must include all enum types from source Result"
);
if (other.isEmpty()) {
return;
}
if (other.hasValue()) {
tag = VALUE_TAG;
new (storage.valueStorage) T(other.value());
return;
}
bool converted = false;
(tryConvertEnum<OtherEnums>(other, converted), ...);
}
Result(const Result& other) : tag(INVALID_TAG) {
copyFrom(other);
}
Result(Result&& other) noexcept : tag(INVALID_TAG) {
moveFrom(std::move(other));
}
Result& operator=(const Result& other) {
if (this != &other) {
cleanup();
copyFrom(other);
}
return *this;
}
Result& operator=(Result&& other) noexcept {
if (this != &other) {
cleanup();
moveFrom(std::move(other));
}
return *this;
}
~Result() {
cleanup();
}
bool hasValue() const {
return tag == VALUE_TAG;
}
template <typename E, typename = std::enable_if_t<contains_type<E, Enums...>::value>>
bool hasError() const {
return tag == getTagForType<E>();
}
bool hasAnyError() const {
return tag > VALUE_TAG;
}
T& value() {
if (tag != VALUE_TAG) {
PANIC("Result does not contain a value");
}
return *reinterpret_cast<T*>(storage.valueStorage);
}
const T& value() const {
if (tag != VALUE_TAG) {
PANIC("Result does not contain a value");
}
return *reinterpret_cast<const T*>(storage.valueStorage);
}
template <typename E, typename = std::enable_if_t<contains_type<E, Enums...>::value>>
E error() const {
if (tag != getTagForType<E>()) {
PANIC("Result does not contain this error type");
}
return *reinterpret_cast<const E*>(storage.errorStorage);
}
T valueOr(const T& defaultValue) const {
return hasValue() ? value() : defaultValue;
}
bool isEmpty() const {
return tag == INVALID_TAG;
}
};
// ==== Result
// errdefer ====
// TODO: check for the type of _result_temp
#define TRY(expr) ({ \
auto _result_temp = (expr); \
__stj_basic_impl::error = _result_temp.hasAnyError(); \
if (__stj_basic_impl::error) { \
return _result_temp; \
} \
_result_temp.value(); \
})
template <typename F>
struct privErrdefer {
F f;
privErrdefer(F f) : f(f) {}
~privErrdefer() { f(); }
};
template <typename F>
privDefer<F> errdefer_func(F f) {
return privDefer<F>(f);
}
// TODO: move stuff to a namepsace
#define ERRDEFER_1(x, y) x##y
#define ERRDEFER_2(x, y) ERRDEFER_1(x, y)
#define ERRDEFER_3(x) ERRDEFER_2(x, __COUNTER__)
#define errdefer(code) auto ERRDEFER_3(_defer_) = errdefer_func([&](){ \
if (__stj_basic_impl::error) {code;} \
});
// ==== errdefer
// single item ptr ====
template <typename T>
class Ptr {
private:
struct PrivateTag {};
Ptr(T* p, PrivateTag) : raw_ptr(p) {} // workaround for creating a Ptr with a nullptr for undefined
public:
// TODO support void pointer
T* raw_ptr;
Ptr(T* p) : raw_ptr(p) {
if (p == nullptr) {
PANIC("Cannot initialize Ptr with nullptr");
}
}
static Ptr<T> undefined() {
return Ptr<T>(nullptr, PrivateTag{});
}
T& v() const {
return *raw_ptr;
}
template <typename... Args>
auto operator()(Args&&... args) const -> decltype((*raw_ptr)(std::forward<Args>(args)...)) {
return (*raw_ptr)(std::forward<Args>(args)...);
}
};
// ==== single item ptr
template <typename T> class MiPtr;
template <typename T> struct Slice;
// many items pointer ====
template <typename T>
class MiPtr {
private:
struct PrivateTag {};
MiPtr(T* p, PrivateTag) : raw_ptr(p) {} // workaround for creating a Ptr with a nullptr for undefined
public:
T* raw_ptr;
MiPtr(T* p) : raw_ptr(p) {
if (p == nullptr) [[unlikely]] {
PANIC("Cannot initialize MiPtr with nullptr");
}
}
static MiPtr<T> undefined() {
return MiPtr<T>(nullptr, PrivateTag{});
}
T& operator[] (usize index) {
return raw_ptr[index];
}
Slice<T> slice(usize start, usize end) {
if (start > end) [[unlikely]] {
PANIC("start is greater than end");
}
MiPtr<T> shifted_ptr = raw_ptr + start;
return Slice<T>{shifted_ptr, end - start};
}
MiPtr<T> slice(usize start) {
return MiPtr<T>(raw_ptr + start);
}
};
// ==== many items pointer
// Slice ====
template <typename T>
struct Slice {
MiPtr<T> ptr;
usize len;
T& operator[] (usize index) {
if (index >= len) [[unlikely]] {
PANIC("index is greater than slice bound");
}
return ptr[index];
}
static Slice empty() {
return Slice{MiPtr<T>::undefined(), 0};
}
Slice<T> slice(usize start, usize end) {
if (end >= len) [[unlikely]] {
PANIC("end is greater than slice bound");
}
if (start > end) [[unlikely]] {
PANIC("start is greater than end");
}
MiPtr<T> shifted_ptr = ptr.slice(start);
return Slice<T>{shifted_ptr, end - start};
}
Slice<T> slice(usize start) {
if (start >= len) [[unlikely]] {
PANIC("start is greater than slice bound");
}
MiPtr<T> shifted_ptr = ptr.slice(start);
return Slice<T>{shifted_ptr, len - start};
}
};
// ==== Slice
namespace stj {
namespace heap {
struct AllocatorVTable {
/// Attempt to allocate exactly `len` bytes.
///
Ptr<Slice<u8>(void* ctx, usize len)> alloc;
/// Attempt to expand or shrink memory in place. `buf.len` must equal the
/// length requested from the most recent successful call to `alloc` or
/// `resize`.
///
/// A result of `true` indicates the resize was successful and the
/// allocation now has the same address but a size of `new_len`. `false`
/// indicates the resize could not be completed without moving the
/// allocation to a different address and thus alloc should be used to
/// complete the resize
///
/// `new_len` must be greater than zero.
///
Ptr<bool(void* ctx, Slice<u8> buf, usize new_len)> resize;
/// Free and invalidate a buffer.
///
/// `buf.len` must equal the most recent length returned by `alloc` or
/// given to a successful `resize` call.
///
Ptr<void(void* ctx, Slice<u8> buf)> free;
};
// Allocator interface
struct Allocator {
void* impl_data;
const AllocatorVTable* vtable;
// Allocate a slice of bytes of given capacity
template <typename T>
Slice<T> alloc(usize count) {
usize byte_size = count * sizeof(T);
Slice<u8> bytes = vtable->alloc(impl_data, byte_size);
if (bytes.len == 0) [[unlikely]] {
PANIC("Memory allocation failed");
}
return Slice<T>{MiPtr<T>(reinterpret_cast<T*>(bytes.ptr.raw_ptr)), count};
}
// Deallocate a previously allocated slice
template <typename T>
void free(Slice<T> slice) {
Slice<u8> bytes{
MiPtr<u8>(reinterpret_cast<u8*>(slice.ptr.raw_ptr)), // TODO: make a cast function for Ptr<>?
slice.len * sizeof(T)
};
vtable->free(impl_data, bytes);
}
// Create a single item
template <typename T>
Ptr<T> create() {
Slice<T> memory = alloc<T>(1);
return memory.ptr;
}
// Destroy a single item
template <typename T>
void destroy(Ptr<T> item) {
Slice<u8> bytes{
MiPtr<u8>(reinterpret_cast<u8*>(item.get_raw_ptr)),
sizeof(T)
};
vtable->free(impl_data, bytes);
}
// Resize an existing allocation
template <typename T>
Slice<T> realloc(Slice<T> old_slice, usize new_count) {
usize new_byte_size = new_count * sizeof(T);
Slice<u8> bytes{
MiPtr<u8>(reinterpret_cast<u8*>(old_slice.ptr.raw_ptr)),
old_slice.len * sizeof(T)
};
bool success = vtable->resize(impl_data, bytes, new_byte_size);
if (success) {
return Slice<T>{old_slice.ptr, new_count};
} else {
Slice<T> new_slice = alloc<T>(new_count);
usize copy_count;
if (old_slice.len < new_count) {
copy_count = old_slice.len;
} else {
copy_count = new_count;
}
for (usize i = 0; i < copy_count; i++) {
new_slice[i] = old_slice[i];
}
free(old_slice);
return new_slice;
}
}
};
namespace c_allocator_impl {
namespace extern_c {
#if defined(_WIN32) || defined(_WIN64)
#if defined(myDLL_EXPORTS)
extern "C" std::size_t __declspec(dllexport) _msize(void* memblock);
#else
extern "C" std::size_t __declspec(dllimport) _msize(void* memblock);
#endif
#elif defined(__APPLE__)
extern "C" std::size_t malloc_size(const void* ptr);
#elif defined(__FreeBSD__) || defined(__linux__)
extern "C" std::size_t malloc_usable_size(const void* ptr);
#endif
}
inline std::size_t get_allocation_size(void* ptr) {
if (ptr == nullptr) return 0;
#if defined(_WIN32) || defined(_WIN64)
return extern_c::_msize(const_cast<void*>(ptr));
#elif defined(__APPLE__)
return extern_c::malloc_size(ptr);
#elif defined(__FreeBSD__) || defined(__linux__)
return extern_c::malloc_usable_size(ptr);
#else
return 0;
#endif
}
static Slice<u8> malloc_alloc(void* /*ctx*/, usize size) {
if (size == 0) return {MiPtr<u8>(nullptr), 0};
u8* ptr = static_cast<u8*>(::malloc(size));
if (ptr == nullptr) [[unlikely]] {
return {MiPtr<u8>(nullptr), 0};
}
return {MiPtr<u8>(ptr), size};
}
static bool malloc_resize(void* /*ctx*/, Slice<u8> buf, usize new_size) {
return new_size <= get_allocation_size(buf.ptr.raw_ptr);
}
static void malloc_free(void* /*ctx*/, Slice<u8> buf) {
::free(buf.ptr.raw_ptr);
}
static const AllocatorVTable malloc_vtable = {
malloc_alloc,
malloc_resize,
malloc_free
};
}
const Allocator c_allocator = {
nullptr,
&c_allocator_impl::malloc_vtable
};
}