Basic module

array_reserve

procedure · 2 overloadsSource

Makes room for at least desired_items elements without changing the count.

Nothing happens when the capacity is already enough, so it never shrinks. The first allocation uses context.allocator and the array keeps that allocator from then on; later growth goes to the same one. Growing may move the elements, which makes every pointer into the array stale. The new space is not initialized. A negative desired_items, or one whose byte size does not fit an s64, fails an assert.

ExampleOpen in playground ↗
values: [..] int;
array_reserve(*values, 100);
assert(values.count == 0 && values.allocated >= 100);

data := values.data;
for 0..99 array_add(*values, it);
assert(values.data == data); // no growth, so nothing moved
array_free(values);
array_reserve :: (array: *[..] $T, desired_items: s64)

Makes room for at least desired_items elements without changing the count.

Nothing happens when the capacity is already enough, so it never shrinks. The first allocation uses context.allocator and the array keeps that allocator from then on; later growth goes to the same one. Growing may move the elements, which makes every pointer into the array stale. The new space is not initialized. A negative desired_items, or one whose byte size does not fit an s64, fails an assert.

Parameters

array: *[..] $T
desired_items: s64
ExampleOpen in playground ↗
values: [..] int;
array_reserve(*values, 100);
assert(values.count == 0 && values.allocated >= 100);

data := values.data;
for 0..99 array_add(*values, it);
assert(values.data == data); // no growth, so nothing moved
array_free(values);
array_reserve :: (
    array: *Resizable_Array,
    desired_items: s64,
    element_size: s64
)

The untyped form of array_reserve, for code that only knows the element size in bytes.

Parameters

array: *Resizable_Array
desired_items: s64
element_size: s64

See also