safe-c-std/pool.h
Raw file ยท Directory on GitHub
1/* pool.h: handles that know when they are stale.
2
3 POOL(conn, struct conn) defines conn_pool, conn_id and functions
4 conn_pool p = {0};
5 conn_id a;
6 if (!conn_add(&p, &a)) ... a new zeroed slot; false if out of memory
7 struct conn *_Nullable c = conn_get(&p, a); null once a was removed
8 conn_remove(&p, a); false if it was already gone
9 conn_free(&p);
10
11 Keep handles wherever a pointer would be a stored borrow (a cache, a graph, a
12 back pointer). A slot's generation changes when it is removed, and a slot
13 whose generation would wrap is not used again, so a stale handle is never
14 mistaken for a new one of the same pool. A handle does not say which pool it
15 is from. T holds no owners: a slot is reused as bytes. */
16#ifndef SAFEC_POOL_H
17#define SAFEC_POOL_H
18#include "safec.h"
19#include <stdbool.h>
20#include <stddef.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
24
25#define POOL(name, T) \
26 typedef struct { uint32_t slot, gen; } name##_id; \
27 typedef struct { uint32_t gen; uint32_t next_free; T value; } name##_slot; \
28 typedef struct { \
29 uint32_t cap; \
30 name##_slot *_Owned _Nullable __counted_by(cap) slots; \
31 uint32_t used, first_free; /* 1 + a slot's number; 0 is none */ \
32 } name##_pool; \
33 /* Odd generations are live. */ \
34 static inline T *_Nullable name##_get(name##_pool *p, name##_id id) { \
35 if (!p->slots || id.slot >= p->used || !(id.gen & 1)) \
36 return NULL; \
37 name##_slot *s = &p->slots[id.slot]; \
38 if (s->gen != id.gen) \
39 return NULL; \
40 return &s->value; \
41 } \
42 static inline bool name##_grow(name##_pool *p) { \
43 if (p->cap > UINT32_MAX / 4) \
44 return false; \
45 uint32_t cap = p->cap ? p->cap * 2 : 8; \
46 name##_slot *_Owned _Nullable n = calloc(cap, sizeof *n); \
47 if (!n) \
48 return false; \
49 if (p->slots) \
50 for (uint32_t i = 0; i < p->used; i++) \
51 n[i] = p->slots[i]; \
52 free(_Take(p->slots)); \
53 p->slots = n; \
54 p->cap = cap; \
55 return true; \
56 } \
57 static inline bool name##_add(name##_pool *p, name##_id *out) { \
58 uint32_t i = p->first_free; \
59 if (i == 0 && p->used == p->cap && !name##_grow(p)) \
60 return false; \
61 if (!p->slots) \
62 return false; \
63 if (i) { \
64 i--; \
65 p->first_free = p->slots[i].next_free; \
66 memset(&p->slots[i].value, 0, sizeof p->slots[i].value); \
67 } else { \
68 i = p->used++; \
69 } \
70 p->slots[i].gen++; \
71 out->slot = i; \
72 out->gen = p->slots[i].gen; \
73 return true; \
74 } \
75 static inline bool name##_remove(name##_pool *p, name##_id id) { \
76 if (!name##_get(p, id) || !p->slots) \
77 return false; \
78 name##_slot *s = &p->slots[id.slot]; \
79 s->gen++; \
80 if (s->gen != 0) { \
81 s->next_free = p->first_free; \
82 p->first_free = id.slot + 1; \
83 } \
84 return true; \
85 } \
86 static inline void name##_free(name##_pool *_Empties p) { \
87 free(_Take(p->slots)); \
88 p->slots = NULL; \
89 p->cap = 0; \
90 p->used = 0; \
91 p->first_free = 0; \
92 } \
93 struct name##_pool_needs_a_semicolon
94
95#endif
96