Safe C

safe-c-std/vec.h

Raw file ยท Directory on GitHub

1/* A growing array of plain data. Include it once for each type, saying what
2   to call it and what it holds:
3     #define VEC_NAME ints
4     #define VEC_TYPE int
5     #include "vec.h"
6   That makes struct ints and these, all checked in bounds:
7     struct ints v = {0};
8     if (!ints_add(&v, 4)) ...                 no memory
9     int *_Nullable p = ints_at(&v, 0);        null if there is no such element
10     int x; if (ints_get(&v, 0, &x)) ...       a copy
11     ints_set(&v, 0, 5); ints_pop(&v, &x); ints_free(&v);
12   What ints_at returns ends at the next call that changes v.
13
14   For owners (strings from strdup, structs from malloc), also say how to see
15   one without owning it:
16     #define VEC_NAME names
17     #define VEC_TYPE char *_Owned _Nullable __null_terminated
18     #define VEC_BORROW char *_Nullable __null_terminated
19     #define VEC_OWNED 1
20     #include "vec.h"
21   Then names_add takes the string (and frees it if there is no memory),
22   names_at gives a borrow of one, names_take moves one out (leaving null),
23   names_pop moves the last out, and names_free frees each and the array.
24   VEC_FREE(x) says how to free one, if not free(x). */
25#if !defined(VEC_NAME) || !defined(VEC_TYPE)
26#error "define VEC_NAME and VEC_TYPE before including vec.h"
27#endif
28#if defined(VEC_OWNED) && VEC_OWNED && !defined(VEC_BORROW)
29#error "an owned VEC_TYPE needs VEC_BORROW: the same pointer, not _Owned"
30#endif
31#ifndef VEC_FREE
32#define VEC_FREE(x) free(x)
33#endif
34#include "safec.h"
35#include <stdint.h>
36#include <stdlib.h>
37#include <string.h>
38
39#define VEC_JOIN_(a, b) a##_##b
40#define VEC_JOIN(a, b) VEC_JOIN_(a, b)
41#define VEC_(f) VEC_JOIN(VEC_NAME, f)
42#if defined(__clang__)
43#pragma clang assume_nonnull begin
44#endif
45
46struct VEC_NAME {
47  size_t cap;
48  VEC_TYPE *_Owned _Nullable __counted_by(cap) p;
49  size_t len;
50};
51
52#if !defined(VEC_OWNED) || !VEC_OWNED
53static inline int VEC_(add)(struct VEC_NAME *v, VEC_TYPE x) {
54  if (!v->p || v->len >= v->cap) {
55    if (v->len > SIZE_MAX / 4 / sizeof(VEC_TYPE))
56      return 0;
57    size_t cap = v->len * 2 + 8;
58    VEC_TYPE *_Owned _Nullable bigger = calloc(cap, sizeof(VEC_TYPE));
59    if (!bigger)
60      return 0;
61    if (v->p && v->len <= v->cap)
62      memcpy(bigger, v->p, v->len * sizeof(VEC_TYPE));
63    free(_Take(v->p));
64    _Set(v->p, v->cap, bigger, cap);
65  }
66  if (!v->p)
67    return 0;
68  v->p[v->len] = x;
69  v->len++;
70  return 1;
71}
72
73static inline VEC_TYPE *_Nullable VEC_(at)(struct VEC_NAME *v, size_t i) {
74  if (!v->p || i >= v->len || i >= v->cap)
75    return NULL;
76  return &v->p[i];
77}
78
79static inline int VEC_(get)(const struct VEC_NAME *v, size_t i, VEC_TYPE *out) {
80  if (!v->p || i >= v->len || i >= v->cap)
81    return 0;
82  *out = v->p[i];
83  return 1;
84}
85
86static inline int VEC_(set)(struct VEC_NAME *v, size_t i, VEC_TYPE x) {
87  if (!v->p || i >= v->len || i >= v->cap)
88    return 0;
89  v->p[i] = x;
90  return 1;
91}
92
93static inline int VEC_(pop)(struct VEC_NAME *v, VEC_TYPE *out) {
94  if (!v->p || v->len == 0)
95    return 0;
96  v->len--;
97  *out = v->p[v->len];
98  return 1;
99}
100
101static inline void VEC_(free)(struct VEC_NAME *_Empties v) {
102  free(_Take(v->p));
103  v->len = 0;
104}
105
106#else
107/* Owners: moved one at a time, since copying an owner's bytes would make two. */
108static inline int VEC_(grow_)(struct VEC_NAME *v) {
109  if (v->len > SIZE_MAX / 4 / sizeof(VEC_TYPE))
110    return 0;
111  size_t cap = v->len * 2 + 8;
112  VEC_TYPE *_Owned _Nullable bigger = calloc(cap, sizeof(VEC_TYPE));
113  if (!bigger)
114    return 0;
115  for (size_t i = 0; v->p && i < v->len && i < v->cap; i++)
116    bigger[i] = _Take(v->p[i]);
117  free(_Take(v->p));
118  _Set(v->p, v->cap, bigger, cap);
119  return 1;
120}
121
122/* Takes x: it is in v, or freed if there is no memory. */
123static inline int VEC_(add)(struct VEC_NAME *v, VEC_TYPE x) {
124  if ((!v->p || v->len >= v->cap) && !VEC_(grow_)(v)) {
125    VEC_FREE(x);
126    return 0;
127  }
128  if (!v->p) {
129    VEC_FREE(x);
130    return 0;
131  }
132  v->p[v->len] = x;
133  v->len++;
134  return 1;
135}
136
137/* One element, borrowed: null if there is no such element, or it was taken. */
138static inline VEC_BORROW VEC_(at)(const struct VEC_NAME *v, size_t i) {
139  if (!v->p || i >= v->len || i >= v->cap)
140    return NULL;
141  VEC_BORROW x = v->p[i];
142  return x;
143}
144
145/* Moves element i out, leaving null in its place. */
146static inline VEC_TYPE VEC_(take)(struct VEC_NAME *v, size_t i) {
147  if (!v->p || i >= v->len || i >= v->cap)
148    return NULL;
149  return _Take(v->p[i]);
150}
151
152/* Moves the last element out, or null if there is none. */
153static inline VEC_TYPE VEC_(pop)(struct VEC_NAME *v) {
154  if (!v->p || v->len == 0 || v->len > v->cap)
155    return NULL;
156  v->len--;
157  return _Take(v->p[v->len]);
158}
159
160static inline void VEC_(free)(struct VEC_NAME *_Empties v) {
161  for (size_t i = 0; v->p && i < v->len && i < v->cap; i++)
162    VEC_FREE(_Take(v->p[i]));
163  free(_Take(v->p));
164  v->len = 0;
165}
166#endif
167
168#if defined(__clang__)
169#pragma clang assume_nonnull end
170#endif
171#undef VEC_
172#undef VEC_JOIN
173#undef VEC_JOIN_
174#undef VEC_NAME
175#undef VEC_TYPE
176#undef VEC_BORROW
177#undef VEC_OWNED
178#undef VEC_FREE
179