1 /*
2 ** This file contains shared definitions that are widely used across upb.
3 */
4 
5 #ifndef UPB_H_
6 #define UPB_H_
7 
8 #include <assert.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <string.h>
14 
15 #include "upb/port_def.inc"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /* upb_status *****************************************************************/
22 
23 #define UPB_STATUS_MAX_MESSAGE 127
24 
25 typedef struct {
26   bool ok;
27   char msg[UPB_STATUS_MAX_MESSAGE];  /* Error message; NULL-terminated. */
28 } upb_status;
29 
30 const char *upb_status_errmsg(const upb_status *status);
31 bool upb_ok(const upb_status *status);
32 
33 /* These are no-op if |status| is NULL. */
34 void upb_status_clear(upb_status *status);
35 void upb_status_seterrmsg(upb_status *status, const char *msg);
36 void upb_status_seterrf(upb_status *status, const char *fmt, ...);
37 void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args);
38 void upb_status_vappenderrf(upb_status *status, const char *fmt, va_list args);
39 
40 /** upb_strview ************************************************************/
41 
42 typedef struct {
43   const char *data;
44   size_t size;
45 } upb_strview;
46 
upb_strview_make(const char * data,size_t size)47 UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) {
48   upb_strview ret;
49   ret.data = data;
50   ret.size = size;
51   return ret;
52 }
53 
upb_strview_makez(const char * data)54 UPB_INLINE upb_strview upb_strview_makez(const char *data) {
55   return upb_strview_make(data, strlen(data));
56 }
57 
upb_strview_eql(upb_strview a,upb_strview b)58 UPB_INLINE bool upb_strview_eql(upb_strview a, upb_strview b) {
59   return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
60 }
61 
62 #define UPB_STRVIEW_INIT(ptr, len) {ptr, len}
63 
64 #define UPB_STRVIEW_FORMAT "%.*s"
65 #define UPB_STRVIEW_ARGS(view) (int)(view).size, (view).data
66 
67 /** upb_alloc *****************************************************************/
68 
69 /* A upb_alloc is a possibly-stateful allocator object.
70  *
71  * It could either be an arena allocator (which doesn't require individual
72  * free() calls) or a regular malloc() (which does).  The client must therefore
73  * free memory unless it knows that the allocator is an arena allocator. */
74 
75 struct upb_alloc;
76 typedef struct upb_alloc upb_alloc;
77 
78 /* A malloc()/free() function.
79  * If "size" is 0 then the function acts like free(), otherwise it acts like
80  * realloc().  Only "oldsize" bytes from a previous allocation are preserved. */
81 typedef void *upb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize,
82                              size_t size);
83 
84 struct upb_alloc {
85   upb_alloc_func *func;
86 };
87 
upb_malloc(upb_alloc * alloc,size_t size)88 UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) {
89   UPB_ASSERT(alloc);
90   return alloc->func(alloc, NULL, 0, size);
91 }
92 
upb_realloc(upb_alloc * alloc,void * ptr,size_t oldsize,size_t size)93 UPB_INLINE void *upb_realloc(upb_alloc *alloc, void *ptr, size_t oldsize,
94                              size_t size) {
95   UPB_ASSERT(alloc);
96   return alloc->func(alloc, ptr, oldsize, size);
97 }
98 
upb_free(upb_alloc * alloc,void * ptr)99 UPB_INLINE void upb_free(upb_alloc *alloc, void *ptr) {
100   assert(alloc);
101   alloc->func(alloc, ptr, 0, 0);
102 }
103 
104 /* The global allocator used by upb.  Uses the standard malloc()/free(). */
105 
106 extern upb_alloc upb_alloc_global;
107 
108 /* Functions that hard-code the global malloc.
109  *
110  * We still get benefit because we can put custom logic into our global
111  * allocator, like injecting out-of-memory faults in debug/testing builds. */
112 
upb_gmalloc(size_t size)113 UPB_INLINE void *upb_gmalloc(size_t size) {
114   return upb_malloc(&upb_alloc_global, size);
115 }
116 
upb_grealloc(void * ptr,size_t oldsize,size_t size)117 UPB_INLINE void *upb_grealloc(void *ptr, size_t oldsize, size_t size) {
118   return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
119 }
120 
upb_gfree(void * ptr)121 UPB_INLINE void upb_gfree(void *ptr) {
122   upb_free(&upb_alloc_global, ptr);
123 }
124 
125 /* upb_arena ******************************************************************/
126 
127 /* upb_arena is a specific allocator implementation that uses arena allocation.
128  * The user provides an allocator that will be used to allocate the underlying
129  * arena blocks.  Arenas by nature do not require the individual allocations
130  * to be freed.  However the Arena does allow users to register cleanup
131  * functions that will run when the arena is destroyed.
132  *
133  * A upb_arena is *not* thread-safe.
134  *
135  * You could write a thread-safe arena allocator that satisfies the
136  * upb_alloc interface, but it would not be as efficient for the
137  * single-threaded case. */
138 
139 typedef void upb_cleanup_func(void *ud);
140 
141 struct upb_arena;
142 typedef struct upb_arena upb_arena;
143 
144 typedef struct {
145   /* We implement the allocator interface.
146    * This must be the first member of upb_arena!
147    * TODO(haberman): remove once handlers are gone. */
148   upb_alloc alloc;
149 
150   char *ptr, *end;
151 } _upb_arena_head;
152 
153 /* Creates an arena from the given initial block (if any -- n may be 0).
154  * Additional blocks will be allocated from |alloc|.  If |alloc| is NULL, this
155  * is a fixed-size arena and cannot grow. */
156 upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc);
157 void upb_arena_free(upb_arena *a);
158 bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func);
159 void upb_arena_fuse(upb_arena *a, upb_arena *b);
160 void *_upb_arena_slowmalloc(upb_arena *a, size_t size);
161 
upb_arena_alloc(upb_arena * a)162 UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; }
163 
_upb_arenahas(upb_arena * a)164 UPB_INLINE size_t _upb_arenahas(upb_arena *a) {
165   _upb_arena_head *h = (_upb_arena_head*)a;
166   return (size_t)(h->end - h->ptr);
167 }
168 
upb_arena_malloc(upb_arena * a,size_t size)169 UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) {
170   _upb_arena_head *h = (_upb_arena_head*)a;
171   void* ret;
172   size = UPB_ALIGN_MALLOC(size);
173 
174   if (UPB_UNLIKELY(_upb_arenahas(a) < size)) {
175     return _upb_arena_slowmalloc(a, size);
176   }
177 
178   ret = h->ptr;
179   h->ptr += size;
180   UPB_UNPOISON_MEMORY_REGION(ret, size);
181 
182 #if UPB_ASAN
183   {
184     size_t guard_size = 32;
185     if (_upb_arenahas(a) >= guard_size) {
186       h->ptr += guard_size;
187     } else {
188       h->ptr = h->end;
189     }
190   }
191 #endif
192 
193   return ret;
194 }
195 
upb_arena_realloc(upb_arena * a,void * ptr,size_t oldsize,size_t size)196 UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize,
197                                    size_t size) {
198   void *ret = upb_arena_malloc(a, size);
199 
200   if (ret && oldsize > 0) {
201     memcpy(ret, ptr, oldsize);
202   }
203 
204   return ret;
205 }
206 
upb_arena_new(void)207 UPB_INLINE upb_arena *upb_arena_new(void) {
208   return upb_arena_init(NULL, 0, &upb_alloc_global);
209 }
210 
211 /* Constants ******************************************************************/
212 
213 /* Generic function type. */
214 typedef void upb_func(void);
215 
216 /* A list of types as they are encoded on-the-wire. */
217 typedef enum {
218   UPB_WIRE_TYPE_VARINT      = 0,
219   UPB_WIRE_TYPE_64BIT       = 1,
220   UPB_WIRE_TYPE_DELIMITED   = 2,
221   UPB_WIRE_TYPE_START_GROUP = 3,
222   UPB_WIRE_TYPE_END_GROUP   = 4,
223   UPB_WIRE_TYPE_32BIT       = 5
224 } upb_wiretype_t;
225 
226 /* The types a field can have.  Note that this list is not identical to the
227  * types defined in descriptor.proto, which gives INT32 and SINT32 separate
228  * types (we distinguish the two with the "integer encoding" enum below). */
229 typedef enum {
230   UPB_TYPE_BOOL     = 1,
231   UPB_TYPE_FLOAT    = 2,
232   UPB_TYPE_INT32    = 3,
233   UPB_TYPE_UINT32   = 4,
234   UPB_TYPE_ENUM     = 5,  /* Enum values are int32. */
235   UPB_TYPE_MESSAGE  = 6,
236   UPB_TYPE_DOUBLE   = 7,
237   UPB_TYPE_INT64    = 8,
238   UPB_TYPE_UINT64   = 9,
239   UPB_TYPE_STRING   = 10,
240   UPB_TYPE_BYTES    = 11
241 } upb_fieldtype_t;
242 
243 /* The repeated-ness of each field; this matches descriptor.proto. */
244 typedef enum {
245   UPB_LABEL_OPTIONAL = 1,
246   UPB_LABEL_REQUIRED = 2,
247   UPB_LABEL_REPEATED = 3
248 } upb_label_t;
249 
250 /* Descriptor types, as defined in descriptor.proto. */
251 typedef enum {
252   /* Old (long) names.  TODO(haberman): remove */
253   UPB_DESCRIPTOR_TYPE_DOUBLE   = 1,
254   UPB_DESCRIPTOR_TYPE_FLOAT    = 2,
255   UPB_DESCRIPTOR_TYPE_INT64    = 3,
256   UPB_DESCRIPTOR_TYPE_UINT64   = 4,
257   UPB_DESCRIPTOR_TYPE_INT32    = 5,
258   UPB_DESCRIPTOR_TYPE_FIXED64  = 6,
259   UPB_DESCRIPTOR_TYPE_FIXED32  = 7,
260   UPB_DESCRIPTOR_TYPE_BOOL     = 8,
261   UPB_DESCRIPTOR_TYPE_STRING   = 9,
262   UPB_DESCRIPTOR_TYPE_GROUP    = 10,
263   UPB_DESCRIPTOR_TYPE_MESSAGE  = 11,
264   UPB_DESCRIPTOR_TYPE_BYTES    = 12,
265   UPB_DESCRIPTOR_TYPE_UINT32   = 13,
266   UPB_DESCRIPTOR_TYPE_ENUM     = 14,
267   UPB_DESCRIPTOR_TYPE_SFIXED32 = 15,
268   UPB_DESCRIPTOR_TYPE_SFIXED64 = 16,
269   UPB_DESCRIPTOR_TYPE_SINT32   = 17,
270   UPB_DESCRIPTOR_TYPE_SINT64   = 18,
271 
272   UPB_DTYPE_DOUBLE   = 1,
273   UPB_DTYPE_FLOAT    = 2,
274   UPB_DTYPE_INT64    = 3,
275   UPB_DTYPE_UINT64   = 4,
276   UPB_DTYPE_INT32    = 5,
277   UPB_DTYPE_FIXED64  = 6,
278   UPB_DTYPE_FIXED32  = 7,
279   UPB_DTYPE_BOOL     = 8,
280   UPB_DTYPE_STRING   = 9,
281   UPB_DTYPE_GROUP    = 10,
282   UPB_DTYPE_MESSAGE  = 11,
283   UPB_DTYPE_BYTES    = 12,
284   UPB_DTYPE_UINT32   = 13,
285   UPB_DTYPE_ENUM     = 14,
286   UPB_DTYPE_SFIXED32 = 15,
287   UPB_DTYPE_SFIXED64 = 16,
288   UPB_DTYPE_SINT32   = 17,
289   UPB_DTYPE_SINT64   = 18
290 } upb_descriptortype_t;
291 
292 #define UPB_MAP_BEGIN ((size_t)-1)
293 
_upb_isle(void)294 UPB_INLINE bool _upb_isle(void) {
295   int x = 1;
296   return *(char*)&x == 1;
297 }
298 
_upb_be_swap32(uint32_t val)299 UPB_INLINE uint32_t _upb_be_swap32(uint32_t val) {
300   if (_upb_isle()) {
301     return val;
302   } else {
303     return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
304            ((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
305   }
306 }
307 
_upb_be_swap64(uint64_t val)308 UPB_INLINE uint64_t _upb_be_swap64(uint64_t val) {
309   if (_upb_isle()) {
310     return val;
311   } else {
312     return ((uint64_t)_upb_be_swap32(val) << 32) | _upb_be_swap32(val >> 32);
313   }
314 }
315 
_upb_lg2ceil(int x)316 UPB_INLINE int _upb_lg2ceil(int x) {
317   if (x <= 1) return 0;
318 #ifdef __GNUC__
319   return 32 - __builtin_clz(x - 1);
320 #else
321   int lg2 = 0;
322   while (1 << lg2 < x) lg2++;
323   return lg2;
324 #endif
325 }
326 
_upb_lg2ceilsize(int x)327 UPB_INLINE int _upb_lg2ceilsize(int x) {
328   return 1 << _upb_lg2ceil(x);
329 }
330 
331 #include "upb/port_undef.inc"
332 
333 #ifdef __cplusplus
334 }  /* extern "C" */
335 #endif
336 
337 #endif  /* UPB_H_ */
338