Skip to content

Commit 08145b2

Browse files
committed
use heap.pageSize() when actually calculating array size/capacity
1 parent 28608e2 commit 08145b2

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

stable_array.zig

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ pub fn StableArrayAligned(comptime T: type, comptime alignment: u29) type {
2828
pub const VariableSlice = [*]align(alignment) T;
2929

3030
pub const k_sizeof: usize = if (alignment > @sizeOf(T)) alignment else @sizeOf(T);
31+
pub const page_size: usize = heap.pageSize();
3132

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

37+
pub fn pageSize(self: *Self) usize {
38+
_ = self;
39+
return Self.page_size;
40+
}
41+
3642
pub fn init(max_virtual_alloc_bytes: usize) Self {
37-
assert(@mod(max_virtual_alloc_bytes, heap.page_size_min) == 0); // max_virtual_alloc_bytes must be a multiple of heap.page_size_min
43+
assert(@mod(max_virtual_alloc_bytes, page_size) == 0); // max_virtual_alloc_bytes must be a multiple of page_size
3844
return Self{
3945
.items = &[_]T{},
4046
.capacity = 0,
@@ -341,7 +347,7 @@ pub fn StableArrayAligned(comptime T: type, comptime alignment: u29) type {
341347
}
342348

343349
fn calcBytesUsedForCapacity(capacity: usize) usize {
344-
return mem.alignForward(usize, k_sizeof * capacity, heap.page_size_min);
350+
return mem.alignForward(usize, k_sizeof * capacity, page_size);
345351
}
346352
};
347353
}
@@ -360,20 +366,22 @@ test "init" {
360366
assert(b.capacity == 0);
361367
assert(b.max_virtual_alloc_bytes == TEST_VIRTUAL_ALLOC_SIZE);
362368
b.deinit();
369+
370+
assert(a.pageSize() == b.pageSize());
363371
}
364372

365373
test "append" {
366374
var a = StableArray(u8).init(TEST_VIRTUAL_ALLOC_SIZE);
367375
try a.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
368-
assert(a.calcTotalUsedBytes() == heap.page_size_min);
376+
assert(a.calcTotalUsedBytes() == a.pageSize());
369377
for (a.items, 0..) |v, i| {
370378
assert(v == i);
371379
}
372380
a.deinit();
373381

374-
var b = StableArrayAligned(u8, heap.page_size_min).init(TEST_VIRTUAL_ALLOC_SIZE);
382+
var b = StableArrayAligned(u8, heap.pageSize()).init(TEST_VIRTUAL_ALLOC_SIZE);
375383
try b.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
376-
assert(b.calcTotalUsedBytes() == heap.page_size_min * 10);
384+
assert(b.calcTotalUsedBytes() == a.pageSize() * 10);
377385
for (b.items, 0..) |v, i| {
378386
assert(v == i);
379387
}
@@ -385,17 +393,17 @@ test "shrinkAndFree" {
385393
try a.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
386394
a.shrinkAndFree(5);
387395

388-
assert(a.calcTotalUsedBytes() == heap.page_size_min);
396+
assert(a.calcTotalUsedBytes() == a.pageSize());
389397
assert(a.items.len == 5);
390398
for (a.items, 0..) |v, i| {
391399
assert(v == i);
392400
}
393401
a.deinit();
394402

395-
var b = StableArrayAligned(u8, heap.page_size_min).init(TEST_VIRTUAL_ALLOC_SIZE);
403+
var b = StableArrayAligned(u8, heap.pageSize()).init(TEST_VIRTUAL_ALLOC_SIZE);
396404
try b.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
397405
b.shrinkAndFree(5);
398-
assert(b.calcTotalUsedBytes() == heap.page_size_min * 5);
406+
assert(b.calcTotalUsedBytes() == a.pageSize() * 5);
399407
assert(b.items.len == 5);
400408
for (b.items, 0..) |v, i| {
401409
assert(v == i);
@@ -405,7 +413,7 @@ test "shrinkAndFree" {
405413
var c = StableArrayAligned(u8, 2048).init(TEST_VIRTUAL_ALLOC_SIZE);
406414
try c.appendSlice(&[_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
407415
c.shrinkAndFree(5);
408-
assert(c.calcTotalUsedBytes() == heap.page_size_min * 3);
416+
assert(c.calcTotalUsedBytes() == a.pageSize() * 3);
409417
assert(c.capacity == 6);
410418
assert(c.items.len == 5);
411419
for (c.items, 0..) |v, i| {
@@ -427,10 +435,10 @@ test "resize" {
427435
}
428436

429437
test "out of memory" {
430-
var a = StableArrayAligned(u8, heap.page_size_min).init(TEST_VIRTUAL_ALLOC_SIZE);
438+
var a = StableArrayAligned(u8, heap.pageSize()).init(TEST_VIRTUAL_ALLOC_SIZE);
431439
defer a.deinit();
432440

433-
const max_capacity: usize = TEST_VIRTUAL_ALLOC_SIZE / heap.page_size_min;
441+
const max_capacity: usize = TEST_VIRTUAL_ALLOC_SIZE / a.pageSize();
434442
try a.appendNTimes(0xFF, max_capacity);
435443
for (a.items) |v| {
436444
assert(v == 0xFF);
@@ -465,8 +473,8 @@ test "growing retains values" {
465473
var a = StableArray(u8).init(TEST_VIRTUAL_ALLOC_SIZE);
466474
defer a.deinit();
467475

468-
try a.resize(heap.page_size_min);
476+
try a.resize(a.pageSize());
469477
a.items[0] = 0xFF;
470-
try a.resize(heap.page_size_min * 2);
478+
try a.resize(a.pageSize() * 2);
471479
assert(a.items[0] == 0xFF);
472480
}

0 commit comments

Comments
 (0)