upb.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * This file contains shared definitions that are widely used across upb.
  29. */
  30. #ifndef UPB_H_
  31. #define UPB_H_
  32. #include <assert.h>
  33. #include <stdarg.h>
  34. #include <stdbool.h>
  35. #include <stddef.h>
  36. #include <stdint.h>
  37. #include <string.h>
  38. #include "upb/port_def.inc"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /* upb_Status *****************************************************************/
  43. #define _kUpb_Status_MaxMessage 127
  44. typedef struct {
  45. bool ok;
  46. char msg[_kUpb_Status_MaxMessage]; /* Error message; NULL-terminated. */
  47. } upb_Status;
  48. const char* upb_Status_ErrorMessage(const upb_Status* status);
  49. bool upb_Status_IsOk(const upb_Status* status);
  50. /* These are no-op if |status| is NULL. */
  51. void upb_Status_Clear(upb_Status* status);
  52. void upb_Status_SetErrorMessage(upb_Status* status, const char* msg);
  53. void upb_Status_SetErrorFormat(upb_Status* status, const char* fmt, ...)
  54. UPB_PRINTF(2, 3);
  55. void upb_Status_VSetErrorFormat(upb_Status* status, const char* fmt,
  56. va_list args) UPB_PRINTF(2, 0);
  57. void upb_Status_VAppendErrorFormat(upb_Status* status, const char* fmt,
  58. va_list args) UPB_PRINTF(2, 0);
  59. /** upb_StringView ************************************************************/
  60. typedef struct {
  61. const char* data;
  62. size_t size;
  63. } upb_StringView;
  64. UPB_INLINE upb_StringView upb_StringView_FromDataAndSize(const char* data,
  65. size_t size) {
  66. upb_StringView ret;
  67. ret.data = data;
  68. ret.size = size;
  69. return ret;
  70. }
  71. UPB_INLINE upb_StringView upb_StringView_FromString(const char* data) {
  72. return upb_StringView_FromDataAndSize(data, strlen(data));
  73. }
  74. UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b) {
  75. return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
  76. }
  77. #define UPB_STRINGVIEW_INIT(ptr, len) \
  78. { ptr, len }
  79. #define UPB_STRINGVIEW_FORMAT "%.*s"
  80. #define UPB_STRINGVIEW_ARGS(view) (int)(view).size, (view).data
  81. /** upb_alloc *****************************************************************/
  82. /* A upb_alloc is a possibly-stateful allocator object.
  83. *
  84. * It could either be an arena allocator (which doesn't require individual
  85. * free() calls) or a regular malloc() (which does). The client must therefore
  86. * free memory unless it knows that the allocator is an arena allocator. */
  87. struct upb_alloc;
  88. typedef struct upb_alloc upb_alloc;
  89. /* A malloc()/free() function.
  90. * If "size" is 0 then the function acts like free(), otherwise it acts like
  91. * realloc(). Only "oldsize" bytes from a previous allocation are preserved. */
  92. typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize,
  93. size_t size);
  94. struct upb_alloc {
  95. upb_alloc_func* func;
  96. };
  97. UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size) {
  98. UPB_ASSERT(alloc);
  99. return alloc->func(alloc, NULL, 0, size);
  100. }
  101. UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr, size_t oldsize,
  102. size_t size) {
  103. UPB_ASSERT(alloc);
  104. return alloc->func(alloc, ptr, oldsize, size);
  105. }
  106. UPB_INLINE void upb_free(upb_alloc* alloc, void* ptr) {
  107. assert(alloc);
  108. alloc->func(alloc, ptr, 0, 0);
  109. }
  110. /* The global allocator used by upb. Uses the standard malloc()/free(). */
  111. extern upb_alloc upb_alloc_global;
  112. /* Functions that hard-code the global malloc.
  113. *
  114. * We still get benefit because we can put custom logic into our global
  115. * allocator, like injecting out-of-memory faults in debug/testing builds. */
  116. UPB_INLINE void* upb_gmalloc(size_t size) {
  117. return upb_malloc(&upb_alloc_global, size);
  118. }
  119. UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize, size_t size) {
  120. return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
  121. }
  122. UPB_INLINE void upb_gfree(void* ptr) { upb_free(&upb_alloc_global, ptr); }
  123. /* upb_Arena ******************************************************************/
  124. /* upb_Arena is a specific allocator implementation that uses arena allocation.
  125. * The user provides an allocator that will be used to allocate the underlying
  126. * arena blocks. Arenas by nature do not require the individual allocations
  127. * to be freed. However the Arena does allow users to register cleanup
  128. * functions that will run when the arena is destroyed.
  129. *
  130. * A upb_Arena is *not* thread-safe.
  131. *
  132. * You could write a thread-safe arena allocator that satisfies the
  133. * upb_alloc interface, but it would not be as efficient for the
  134. * single-threaded case. */
  135. typedef void upb_CleanupFunc(void* ud);
  136. struct upb_Arena;
  137. typedef struct upb_Arena upb_Arena;
  138. typedef struct {
  139. /* We implement the allocator interface.
  140. * This must be the first member of upb_Arena!
  141. * TODO(haberman): remove once handlers are gone. */
  142. upb_alloc alloc;
  143. char *ptr, *end;
  144. } _upb_ArenaHead;
  145. /* Creates an arena from the given initial block (if any -- n may be 0).
  146. * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this
  147. * is a fixed-size arena and cannot grow. */
  148. upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc);
  149. void upb_Arena_Free(upb_Arena* a);
  150. bool upb_Arena_AddCleanup(upb_Arena* a, void* ud, upb_CleanupFunc* func);
  151. bool upb_Arena_Fuse(upb_Arena* a, upb_Arena* b);
  152. void* _upb_Arena_SlowMalloc(upb_Arena* a, size_t size);
  153. UPB_INLINE upb_alloc* upb_Arena_Alloc(upb_Arena* a) { return (upb_alloc*)a; }
  154. UPB_INLINE size_t _upb_ArenaHas(upb_Arena* a) {
  155. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  156. return (size_t)(h->end - h->ptr);
  157. }
  158. UPB_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) {
  159. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  160. void* ret;
  161. size = UPB_ALIGN_MALLOC(size);
  162. if (UPB_UNLIKELY(_upb_ArenaHas(a) < size)) {
  163. return _upb_Arena_SlowMalloc(a, size);
  164. }
  165. ret = h->ptr;
  166. h->ptr += size;
  167. UPB_UNPOISON_MEMORY_REGION(ret, size);
  168. #if UPB_ASAN
  169. {
  170. size_t guard_size = 32;
  171. if (_upb_ArenaHas(a) >= guard_size) {
  172. h->ptr += guard_size;
  173. } else {
  174. h->ptr = h->end;
  175. }
  176. }
  177. #endif
  178. return ret;
  179. }
  180. UPB_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize,
  181. size_t size) {
  182. void* ret = upb_Arena_Malloc(a, size);
  183. if (ret && oldsize > 0) {
  184. memcpy(ret, ptr, oldsize);
  185. }
  186. return ret;
  187. }
  188. UPB_INLINE upb_Arena* upb_Arena_New(void) {
  189. return upb_Arena_Init(NULL, 0, &upb_alloc_global);
  190. }
  191. /* Constants ******************************************************************/
  192. /* A list of types as they are encoded on-the-wire. */
  193. typedef enum {
  194. kUpb_WireType_Varint = 0,
  195. kUpb_WireType_64Bit = 1,
  196. kUpb_WireType_Delimited = 2,
  197. kUpb_WireType_StartGroup = 3,
  198. kUpb_WireType_EndGroup = 4,
  199. kUpb_WireType_32Bit = 5
  200. } upb_WireType;
  201. /* The types a field can have. Note that this list is not identical to the
  202. * types defined in descriptor.proto, which gives INT32 and SINT32 separate
  203. * types (we distinguish the two with the "integer encoding" enum below). */
  204. typedef enum {
  205. kUpb_CType_Bool = 1,
  206. kUpb_CType_Float = 2,
  207. kUpb_CType_Int32 = 3,
  208. kUpb_CType_UInt32 = 4,
  209. kUpb_CType_Enum = 5, /* Enum values are int32. */
  210. kUpb_CType_Message = 6,
  211. kUpb_CType_Double = 7,
  212. kUpb_CType_Int64 = 8,
  213. kUpb_CType_UInt64 = 9,
  214. kUpb_CType_String = 10,
  215. kUpb_CType_Bytes = 11
  216. } upb_CType;
  217. /* The repeated-ness of each field; this matches descriptor.proto. */
  218. typedef enum {
  219. kUpb_Label_Optional = 1,
  220. kUpb_Label_Required = 2,
  221. kUpb_Label_Repeated = 3
  222. } upb_Label;
  223. /* Descriptor types, as defined in descriptor.proto. */
  224. typedef enum {
  225. kUpb_FieldType_Double = 1,
  226. kUpb_FieldType_Float = 2,
  227. kUpb_FieldType_Int64 = 3,
  228. kUpb_FieldType_UInt64 = 4,
  229. kUpb_FieldType_Int32 = 5,
  230. kUpb_FieldType_Fixed64 = 6,
  231. kUpb_FieldType_Fixed32 = 7,
  232. kUpb_FieldType_Bool = 8,
  233. kUpb_FieldType_String = 9,
  234. kUpb_FieldType_Group = 10,
  235. kUpb_FieldType_Message = 11,
  236. kUpb_FieldType_Bytes = 12,
  237. kUpb_FieldType_UInt32 = 13,
  238. kUpb_FieldType_Enum = 14,
  239. kUpb_FieldType_SFixed32 = 15,
  240. kUpb_FieldType_SFixed64 = 16,
  241. kUpb_FieldType_SInt32 = 17,
  242. kUpb_FieldType_SInt64 = 18
  243. } upb_FieldType;
  244. #define kUpb_Map_Begin ((size_t)-1)
  245. UPB_INLINE bool _upb_IsLittleEndian(void) {
  246. int x = 1;
  247. return *(char*)&x == 1;
  248. }
  249. UPB_INLINE uint32_t _upb_BigEndian_Swap32(uint32_t val) {
  250. if (_upb_IsLittleEndian()) {
  251. return val;
  252. } else {
  253. return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
  254. ((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
  255. }
  256. }
  257. UPB_INLINE uint64_t _upb_BigEndian_Swap64(uint64_t val) {
  258. if (_upb_IsLittleEndian()) {
  259. return val;
  260. } else {
  261. return ((uint64_t)_upb_BigEndian_Swap32(val) << 32) |
  262. _upb_BigEndian_Swap32(val >> 32);
  263. }
  264. }
  265. UPB_INLINE int _upb_Log2Ceiling(int x) {
  266. if (x <= 1) return 0;
  267. #ifdef __GNUC__
  268. return 32 - __builtin_clz(x - 1);
  269. #else
  270. int lg2 = 0;
  271. while (1 << lg2 < x) lg2++;
  272. return lg2;
  273. #endif
  274. }
  275. UPB_INLINE int _upb_Log2Ceilingsize(int x) { return 1 << _upb_Log2Ceiling(x); }
  276. #include "upb/port_undef.inc"
  277. #ifdef __cplusplus
  278. } /* extern "C" */
  279. #endif
  280. #endif /* UPB_H_ */