2 ** Functions for use by generated code. These are not public and users must
3 ** not call them directly.
6 #ifndef UPB_GENERATED_UTIL_H_
7 #define UPB_GENERATED_UTIL_H_
12 #include "upb/port_def.inc"
14 #define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs)
16 UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs,
18 const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*);
20 if (size) *size = arr->len;
28 UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
30 upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
32 if (size) *size = arr->len;
40 /* TODO(haberman): this is a mess. It will improve when upb_array no longer
41 * carries reflective state (type, elem_size). */
42 UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
46 upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
49 arr = upb_array_new(arena);
50 if (!arr) return NULL;
51 *PTR_AT(msg, ofs, upb_array*) = arr;
54 if (size > arr->size) {
55 size_t new_size = UPB_MAX(arr->size, 4);
56 size_t old_bytes = arr->size * elem_size;
58 while (new_size < size) new_size *= 2;
59 new_bytes = new_size * elem_size;
60 arr->data = upb_arena_realloc(arena, arr->data, old_bytes, new_bytes);
71 UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
76 upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
77 size_t i = arr ? arr->len : 0;
79 _upb_array_resize_accessor(msg, ofs, i + 1, elem_size, type, arena);
80 if (!data) return false;
81 memcpy(PTR_AT(data, i * elem_size, char), value, elem_size);
85 UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) {
86 return (*PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0;
89 UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) {
90 return (*PTR_AT(msg, idx / 8, char)) |= (char)(1 << (idx % 8));
93 UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) {
94 return (*PTR_AT(msg, idx / 8, char)) &= (char)(~(1 << (idx % 8)));
97 UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) {
98 return *PTR_AT(msg, case_ofs, int32_t) == num;
103 #include "upb/port_undef.inc"
105 #endif /* UPB_GENERATED_UTIL_H_ */