2 ** This file contains shared definitions that are widely used across upb.
15 #include "upb/port_def.inc"
21 /* upb_status *****************************************************************/
23 #define UPB_STATUS_MAX_MESSAGE 127
27 char msg[UPB_STATUS_MAX_MESSAGE]; /* Error message; NULL-terminated. */
30 const char *upb_status_errmsg(const upb_status *status);
31 bool upb_ok(const upb_status *status);
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);
40 /** upb_strview ************************************************************/
47 UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) {
54 UPB_INLINE upb_strview upb_strview_makez(const char *data) {
55 return upb_strview_make(data, strlen(data));
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;
62 #define UPB_STRVIEW_INIT(ptr, len) {ptr, len}
64 #define UPB_STRVIEW_FORMAT "%.*s"
65 #define UPB_STRVIEW_ARGS(view) (int)(view).size, (view).data
67 /** upb_alloc *****************************************************************/
69 /* A upb_alloc is a possibly-stateful allocator object.
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. */
76 typedef struct upb_alloc upb_alloc;
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,
88 UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) {
90 return alloc->func(alloc, NULL, 0, size);
93 UPB_INLINE void *upb_realloc(upb_alloc *alloc, void *ptr, size_t oldsize,
96 return alloc->func(alloc, ptr, oldsize, size);
99 UPB_INLINE void upb_free(upb_alloc *alloc, void *ptr) {
101 alloc->func(alloc, ptr, 0, 0);
104 /* The global allocator used by upb. Uses the standard malloc()/free(). */
106 extern upb_alloc upb_alloc_global;
108 /* Functions that hard-code the global malloc.
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. */
113 UPB_INLINE void *upb_gmalloc(size_t size) {
114 return upb_malloc(&upb_alloc_global, 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);
121 UPB_INLINE void upb_gfree(void *ptr) {
122 upb_free(&upb_alloc_global, ptr);
125 /* upb_arena ******************************************************************/
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.
133 * A upb_arena is *not* thread-safe.
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. */
139 typedef void upb_cleanup_func(void *ud);
142 typedef struct upb_arena upb_arena;
145 /* We implement the allocator interface.
146 * This must be the first member of upb_arena!
147 * TODO(haberman): remove once handlers are gone. */
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);
162 UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)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);
169 UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) {
170 _upb_arena_head *h = (_upb_arena_head*)a;
172 size = UPB_ALIGN_MALLOC(size);
174 if (UPB_UNLIKELY(_upb_arenahas(a) < size)) {
175 return _upb_arena_slowmalloc(a, size);
180 UPB_UNPOISON_MEMORY_REGION(ret, size);
184 size_t guard_size = 32;
185 if (_upb_arenahas(a) >= guard_size) {
186 h->ptr += guard_size;
196 UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize,
198 void *ret = upb_arena_malloc(a, size);
200 if (ret && oldsize > 0) {
201 memcpy(ret, ptr, oldsize);
207 UPB_INLINE upb_arena *upb_arena_new(void) {
208 return upb_arena_init(NULL, 0, &upb_alloc_global);
211 /* Constants ******************************************************************/
213 /* Generic function type. */
214 typedef void upb_func(void);
216 /* A list of types as they are encoded on-the-wire. */
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
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). */
234 UPB_TYPE_ENUM = 5, /* Enum values are int32. */
235 UPB_TYPE_MESSAGE = 6,
239 UPB_TYPE_STRING = 10,
243 /* The repeated-ness of each field; this matches descriptor.proto. */
245 UPB_LABEL_OPTIONAL = 1,
246 UPB_LABEL_REQUIRED = 2,
247 UPB_LABEL_REPEATED = 3
250 /* Descriptor types, as defined in descriptor.proto. */
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,
272 UPB_DTYPE_DOUBLE = 1,
275 UPB_DTYPE_UINT64 = 4,
277 UPB_DTYPE_FIXED64 = 6,
278 UPB_DTYPE_FIXED32 = 7,
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,
286 UPB_DTYPE_SFIXED32 = 15,
287 UPB_DTYPE_SFIXED64 = 16,
288 UPB_DTYPE_SINT32 = 17,
289 UPB_DTYPE_SINT64 = 18
290 } upb_descriptortype_t;
292 #define UPB_MAP_BEGIN ((size_t)-1)
294 UPB_INLINE bool _upb_isle(void) {
296 return *(char*)&x == 1;
299 UPB_INLINE uint32_t _upb_be_swap32(uint32_t val) {
303 return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
304 ((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
308 UPB_INLINE uint64_t _upb_be_swap64(uint64_t val) {
312 return ((uint64_t)_upb_be_swap32(val) << 32) | _upb_be_swap32(val >> 32);
316 UPB_INLINE int _upb_lg2ceil(int x) {
317 if (x <= 1) return 0;
319 return 32 - __builtin_clz(x - 1);
322 while (1 << lg2 < x) lg2++;
327 UPB_INLINE int _upb_lg2ceilsize(int x) {
328 return 1 << _upb_lg2ceil(x);
331 #include "upb/port_undef.inc"