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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
|
package mem
import "base:runtime"
//NOTE(bill, 2019-12-31): These are defined in `package runtime` as they are used in the `context`. This is to prevent an import definition cycle.
/*
A request to allocator procedure.
This type represents a type of allocation request made to an allocator
procedure. There is one allocator procedure per allocator, and this value is
used to discriminate between different functions of the allocator.
The type is defined as follows:
Allocator_Mode :: enum byte {
Alloc,
Alloc_Non_Zeroed,
Free,
Free_All,
Resize,
Resize_Non_Zeroed,
Query_Features,
}
Depending on which value is used, the allocator procedure will perform different
functions:
- `Alloc`: Allocates a memory region with a given `size` and `alignment`.
- `Alloc_Non_Zeroed`: Same as `Alloc` without explicit zero-initialization of
the memory region.
- `Free`: Free a memory region located at address `ptr` with a given `size`.
- `Free_All`: Free all memory allocated using this allocator.
- `Resize`: Resize a memory region located at address `old_ptr` with size
`old_size` to be `size` bytes in length and have the specified `alignment`,
in case a re-alllocation occurs.
- `Resize_Non_Zeroed`: Same as `Resize`, without explicit zero-initialization.
*/
Allocator_Mode :: runtime.Allocator_Mode
/*
A set of allocator features.
This type represents values that contain a set of features an allocator has.
Currently the type is defined as follows:
Allocator_Mode_Set :: distinct bit_set[Allocator_Mode];
*/
Allocator_Mode_Set :: runtime.Allocator_Mode_Set
/*
Allocator information.
This type represents information about a given allocator at a specific point
in time. Currently the type is defined as follows:
Allocator_Query_Info :: struct {
pointer: rawptr,
size: Maybe(int),
alignment: Maybe(int),
}
- `pointer`: Pointer to a backing buffer.
- `size`: Size of the backing buffer.
- `alignment`: The allocator's alignment.
If not applicable, any of these fields may be `nil`.
*/
Allocator_Query_Info :: runtime.Allocator_Query_Info
/*
An allocation request error.
This type represents error values the allocators may return upon requests.
Allocator_Error :: enum byte {
None = 0,
Out_Of_Memory = 1,
Invalid_Pointer = 2,
Invalid_Argument = 3,
Mode_Not_Implemented = 4,
}
The meaning of the errors is as follows:
- `None`: No error.
- `Out_Of_Memory`: Either:
1. The allocator has ran out of the backing buffer, or the requested
allocation size is too large to fit into a backing buffer.
2. The operating system error during memory allocation.
3. The backing allocator was used to allocate a new backing buffer and the
backing allocator returned Out_Of_Memory.
- `Invalid_Pointer`: The pointer referring to a memory region does not belong
to any of the allocators backing buffers or does not point to a valid start
of an allocation made in that allocator.
- `Invalid_Argument`: Can occur if one of the arguments makes it impossible to
satisfy a request (i.e. having alignment larger than the backing buffer
of the allocation).
- `Mode_Not_Implemented`: The allocator does not support the specified
operation. For example, an arena does not support freeing individual
allocations.
*/
Allocator_Error :: runtime.Allocator_Error
/*
The allocator procedure.
This type represents allocation procedures. An allocation procedure is a single
procedure, implementing all allocator functions such as allocating the memory,
freeing the memory, etc.
Currently the type is defined as follows:
Allocator_Proc :: #type proc(
allocator_data: rawptr,
mode: Allocator_Mode,
size: int,
alignment: int,
old_memory: rawptr,
old_size: int,
location: Source_Code_Location = #caller_location,
) -> ([]byte, Allocator_Error);
The function of this procedure and the meaning of parameters depends on the
value of the `mode` parameter. For any operation the following constraints
apply:
- The `alignment` must be a power of two.
- The `size` must be a positive integer.
## 1. `.Alloc`, `.Alloc_Non_Zeroed`
Allocates a memory region of size `size`, aligned on a boundary specified by
`alignment`.
**Inputs**:
- `allocator_data`: Pointer to the allocator data.
- `mode`: `.Alloc` or `.Alloc_Non_Zeroed`.
- `size`: The desired size of the memory region.
- `alignment`: The desired alignmnet of the allocation.
- `old_memory`: Unused, should be `nil`.
- `old_size`: Unused, should be 0.
**Returns**:
1. The memory region, if allocated successfully, or `nil` otherwise.
2. An error, if allocation failed.
**Note**: The nil allocator may return `nil`, even if no error is returned.
Always check both the error and the allocated buffer.
**Note**: The `.Alloc` mode is required to be implemented for an allocator
and can not return a `.Mode_Not_Implemented` error.
## 2. `Free`
Frees a memory region located at the address specified by `old_memory`. If the
allocator does not track sizes of allocations, the size should be specified in
the `old_size` parameter.
**Inputs**:
- `allocator_data`: Pointer to the allocator data.
- `mode`: `.Free`.
- `size`: Unused, should be 0.
- `alignment`: Unused, should be 0.
- `old_memory`: Pointer to the memory region to free.
- `old_size`: The size of the memory region to free. This parameter is optional
if the allocator keeps track of the sizes of allocations.
**Returns**:
1. `nil`
2. Error, if freeing failed.
## 3. `Free_All`
Frees all allocations, associated with the allocator, making it available for
further allocations using the same backing buffers.
**Inputs**:
- `allocator_data`: Pointer to the allocator data.
- `mode`: `.Free_All`.
- `size`: Unused, should be 0.
- `alignment`: Unused, should be 0.
- `old_memory`: Unused, should be `nil`.
- `old_size`: Unused, should be `0`.
**Returns**:
1. `nil`.
2. Error, if freeing failed.
## 4. `Resize`, `Resize_Non_Zeroed`
Resizes the memory region, of the size `old_size` located at the address
specified by `old_memory` to have the new size `size`. The slice of the new
memory region is returned from the procedure. The allocator may attempt to
keep the new memory region at the same address as the previous allocation,
however no such guarantee is made. Do not assume the new memory region will
be at the same address as the old memory region.
If `old_memory` pointer is `nil`, this function acts just like `.Alloc` or
`.Alloc_Non_Zeroed`, using `size` and `alignment` to allocate a new memory
region.
If `new_size` is `nil`, the procedure acts just like `.Free`, freeing the
memory region `old_size` bytes in length, located at the address specified by
`old_memory`.
If the `old_memory` pointer is not aligned to the boundary specified by
`alignment`, the procedure relocates the buffer such that the reallocated
buffer is aligned to the boundary specified by `alignment`.
**Inputs**:
- `allocator_data`: Pointer to the allocator data.
- `mode`: `.Resize` or `.Resize_All`.
- `size`: The desired new size of the memory region.
- `alignment`: The alignment of the new memory region, if its allocated
- `old_memory`: The pointer to the memory region to resize.
- `old_size`: The size of the memory region to resize. If the allocator
keeps track of the sizes of allocations, this parameter is optional.
**Returns**:
1. The slice of the memory region after resize operation, if successfull,
`nil` otherwise.
2. An error, if the resize failed.
**Note**: Some allocators may return `nil`, even if no error is returned.
Always check both the error and the allocated buffer.
**Note**: if `old_size` is `0` and `old_memory` is `nil`, this operation is a
no-op, and should not return errors.
*/
Allocator_Proc :: runtime.Allocator_Proc
/*
Allocator.
This type represents generic interface for all allocators. Currently this type
is defined as follows:
Allocator :: struct {
procedure: Allocator_Proc,
data: rawptr,
}
- `procedure`: Pointer to the allocation procedure.
- `data`: Pointer to the allocator data.
*/
Allocator :: runtime.Allocator
/*
Default alignment.
This value is the default alignment for all platforms that is used, if the
alignment is not specified explicitly.
*/
DEFAULT_ALIGNMENT :: 2*align_of(rawptr)
/*
Default page size.
This value is the default page size for the current platform.
*/
DEFAULT_PAGE_SIZE ::
64 * 1024 when ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32 else
16 * 1024 when ODIN_OS == .Darwin && ODIN_ARCH == .arm64 else
4 * 1024
/*
Allocate memory.
This function allocates `size` bytes of memory, aligned to a boundary specified
by `alignment` using the allocator specified by `allocator`.
If the `size` parameter is `0`, the operation is a no-op.
**Inputs**:
- `size`: The desired size of the allocated memory region.
- `alignment`: The desired alignment of the allocated memory region.
- `allocator`: The allocator to allocate from.
**Returns**:
1. Pointer to the allocated memory, or `nil` if allocation failed.
2. Error, if the allocation failed.
**Errors**:
- `None`: If no error occurred.
- `Out_Of_Memory`: Occurs when the allocator runs out of space in any of its
backing buffers, the backing allocator has ran out of space, or an operating
system failure occurred.
- `Invalid_Argument`: If the supplied `size` is negative, alignment is not a
power of two.
*/
@(require_results)
alloc :: proc(
size: int,
alignment: int = DEFAULT_ALIGNMENT,
allocator := context.allocator,
loc := #caller_location,
) -> (rawptr, Allocator_Error) {
data, err := runtime.mem_alloc(size, alignment, allocator, loc)
return raw_data(data), err
}
/*
Allocate memory.
This function allocates `size` bytes of memory, aligned to a boundary specified
by `alignment` using the allocator specified by `allocator`.
**Inputs**:
- `size`: The desired size of the allocated memory region.
- `alignment`: The desired alignment of the allocated memory region.
- `allocator`: The allocator to allocate from.
**Returns**:
1. Slice of the allocated memory region, or `nil` if allocation failed.
2. Error, if the allocation failed.
**Errors**:
- `None`: If no error occurred.
- `Out_Of_Memory`: Occurs when the allocator runs out of space in any of its
backing buffers, the backing allocator has ran out of space, or an operating
system failure occurred.
- `Invalid_Argument`: If the supplied `size` is negative, alignment is not a
power of two.
*/
@(require_results)
alloc_bytes :: proc(
size: int,
alignment: int = DEFAULT_ALIGNMENT,
allocator := context.allocator,
loc := #caller_location,
) -> ([]byte, Allocator_Error) {
return runtime.mem_alloc(size, alignment, allocator, loc)
}
/*
Allocate non-zeroed memory.
This function allocates `size` bytes of memory, aligned to a boundary specified
by `alignment` using the allocator specified by `allocator`. This procedure
does not explicitly zero-initialize allocated memory region.
**Inputs**:
- `size`: The desired size of the allocated memory region.
- `alignment`: The desired alignment of the allocated memory region.
- `allocator`: The allocator to allocate from.
**Returns**:
1. Slice of the allocated memory region, or `nil` if allocation failed.
2. Error, if the allocation failed.
**Errors**:
- `None`: If no error occurred.
- `Out_Of_Memory`: Occurs when the allocator runs out of space in any of its
backing buffers, the backing allocator has ran out of space, or an operating
system failure occurred.
- `Invalid_Argument`: If the supplied `size` is negative, alignment is not a
power of two.
*/
@(require_results)
alloc_bytes_non_zeroed :: proc(
size: int,
alignment: int = DEFAULT_ALIGNMENT,
allocator := context.allocator,
loc := #caller_location,
) -> ([]byte, Allocator_Error) {
return runtime.mem_alloc_non_zeroed(size, alignment, allocator, loc)
}
/*
Free memory.
This procedure frees memory region located at the address, specified by `ptr`,
allocated from the allocator specified by `allocator`.
**Inputs**:
- `ptr`: Pointer to the memory region to free.
- `allocator`: The allocator to free to.
**Returns**:
- The error, if freeing failed.
**Errors**:
- `None`: When no error has occurred.
- `Invalid_Pointer`: The specified pointer is not owned by the specified allocator,
or does not point to a valid allocation.
- `Mode_Not_Implemented`: If the specified allocator does not support the `.Free`
mode.
*/
free :: proc(
ptr: rawptr,
allocator := context.allocator,
loc := #caller_location,
) -> Allocator_Error {
return runtime.mem_free(ptr, allocator, loc)
}
/*
Free a memory region.
This procedure frees `size` bytes of memory region located at the address,
specified by `ptr`, allocated from the allocator specified by `allocator`.
If the `size` parameter is `0`, this call is equivalent to `free()`.
**Inputs**:
- `ptr`: Pointer to the memory region to free.
- `size`: The size of the memory region to free.
- `allocator`: The allocator to free to.
**Returns**:
- The error, if freeing failed.
**Errors**:
- `None`: When no error has occurred.
- `Invalid_Pointer`: The specified pointer is not owned by the specified allocator,
or does not point to a valid allocation.
- `Mode_Not_Implemented`: If the specified allocator does not support the `.Free`
mode.
*/
free_with_size :: proc(
ptr: rawptr,
size: int,
allocator := context.allocator,
loc := #caller_location,
) -> Allocator_Error {
return runtime.mem_free_with_size(ptr, size, allocator, loc)
}
/*
Free a memory region.
This procedure frees memory region, specified by `bytes`, allocated from the
allocator specified by `allocator`.
If the length of the specified slice is zero, the `.Invalid_Argument` error
is returned.
**Inputs**:
- `bytes`: The memory region to free.
- `allocator`: The allocator to free to.
**Returns**:
- The error, if freeing failed.
**Errors**:
- `None`: When no error has occurred.
- `Invalid_Pointer`: The specified pointer is not owned by the specified allocator,
or does not point to a valid allocation.
- `Mode_Not_Implemented`: If the specified allocator does not support the `.Free`
mode.
*/
free_bytes :: proc(
bytes: []byte,
allocator := context.allocator,
loc := #caller_location,
) -> Allocator_Error {
return runtime.mem_free_bytes(bytes, allocator, loc)
}
/*
Free all allocations.
This procedure frees all allocations made on the allocator specified by
`allocator` to that allocator, making it available for further allocations.
**Inputs**:
- `allocator`: The allocator to free to.
**Errors**:
- `None`: When no error has occurred.
- `Mode_Not_Implemented`: If the specified allocator does not support the `.Free`
mode.
*/
free_all :: proc(allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
return runtime.mem_free_all(allocator, loc)
}
/*
Resize a memory region.
This procedure resizes a memory region, `old_size` bytes in size, located at
the address specified by `ptr`, such that it has a new size, specified by
`new_size` and and is aligned on a boundary specified by `alignment`.
If the `ptr` parameter is `nil`, `resize()` acts just like `alloc()`, allocating
`new_size` bytes, aligned on a boundary specified by `alignment`.
If the `new_size` parameter is `0`, `resize()` acts just like `free()`, freeing
the memory region `old_size` bytes in length, located at the address specified
by `ptr`.
If the `old_memory` pointer is not aligned to the boundary specified by
`alignment`, the procedure relocates the buffer such that the reallocated
buffer is aligned to the boundary specified by `alignment`.
**Inputs**:
- `ptr`: Pointer to the memory region to resize.
- `old_size`: Size of the memory region to resize.
- `new_size`: The desired size of the resized memory region.
- `alignment`: The desired alignment of the resized memory region.
- `allocator`: The owner of the memory region to resize.
**Returns**:
1. The pointer to the resized memory region, if successfull, `nil` otherwise.
2. Error, if resize failed.
**Errors**:
- `None`: No error.
- `Out_Of_Memory`: When the allocator's backing buffer or it's backing
allocator does not have enough space to fit in an allocation with the new
size, or an operating system failure occurs.
- `Invalid_Pointer`: The pointer referring to a memory region does not belong
to any of the allocators backing buffers or does not point to a valid start
of an allocation made in that allocator.
- `Invalid_Argument`: When `size` is negative, alignment is not a power of two,
or the `old_size` argument is incorrect.
- `Mode_Not_Implemented`: The allocator does not support the `.Realloc` mode.
**Note**: if `old_size` is `0` and `old_memory` is `nil`, this operation is a
no-op, and should not return errors.
*/
@(require_results)
resize :: proc(
ptr: rawptr,
old_size: int,
new_size: int,
alignment: int = DEFAULT_ALIGNMENT,
allocator := context.allocator,
loc := #caller_location,
) -> (rawptr, Allocator_Error) {
data, err := runtime.mem_resize(ptr, old_size, new_size, alignment, allocator, loc)
return raw_data(data), err
}
/*
Resize a memory region without zero-initialization.
This procedure resizes a memory region, `old_size` bytes in size, located at
the address specified by `ptr`, such that it has a new size, specified by
`new_size` and and is aligned on a boundary specified by `alignment`.
If the `ptr` parameter is `nil`, `resize()` acts just like `alloc()`, allocating
`new_size` bytes, aligned on a boundary specified by `alignment`.
If the `new_size` parameter is `0`, `resize()` acts just like `free()`, freeing
the memory region `old_size` bytes in length, located at the address specified
by `ptr`.
If the `old_memory` pointer is not aligned to the boundary specified by
`alignment`, the procedure relocates the buffer such that the reallocated
buffer is aligned to the boundary specified by `alignment`.
Unlike `resize()`, this procedure does not explicitly zero-initialize any new
memory.
**Inputs**:
- `ptr`: Pointer to the memory region to resize.
- `old_size`: Size of the memory region to resize.
- `new_size`: The desired size of the resized memory region.
- `alignment`: The desired alignment of the resized memory region.
- `allocator`: The owner of the memory region to resize.
**Returns**:
1. The pointer to the resized memory region, if successfull, `nil` otherwise.
2. Error, if resize failed.
**Errors**:
- `None`: No error.
- `Out_Of_Memory`: When the allocator's backing buffer or it's backing
allocator does not have enough space to fit in an allocation with the new
size, or an operating system failure occurs.
- `Invalid_Pointer`: The pointer referring to a memory region does not belong
to any of the allocators backing buffers or does not point to a valid start
of an allocation made in that allocator.
- `Invalid_Argument`: When `size` is negative, alignment is not a power of two,
or the `old_size` argument is incorrect.
- `Mode_Not_Implemented`: The allocator does not support the `.Realloc` mode.
**Note**: if `old_size` is `0` and `old_memory` is `nil`, this operation is a
no-op, and should not return errors.
*/
@(require_results)
resize_non_zeroed :: proc(
ptr: rawptr,
old_size: int,
new_size: int,
alignment: int = DEFAULT_ALIGNMENT,
allocator := context.allocator,
loc := #caller_location,
) -> (rawptr, Allocator_Error) {
data, err := runtime.non_zero_mem_resize(ptr, old_size, new_size, alignment, allocator, loc)
return raw_data(data), err
}
/*
Resize a memory region.
This procedure resizes a memory region, specified by `old_data`, such that it
has a new size, specified by `new_size` and and is aligned on a boundary
specified by `alignment`.
If the `old_data` parameter is `nil`, `resize_bytes()` acts just like
`alloc_bytes()`, allocating `new_size` bytes, aligned on a boundary specified
by `alignment`.
If the `new_size` parameter is `0`, `resize_bytes()` acts just like
`free_bytes()`, freeing the memory region specified by `old_data`.
If the `old_memory` pointer is not aligned to the boundary specified by
`alignment`, the procedure relocates the buffer such that the reallocated
buffer is aligned to the boundary specified by `alignment`.
**Inputs**:
- `old_data`: Pointer to the memory region to resize.
- `new_size`: The desired size of the resized memory region.
- `alignment`: The desired alignment of the resized memory region.
- `allocator`: The owner of the memory region to resize.
**Returns**:
1. The resized memory region, if successfull, `nil` otherwise.
2. Error, if resize failed.
**Errors**:
- `None`: No error.
- `Out_Of_Memory`: When the allocator's backing buffer or it's backing
allocator does not have enough space to fit in an allocation with the new
size, or an operating system failure occurs.
- `Invalid_Pointer`: The pointer referring to a memory region does not belong
to any of the allocators backing buffers or does not point to a valid start
of an allocation made in that allocator.
- `Invalid_Argument`: When `size` is negative, alignment is not a power of two,
or the `old_size` argument is incorrect.
- `Mode_Not_Implemented`: The allocator does not support the `.Realloc` mode.
**Note**: if `old_size` is `0` and `old_memory` is `nil`, this operation is a
no-op, and should not return errors.
*/
@(require_results)
resize_bytes :: proc(
old_data: []byte,
new_size: int,
alignment: int = DEFAULT_ALIGNMENT,
allocator := context.allocator,
loc := #caller_location,
) -> ([]byte, Allocator_Error) {
return runtime.mem_resize(raw_data(old_data), len(old_data), new_size, alignment, allocator, loc)
}
/*
Resize a memory region.
This procedure resizes a memory region, specified by `old_data`, such that it
has a new size, specified by `new_size` and and is aligned on a boundary
specified by `alignment`.
If the `old_data` parameter is `nil`, `resize_bytes()` acts just like
`alloc_bytes()`, allocating `new_size` bytes, aligned on a boundary specified
by `alignment`.
If the `new_size` parameter is `0`, `resize_bytes()` acts just like
`free_bytes()`, freeing the memory region specified by `old_data`.
If the `old_memory` pointer is not aligned to the boundary specified by
`alignment`, the procedure relocates the buffer such that the reallocated
buffer is aligned to the boundary specified by `alignment`.
Unlike `resize_bytes()`, this procedure does not explicitly zero-initialize
any new memory.
**Inputs**:
- `old_data`: Pointer to the memory region to resize.
- `new_size`: The desired size of the resized memory region.
- `alignment`: The desired alignment of the resized memory region.
- `allocator`: The owner of the memory region to resize.
**Returns**:
1. The resized memory region, if successfull, `nil` otherwise.
2. Error, if resize failed.
**Errors**:
- `None`: No error.
- `Out_Of_Memory`: When the allocator's backing buffer or it's backing
allocator does not have enough space to fit in an allocation with the new
size, or an operating system failure occurs.
- `Invalid_Pointer`: The pointer referring to a memory region does not belong
to any of the allocators backing buffers or does not point to a valid start
of an allocation made in that allocator.
- `Invalid_Argument`: When `size` is negative, alignment is not a power of two,
or the `old_size` argument is incorrect.
- `Mode_Not_Implemented`: The allocator does not support the `.Realloc` mode.
**Note**: if `old_size` is `0` and `old_memory` is `nil`, this operation is a
no-op, and should not return errors.
*/
@(require_results)
resize_bytes_non_zeroed :: proc(
old_data: []byte,
new_size: int,
alignment: int = DEFAULT_ALIGNMENT,
allocator := context.allocator,
loc := #caller_location,
) -> ([]byte, Allocator_Error) {
return runtime.non_zero_mem_resize(raw_data(old_data), len(old_data), new_size, alignment, allocator, loc)
}
/*
Query allocator features.
*/
@(require_results)
query_features :: proc(allocator: Allocator, loc := #caller_location) -> (set: Allocator_Mode_Set) {
if allocator.procedure != nil {
allocator.procedure(allocator.data, .Query_Features, 0, 0, &set, 0, loc)
return set
}
return nil
}
/*
Query allocator information.
*/
@(require_results)
query_info :: proc(
pointer: rawptr,
allocator: Allocator,
loc := #caller_location,
) -> (props: Allocator_Query_Info) {
props.pointer = pointer
if allocator.procedure != nil {
allocator.procedure(allocator.data, .Query_Info, 0, 0, &props, 0, loc)
}
return
}
/*
Free a string.
*/
delete_string :: proc(
str: string,
allocator := context.allocator,
loc := #caller_location,
) -> Allocator_Error {
return runtime.delete_string(str, allocator, loc)
}
/*
Free a cstring.
*/
delete_cstring :: proc(
str: cstring,
allocator := context.allocator,
loc := #caller_location,
) -> Allocator_Error {
return runtime.delete_cstring(str, allocator, loc)
}
/*
Free a dynamic array.
*/
delete_dynamic_array :: proc(
array: $T/[dynamic]$E,
loc := #caller_location,
) -> Allocator_Error {
return runtime.delete_dynamic_array(array, loc)
}
/*
Free a slice.
*/
delete_slice :: proc(
array: $T/[]$E,
allocator := context.allocator,
loc := #caller_location,
) -> Allocator_Error {
return runtime.delete_slice(array, allocator, loc)
}
/*
Free a map.
*/
delete_map :: proc(
m: $T/map[$K]$V,
loc := #caller_location,
) -> Allocator_Error {
return runtime.delete_map(m, loc)
}
/*
Free an SoA slice.
*/
delete_soa_slice :: proc(
array: $T/#soa[]$E,
allocator := context.allocator,
loc := #caller_location,
) -> Allocator_Error {
return runtime.delete_soa_slice(array, allocator, loc)
}
/*
Free an SoA dynamic array.
*/
delete_soa_dynamic_array :: proc(
array: $T/#soa[dynamic]$E,
loc := #caller_location,
) -> Allocator_Error {
return runtime.delete_soa_dynamic_array(array, loc)
}
/*
Free.
*/
delete :: proc{
delete_string,
delete_cstring,
delete_dynamic_array,
delete_slice,
delete_map,
delete_soa_slice,
delete_soa_dynamic_array,
}
/*
Allocate a new object.
This procedure allocates a new object of type `T` using an allocator specified
by `allocator`, and returns a pointer to the allocated object, if allocated
successfully, or `nil` otherwise.
*/
@(require_results)
new :: proc(
$T: typeid,
allocator := context.allocator,
loc := #caller_location,
) -> (^T, Allocator_Error) {
return new_aligned(T, align_of(T), allocator, loc)
}
/*
Allocate a new object with alignment.
This procedure allocates a new object of type `T` using an allocator specified
by `allocator`, and returns a pointer, aligned on a boundary specified by
`alignment` to the allocated object, if allocated successfully, or `nil`
otherwise.
*/
@(require_results)
new_aligned :: proc(
$T: typeid,
alignment: int,
allocator := context.allocator,
loc := #caller_location,
) -> (t: ^T, err: Allocator_Error) {
return runtime.new_aligned(T, alignment, allocator, loc)
}
/*
Allocate a new object and initialize it with a value.
This procedure allocates a new object of type `T` using an allocator specified
by `allocator`, and returns a pointer, aligned on a boundary specified by
`alignment` to the allocated object, if allocated successfully, or `nil`
otherwise. The allocated object is initialized with `data`.
*/
@(require_results)
new_clone :: proc(
data: $T,
allocator := context.allocator,
loc := #caller_location,
) -> (t: ^T, err: Allocator_Error) {
return runtime.new_clone(data, allocator, loc)
}
/*
Allocate a new slice with alignment.
This procedure allocates a new slice of type `T` with length `len`, aligned
on a boundary specified by `alignment` from an allocator specified by
`allocator`, and returns the allocated slice.
*/
@(require_results)
make_aligned :: proc(
$T: typeid/[]$E,
#any_int len: int,
alignment: int,
allocator := context.allocator,
loc := #caller_location,
) -> (slice: T, err: Allocator_Error) {
return runtime.make_aligned(T, len, alignment, allocator, loc)
}
/*
Allocate a new slice with alignment for allocators that might not support the
specified alignment requirement.
This procedure allocates a new slice of type `T` with length `len`, aligned
on a boundary specified by `alignment` from an allocator specified by
`allocator`, and returns the allocated slice.
The user should `delete` the return `original_data` slice not the typed `slice`.
*/
@(require_results)
make_over_aligned :: proc(
$T: typeid/[]$E,
#any_int len: int,
alignment: int,
allocator: runtime.Allocator,
loc := #caller_location,
) -> (slice: T, original_data: []byte, err: Allocator_Error) {
size := size_of(E)*len + alignment-1
original_data, err = runtime.make([]byte, size, allocator, loc)
if err == nil {
ptr := align_forward(raw_data(original_data), uintptr(alignment))
slice = ([^]E)(ptr)[:len]
}
return
}
/*
Allocate a new slice.
This procedure allocates a new slice of type `T` with length `len`, from an
allocator specified by `allocator`, and returns the allocated slice.
*/
@(require_results)
make_slice :: proc(
$T: typeid/[]$E,
#any_int len: int,
allocator := context.allocator,
loc := #caller_location,
) -> (T, Allocator_Error) {
return runtime.make_slice(T, len, allocator, loc)
}
/*
Allocate a dynamic array.
This procedure creates a dynamic array of type `T`, with `allocator` as its
backing allocator, and initial length and capacity of `0`.
*/
@(require_results)
make_dynamic_array :: proc(
$T: typeid/[dynamic]$E,
allocator := context.allocator,
loc := #caller_location,
) -> (T, Allocator_Error) {
return runtime.make_dynamic_array(T, allocator, loc)
}
/*
Allocate a dynamic array with initial length.
This procedure creates a dynamic array of type `T`, with `allocator` as its
backing allocator, and initial capacity and length specified by `len`.
*/
@(require_results)
make_dynamic_array_len :: proc(
$T: typeid/[dynamic]$E,
#any_int len: int,
allocator := context.allocator,
loc := #caller_location,
) -> (T, Allocator_Error) {
return runtime.make_dynamic_array_len(T, len, allocator, loc)
}
/*
Allocate a dynamic array with initial length and capacity.
This procedure creates a dynamic array of type `T`, with `allocator` as its
backing allocator, and initial capacity specified by `cap`, and initial length
specified by `len`.
*/
@(require_results)
make_dynamic_array_len_cap :: proc(
$T: typeid/[dynamic]$E,
#any_int len: int,
#any_int cap: int,
allocator := context.allocator,
loc := #caller_location,
) -> (array: T, err: Allocator_Error) {
return runtime.make_dynamic_array_len_cap(T, len, cap, allocator, loc)
}
/*
Create a map with no initial allocation.
This procedure creates a map of type `T` with no initial allocation, which will
use the allocator specified by `allocator` as its backing allocator when it
allocates.
*/
@(require_results)
make_map :: proc(
$T: typeid/map[$K]$E,
allocator := context.allocator,
loc := #caller_location,
) -> (m: T) {
return runtime.make_map(T, allocator, loc)
}
/*
Allocate a map.
This procedure creates a map of type `T` with initial capacity specified by
`cap`, that is using an allocator specified by `allocator` as its backing
allocator.
*/
@(require_results)
make_map_cap :: proc(
$T: typeid/map[$K]$E,
#any_int cap: int,
allocator := context.allocator,
loc := #caller_location,
) -> (m: T, err: Allocator_Error) {
return runtime.make_map_cap(T, cap, allocator, loc)
}
/*
Allocate a multi pointer.
This procedure allocates a multipointer of type `T` pointing to `len` elements,
from an allocator specified by `allocator`.
*/
@(require_results)
make_multi_pointer :: proc(
$T: typeid/[^]$E,
#any_int len: int,
allocator := context.allocator,
loc := #caller_location
) -> (mp: T, err: Allocator_Error) {
return runtime.make_multi_pointer(T, len, allocator, loc)
}
/*
Allocate an SoA slice.
This procedure allocates an SoA slice of type `T` with length `len`, from an
allocator specified by `allocator`, and returns the allocated SoA slice.
*/
@(require_results)
make_soa_slice :: proc(
$T: typeid/#soa[]$E,
#any_int len: int,
allocator := context.allocator,
loc := #caller_location
) -> (array: T, err: Allocator_Error) {
return runtime.make_soa_slice(T, len, allocator, loc)
}
/*
Allocate an SoA dynamic array.
This procedure creates an SoA dynamic array of type `T`, with `allocator` as
its backing allocator, and initial length and capacity of `0`.
*/
@(require_results)
make_soa_dynamic_array :: proc(
$T: typeid/#soa[dynamic]$E,
allocator := context.allocator,
loc := #caller_location
) -> (array: T, err: Allocator_Error) {
return runtime.make_soa_dynamic_array(T, allocator, loc)
}
/*
Allocate an SoA dynamic array with initial length.
This procedure creates an SoA dynamic array of type `T`, with `allocator` as its
backing allocator, and initial capacity and length specified by `len`.
*/
@(require_results)
make_soa_dynamic_array_len :: proc(
$T: typeid/#soa[dynamic]$E,
#any_int len: int,
allocator := context.allocator,
loc := #caller_location
) -> (array: T, err: Allocator_Error) {
return runtime.make_soa_dynamic_array_len(T, len, allocator, loc)
}
/*
Allocate an SoA dynamic array with initial length and capacity.
This procedure creates an SoA dynamic array of type `T`, with `allocator` as its
backing allocator, and initial capacity specified by `cap`, and initial length
specified by `len`.
*/
@(require_results)
make_soa_dynamic_array_len_cap :: proc(
$T: typeid/#soa[dynamic]$E,
#any_int len: int,
#any_int cap: int,
allocator := context.allocator,
loc := #caller_location
) -> (array: T, err: Allocator_Error) {
return runtime.make_soa_dynamic_array_len_cap(T, len, cap, allocator, loc)
}
/*
Allocate.
*/
make :: proc{
make_slice,
make_dynamic_array,
make_dynamic_array_len,
make_dynamic_array_len_cap,
make_map,
make_map_cap,
make_multi_pointer,
make_soa_slice,
make_soa_dynamic_array,
make_soa_dynamic_array_len,
make_soa_dynamic_array_len_cap,
}
/*
Default resize procedure.
When allocator does not support resize operation, but supports `.Alloc` and
`.Free`, this procedure is used to implement allocator's default behavior on
resize.
The behavior of the function is as follows:
- If `new_size` is `0`, the function acts like `free()`, freeing the memory
region of `old_size` bytes located at `old_memory`.
- If `old_memory` is `nil`, the function acts like `alloc()`, allocating
`new_size` bytes of memory aligned on a boundary specified by `alignment`.
- Otherwise, a new memory region of size `new_size` is allocated, then the
data from the old memory region is copied and the old memory region is
freed.
*/
@(require_results)
default_resize_align :: proc(
old_memory: rawptr,
old_size: int,
new_size: int,
alignment: int,
allocator := context.allocator,
loc := #caller_location,
) -> (res: rawptr, err: Allocator_Error) {
data: []byte
data, err = default_resize_bytes_align(
([^]byte) (old_memory)[:old_size],
new_size,
alignment,
allocator,
loc,
)
res = raw_data(data)
return
}
/*
Default resize procedure.
When allocator does not support resize operation, but supports
`.Alloc_Non_Zeroed` and `.Free`, this procedure is used to implement allocator's
default behavior on resize.
Unlike `default_resize_align` no new memory is being explicitly
zero-initialized.
The behavior of the function is as follows:
- If `new_size` is `0`, the function acts like `free()`, freeing the memory
region of `old_size` bytes located at `old_memory`.
- If `old_memory` is `nil`, the function acts like `alloc()`, allocating
`new_size` bytes of memory aligned on a boundary specified by `alignment`.
- Otherwise, a new memory region of size `new_size` is allocated, then the
data from the old memory region is copied and the old memory region is
freed.
*/
@(require_results)
default_resize_bytes_align_non_zeroed :: proc(
old_data: []byte,
new_size: int,
alignment: int,
allocator := context.allocator,
loc := #caller_location,
) -> ([]byte, Allocator_Error) {
return _default_resize_bytes_align(old_data, new_size, alignment, false, allocator, loc)
}
/*
Default resize procedure.
When allocator does not support resize operation, but supports `.Alloc` and
`.Free`, this procedure is used to implement allocator's default behavior on
resize.
The behavior of the function is as follows:
- If `new_size` is `0`, the function acts like `free()`, freeing the memory
region specified by `old_data`.
- If `old_data` is `nil`, the function acts like `alloc()`, allocating
`new_size` bytes of memory aligned on a boundary specified by `alignment`.
- Otherwise, a new memory region of size `new_size` is allocated, then the
data from the old memory region is copied and the old memory region is
freed.
*/
@(require_results)
default_resize_bytes_align :: proc(
old_data: []byte,
new_size: int,
alignment: int,
allocator := context.allocator,
loc := #caller_location,
) -> ([]byte, Allocator_Error) {
return _default_resize_bytes_align(old_data, new_size, alignment, true, allocator, loc)
}
@(require_results)
_default_resize_bytes_align :: #force_inline proc(
old_data: []byte,
new_size: int,
alignment: int,
should_zero: bool,
allocator := context.allocator,
loc := #caller_location,
) -> ([]byte, Allocator_Error) {
old_memory := raw_data(old_data)
old_size := len(old_data)
if old_memory == nil {
if should_zero {
return alloc_bytes(new_size, alignment, allocator, loc)
} else {
return alloc_bytes_non_zeroed(new_size, alignment, allocator, loc)
}
}
if new_size == 0 {
err := free_bytes(old_data, allocator, loc)
return nil, err
}
if new_size == old_size && is_aligned(old_memory, alignment) {
return old_data, .None
}
new_memory : []byte
err : Allocator_Error
if should_zero {
new_memory, err = alloc_bytes(new_size, alignment, allocator, loc)
} else {
new_memory, err = alloc_bytes_non_zeroed(new_size, alignment, allocator, loc)
}
if new_memory == nil || err != nil {
return nil, err
}
runtime.copy(new_memory, old_data)
free_bytes(old_data, allocator, loc)
return new_memory, err
}
|