cvector · Section 3
cvector_reserve
NAME
cvector_reserve - reserve capacity for future elements
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cvector.h> bool cvector_reserve(cvec v, size_t new_capacity_count);
DESCRIPTION
Reserves capacity for at least new_capacity_count elements, rounding the capacity up to the nearest power of two (minimum 4). Reserving the expected size up front saves you a series of reallocations.
PARAMETERS
v Vector to reserve capacity for new_capacity_count Minimum number of elements to reserve
RETURN VALUE
true when the reservation succeeds, and also when the capacity is already large enough. false when the allocation fails, when the capacity would be above ccol_max_elem_count, or when the capacity times the element size overflows a size_t.
NOTES
A request <= the current capacity does nothing. Because of the rounding to a power of 2 the real capacity can be larger than requested. Both limits of RETURN VALUE are tested against the rounded-up capacity, not against new_capacity_count itself, so a request within a limit can still fail after the rounding. A vector of wide elements hits the size_t overflow limit first. That limit caps the capacity at SIZE_MAX divided by the element size, which for any element wider than one byte is the lower of the two. The element count doesn't change. Asserts if v is NULL. The cvec_reserve(3) macro evaluates new_capacity_count exactly once, on the failure path too, so an expression with a side effect is safe to pass.