-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.zig
679 lines (576 loc) · 25.4 KB
/
lib.zig
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
const std = @import("std");
const helpers = @import("helpers.zig");
const assert = std.debug.assert;
pub fn Interface(comptime T: type) type {
return struct {
const required = struct {
/// Attempt to allocate exactly `len` bytes aligned to `1 << ptr_align`.
///
/// `ret_addr` is optionally provided as the first return address of the
/// allocation call stack. If the value is `0` it means no return address
/// has been provided.
const alloc = fn (self: *T, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8;
/// 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`. `buf_align` must equal the same value that was passed as the
/// `ptr_align` parameter to the original `alloc` call.
///
/// 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.
///
/// `new_len` must be greater than zero.
///
/// `ret_addr` is optionally provided as the first return address of the
/// allocation call stack. If the value is `0` it means no return address
/// has been provided.
const resize = fn (self: *T, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool;
/// Free and invalidate a buffer.
///
/// `buf.len` must equal the most recent length returned by `alloc` or
/// given to a successful `resize` call.
///
/// `buf_align` must equal the same value that was passed as the
/// `ptr_align` parameter to the original `alloc` call.
///
/// `ret_addr` is optionally provided as the first return address of the
/// allocation call stack. If the value is `0` it means no return address
/// has been provided.
const free = fn (self: *T, buf: []u8, buf_align: u8, ret_addr: usize) void;
};
const optional = struct {
usingnamespace if (@hasDecl(T, "init")) struct {
/// initialise an allocator
pub const init = fn () T;
comptime {
assert(init == @TypeOf(T.init));
}
} else struct {};
usingnamespace if (@hasDecl(T, "initInPlace")) struct {
pub const initInPlace = fn (self: *T) void;
comptime {
assert(initInPlace == @TypeOf(T.initInPlace));
}
} else struct {};
usingnamespace if (@hasDecl(T, "default_init")) struct {
pub const default_init = T.default_init;
comptime {
assert(@TypeOf(default_init) == T);
}
} else struct {};
usingnamespace if (@hasDecl(T, "initExtra")) struct {
pub const initExtra = @TypeOf(T.initExtra);
comptime {
assert(@typeInfo(initExtra).Fn.return_type == T);
}
} else struct {};
usingnamespace if (@hasDecl(T, "initInPlaceExtra")) struct {
pub const initInPlaceExtra = @TypeOf(T.initInPlaceExtra);
comptime {
assert(@typeInfo(initInPlaceExtra).Fn.params[0].type == *T);
assert(@typeInfo(initInPlaceExtra).Fn.return_type == void);
}
} else struct {};
/// Checks whether the allocator owns the memory for `buf`.
/// Composite allocators will generally pass `buf` to the underlying
/// allocators, so it is not advisable to have composite allocators
/// share backing allocators that define `owns`
const owns = fn (self: *T, buf: []u8) bool;
/// Attempt to return all remaining memory available to the allocator, or
/// return null, if there isn't any.
const allocAll = fn (self: *T) ?[]u8;
/// Free all allocated memory.
const freeAll = fn (self: *T) void;
};
};
}
pub fn allocator(a: anytype) std.mem.Allocator {
const Self = @typeInfo(@TypeOf(a)).Pointer.child;
comptime {
validateAllocator(Self);
}
return .{
.ptr = a,
.vtable = &.{
.alloc = &struct {
fn alloc(ptr: *anyopaque, len: usize, log2_ptr_align: u8, ret_addr: usize) ?[*]u8 {
const self: *Self = @ptrCast(@alignCast(ptr));
return self.alloc(len, log2_ptr_align, ret_addr);
}
}.alloc,
.resize = &struct {
fn resize(ptr: *anyopaque, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
const self: *Self = @ptrCast(@alignCast(ptr));
return self.resize(buf, log2_buf_align, new_len, ret_addr);
}
}.resize,
.free = &struct {
fn free(ptr: *anyopaque, buf: []u8, log2_buf_align: u8, ret_addr: usize) void {
const self: *Self = @ptrCast(@alignCast(ptr));
self.free(buf, log2_buf_align, ret_addr);
}
}.free,
},
};
}
pub fn validateAllocator(comptime T: type) void {
comptime {
for (std.meta.declarations(Interface(T).required)) |decl| {
const E = @field(Interface(T).required, decl.name);
if (!@hasDecl(T, decl.name)) {
@compileError("type " ++ @typeName(T) ++ " must have declaration " ++ decl.name ++
" of type " ++ @typeName(T));
}
const D = @TypeOf(@field(T, decl.name));
if (D != E) {
@compileError("declaration " ++ decl.name ++ " in type " ++ @typeName(T) ++
" is expected to have type " ++ @typeName(E) ++ ", found " ++ @typeName(D));
}
}
for (std.meta.declarations(Interface(T).optional)) |decl| {
if (@hasDecl(T, decl.name)) {
if (std.mem.startsWith(u8, decl.name, "usingnamespace_")) {
// BUG: this doesn't work, there seems to be limitations/bugs in @typeInfo
// that prevent getting the decls of an included namespace
// (the info.decls slice below always has length 0.
const ns = @field(Interface(T).optional, decl.name);
const info = @typeInfo(ns).Struct;
if (info.decls.len == 0) continue;
const E = @field(ns, info.decls[0].name);
const D = @TypeOf(@field(@field(T, decl.name), info.decls[0].name));
if (D != E) {
@compileError("declaration " ++ decl.name ++ " in type " ++ @typeName(T) ++
" is expected to have type " ++ @typeName(E) ++ ", found " ++ @typeName(D));
}
} else {
const E = @field(Interface(T).optional, decl.name);
const D = @TypeOf(@field(T, decl.name));
if (D != E) {
@compileError("declaration " ++ decl.name ++ " in type " ++ @typeName(T) ++
" is expected to have type " ++ @typeName(E) ++ ", found " ++ @typeName(D));
}
}
}
}
}
}
pub const Std = struct {
a: std.mem.Allocator,
pub fn initExtra(a: std.mem.Allocator) Std {
return Std{ .a = a };
}
pub fn alloc(self: *Std, len: usize, log2_ptr_align: u8, ret_addr: usize) ?[*]u8 {
return self.a.rawAlloc(len, log2_ptr_align, ret_addr);
}
pub fn resize(self: *Std, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
return self.a.rawResize(buf, log2_buf_align, new_len, ret_addr);
}
pub fn free(self: *Std, buf: []u8, log2_buf_align: u8, ret_addr: usize) void {
return self.a.rawFree(buf, log2_buf_align, ret_addr);
}
};
pub const Null = struct {
pub fn alloc(_: *Null, _: usize, _: u8, _: usize) ?[*]u8 {
return null;
}
pub fn resize(_: *Null, buf: []u8, _: u8, new_len: usize, _: usize) bool {
assert(buf.len == 0);
return new_len == 0;
}
pub fn free(_: *Null, buf: []u8, _: u8, _: usize) void {
assert(buf.len == 0);
}
pub fn allocAll(_: *Null) ?[]u8 {
return null;
}
pub fn freeAll(_: *Null) void {}
};
pub const FixedBuffer = struct {
buffer: []u8,
len: usize,
const Self = @This();
fn isLastAllocation(self: Self, buf: []u8) bool {
return self.buffer.ptr + self.len == buf.ptr + buf.len;
}
pub fn alloc(self: *Self, len: usize, log2_ptr_align: u8, ret_addr: usize) ?[*]u8 {
_ = ret_addr;
const ptr_align = @as(usize, 1) << @intCast(log2_ptr_align);
const align_offset = std.mem.alignPointerOffset(self.buffer.ptr + self.len, ptr_align) orelse return null;
const start_index = self.len + align_offset;
const new_len = start_index + len;
if (new_len > self.buffer.len) return null;
self.len = new_len;
return self.buffer.ptr + start_index;
}
pub fn resize(self: *Self, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
_ = log2_buf_align;
_ = ret_addr;
assert(self.owns(buf));
if (!self.isLastAllocation(buf)) {
if (new_len > buf.len) return false;
return true;
}
if (new_len <= buf.len) {
self.len -= buf.len - new_len;
return true;
}
const new_total_len = self.len + (new_len - buf.len);
if (new_total_len > self.buffer.len) return false;
self.len = new_total_len;
return true;
}
pub fn free(self: *Self, buf: []u8, log2_buf_align: u8, ret_addr: usize) void {
_ = log2_buf_align;
_ = ret_addr;
assert(self.owns(buf));
if (self.isLastAllocation(buf)) {
self.len -= buf.len;
}
}
pub fn owns(self: *Self, buf: []u8) bool {
return sliceContainsSlice(self.buffer, buf);
}
pub fn allocAll(self: *Self) ?[]u8 {
if (self.len == self.buffer.len) return null;
self.len = self.buffer.len;
return self.buffer[self.len..self.buffer.len];
}
pub fn freeAll(self: *Self) void {
self.len = 0;
}
};
pub fn Fallback(comptime PrimaryAllocator: type, comptime FallbackAllocator: type) type {
comptime {
validateAllocator(PrimaryAllocator);
validateAllocator(FallbackAllocator);
}
return struct {
primary: PrimaryAllocator,
fallback: FallbackAllocator,
const Self = @This();
pub fn alloc(self: *Self, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
return self.primary.alloc(len, ptr_align, ret_addr) orelse {
return self.fallback.alloc(len, ptr_align, ret_addr);
};
}
pub fn resize(self: *Self, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool {
if (self.primary.owns(buf)) {
return self.primary.resize(buf, buf_align, new_len, ret_addr);
} else {
return self.fallback.resize(buf, buf_align, new_len, ret_addr);
}
}
pub fn free(self: *Self, buf: []u8, buf_align: u8, ret_addr: usize) void {
if (self.primary.owns(buf)) {
self.primary.free(buf, buf_align, ret_addr);
} else {
self.fallback.free(buf, buf_align, ret_addr);
}
}
pub usingnamespace if (helpers.hasInit2(PrimaryAllocator, FallbackAllocator)) struct {
pub const init = helpers.init2(Self, "primary", PrimaryAllocator, "fallback", FallbackAllocator);
} else struct {};
pub usingnamespace if (helpers.hasDefaultInit2(PrimaryAllocator, FallbackAllocator)) struct {
pub const default_init = helpers.defaultInit2(Self, "primary", PrimaryAllocator, "fallback", FallbackAllocator);
} else struct {};
pub usingnamespace if (helpers.hasInitInPlace2(PrimaryAllocator, FallbackAllocator)) struct {
pub const initInPlace = helpers.initInPlace2(Self, "primary", PrimaryAllocator, "fallback", FallbackAllocator);
} else struct {};
pub usingnamespace if (helpers.hasInitExtra2(PrimaryAllocator, FallbackAllocator)) struct {
pub const initExtra = helpers.initExtra2(Self, "primary", PrimaryAllocator, "fallback", FallbackAllocator);
} else struct {};
pub usingnamespace if (helpers.hasInitInPlaceExtra2(PrimaryAllocator, FallbackAllocator)) struct {
pub const initInPlaceExtra = helpers.initInPlaceExtra2(Self, "primary", PrimaryAllocator, "fallback", FallbackAllocator);
} else struct {};
pub usingnamespace if (@hasDecl(FallbackAllocator, "owns")) struct {
pub fn owns(self: *Self, buf: []u8) bool {
return self.primary.owns(buf) or self.fallback.owns(buf);
}
} else struct {};
pub usingnamespace if (@hasDecl(PrimaryAllocator, "freeAll") and @hasDecl(FallbackAllocator, "freeAll")) struct {
pub fn freeAll(self: *Self) void {
self.primary.freeAll();
self.fallback.freeAll();
}
} else struct {};
};
}
fn sliceContainsSlice(slice: []u8, other: []u8) bool {
return @intFromPtr(slice.ptr) <= @intFromPtr(other.ptr) and
@intFromPtr(slice.ptr) + slice.len >= @intFromPtr(other.ptr) + other.len;
}
pub fn Stack(comptime capacity: usize) type {
return struct {
buffer: [capacity]u8 = undefined,
fba: FixedBuffer,
const Self = @This();
pub fn initInPlace(self: *Self) void {
self.fba = FixedBuffer{ .buffer = &self.buffer, .len = 0 };
}
pub fn alloc(self: *Self, len: usize, log2_ptr_align: u8, ret_addr: usize) ?[*]u8 {
return self.fba.alloc(len, log2_ptr_align, ret_addr);
}
pub fn resize(self: *Self, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
return self.fba.resize(buf, log2_buf_align, new_len, ret_addr);
}
pub fn free(self: *Self, buf: []u8, log2_buf_align: u8, ret_addr: usize) void {
self.fba.free(buf, log2_buf_align, ret_addr);
}
pub fn owns(self: *Self, buf: []u8) bool {
return self.fba.owns(buf);
}
pub fn allocAll(self: *Self) ?[]u8 {
return self.fba.allocAll();
}
pub fn freeAll(self: *Self) void {
self.fba.freeAll();
}
};
}
pub fn FreeList(
comptime BackingAllocator: type,
comptime block_size: usize,
comptime alloc_count: usize, // number of blocks to allocate at a time,
comptime max_list_size: ?usize,
) type {
comptime {
validateAllocator(BackingAllocator);
}
return struct {
free_list: std.SinglyLinkedList(void),
free_size: usize,
backing_allocator: BackingAllocator,
const Self = @This();
const Node = std.SinglyLinkedList(void).Node;
const log2_block_align = @ctz(block_size);
const block_align = 1 << log2_block_align;
comptime {
assert(alloc_count != 0);
assert(@sizeOf(Node) <= block_size);
assert(block_size % block_align == 0);
assert(@alignOf(Node) <= block_align);
}
fn addBlocksToFreeList(self: *Self, blocks: []align(block_align) [block_size]u8) void {
var i: usize = blocks.len;
while (i > 0) : (i -= 1) {
var node: *Node = @ptrCast(@alignCast(&blocks[i - 1]));
self.free_list.prepend(node);
}
self.free_size += blocks.len;
}
pub fn alloc(self: *Self, len: usize, log2_ptr_align: u8, ret_addr: usize) ?[*]u8 {
// TODO: should we check len < block_size and/or check if ptr_align == block_size
// to try and avoid gcd calculation?
// blocks are always aligned to block_size, so the requested alignment
// must divide block_size
assert(block_align >= (@as(usize, 1) << @intCast(log2_ptr_align)));
assert(len <= block_size);
if (self.free_list.popFirst()) |node| {
self.free_size -= 1;
return @ptrCast(node);
}
if (alloc_count > 1 and if (max_list_size) |max| self.free_size + alloc_count - 1 < max else true) {
const ptr = self.backing_allocator.alloc(block_size * alloc_count, block_align, ret_addr);
const block_ptr: [*]align(block_align)[block_size]u8 = @ptrCast(@alignCast(ptr));
const blocks = block_ptr[1..alloc_count];
self.addBlocksToFreeList(blocks);
return ptr;
} else {
return self.backing_allocator.alloc(block_size * alloc_count, block_align, ret_addr);
}
}
pub fn resize(self: *Self, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
_ = self;
_ = buf;
_ = log2_buf_align;
_ = ret_addr;
return new_len <= block_size;
}
pub fn free(self: *Self, buf: []u8, log2_buf_align: u8, ret_addr: usize) void {
assert(block_align >= (@as(usize, 1) << @intCast(log2_buf_align)));
assert(block_align % (@as(usize, 1) << @intCast(log2_buf_align)) == 0);
if (max_list_size == null or self.free_size < max_list_size.?) {
const node: *align(block_align) Node = @ptrCast(@alignCast(buf.ptr));
self.free_list.prepend(node);
self.free_size += 1;
return;
} else {
self.backing_allocator.free(buf, log2_buf_align, ret_addr);
}
}
pub fn freeAll(self: *Self) void {
while (self.free_list.popFirst()) |node| {
const casted_ptr: *align(block_align) [block_size]u8 = @alignCast(@ptrCast(node));
self.backing_allocator.free(casted_ptr, log2_block_align, @returnAddress());
}
}
pub usingnamespace if (@hasDecl(BackingAllocator, "init")) struct {
pub fn init() Self {
return Self{
.free_list = .{ .first = null },
.free_size = 0,
.backing_allocator = BackingAllocator.init(),
};
}
} else struct {};
pub usingnamespace if (@hasDecl(BackingAllocator, "initExtra")) struct {
pub fn initExtra(args: std.meta.ArgsTuple(@TypeOf(BackingAllocator.initExtra))) Self {
return Self{
.free_list = .{ .first = null },
.free_size = 0,
.backing_allocator = @call(.auto, BackingAllocator.initExtra, args),
};
}
} else struct {};
pub usingnamespace if (@hasDecl(BackingAllocator, "initInPlace")) struct {
pub fn initInPlace(self: *Self) void {
self.free_list = .{ .first = null };
self.free_size = 0;
self.backing_allocator.initInPlace();
}
} else struct {};
pub usingnamespace if (@hasDecl(BackingAllocator, "initInPlaceExtra")) struct {
pub fn initInPlaceExtra(self: *Self, args: helpers.ArgsIIPE(@TypeOf(BackingAllocator.initInPlaceExtra))) void {
self.free_list = .{ .first = null };
self.free_size = 0;
@call(.auto, self.backing_allocator.initInPlaceExtra, .{&self.backing_allocator} ++ args);
}
} else struct {};
pub usingnamespace if (@hasDecl(BackingAllocator, "default_init")) struct {
pub fn init() Self {
return Self{
.free_list = .{ .first = null },
.free_size = 0,
.backing_allocator = BackingAllocator.default_init,
};
}
} else struct {};
};
}
pub fn Segregated(
comptime SmallAllocator: type,
comptime LargeAllocator: type,
comptime threshold: usize, // largest size to use the small allocator for
) type {
comptime {
validateAllocator(SmallAllocator);
validateAllocator(LargeAllocator);
}
return struct {
small_allocator: SmallAllocator,
large_allocator: LargeAllocator,
const Self = @This();
pub fn alloc(self: *Self, len: usize, log2_ptr_align: u8, ret_addr: usize) ?[*]u8 {
return if (len <= threshold)
self.small_allocator.alloc(len, log2_ptr_align, ret_addr)
else
self.large_allocator.alloc(len, log2_ptr_align, ret_addr);
}
pub fn resize(self: *Self, buf: []u8, log2_buf_align: u8, new_len: usize, ret_addr: usize) bool {
return if (buf.len <= threshold and new_len <= threshold)
self.small_allocator.resize(buf, log2_buf_align, new_len, ret_addr)
else if (buf.len > threshold and new_len > threshold)
self.large_allocator.resize(buf, log2_buf_align, new_len, ret_addr)
else
false;
}
pub fn free(self: *Self, buf: []u8, log2_buf_align: u8, ret_addr: usize) void {
if (buf.len <= threshold) {
self.small_allocator.free(buf, log2_buf_align, ret_addr);
} else {
self.large_allocator.free(buf, log2_buf_align, ret_addr);
}
}
pub usingnamespace if (helpers.hasInit2(SmallAllocator, LargeAllocator)) struct {
pub const init = helpers.init2(
Self,
"small_allocator",
SmallAllocator,
"large_allocator",
LargeAllocator,
);
} else struct {};
pub usingnamespace if (helpers.hasDefaultInit2(SmallAllocator, LargeAllocator)) struct {
pub const default_init = helpers.defaultInit2(
Self,
"small_allocator",
SmallAllocator,
"large_allocator",
LargeAllocator,
);
} else struct {};
pub usingnamespace if (helpers.hasInitInPlace2(SmallAllocator, LargeAllocator)) struct {
pub const initInPlace = helpers.initInPlace2(
Self,
"small_allocator",
SmallAllocator,
"large_allocator",
LargeAllocator,
);
} else struct {};
pub usingnamespace if (helpers.hasInitExtra2(SmallAllocator, LargeAllocator)) struct {
pub const initExtra = helpers.initExtra2(
Self,
"small_allocator",
SmallAllocator,
"large_allocator",
LargeAllocator,
);
} else struct {};
pub usingnamespace if (helpers.hasInitInPlaceExtra2(SmallAllocator, LargeAllocator)) struct {
pub const initInPlaceExtra = helpers.initInPlaceExtra2(
Self,
"small_allocator",
SmallAllocator,
"large_allocator",
LargeAllocator,
);
} else struct {};
pub usingnamespace if (@hasDecl(SmallAllocator, "owns") and @hasDecl(LargeAllocator, "owns")) struct {
pub fn owns(self: *Self, buf: []u8) bool {
return if (buf.len <= threshold)
self.small_allocator.owns(buf)
else
self.large_allocator.owns(buf);
}
} else struct {};
pub usingnamespace if (@hasDecl(SmallAllocator, "freeAll") and @hasDecl(LargeAllocator, "freeAll")) struct {
pub fn freeAll(self: *Self) void {
self.small_allocator.freeAll();
self.large_allocator.freeAll();
}
} else struct {};
};
}
test {
const StackFallback = Fallback(Stack(1024), Std);
const StackSegregatedFallback = Fallback(
Stack(1024),
Segregated(
FreeList(FixedBuffer, 64, 2, null),
Std,
64,
),
);
comptime {
validateAllocator(Null);
validateAllocator(FixedBuffer);
validateAllocator(StackFallback);
validateAllocator(StackSegregatedFallback);
}
std.testing.refAllDecls(@This());
std.testing.refAllDecls(Fallback(FixedBuffer, Null));
std.testing.refAllDecls(Stack(1024));
std.testing.refAllDecls(FreeList(Stack(1024), 64, 1, null));
std.testing.refAllDecls(FreeList(Stack(1024), 8, 1, 32));
std.testing.refAllDecls(StackFallback);
std.testing.refAllDecls(StackSegregatedFallback);
var test_allocator: Fallback(Stack(1024), Std) = undefined;
test_allocator.initInPlaceExtra(.{std.testing.allocator});
const a = allocator(&test_allocator);
try std.heap.testAllocator(a); // the first realloc makes the test fail
try std.heap.testAllocatorAligned(a);
try std.heap.testAllocatorAlignedShrink(a);
try std.heap.testAllocatorLargeAlignment(a);
}