BitVecReserve
Description
Ensure bv can hold at least n bits without further reallocation.
Parameters
| Name | Direction | Description |
|---|---|---|
bv |
in,out | Bitvector whose capacity should grow. |
n |
in | Minimum bit capacity required after the call. |
Success
Returns true; bv->capacity is at least n and existing bits are preserved.
Failure
Returns false on allocator OOM; bv is left unchanged.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Init.h:27:
#define BitVecMustReserve(bv, n) \
do { \
if (!BitVecReserve((bv), (n))) { \
LOG_FATAL("BitVecMustReserve failed"); \
} \- In
Init.h:67:
/// TAGS: Int, Reserve, Capacity
///
#define IntReserve(value, capacity) BitVecReserve(&(value)->bits, (capacity))
#endif // MISRA_STD_CONTAINER_INT_INIT_H
- In
BitVec.c:130:
ValidateBitVec(bitvec);
if (new_size > bitvec->capacity) {
if (!BitVecReserve(bitvec, new_size)) {
return false;
}- In
BitVec.c:159:
}
bool BitVecReserve(BitVec *bitvec, u64 n) {
ValidateBitVec(bitvec);
if (n <= bitvec->capacity)- In
BitVec.c:254:
}
if (!BitVecReserve(out, bv->length) || !BitVecResize(out, bv->length)) {
BitVecDeinit(out);
*out = BitVecInit(bv->allocator);- In
BitVec.c:325:
if (bitvec->length >= bitvec->capacity) {
u64 new_capacity = bitvec->capacity == 0 ? 8 : bitvec->capacity * 2;
if (!BitVecReserve(bitvec, new_capacity)) {
return false;
}- In
BitVec.c:934:
*out = BitVecInit(alloc);
if (!BitVecReserve(out, str_len)) {
return false;
}- In
BitVec.c:1037:
}
if (!BitVecReserve(out, bit_len) || !BitVecResize(out, bit_len)) {
BitVecDeinit(out);
*out = BitVecInit(alloc);- In
BitVec.c:1095:
}
if (!BitVecReserve(out, bits) || !BitVecResize(out, bits)) {
BitVecDeinit(out);
*out = BitVecInit(alloc);- In
Memory.c:49:
// Force capacity to be larger than needed by reserving space
BitVecReserve(&bv, 100);
// Check that capacity is larger than length
- In
Memory.c:79:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecReserve\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));- In
Memory.c:88:
// Set capacity to a specific value
BitVecReserve(&bv, 50);
// Check that capacity was set correctly
- In
Memory.c:98:
// Try to set capacity smaller than current length (should not shrink below length)
BitVecReserve(&bv, 1);
// Capacity should still accommodate at least the current length
- In
Memory.c:121:
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecReserve(&bv, 64);
bool result = (BitVecCapacity(&bv) == 64);- In
Memory.c:137:
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecReserve(&bv, 64);
bool result = (BitVecByteSize(&bv) == 8);- In
Memory.c:304:
// Test shrink after reserve and clear
BitVecReserve(&bv, 1000);
BitVecClear(&bv);
BitVecShrinkToFit(&bv);- In
Memory.c:317:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecReserve edge cases\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));- In
Memory.c:323:
// Test set capacity on empty bitvec
BitVecReserve(&bv, 100);
result = result && (BitVecCapacity(&bv) >= 100) && (BitVecLen(&bv) == 0);- In
Memory.c:341:
// Test setting very large capacity
BitVecReserve(&bv, 10000);
result = result && (BitVecCapacity(&bv) >= 10000);- In
Memory.c:458:
BitVecSwap(&bv1, &bv2);
BitVecReserve(&bv1, cycle * 20);
BitVecShrinkToFit(&bv2);- In
Memory.c:558:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecReserve with zero capacity returns true\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));- In
Memory.c:562:
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
bool result = (BitVecReserve(&bv, 0) == true);
result = result && (BitVecLen(&bv) == 0);- In
Memory.c:586:
// Kills: 139:5 remove_void_call (ValidateBitVec dropped).
bool test_reserve_null_aborts(void) {
WriteFmt("Testing BitVecReserve NULL handle aborts\n");
BitVecReserve(NULL, 5);- In
Memory.c:588:
WriteFmt("Testing BitVecReserve NULL handle aborts\n");
BitVecReserve(NULL, 5);
return false; // Should never reach here
- In
Init.c:81:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecReserve\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));- In
Init.c:86:
// Reserve space for 50 bits
BitVecReserve(&bv, 50);
// Check that capacity was increased
- In
Init.c:103:
// Test reserving less than current capacity (should be no-op)
u64 original_capacity = BitVecCapacity(&bv);
BitVecReserve(&bv, 25);
result = result && (BitVecCapacity(&bv) == original_capacity);- In
Init.c:225:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecReserve edge cases\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));- In
Init.c:231:
// Test reserving 0 (should be safe no-op)
BitVecReserve(&bv, 0);
result = result && (BitVecCapacity(&bv) == 0) && (BitVecData(&bv) == NULL);- In
Init.c:235:
// Test reserving 1 bit (minimum meaningful size)
BitVecReserve(&bv, 1);
result = result && (BitVecCapacity(&bv) >= 1) && (BitVecData(&bv) != NULL);- In
Init.c:239:
// Test very large but reasonable reservation
BitVecReserve(&bv, 10000);
result = result && (BitVecCapacity(&bv) >= 10000);- In
Init.c:244:
// Test reserving same u64 repeatedly (should be no-op)
u64 cap_before = BitVecCapacity(&bv);
BitVecReserve(&bv, BitVecCapacity(&bv));
BitVecReserve(&bv, BitVecCapacity(&bv));
result = result && (BitVecCapacity(&bv) == cap_before);- In
Init.c:245:
u64 cap_before = BitVecCapacity(&bv);
BitVecReserve(&bv, BitVecCapacity(&bv));
BitVecReserve(&bv, BitVecCapacity(&bv));
result = result && (BitVecCapacity(&bv) == cap_before);
// Reserve more space
BitVecReserve(&bv, 100);
result = result && (BitVecLen(&bv) == 3);
result = result && (BitVecCapacity(&bv) >= 100); // Create large bitvector
const int size = 10000;
BitVecReserve(&bv, size);
// Set alternating pattern
Last updated on