BitVecShrinkToFit
Description
Reduce bitvector capacity to match its current length. Frees any unused memory allocated beyond the current length.
Parameters
| Name | Direction | Description |
|---|---|---|
bv |
in,out | Bitvector to shrink. |
Usage example (from documentation)
BitVecShrinkToFit(&flags); // Free unused capacity
Success
Returns to the caller. Capacity now equals length; any over-allocated tail bytes have been returned to the allocator. The values of stored bits are preserved.
Failure
Function cannot fail in an observable way - if the shrink-reallocation fails, the bitvector is left unchanged but the caller is not informed (the helper silently keeps the larger buffer because the data is still consistent).
Usage example (Cross-references)
Usage examples (Cross-references)
- In
BitVec.c:161:
}
void BitVecShrinkToFit(BitVec *bv) {
ValidateBitVec(bv);
if (bv->length == 0) { DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecShrinkToFit\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Shrink to fit
BitVecShrinkToFit(&bv);
// Check that capacity is now closer to length
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecShrinkToFit edge cases\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Test shrink on empty bitvec
BitVecShrinkToFit(&bv);
result = result && (BitVecLen(&bv) == 0) && (BitVecCapacity(&bv) >= 0); // Test shrink on single element
BitVecPush(&bv, true);
BitVecShrinkToFit(&bv);
result = result && (BitVecLen(&bv) == 1) && (BitVecCapacity(&bv) >= 1);
result = result && (BitVecGet(&bv, 0) == true);
// Test multiple shrinks (should be safe)
BitVecShrinkToFit(&bv);
BitVecShrinkToFit(&bv);
result = result && (BitVecLen(&bv) == 1); // Test multiple shrinks (should be safe)
BitVecShrinkToFit(&bv);
BitVecShrinkToFit(&bv);
result = result && (BitVecLen(&bv) == 1); BitVecReserve(&bv, 1000);
BitVecClear(&bv);
BitVecShrinkToFit(&bv);
result = result && (BitVecLen(&bv) == 0);
BitVecReserve(&bv1, cycle * 20);
BitVecShrinkToFit(&bv2);
// Verify data integrity
// Test NULL bitvec pointer - should abort
BitVecShrinkToFit(NULL);
return false;
Last updated on