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
Pe.c:546:
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:560:
LOG_FATAL("PeOpen: NULL argument (contract violation)");
}
Buf data = BufInit(alloc);
if (FileReadAndClose(path, &data) < 0) {
BufDeinit(&data);- In
Pdb.c:734:
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:748:
LOG_FATAL("PdbOpen: NULL argument (contract violation)");
}
Buf data = BufInit(alloc);
if (FileReadAndClose(path, &data) < 0) {
BufDeinit(&data);- In
Tzif.c:190:
bool TzifLocalOffsetSeconds(i64 unix_seconds, i32 *out_offset_seconds, Allocator *alloc) {
Buf data = BufInit(alloc);
if (FileReadAndClose("/etc/localtime", &data) < 0) {
BufDeinit(&data);- In
MachO.c:451:
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:465:
LOG_FATAL("MachoOpen: NULL argument (contract violation)");
}
Buf data = BufInit(alloc);
if (FileReadAndClose(path, &data) < 0) {
BufDeinit(&data);- In
Elf.c:513:
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:529:
LOG_FATAL("ElfOpen: NULL argument (contract violation)");
}
Buf data = BufInit(alloc);
if (FileReadAndClose(path, &data) < 0) {
BufDeinit(&data);- In
File.c:822:
N = 10000
};
Buf src = BufInit(alloc_base);
bool ok = BufResize(&src, N);
for (i32 i = 0; i < N; ++i) {- In
File.c:832:
File r = FileOpen(&path, "rb");
ok = ok && FileIsOpen(&r);
Buf dst = BufInit(alloc_base);
i64 got = FileRead(&r, &dst);
FileClose(&r);- In
File.c:930:
FileClose(&seed);
Buf in = BufInit(alloc_base);
bool ok = BufResize(&in, 5);
MemCopy(BufData(&in), "12345", 5);- 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
- In
Write.c:721:
DefaultAllocator alloc = DefaultAllocatorInit();
Str output = StrInit(&alloc);
Buf b = BufInit(&alloc);
BufPushBytes(&b, (const u8 *)"hello", 5);- In
Write.c:783:
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:822:
bool test_buf_literal_magic_roundtrip(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:857:
bool test_buf_literal_brace_escape(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:877:
bool test_buf_literal_trailing_byte(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:1853:
WriteFmt("m16: u8 width-1 round-trip\n");
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:1879:
WriteFmt("m16: u32 width-4 round-trip\n");
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:1903:
WriteFmt("m16: u64 width-8 round-trip\n");
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:1927:
WriteFmt("m16: i64 width-8 round-trip\n");
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:1957:
WriteFmt("m16: non-raw spec {2} must abort\n");
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
// Lay down 2 bytes so the mutant's (wrongly accepted) width-2 read has
- In
Write.c:3497:
static bool test_m30_patch_exact_fit_succeeds(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:3523:
static bool test_m30_patch_empty_at_end_succeeds(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:3551:
static bool test_m30_patch_overlong_fails(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:3579:
static bool test_m31_raw_u8(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufAppendFmt(&b, "{<1r}", (u8)0x7E);
const u8 expect[] = {0x7E};- In
Write.c:3595:
static bool test_m31_raw_i64(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufAppendFmt(&b, "{<8r}", (i64)0x0102030405060708ll);
const u8 expect[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01};- In
Write.c:3609:
static bool test_m31_raw_f64(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
union {
f64 f;- In
Write.c:3696:
static bool test_m32_raw_width1_byte_writes(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufWriteFmt(&b, "{<1r}", (u8)0xAB);
ok = ok && (BufLength(&b) == 1) && (BufData(&b)[0] == 0xAB);- In
Write.c:3737:
static bool test_m32_deadend_nonraw_legal_width(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteFmt(&b, "{4}", (u32)0x11223344); // real: LOG_FATAL before return
BufDeinit(&b);- In
Write.c:3800:
static bool test_m33_buf_append_fmt_emits_bytes(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool r = BufAppendFmt(&b, "{>4r}", (u32)0xAABBCCDD);- In
Write.c:3895:
static bool test_m33_write_r16_explicit_big(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufWriteFmt(&b, "{>2r}", (u16)0x1234);- In
Write.c:3908:
static bool test_m33_write_r16_native(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufWriteFmt(&b, "{^2r}", (u16)0x1234);- In
Write.c:3922:
static bool test_m33_write_r32_native(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufWriteFmt(&b, "{^4r}", (u32)0x11223344);- In
Write.c:3935:
static bool test_m33_write_r64_native(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufWriteFmt(&b, "{^8r}", (u64)0x0102030405060708ULL);- In
Write.c:4089:
bool test_if_909_buf_read_u64_dispatch(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:4623:
static bool test_m8_caret_native_endian_raw_roundtrip(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Write.c:4758:
WriteFmt("bufread too-few-args must abort\n");
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
BufWriteFmt(&b, "{<1r}{<1r}", (u8)0x11, (u8)0x22); // 2 bytes available
BufIter it = BufIterFromBuf(&b);- In
Write.c:5049:
static bool test_buf_write_multi(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufWriteFmt(&b, "{<2r}{>4r}", (u16)0x1234, (u32)0xAABBCCDD);
ok = ok && (BufLength(&b) == 6);- In
Read.c:1215:
static bool test_m21_read_r32_big_endian_exact(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Read.c:1248:
static bool test_m21_read_r32_little_endian_exact(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Read.c:1273:
static bool test_m21_read_r32_little_endian_reversed(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Read.c:1308:
static bool test_m21_read_r32_native_resolves_roundtrip(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Read.c:1348:
static bool test_m22_little_endian_read_roundtrip_u64(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Read.c:1375:
static bool test_m22_little_endian_read_distinct_bytes(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Read.c:1405:
static bool test_m22_native_endian_read_roundtrip_u64(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = true;- In
Read.c:1590:
static bool test_m33_read_r8_value(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufAppendFmt(&b, "{<1r}", (u8)0xAB);- In
Read.c:2217:
static bool test_buf_read_u16(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
bool ok = BufWriteFmt(&b, "{<2r}", (u16)0xBEEF);
BufIter it = BufIterFromBuf(&b);- In
Read.c:2231:
static bool test_buf_read_f64(void) {
DefaultAllocator alloc = DefaultAllocatorInit();
Buf b = BufInit(&alloc);
union {
f64 f;- In
Pdb.c:2395:
// Use the L-form so the taken Buf is the allocator's responsibility:
// a copy buffer through `base`, handed to PdbOpenFromMemory.
Buf in = BufInit(base);
if (!BufReserve(&in, sizeof(sblob))) {
DebugAllocatorDeinit(&dbg);- In
Tzif.c:61:
static bool test_v1_resolve(void) {
DefaultAllocator a = DefaultAllocatorInit();
Buf b = BufInit(&a);
build_v1(&b);
i32 off;- In
Tzif.c:80:
static bool test_v2_resolve(void) {
DefaultAllocator a = DefaultAllocatorInit();
Buf b = BufInit(&a);
build_v2(&b);
i32 off;- In
Tzif.c:111:
static bool test_no_transitions_uses_std_fallback(void) {
DefaultAllocator a = DefaultAllocatorInit();
Buf b = BufInit(&a);
BufAppendFmt(&b, "TZif{>1r}", (u8)0);
put_reserved15(&b);- In
Tzif.c:126:
static bool test_v1_no_std_type_fails(void) {
DefaultAllocator a = DefaultAllocatorInit();
Buf b = BufInit(&a);
BufAppendFmt(&b, "TZif{>1r}", (u8)0);
put_reserved15(&b);- In
Tzif.c:140:
static bool test_v2_no_std_type_fails(void) {
DefaultAllocator a = DefaultAllocatorInit();
Buf b = BufInit(&a);
BufAppendFmt(&b, "TZif{>1r}", (u8)'2');
put_reserved15(&b);
Last updated on