Skip to content

BufAllocator

Description

Allocator backing b’s storage. Read-only; rebinding the allocator after init is not supported.

Usage example (Cross-references)

Usage examples (Cross-references)
    // cleans up via PeDeinit -- the buffer never leaks.
    bool PeOpenFromMemory(Pe *out, Buf *in) {
        if (!out || !in || !BufData(in) || !BufAllocator(in)) {
            LOG_FATAL("PeOpenFromMemory: NULL argument (contract violation)");
        }
        // Initialize the sections vec up-front so PeDeinit on a
        // failed-parse path doesn't trip ValidateVec.
        out->sections = VecInitT(out->sections, BufAllocator(&taken));
    
        PeContext ctx = {
        }
    
        self->stream_sizes        = AllocatorAlloc(BufAllocator(&self->data), (size)sizes_bytes, 0);
        self->stream_blocks       = AllocatorAlloc(BufAllocator(&self->data), (size)ptrs_bytes, 0);
        self->stream_block_counts = AllocatorAlloc(BufAllocator(&self->data), (size)counts_bytes, 0);
    
        self->stream_sizes        = AllocatorAlloc(BufAllocator(&self->data), (size)sizes_bytes, 0);
        self->stream_blocks       = AllocatorAlloc(BufAllocator(&self->data), (size)ptrs_bytes, 0);
        self->stream_block_counts = AllocatorAlloc(BufAllocator(&self->data), (size)counts_bytes, 0);
        if (!self->stream_sizes || !self->stream_blocks || !self->stream_block_counts)
        self->stream_sizes        = AllocatorAlloc(BufAllocator(&self->data), (size)sizes_bytes, 0);
        self->stream_blocks       = AllocatorAlloc(BufAllocator(&self->data), (size)ptrs_bytes, 0);
        self->stream_block_counts = AllocatorAlloc(BufAllocator(&self->data), (size)counts_bytes, 0);
        if (!self->stream_sizes || !self->stream_blocks || !self->stream_block_counts)
            return false;
        if (out_bytes > (u64)((size)-1))
            return NULL;
        SectionRva *out = AllocatorAlloc(BufAllocator(&self->data), (size)out_bytes, 0);
        if (!out)
            return NULL;
            return NULL;
    
        u8 *buf = AllocatorAlloc(BufAllocator(&self->data), sz, 0);
        if (!buf) {
            AllocatorFree(BufAllocator(&self->data), out);
        u8 *buf = AllocatorAlloc(BufAllocator(&self->data), sz, 0);
        if (!buf) {
            AllocatorFree(BufAllocator(&self->data), out);
            return NULL;
        }
        bool read_ok = stream_read(self, section_hdr_stream, 0, buf, sz);
        if (!read_ok) {
            AllocatorFree(BufAllocator(&self->data), buf);
            AllocatorFree(BufAllocator(&self->data), out);
            return NULL;
        if (!read_ok) {
            AllocatorFree(BufAllocator(&self->data), buf);
            AllocatorFree(BufAllocator(&self->data), out);
            return NULL;
        }
            (void)BufReadU32LE(&rec, &out[i].virtual_address);
        }
        AllocatorFree(BufAllocator(&self->data), buf);
        *out_count = n;
        return out;
        // Stream into a flat buffer; the record stream is typically large
        // but not unbounded.
        u8 *buf = AllocatorAlloc(BufAllocator(&self->data), sz, 0);
        if (!buf)
            return false;
            return false;
        if (!stream_read(self, symrec_stream, 0, buf, sz)) {
            AllocatorFree(BufAllocator(&self->data), buf);
            return false;
        }
                        pp.rva = rva;
                        if (!pool_append_cstr(pool, name, &pp.name_offset_in_pool)) {
                            AllocatorFree(BufAllocator(&self->data), buf);
                            return false;
                        }
                        }
                        if (!VecPushBackR(pending, pp)) {
                            AllocatorFree(BufAllocator(&self->data), buf);
                            return false;
                        }
        }
    
        AllocatorFree(BufAllocator(&self->data), buf);
        return true;
    }
        if (!sections || num_sections == 0) {
            if (sections)
                AllocatorFree(BufAllocator(&self->data), sections);
            return true; // can't compute RVAs without section table
        }
        // the pool may grow during the walk -- StrBegin() can move under
        // us each time pool_append_cstr() grows the backing storage.
        PendingPubs pending = VecInitT(pending, BufAllocator(&self->data));
        bool        ok      = walk_publics(self, dbi.symrec_stream, sections, num_sections, &self->name_pool, &pending);
        AllocatorFree(BufAllocator(&self->data), sections);
        PendingPubs pending = VecInitT(pending, BufAllocator(&self->data));
        bool        ok      = walk_publics(self, dbi.symrec_stream, sections, num_sections, &self->name_pool, &pending);
        AllocatorFree(BufAllocator(&self->data), sections);
    
        if (!ok) {
    // or failure); allocator and bytes are carried by `taken`.
    bool PdbOpenFromMemory(Pdb *out, Buf *in) {
        if (!out || !in || !BufData(in) || !BufAllocator(in)) {
            LOG_FATAL("PdbOpenFromMemory: NULL argument (contract violation)");
        }
        MemSet(out, 0, sizeof(*out));
        out->data      = taken;
        out->functions = VecInitT(out->functions, BufAllocator(&taken));
        out->name_pool = StrInit(BufAllocator(&taken));
        out->data      = taken;
        out->functions = VecInitT(out->functions, BufAllocator(&taken));
        out->name_pool = StrInit(BufAllocator(&taken));
    
        u32 num_dir_bytes  = 0;
        if (!self)
            return;
        Allocator *alloc = BufAllocator(&self->data);
        if (alloc) {
            if (self->stream_dir)
    // snapshot cleans up via MachoDeinit -- the buffer never leaks.
    bool MachoOpenFromMemory(Macho *out, Buf *in) {
        if (!out || !in || !BufData(in) || !BufAllocator(in)) {
            LOG_FATAL("MachoOpenFromMemory: NULL argument (contract violation)");
        }
        MemSet(out, 0, sizeof(*out));
        out->data     = taken;
        out->segments = VecInitT(out->segments, BufAllocator(&taken));
        out->sections = VecInitT(out->sections, BufAllocator(&taken));
        out->symbols  = VecInitT(out->symbols, BufAllocator(&taken));
        out->data     = taken;
        out->segments = VecInitT(out->segments, BufAllocator(&taken));
        out->sections = VecInitT(out->sections, BufAllocator(&taken));
        out->symbols  = VecInitT(out->symbols, BufAllocator(&taken));
        out->segments = VecInitT(out->segments, BufAllocator(&taken));
        out->sections = VecInitT(out->sections, BufAllocator(&taken));
        out->symbols  = VecInitT(out->symbols, BufAllocator(&taken));
    
        MachoContext ctx = {.out = out};
    // so the buffer never leaks.
    bool ElfOpenFromMemory(Elf *out, Buf *in) {
        if (!out || !in || !BufData(in) || !BufAllocator(in)) {
            LOG_FATAL("ElfOpenFromMemory: NULL argument (contract violation)");
        }
        MemSet(out, 0, sizeof(*out));
        out->data            = taken;
        out->sections        = VecInitT(out->sections, BufAllocator(&taken));
        out->segments        = VecInitT(out->segments, BufAllocator(&taken));
        out->symbols         = VecInitT(out->symbols, BufAllocator(&taken));
        out->data            = taken;
        out->sections        = VecInitT(out->sections, BufAllocator(&taken));
        out->segments        = VecInitT(out->segments, BufAllocator(&taken));
        out->symbols         = VecInitT(out->symbols, BufAllocator(&taken));
        out->dynamic_symbols = VecInitT(out->dynamic_symbols, BufAllocator(&taken));
        out->sections        = VecInitT(out->sections, BufAllocator(&taken));
        out->segments        = VecInitT(out->segments, BufAllocator(&taken));
        out->symbols         = VecInitT(out->symbols, BufAllocator(&taken));
        out->dynamic_symbols = VecInitT(out->dynamic_symbols, BufAllocator(&taken));
        out->segments        = VecInitT(out->segments, BufAllocator(&taken));
        out->symbols         = VecInitT(out->symbols, BufAllocator(&taken));
        out->dynamic_symbols = VecInitT(out->dynamic_symbols, BufAllocator(&taken));
    
        if (!elf_decode_header(out))
Last updated on