Skip to content

VecInitT

Description

Typed-cast variant of VecInit for assigning into a typed Vec variable. The cast makes the macro usable both as an in-place initializer (Vec(int) v = VecInitT(v, alloc);) and as an assignment target. The allocator argument is optional inside a Scope block.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        deinit_vec(GENERIC_VEC(&slot->out_neighbors), sizeof(GraphNodeId));
        slot->out_neighbors = VecInitT(slot->out_neighbors, graph->allocator);
        deinit_vec(GENERIC_VEC(&slot->in_neighbors), sizeof(GraphNodeId));
        slot->in_neighbors = VecInitT(slot->in_neighbors, graph->allocator);
        slot->out_neighbors = VecInitT(slot->out_neighbors, graph->allocator);
        deinit_vec(GENERIC_VEC(&slot->in_neighbors), sizeof(GraphNodeId));
        slot->in_neighbors = VecInitT(slot->in_neighbors, graph->allocator);
    
        slot->visit_count = 0;
            } else {
                deinit_vec(GENERIC_VEC(&slot->out_neighbors), sizeof(GraphNodeId));
                slot->out_neighbors = VecInitT(slot->out_neighbors, graph->allocator);
                deinit_vec(GENERIC_VEC(&slot->in_neighbors), sizeof(GraphNodeId));
                slot->in_neighbors = VecInitT(slot->in_neighbors, graph->allocator);
                slot->out_neighbors = VecInitT(slot->out_neighbors, graph->allocator);
                deinit_vec(GENERIC_VEC(&slot->in_neighbors), sizeof(GraphNodeId));
                slot->in_neighbors = VecInitT(slot->in_neighbors, graph->allocator);
                slot->visit_count  = 0;
                slot->flags        = 0;
            }
    
            slot_ptr->out_neighbors = VecInitT(slot_ptr->out_neighbors, graph->allocator);
            slot_ptr->in_neighbors  = VecInitT(slot_ptr->in_neighbors, graph->allocator);
            slot_ptr->data          = graph_alloc_node_data(graph, item_size);
    
            slot_ptr->out_neighbors = VecInitT(slot_ptr->out_neighbors, graph->allocator);
            slot_ptr->in_neighbors  = VecInitT(slot_ptr->in_neighbors, graph->allocator);
            slot_ptr->data          = graph_alloc_node_data(graph, item_size);
            if (!slot_ptr->data) {
                slot_ptr->data = NULL;
                deinit_vec(GENERIC_VEC(&slot_ptr->out_neighbors), sizeof(GraphNodeId));
                slot_ptr->out_neighbors = VecInitT(slot_ptr->out_neighbors, graph->allocator);
                deinit_vec(GENERIC_VEC(&slot_ptr->in_neighbors), sizeof(GraphNodeId));
                slot_ptr->in_neighbors = VecInitT(slot_ptr->in_neighbors, graph->allocator);
                slot_ptr->out_neighbors = VecInitT(slot_ptr->out_neighbors, graph->allocator);
                deinit_vec(GENERIC_VEC(&slot_ptr->in_neighbors), sizeof(GraphNodeId));
                slot_ptr->in_neighbors = VecInitT(slot_ptr->in_neighbors, graph->allocator);
                slot_ptr->visit_count  = 0;
                slot_ptr->flags        = 0;
        }
    
        slot.out_neighbors = VecInitT(slot.out_neighbors, graph->allocator);
        slot.in_neighbors  = VecInitT(slot.in_neighbors, graph->allocator);
        slot.data          = graph_alloc_node_data(graph, item_size);
    
        slot.out_neighbors = VecInitT(slot.out_neighbors, graph->allocator);
        slot.in_neighbors  = VecInitT(slot.in_neighbors, graph->allocator);
        slot.data          = graph_alloc_node_data(graph, item_size);
        if (!slot.data) {
        MemSet(out, 0, sizeof(*out));
        out->allocator = alloc;
        out->cache     = VecInitT(out->cache, alloc);
        if (!ProcMapsLoad(&out->maps, alloc)) {
            VecDeinit(&out->cache);
        MemSet(out, 0, sizeof(*out));
        out->raw     = StrInit(alloc);
        out->entries = VecInitT(out->entries, alloc);
    
        // `/proc/self/maps` reports stat-size 0 because it's generated by
        MemSet(out, 0, sizeof(*out));
        out->allocator   = alloc;
        out->hosts       = VecInitT(out->hosts, alloc);
        out->nameservers = VecInitT(out->nameservers, alloc);
        out->timeout_ms  = 5000;
        out->allocator   = alloc;
        out->hosts       = VecInitT(out->hosts, alloc);
        out->nameservers = VecInitT(out->nameservers, alloc);
        out->timeout_ms  = 5000;
        out->retries     = 2;
        ArenaAllocator scratch = ArenaAllocatorInit();
    
        DnsWireBuf query = VecInitT(query, &scratch);
        u16        id    = random_query_id();
        if (!DnsBuildQuery(&query, id, hostname, qtype)) {
            return false;
        }
        DnsAddrs addrs    = VecInitT(addrs, self->allocator);
        bool     ok       = dns_resolve_4_vec_zstr(self, spec, kind, &addrs);
        bool     have_one = ok && VecLen(&addrs) > 0;
        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};
        MemSet(out, 0, sizeof(*out));
        out->data            = taken;
        out->sections        = VecInitT(out->sections, BufAllocator(&taken));
        out->symbols         = VecInitT(out->symbols, BufAllocator(&taken));
        out->dynamic_symbols = VecInitT(out->dynamic_symbols, BufAllocator(&taken));
        out->data            = taken;
        out->sections        = VecInitT(out->sections, 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->symbols         = VecInitT(out->symbols, BufAllocator(&taken));
        out->dynamic_symbols = VecInitT(out->dynamic_symbols, BufAllocator(&taken));
    
        if (!elf_decode_header(out))
    
    static void cu_strings_init(CuStrings *cs, Allocator *alloc) {
        cs->dir_offsets  = VecInitT(cs->dir_offsets, alloc);
        cs->file_dir_idx = VecInitT(cs->file_dir_idx, alloc);
        cs->file_offsets = VecInitT(cs->file_offsets, alloc);
    static void cu_strings_init(CuStrings *cs, Allocator *alloc) {
        cs->dir_offsets  = VecInitT(cs->dir_offsets, alloc);
        cs->file_dir_idx = VecInitT(cs->file_dir_idx, alloc);
        cs->file_offsets = VecInitT(cs->file_offsets, alloc);
    }
        cs->dir_offsets  = VecInitT(cs->dir_offsets, alloc);
        cs->file_dir_idx = VecInitT(cs->file_dir_idx, alloc);
        cs->file_offsets = VecInitT(cs->file_offsets, alloc);
    }
        MemSet(out, 0, sizeof(*out));
        out->allocator   = alloc;
        out->entries     = VecInitT(out->entries, alloc);
        out->string_pool = StrInit(alloc);
        // Parallel arrays of file / dir offsets, one per emitted row. We
        // resolve them to pointers at the end after string_pool is final.
        U64Vec pending_file_offsets = VecInitT(pending_file_offsets, alloc);
        U64Vec pending_dir_offsets  = VecInitT(pending_dir_offsets, alloc);
        // resolve them to pointers at the end after string_pool is final.
        U64Vec pending_file_offsets = VecInitT(pending_file_offsets, alloc);
        U64Vec pending_dir_offsets  = VecInitT(pending_dir_offsets, alloc);
    
        BufIter section_cur = BufIterFromMemory(BufData(ElfBuf(elf)) + line_section->offset, line_section->size);
    // by an entry with code 0. Returns false on malformed input.
    static bool parse_abbrev_table(BufIter cur, AbbrevTable *out, Allocator *alloc) {
        *out = VecInitT(*out, alloc);
        while (IterRemainingLength(&cur) > 0) {
            u64 code;
            e.tag          = (u32)tag;
            e.has_children = has_children != 0;
            e.attrs        = VecInitT(e.attrs, alloc);
    
            for (;;) {
        MemSet(out, 0, sizeof(*out));
        out->allocator   = alloc;
        out->entries     = VecInitT(out->entries, alloc);
        out->string_pool = StrInit(alloc);
        BufIter info_cur = BufIterFromMemory(info_bytes, info_size);
    
        PendingFns pending = VecInitT(pending, alloc);
    
        bool ok = true;
        // 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);
        MemSet(out, 0, sizeof(*out));
        out->data      = taken;
        out->functions = VecInitT(out->functions, BufAllocator(&taken));
        out->name_pool = StrInit(BufAllocator(&taken));
        MemSet(out, 0, sizeof(*out));
        out->allocator = alloc;
        out->cies      = VecInitT(out->cies, alloc);
        out->fdes      = VecInitT(out->fdes, alloc);
        out->allocator = alloc;
        out->cies      = VecInitT(out->cies, alloc);
        out->fdes      = VecInitT(out->fdes, alloc);
    
        const ElfSection *eh = ElfFindSection(elf, ".eh_frame");
        rec->name   = StrInit(alloc);
        rec->target = StrInit(alloc);
        rec->rdata  = VecInitT(rec->rdata, alloc);
        MemSet(rec->ipv4, 0, sizeof(rec->ipv4));
        MemSet(rec->ipv6, 0, sizeof(rec->ipv6));
        }
        MemSet(out, 0, sizeof(*out));
        out->answers    = VecInitT(out->answers, alloc);
        out->authority  = VecInitT(out->authority, alloc);
        out->additional = VecInitT(out->additional, alloc);
        MemSet(out, 0, sizeof(*out));
        out->answers    = VecInitT(out->answers, alloc);
        out->authority  = VecInitT(out->authority, alloc);
        out->additional = VecInitT(out->additional, alloc);
        out->answers    = VecInitT(out->answers, alloc);
        out->authority  = VecInitT(out->authority, alloc);
        out->additional = VecInitT(out->additional, alloc);
    
        if (len < 12) {
        // 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 = {
            }
    
            DnsAddrs addrs = VecInitT(addrs, alloc);
            if (!DnsResolve(&r, hostname, 0, SOCKET_KIND_TCP, &addrs)) {
                DnsResolverDeinit(&r);
    
    void init_int_vec(IntVec *vec, DefaultAllocator *alloc) {
        *vec = VecInitT(*vec, alloc);
    }
    
            case VEC_INT_MERGE : {
                IntVec  temp  = VecInitT(temp, alloc);
                uint8_t count = extract_u8(data, offset, data_size);
                count         = count % 4;
    
            case VEC_INT_INIT_CLONE : {
                IntVec  temp  = VecInitT(temp, alloc);
                uint8_t count = extract_u8(data, offset, data_size);
                count         = count % 4;
        VecPushBack(&original.config.features, feature2);
    
        original.numbers = VecInitT(original.numbers, &alloc);
        i32 vals[3]      = {10, 20, -5};
        for (size i = 0; i < 3; i++) {
        }
    
        original.flags = VecInitT(original.flags, &alloc);
        bool bools[3]  = {true, false, true};
        for (size i = 0; i < 3; i++) {
        parsed.config.log_level  = StrInit(&alloc);
        parsed.config.features   = VecInitWithDeepCopyT(parsed.config.features, NULL, StrDeinit, &alloc);
        parsed.numbers           = VecInitT(parsed.numbers, &alloc);
        parsed.flags             = VecInitT(parsed.flags, &alloc);
        parsed.config.features   = VecInitWithDeepCopyT(parsed.config.features, NULL, StrDeinit, &alloc);
        parsed.numbers           = VecInitT(parsed.numbers, &alloc);
        parsed.flags             = VecInitT(parsed.flags, &alloc);
    
        StrIter si = StrIterFromStr(json);
        Allocator       *alloc_base = ALLOCATOR_OF(&alloc);
    
        StackFrames frames = VecInitT(frames, alloc_base);
        bool        ok     = bt_vec_capture_outer(&frames);
        ok                 = ok && VecLen(&frames) >= 2;
        }
    
        DnsAddrs out = VecInitT(out, a);
        bool     got = DnsResolve(&r, "localhost", 8080, SOCKET_KIND_TCP, &out);
        DnsResolverInit(&r, a);
    
        DnsAddrs out = VecInitT(out, a);
        bool     got = DnsResolve(&r, "LocalHost", 0, SOCKET_KIND_TCP, &out);
        DnsResolverInit(&r, a);
    
        DnsAddrs out = VecInitT(out, a);
        bool     got = DnsResolve(&r, "localhost.", 0, SOCKET_KIND_TCP, &out);
        DnsResolverInit(&r, a);
    
        DnsAddrs out = VecInitT(out, a);
        bool     got = DnsResolve(&r, "203.0.113.7:9999", SOCKET_KIND_TCP, &out);
        DnsResolverInit(&r, a);
    
        DnsAddrs out = VecInitT(out, a);
        bool     got = DnsResolve(&r, "[::1]:443", SOCKET_KIND_TCP, &out);
        DnsResolverInit(&r, a);
    
        DnsAddrs out = VecInitT(out, a);
        bool     got = DnsResolve(&r, "localhost:53", SOCKET_KIND_TCP, &out);
        DnsResolverInit(&r, a);
    
        DnsAddrs out = VecInitT(out, a);
        bool     got = DnsResolve(&r, "localhost", SOCKET_KIND_TCP, &out);
        DnsResolverInit(&r, a);
    
        DnsAddrs out = VecInitT(out, a);
        bool     got = DnsResolve(&r, "localhost:abc", SOCKET_KIND_TCP, &out);
        BitVecPush(&pattern, true);
    
        BitVecMatchIndices matches = VecInitT(matches, base);
        result                     = result && BitVecFindAllPattern(&source, &pattern, &matches);
        result                     = result && VecLen(&matches) == 5;
    
        TestVec vec_a = VecInit(&h_default);
        TestVec vec_b = VecInitT(vec_b, &h_default);
        TestVec vec_c = VecInitWithDeepCopy(TestItemCopyInit, TestItemDeinit, &h_default);
        TestVec vec_d = VecInitWithDeepCopyT(vec_d, TestItemCopyInit, TestItemDeinit, &h_default);
        TestVec vec_d = VecInitWithDeepCopyT(vec_d, TestItemCopyInit, TestItemDeinit, &h_default);
        TestVec vec_e = VecInit(&h8);
        TestVec vec_f = VecInitT(vec_f, &h16);
        TestVec vec_g = VecInitWithDeepCopy(TestItemCopyInit, TestItemDeinit, &h32);
        TestVec vec_h = VecInitWithDeepCopyT(vec_h, TestItemCopyInit, TestItemDeinit, &h64);
        BitVecPush(&bv, true);
    
        BitVecRuns runs   = VecInitT(runs, base);
        bool       result = BitVecRunLengths(&bv, &runs);
        result            = result && VecLen(&runs) == 5;
    
        // Create a new vector for testing fast delete operations
        vec = VecInitT(vec, &alloc);
    
        // Add elements again
    
        // Test with an L-value index
        vec = VecInitT(vec, &alloc);
    
        // Add elements again
        // Test with large number of elements
        VecDeinit(&vec);
        vec = VecInitT(vec, &alloc);
    
        // Push a large number of elements
        // Test with zero-capacity vector
        VecDeinit(&vec);
        vec = VecInitT(vec, &alloc);
    
        // Reserve zero capacity
        Allocator       *a     = ALLOCATOR_OF(&alloc);
    
        DnsWireBuf buf = VecInitT(buf, a);
        bool       ok  = DnsBuildQuery(&buf, 0x1234, "example.com", DNS_TYPE_A);
        Allocator       *a     = ALLOCATOR_OF(&alloc);
    
        DnsWireBuf no_dot = VecInitT(no_dot, a);
        DnsWireBuf w_dot  = VecInitT(w_dot, a);
        bool ok = DnsBuildQuery(&no_dot, 1, "a.b.c", DNS_TYPE_AAAA) && DnsBuildQuery(&w_dot, 1, "a.b.c.", DNS_TYPE_AAAA);
    
        DnsWireBuf no_dot = VecInitT(no_dot, a);
        DnsWireBuf w_dot  = VecInitT(w_dot, a);
        bool ok = DnsBuildQuery(&no_dot, 1, "a.b.c", DNS_TYPE_AAAA) && DnsBuildQuery(&w_dot, 1, "a.b.c.", DNS_TYPE_AAAA);
Last updated on