kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

vec.h (1050B)


      1 #ifndef KIT_VEC_H
      2 #define KIT_VEC_H
      3 
      4 /* Generic doubling growth for embedded {T* ptr; u32 cap} pairs.
      5  *
      6  * Replaces the per-type *_grow functions previously hand-written in
      7  * obj.c, link.c, link_layout.c. Heap-backed only; arena-backed callers
      8  * (cg/aarch64.c AASlot) keep their own logic since the arena interface
      9  * cannot reallocate. */
     10 
     11 #include "core/core.h"
     12 #include "core/heap.h"
     13 
     14 /* Ensures *cap >= want. On growth, calls heap->realloc and updates *ptr
     15  * and *cap. Returns 0 on success (including the no-op case when *cap is
     16  * already large enough), or 1 on allocation failure. Initial cap when
     17  * growing from 0 is VEC_INIT_CAP (8). */
     18 int vec_grow_(Heap* h, void** ptr, u32* cap, u32 want, size_t elem_size,
     19               size_t elem_align);
     20 
     21 /* VEC_GROW(heap, ptr_lvalue, cap_lvalue, want)
     22  * — derives element size/alignment from *ptr's type. */
     23 #define VEC_GROW(h, ptr, cap, want)                                   \
     24   vec_grow_((h), (void**)&(ptr), &(cap), (u32)(want), sizeof(*(ptr)), \
     25             __alignof__(*(ptr)))
     26 
     27 #endif