Skip to content

Commit 53458d7

Browse files
committed
avoid hardcoding shrinkAndFree test page sizes, alignment getter
1 parent dd98880 commit 53458d7

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

stable_array.zig

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn StableArray(comptime T: type) type {
1616
return StableArrayAligned(T, @alignOf(T));
1717
}
1818

19-
pub fn StableArrayAligned(comptime T: type, comptime alignment: u29) type {
19+
pub fn StableArrayAligned(comptime T: type, comptime _alignment: u29) type {
2020
if (@sizeOf(T) == 0) {
2121
@compileError("StableArray does not support types of size 0. Use ArrayList instead.");
2222
}
@@ -29,16 +29,22 @@ pub fn StableArrayAligned(comptime T: type, comptime alignment: u29) type {
2929

3030
pub const k_sizeof: usize = if (alignment > @sizeOf(T)) alignment else @sizeOf(T);
3131
pub const page_size: usize = heap.pageSize();
32+
pub const alignment = _alignment;
3233

3334
items: Slice,
3435
capacity: usize,
3536
max_virtual_alloc_bytes: usize,
3637

37-
pub fn pageSize(self: *Self) usize {
38+
pub fn getPageSize(self: *Self) usize {
3839
_ = self;
3940
return Self.page_size;
4041
}
4142

43+
pub fn getAlignment(self: *Self) usize {
44+
_ = self;
45+
return Self.alignment;
46+
}
47+
4248
pub fn init(max_virtual_alloc_bytes: usize) Self {
4349
assert(@mod(max_virtual_alloc_bytes, page_size) == 0); // max_virtual_alloc_bytes must be a multiple of page_size
4450
return Self{
@@ -362,38 +368,40 @@ test "init" {
362368
a.deinit();
363369

364370
var b = StableArrayAligned(u8, 16).init(TEST_VIRTUAL_ALLOC_SIZE);
371+
assert(b.getAlignment() == 16);
365372
assert(b.items.len == 0);
366373
assert(b.capacity == 0);
367374
assert(b.max_virtual_alloc_bytes == TEST_VIRTUAL_ALLOC_SIZE);
368375
b.deinit();
369376

370-
assert(a.pageSize() == b.pageSize());
377+
assert(a.getPageSize() == b.getPageSize());
371378
}
372379

373380
test "append" {
374381
var a = StableArray(u8).init(TEST_VIRTUAL_ALLOC_SIZE);
375382
try a.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
376-
assert(a.calcTotalUsedBytes() == a.pageSize());
383+
assert(a.calcTotalUsedBytes() == a.getPageSize());
377384
for (a.items, 0..) |v, i| {
378385
assert(v == i);
379386
}
380387
a.deinit();
381388

382389
var b = StableArrayAligned(u8, heap.pageSize()).init(TEST_VIRTUAL_ALLOC_SIZE);
383390
try b.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
384-
assert(b.calcTotalUsedBytes() == a.pageSize() * 10);
391+
assert(b.calcTotalUsedBytes() == a.getPageSize() * 10);
385392
for (b.items, 0..) |v, i| {
386393
assert(v == i);
387394
}
388395
b.deinit();
389396
}
390397

391398
test "shrinkAndFree" {
399+
const page_size = heap.pageSize();
400+
392401
var a = StableArray(u8).init(TEST_VIRTUAL_ALLOC_SIZE);
393402
try a.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
394403
a.shrinkAndFree(5);
395-
396-
assert(a.calcTotalUsedBytes() == a.pageSize());
404+
assert(a.calcTotalUsedBytes() == page_size); // still using only a page
397405
assert(a.items.len == 5);
398406
for (a.items, 0..) |v, i| {
399407
assert(v == i);
@@ -403,17 +411,18 @@ test "shrinkAndFree" {
403411
var b = StableArrayAligned(u8, heap.pageSize()).init(TEST_VIRTUAL_ALLOC_SIZE);
404412
try b.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
405413
b.shrinkAndFree(5);
406-
assert(b.calcTotalUsedBytes() == a.pageSize() * 5);
414+
assert(b.calcTotalUsedBytes() == page_size * 5); // alignment of each item is 1 page
407415
assert(b.items.len == 5);
408416
for (b.items, 0..) |v, i| {
409417
assert(v == i);
410418
}
411419
b.deinit();
412420

413-
var c = StableArrayAligned(u8, 2048).init(TEST_VIRTUAL_ALLOC_SIZE);
421+
var c = StableArrayAligned(u8, page_size / 2).init(TEST_VIRTUAL_ALLOC_SIZE);
422+
assert(c.getAlignment() == page_size / 2);
414423
try c.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
415424
c.shrinkAndFree(5);
416-
assert(c.calcTotalUsedBytes() == a.pageSize() * 3);
425+
assert(c.calcTotalUsedBytes() == page_size * 3);
417426
assert(c.capacity == 6);
418427
assert(c.items.len == 5);
419428
for (c.items, 0..) |v, i| {
@@ -438,7 +447,7 @@ test "out of memory" {
438447
var a = StableArrayAligned(u8, heap.pageSize()).init(TEST_VIRTUAL_ALLOC_SIZE);
439448
defer a.deinit();
440449

441-
const max_capacity: usize = TEST_VIRTUAL_ALLOC_SIZE / a.pageSize();
450+
const max_capacity: usize = TEST_VIRTUAL_ALLOC_SIZE / a.getPageSize();
442451
try a.appendNTimes(0xFF, max_capacity);
443452
for (a.items) |v| {
444453
assert(v == 0xFF);
@@ -473,8 +482,8 @@ test "growing retains values" {
473482
var a = StableArray(u8).init(TEST_VIRTUAL_ALLOC_SIZE);
474483
defer a.deinit();
475484

476-
try a.resize(a.pageSize());
485+
try a.resize(a.getPageSize());
477486
a.items[0] = 0xFF;
478-
try a.resize(a.pageSize() * 2);
487+
try a.resize(a.getPageSize() * 2);
479488
assert(a.items[0] == 0xFF);
480489
}

0 commit comments

Comments
 (0)