@@ -28,16 +28,15 @@ pub fn StableArrayAligned(comptime T: type, comptime _alignment: u29) type {
28
28
pub const VariableSlice = [* ]align (alignment ) T ;
29
29
30
30
pub const k_sizeof : usize = if (alignment > @sizeOf (T )) alignment else @sizeOf (T );
31
- pub const page_size : usize = heap .pageSize ();
32
31
pub const alignment = _alignment ;
33
32
34
33
items : Slice ,
35
34
capacity : usize ,
36
35
max_virtual_alloc_bytes : usize ,
36
+ page_size : usize ,
37
37
38
38
pub fn getPageSize (self : * Self ) usize {
39
- _ = self ;
40
- return Self .page_size ;
39
+ return self .page_size ;
41
40
}
42
41
43
42
pub fn getAlignment (self : * Self ) usize {
@@ -46,11 +45,13 @@ pub fn StableArrayAligned(comptime T: type, comptime _alignment: u29) type {
46
45
}
47
46
48
47
pub fn init (max_virtual_alloc_bytes : usize ) Self {
48
+ const page_size = heap .pageSize ();
49
49
assert (@mod (max_virtual_alloc_bytes , page_size ) == 0 ); // max_virtual_alloc_bytes must be a multiple of page_size
50
50
return Self {
51
51
.items = &[_ ]T {},
52
52
.capacity = 0 ,
53
53
.max_virtual_alloc_bytes = max_virtual_alloc_bytes ,
54
+ .page_size = page_size ,
54
55
};
55
56
}
56
57
@@ -208,8 +209,8 @@ pub fn StableArrayAligned(comptime T: type, comptime _alignment: u29) type {
208
209
pub fn shrinkAndFree (self : * Self , new_len : usize ) void {
209
210
assert (new_len <= self .items .len );
210
211
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 );
213
214
214
215
if (new_capacity_bytes < current_capacity_bytes ) {
215
216
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 {
268
269
}
269
270
270
271
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 );
273
274
274
275
if (current_capacity_bytes < new_capacity_bytes ) {
275
276
if (self .capacity == 0 ) {
@@ -349,11 +350,11 @@ pub fn StableArrayAligned(comptime T: type, comptime _alignment: u29) type {
349
350
}
350
351
351
352
pub fn calcTotalUsedBytes (self : Self ) usize {
352
- return calcBytesUsedForCapacity (self .capacity );
353
+ return self . calcBytesUsedForCapacity (self .capacity );
353
354
}
354
355
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 );
357
358
}
358
359
};
359
360
}
0 commit comments