BufInit
Description
Construct an empty Buf. The variadic form accepts an optional allocator pointer; with no argument, the allocator is taken from the enclosing Scope / ScopeWith block via MisraScope.
Success
Returns an initialized Buf with zero length and a lazily-allocated backing store.
Failure
Macro cannot fail; the backing allocator’s failure behaviour applies only on subsequent growth operations.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
MachO.c:433:
LOG_FATAL("MachoOpenFromMemoryCopy: NULL argument (contract violation)");
}
Buf copy = BufInit(alloc);
if (!VecReserve(©, (u64)data_size)) {
LOG_ERROR("MachoOpenFromMemoryCopy: allocation failed ({} bytes)", (u64)data_size);- In
MachO.c:447:
LOG_FATAL("MachoOpen: NULL argument (contract violation)");
}
Buf data = BufInit(alloc);
if (FileReadAndClose(path, &data) < 0) {
BufDeinit(&data);- In
Elf.c:465:
LOG_FATAL("ElfOpenFromMemoryCopy: NULL argument (contract violation)");
}
Buf copy = BufInit(alloc);
if (!VecReserve(©, (u64)data_size)) {
LOG_ERROR("ElfOpenFromMemoryCopy: allocation failed ({} bytes)", (u64)data_size);- In
Elf.c:481:
LOG_FATAL("ElfOpen: NULL argument (contract violation)");
}
Buf data = BufInit(alloc);
if (FileReadAndClose(path, &data) < 0) {
BufDeinit(&data);- In
Pdb.c:738:
LOG_FATAL("PdbOpenFromMemoryCopy: NULL argument (contract violation)");
}
Buf copy = BufInit(alloc);
if (!BufReserve(©, (u64)data_size)) {
LOG_ERROR("PdbOpenFromMemoryCopy: allocation failed ({} bytes)", (u64)data_size);- In
Pdb.c:752:
LOG_FATAL("PdbOpen: NULL argument (contract violation)");
}
Buf data = BufInit(alloc);
if (FileReadAndClose(path, &data) < 0) {
BufDeinit(&data);- In
Pe.c:538:
LOG_FATAL("PeOpenFromMemoryCopy: NULL argument (contract violation)");
}
Buf copy = BufInit(alloc);
if (!BufReserve(©, (u64)data_size)) {
LOG_ERROR("PeOpenFromMemoryCopy: allocation failed ({} bytes)", (u64)data_size);- In
Pe.c:552:
LOG_FATAL("PeOpen: NULL argument (contract violation)");
}
Buf data = BufInit(alloc);
if (FileReadAndClose(path, &data) < 0) {
BufDeinit(&data);- In
Buf.c:15:
bool test_buf_init_clear(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufLength(&b) == 0;
BufWriteU8(&b, 0x42);- In
Buf.c:29:
bool test_buf_write_u_le_be(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteU16LE(&b, 0x1234);
BufWriteU16BE(&b, 0x1234);- In
Buf.c:52:
bool test_buf_write_leb128(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteULeb128(&b, 0);
BufWriteULeb128(&b, 127);- In
Buf.c:79:
bool test_buf_write_zstr(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteZstr(&b, "hi");
const u8 expect[] = {'h', 'i', 0};- In
Buf.c:94:
bool test_buf_read_round_trip(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteU16LE(&b, 0xABCD);
BufWriteU32BE(&b, 0x12345678);- In
Buf.c:115:
bool test_buf_read_leb128_round_trip(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteULeb128(&b, 624485);
BufWriteSLeb128(&b, -123456);- In
Buf.c:133:
bool test_buf_read_zstr_round_trip(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteZstr(&b, "hello");
BufWriteZstr(&b, "world");- In
Buf.c:154:
bool test_buf_append_fmt(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteU8(&b, 0x99); // existing byte; append must preserve
bool ok = BufAppendFmt(&b, "{<2r}{>4r}", (u16)0xCAFE, (u32)0xDEADBEEF);- In
Buf.c:166:
bool test_buf_write_fmt_clears(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteU8(&b, 0xAA);
BufWriteU8(&b, 0xBB);- In
Buf.c:180:
bool test_buf_patch_fmt(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
// Reserve a 4-byte length placeholder followed by 8 bytes of body.
BufAppendFmt(&b, "{<4r}", (u32)0);- In
Buf.c:203:
// Out-of-range patch must fail and leave the buf unchanged.
Buf snapshot = BufInit(&alloc);
BufPushBytes(&snapshot, BufData(&b), BufLength(&b));
ok = ok && !BufPatchFmt(&b, BufLength(&b), "{<2r}", (u16)0);- In
Buf.c:217:
bool test_buf_read_fmt(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufAppendFmt(&b, "{<2r}{>4r}{<8r}", (u16)0x1234, (u32)0xDEADBEEF, (u64)0x0102030405060708ull);- In
Buf.c:235:
bool test_buf_read_fmt_truncated_atomic(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufAppendFmt(&b, "{<2r}", (u16)0xABCD); // only 2 bytes; reader wants 6
Last updated on