Skip to content

Commit f894011

Browse files
Fix linux aarch64 build error (#13)
* heap.page_size isn't available at comptime, so we'll store the value once on `init()` as a struct member and reuse it in local calculations.
1 parent 9cdccbb commit f894011

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

stable_array.zig

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ 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();
3231
pub const alignment = _alignment;
3332

3433
items: Slice,
3534
capacity: usize,
3635
max_virtual_alloc_bytes: usize,
36+
page_size: usize,
3737

3838
pub fn getPageSize(self: *Self) usize {
39-
_ = self;
40-
return Self.page_size;
39+
return self.page_size;
4140
}
4241

4342
pub fn getAlignment(self: *Self) usize {
@@ -46,11 +45,13 @@ pub fn StableArrayAligned(comptime T: type, comptime _alignment: u29) type {
4645
}
4746

4847
pub fn init(max_virtual_alloc_bytes: usize) Self {
48+
const page_size = heap.pageSize();
4949
assert(@mod(max_virtual_alloc_bytes, page_size) == 0); // max_virtual_alloc_bytes must be a multiple of page_size
5050
return Self{
5151
.items = &[_]T{},
5252
.capacity = 0,
5353
.max_virtual_alloc_bytes = max_virtual_alloc_bytes,
54+
.page_size = page_size,
5455
};
5556
}
5657

@@ -208,8 +209,8 @@ pub fn StableArrayAligned(comptime T: type, comptime _alignment: u29) type {
208209
pub fn shrinkAndFree(self: *Self, new_len: usize) void {
209210
assert(new_len <= self.items.len);
210211

211-
const new_capacity_bytes = calcBytesUsedForCapacity(new_len);
212-
const current_capacity_bytes: usize = calcBytesUsedForCapacity(self.capacity);
212+
const new_capacity_bytes = self.calcBytesUsedForCapacity(new_len);
213+
const current_capacity_bytes: usize = self.calcBytesUsedForCapacity(self.capacity);
213214

214215
if (new_capacity_bytes < current_capacity_bytes) {
215216
const bytes_to_free: usize = current_capacity_bytes - new_capacity_bytes;
@@ -268,8 +269,8 @@ pub fn StableArrayAligned(comptime T: type, comptime _alignment: u29) type {
268269
}
269270

270271
pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) AllocError!void {
271-
const new_capacity_bytes = calcBytesUsedForCapacity(new_capacity);
272-
const current_capacity_bytes: usize = calcBytesUsedForCapacity(self.capacity);
272+
const new_capacity_bytes = self.calcBytesUsedForCapacity(new_capacity);
273+
const current_capacity_bytes: usize = self.calcBytesUsedForCapacity(self.capacity);
273274

274275
if (current_capacity_bytes < new_capacity_bytes) {
275276
if (self.capacity == 0) {
@@ -349,11 +350,11 @@ pub fn StableArrayAligned(comptime T: type, comptime _alignment: u29) type {
349350
}
350351

351352
pub fn calcTotalUsedBytes(self: Self) usize {
352-
return calcBytesUsedForCapacity(self.capacity);
353+
return self.calcBytesUsedForCapacity(self.capacity);
353354
}
354355

355-
fn calcBytesUsedForCapacity(capacity: usize) usize {
356-
return mem.alignForward(usize, k_sizeof * capacity, page_size);
356+
fn calcBytesUsedForCapacity(self: Self, capacity: usize) usize {
357+
return mem.alignForward(usize, k_sizeof * capacity, self.page_size);
357358
}
358359
};
359360
}

0 commit comments

Comments
 (0)