pool.h (1045B)
1 #ifndef KIT_POOL_H 2 #define KIT_POOL_H 3 4 #include "core/arena.h" 5 #include "core/core.h" 6 #include "core/heap.h" 7 #include "core/slice.h" 8 9 typedef struct PoolEntry { 10 const char* data; 11 u32 len; 12 u32 hash; 13 } PoolEntry; 14 15 struct Pool { 16 Heap* heap; 17 Arena arena; /* string storage */ 18 19 /* Hash table: 0 means empty. Otherwise it's a Sym id (1-based). */ 20 Sym* table; 21 u32 cap; /* always a power of two */ 22 u32 used; 23 24 /* Sym → string mapping. Index 0 reserved as Sym = 0 ("none"). */ 25 PoolEntry* entries; 26 u32 nentries; 27 u32 entries_cap; 28 29 /* Frontends may hang language-specific interning state here. */ 30 void* type_cache; 31 }; 32 33 void pool_init(Pool*, Heap*); 34 void pool_fini(Pool*); 35 36 /* Interning. Returns the canonical id; equal byte sequences → equal ids 37 * (Sym 0 for the empty slice). pool_slice returns the interned bytes as a 38 * slice (SLICE_NULL for Sym 0); its pointer is NUL-terminated as a boundary 39 * convenience, with the NUL excluded from len. */ 40 Sym pool_intern_slice(Pool*, Slice); 41 Slice pool_slice(Pool*, Sym); 42 43 #endif