Safe C

safe-c-std/map.h

Raw file ยท Directory on GitHub

1/* Text to plain data. Include it once for each map, saying what to call it
2   and what it holds:
3     #define MAP_NAME counts
4     #define MAP_VALUE int
5     #include "map.h"
6   That makes struct counts and these:
7     struct counts m = {0};
8     if (!counts_put(&m, STR("a"), 1)) ...        no memory
9     int *_Nullable n = counts_get(&m, STR("a"));   null if it is not there
10     counts_remove(&m, STR("a")); counts_free(&m);
11   What counts_get returns ends at the next call that changes the map. Keys are
12   copied. The hash is not keyed: do not let an attacker pick many keys. */
13#if !defined(MAP_NAME) || !defined(MAP_VALUE)
14#error "define MAP_NAME and MAP_VALUE before including map.h"
15#endif
16#include "text.h"
17
18#ifndef SAFEC_MAP_HASH
19#define SAFEC_MAP_HASH
20static inline uint32_t map_hash_(struct str s) {
21  uint32_t h = 2166136261u;
22  for (size_t i = 0; i < s.len; i++)
23    h = (h ^ (unsigned char)s.p[i]) * 16777619u;
24  return h;
25}
26#endif
27
28#define MAP_JOIN_(a, b) a##_##b
29#define MAP_JOIN(a, b) MAP_JOIN_(a, b)
30#define MAP_(f) MAP_JOIN(MAP_NAME, f)
31#if defined(__clang__)
32#pragma clang assume_nonnull begin
33#endif
34
35/* Keys are kept end to end in one text; a slot says where its own is. */
36struct MAP_(slot) {
37  int state; /* 0 empty, 1 used, 2 removed */
38  uint32_t hash;
39  size_t at, len;
40  MAP_VALUE value;
41};
42struct MAP_NAME {
43  size_t cap;
44  struct MAP_(slot) *_Owned _Nullable __counted_by(cap) p;
45  size_t len, filled;
46  struct text keys;
47};
48/* The key's slot, or cap. */
49static inline size_t MAP_(find_)(const struct MAP_NAME *m, struct str key) {
50  if (!m->p || m->cap == 0)
51    return m->cap;
52  uint32_t h = map_hash_(key);
53  size_t i = h % m->cap;
54  for (size_t n = 0; n < m->cap; n++) {
55    if (m->p[i].state == 0)
56      return m->cap;
57    if (m->p[i].state == 1 && m->p[i].hash == h &&
58        str_eq(str_part(text_str(&m->keys), m->p[i].at, m->p[i].len), key))
59      return i;
60    i = (i + 1) % m->cap;
61  }
62  return m->cap;
63}
64static inline int MAP_(grow_)(struct MAP_NAME *m) {
65  if (m->len > SIZE_MAX / 8 / sizeof(struct MAP_(slot)))
66    return 0;
67  size_t cap = m->len * 4 + 8;
68  struct MAP_(slot) *_Owned _Nullable bigger =
69      calloc(cap, sizeof(struct MAP_(slot)));
70  if (!bigger)
71    return 0;
72  for (size_t o = 0; m->p && o < m->cap; o++)
73    if (m->p[o].state == 1) {
74      size_t i = m->p[o].hash % cap;
75      while (bigger[i].state)
76        i = (i + 1) % cap;
77      bigger[i] = m->p[o];
78    }
79  free(_Take(m->p));
80  _Set(m->p, m->cap, bigger, cap);
81  m->filled = m->len;
82  return 1;
83}
84static inline MAP_VALUE *_Nullable MAP_(get)(struct MAP_NAME *m, struct str key) {
85  size_t i = MAP_(find_)(m, key);
86  if (!m->p || i >= m->cap)
87    return NULL;
88  return &m->p[i].value;
89}
90static inline int MAP_(put)(struct MAP_NAME *m, struct str key, MAP_VALUE value) {
91  size_t i = MAP_(find_)(m, key);
92  if (m->p && i < m->cap) {
93    m->p[i].value = value;
94    return 1;
95  }
96  if (m->filled >= m->cap / 2 && !MAP_(grow_)(m))
97    return 0;
98  size_t at = m->keys.len;
99  if (!m->p || m->cap == 0 || !text_add(&m->keys, key.p, key.len))
100    return 0;
101  uint32_t h = map_hash_(key);
102  i = h % m->cap;
103  while (m->p[i].state)
104    i = (i + 1) % m->cap;
105  struct MAP_(slot) s = {1, h, at, key.len, value};
106  m->p[i] = s;
107  m->len++;
108  m->filled++;
109  return 1;
110}
111static inline int MAP_(remove)(struct MAP_NAME *m, struct str key) {
112  size_t i = MAP_(find_)(m, key);
113  if (!m->p || i >= m->cap)
114    return 0;
115  m->p[i].state = 2;
116  m->len--;
117  return 1;
118}
119static inline void MAP_(free)(struct MAP_NAME *_Empties m) {
120  free(_Take(m->p));
121  m->len = m->filled = 0;
122  text_free(&m->keys);
123}
124
125#if defined(__clang__)
126#pragma clang assume_nonnull end
127#endif
128#undef MAP_
129#undef MAP_JOIN
130#undef MAP_JOIN_
131#undef MAP_NAME
132#undef MAP_VALUE
133