• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SRI-CSL / yices2 / 44167754935

17 Nov 2026 08:17PM UTC coverage: 71.322% (+0.003%) from 71.319%
44167754935

Pull #697

github

web-flow
Merge 10b1dd050 into ccdf89988
Pull Request #697: Integrate Yices 2.8.0 licensing and release fixes into master

96243 of 134941 relevant lines covered (71.32%)

1720253.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

39.86
/src/utils/string_hash_map.c
1
/*
2
 * Copyright (c) 2026, SRI International
3
 * SPDX-License-Identifier: BSD-3-Clause
4
 */
5

6
/*
7
 * HASH MAP: KEYS ARE STRINGS, VALUES ARE 32 BIT INTEGERS
8
 */
9

10
#include <assert.h>
11
#include <stddef.h>
12
#include <string.h>
13

14
#include "utils/hash_functions.h"
15
#include "utils/memalloc.h"
16
#include "utils/refcount_strings.h"
17
#include "utils/string_hash_map.h"
18

19

20
/*
21
 * For debugging: check whether n is a power of 2 (or n == 0)
22
 */
23
#ifndef NDEBUG
24
static bool is_power_of_two(uint32_t n) {
25
  return (n & (n - 1)) == 0;
26
}
27
#endif
28

29

30
/*
31
 * Initialization:
32
 * - n = initial size (must be a power of 2)
33
 * - if n = 0, the default size is used
34
 */
35
void init_strmap(strmap_t *hmap, uint32_t n) {
1,122 ✔
36
  strmap_rec_t *tmp;
37
  uint32_t i;
38

39
  if (n == 0) {
1,122 ✔
40
    n = STRMAP_DEF_SIZE;
1,122 ✔
41
  }
42

43
  if (n >= STRMAP_MAX_SIZE) {
1,122 ✔
44
    out_of_memory();
×
45
  }
46

47
  assert(is_power_of_two(n));
48
  tmp = (strmap_rec_t *) safe_malloc(n * sizeof(strmap_rec_t));
1,122 ✔
49
  for (i=0; i<n; i++) {
37,026 ✔
50
    tmp[i].key = NULL;
35,904 ✔
51
  }
52

53
  hmap->data = tmp;
1,122 ✔
54
  hmap->size = n;
1,122 ✔
55
  hmap->nelems = 0;
1,122 ✔
56
  hmap->ndeleted = 0;
1,122 ✔
57

58
  hmap->resize_threshold = (uint32_t) (n * STRMAP_RESIZE_RATIO);
1,122 ✔
59
  hmap->cleanup_threshold = (uint32_t) (n * STRMAP_CLEANUP_RATIO);
1,122 ✔
60
}
1,122 ✔
61

62
/*
63
 * Deleted records are marked by setting key to DELETED_KEY.
64
 * Empty records have key = NULL.
65
 */
66
#define DELETED_KEY ((char *) 1)
67

68
/*
69
 * Check whether key is valid (i.e., not NULL or DELETED_KEY)
70
 */
71
#define MASK_TAG ((uintptr_t) 3)
72

73
static inline bool valid_key(const char *key) {
76,718 ✔
74
  return (((uintptr_t) key) & ~MASK_TAG) != ((uintptr_t) NULL);
76,718 ✔
75
}
76

77

78
/*
79
 * Delete: call decref on all keys
80
 */
81
void delete_strmap(strmap_t *hmap) {
1,122 ✔
82
  uint32_t i, n;
83
  char *key;
84

85
  n = hmap->size;
1,122 ✔
86
  for (i=0; i<n; i++) {
37,026 ✔
87
    key = hmap->data[i].key;
35,904 ✔
88
    if (valid_key(key)) {
35,904 ✔
89
      string_decref(key);
3,125 ✔
90
    }
91
  }
92

93
  safe_free(hmap->data);
1,122 ✔
94
  hmap->data = NULL;
1,122 ✔
95
}
1,122 ✔
96

97

98
/*
99
 * Empty the table
100
 */
101
void reset_strmap(strmap_t *hmap) {
×
102
  uint32_t i, n;
103
  char *key;
104

105
  n = hmap->size;
×
106
  for (i=0; i<n; i++) {
×
107
    key = hmap->data[i].key;
×
108
    hmap->data[i].key = NULL;
×
109
    if (valid_key(key)) {
×
110
      string_decref(key);
×
111
    }
112
  }
113

114
  hmap->nelems = 0;
×
115
  hmap->ndeleted = 0;
×
116
}
×
117

118

119
/*
120
 * Make a copy of r in a clean array data
121
 * - r->hash must be the hash of r->key
122
 * - mask = size of data - 1
123
 * - there must be room for data in r
124
 */
125
static void strmap_clean_copy(strmap_rec_t *data, strmap_rec_t *r, uint32_t mask) {
×
126
  uint32_t i;
127

128
  i = r->hash & mask;
×
129
  while (data[i].key != NULL) {
×
130
    i ++;
×
131
    i &= mask;
×
132
  }
133

134
  data[i] = *r;
×
135
}
×
136

137

138
/*
139
 * Remove the deleted records
140
 */
141
static void strmap_cleanup(strmap_t *hmap) {
×
142
  strmap_rec_t *r, *tmp;
143
  uint32_t i, n, mask;
144

145
  n = hmap->size;
×
146
  tmp = (strmap_rec_t *) safe_malloc(n * sizeof(strmap_rec_t));
×
147
  for (i=0; i<n; i++) {
×
148
    tmp[i].key = NULL;
×
149
  }
150

151
  mask = n - 1;
×
152
  r = hmap->data;
×
153
  for (i=0; i<n; i++) {
×
154
    if (valid_key(r->key)) {
×
155
      strmap_clean_copy(tmp, r, mask);
×
156
    }
157
    r ++;
×
158
  }
159

160
  safe_free(hmap->data);
×
161
  hmap->data = tmp;
×
162
  hmap->ndeleted = 0;
×
163
}
×
164

165

166
/*
167
 * Remove deleted records and double the table size
168
 */
169
static void strmap_extend(strmap_t *hmap) {
×
170
  strmap_rec_t *r, *tmp;
171
  uint32_t i, n, n2, mask;
172

173
  n = hmap->size;
×
174
  n2 = n << 1;
×
175
  if (n2 >= STRMAP_MAX_SIZE) {
×
176
    out_of_memory();
×
177
  }
178

179
  tmp = (strmap_rec_t *) safe_malloc(n2 * sizeof(strmap_rec_t));
×
180
  for (i=0; i<n2; i++) {
×
181
    tmp[i].key = NULL;
×
182
  }
183

184
  mask = n2 - 1;
×
185
  r = hmap->data;
×
186
  for (i=0; i<n; i++) {
×
187
    if (valid_key(r->key)) {
×
188
      strmap_clean_copy(tmp, r, mask);
×
189
    }
190
    r ++;
×
191
  }
192

193
  safe_free(hmap->data);
×
194
  hmap->data = tmp;
×
195
  hmap->size = n2;
×
196
  hmap->ndeleted = 0;
×
197

198
  hmap->resize_threshold = (uint32_t) (n2 * STRMAP_RESIZE_RATIO);
×
199
  hmap->cleanup_threshold = (uint32_t) (n2 * STRMAP_CLEANUP_RATIO);
×
200
}
×
201

202

203
/*
204
 * Search for record with the given key
205
 * - return NULL if not found
206
 */
207
strmap_rec_t *strmap_find(strmap_t *hmap, const char *key) {
×
208
  uint32_t mask, i, h;
209
  strmap_rec_t *d;
210

211
  assert(is_power_of_two(hmap->size) && hmap->nelems + hmap->ndeleted < hmap->size);
212

213
  mask = hmap->size - 1;
×
214
  h = jenkins_hash_string(key);
×
215
  i = h & mask;
×
216
  for (;;) {
217
    d = hmap->data + i;
×
218
    if (d->key == NULL) return NULL;
×
219
    if (d->key != DELETED_KEY && d->hash == h && strcmp(d->key, key) == 0) {
×
220
      return d;
×
221
    }
222
    i ++;
×
223
    i &= mask;
×
224
  }
225
}
226

227

228
/*
229
 * Return a pointer to a new record in a fresh data array
230
 * - h = hash of the key to add
231
 * - hmap->data must not contain records with DELETED_KEY
232
 * - no record in hmap->data has the same key
233
 */
234
static strmap_rec_t *strmap_get_clean(strmap_t *hmap, uint32_t h) {
×
235
  uint32_t i, mask;
236
  strmap_rec_t *d;
237

238
  assert(is_power_of_two(hmap->size) && hmap->ndeleted == 0 && hmap->nelems < hmap->size);
239

240
  mask = hmap->size - 1;
×
241
  i = h & mask;
×
242
  for (;;) {
243
    d = hmap->data + i;
×
244
    if (d->key == NULL) break;
×
245
    i ++;
×
246
    i &= mask;
×
247
  }
248

249
  return d;
×
250
}
251

252
/*
253
 * Find or create record with the given key
254
 * - set is_new to true if that's a new record
255
 * - set is_new to false otherwise
256
 */
257
strmap_rec_t *strmap_get(strmap_t *hmap, const char *key, bool *is_new) {
3,521 ✔
258
  uint32_t mask, i, h;
259
  strmap_rec_t *d, *aux;
260
  char *clone;
261

262
  assert(is_power_of_two(hmap->size) && hmap->nelems + hmap->ndeleted < hmap->size);
263

264
  mask = hmap->size - 1;
3,521 ✔
265
  h = jenkins_hash_string(key);
3,521 ✔
266
  i = h & mask;
3,521 ✔
267
  for (;;) {
268
    d = hmap->data + i;
4,910 ✔
269
    if (! valid_key(d->key)) break;
4,910 ✔
270
    if (d->hash == h && strcmp(d->key, key) == 0) goto found;
1,785 ✔
271
    i ++;
1,389 ✔
272
    i &= mask;
1,389 ✔
273
  }
274

275
  aux = d; // this is where the new record will go if needed
3,125 ✔
276
  while (d->key != NULL) {
3,125 ✔
277
    i ++;
×
278
    i &= mask;
×
279
    if (d->key != DELETED_KEY && d->hash == h && strcmp(d->key, key) == 0) goto found;
×
280
  }
281

282
  // not found: add a new record
283
  clone = clone_string(key);
3,125 ✔
284
  string_incref(clone);
3,125 ✔
285
  hmap->nelems ++;
3,125 ✔
286
  if (aux->key == DELETED_KEY) {
3,125 ✔
287
    assert(hmap->ndeleted > 0);
288
    hmap->ndeleted --;
×
289
  } else if (hmap->nelems + hmap->ndeleted > hmap->resize_threshold) {
3,125 ✔
290
    // resize: we can't use the current aux
291
    strmap_extend(hmap);
×
292
    aux = strmap_get_clean(hmap, h);
×
293
  }
294
  aux->key = clone;
3,125 ✔
295
  aux->hash = h;
3,125 ✔
296
  aux->val = 0;
3,125 ✔
297

298
  *is_new = true;
3,125 ✔
299
  return aux;
3,125 ✔
300

301
 found:
396 ✔
302
  *is_new = false;
396 ✔
303
  return d;
396 ✔
304
}
305

306

307
/*
308
 * Remove record r
309
 */
310
void strmap_erase(strmap_t *hmap, strmap_rec_t *r) {
×
311
  assert(strmap_find(hmap, r->key) == r);
312

313
  string_decref(r->key);
×
314
  r->key = DELETED_KEY;
×
315
  hmap->nelems --;
×
316
  hmap->ndeleted ++;
×
317
  if (hmap->ndeleted > hmap->cleanup_threshold) {
×
318
    strmap_cleanup(hmap);
×
319
  }
320
}
×
321

322

323
/*
324
 * Iterator: apply f(aux, r) to all records r in the table
325
 */
326
void strmap_iterate(strmap_t *hmap, void *aux, strmap_iterator_t f) {
1,122 ✔
327
  uint32_t i, n;
328
  strmap_rec_t *r;
329

330
  n = hmap->size;
1,122 ✔
331
  r = hmap->data;
1,122 ✔
332
  for (i=0; i<n; i++) {
37,026 ✔
333
    if (valid_key(r->key)) {
35,904 ✔
334
      f(aux, r);
3,125 ✔
335
    }
336
    r ++;
35,904 ✔
337
  }
338
}
1,122 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc