From d5566c6038845e505f7c16130b2368ef9bb3a373 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Fri, 15 Jan 2010 19:11:54 -0800 Subject: Remove struct keyword from all types, use typedef instead. --- src/upb.c | 4 +- src/upb.h | 22 ++--- src/upb_data.c | 52 ++++++------ src/upb_data.h | 43 +++++----- src/upb_decoder.c | 67 +++++++-------- src/upb_decoder.h | 5 +- src/upb_def.c | 250 ++++++++++++++++++++++++++---------------------------- src/upb_def.h | 198 +++++++++++++++++++++--------------------- src/upb_encoder.c | 16 ++-- src/upb_encoder.h | 3 - src/upb_sink.h | 12 +-- src/upb_table.c | 68 +++++++-------- src/upb_table.h | 84 +++++++++--------- src/upb_text.c | 30 +++---- src/upb_text.h | 12 +-- 15 files changed, 413 insertions(+), 453 deletions(-) (limited to 'src') diff --git a/src/upb.c b/src/upb.c index 2412be3..4cd4bba 100644 --- a/src/upb.c +++ b/src/upb.c @@ -15,7 +15,7 @@ [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ ## proto_type] = \ {alignof(ctype), sizeof(ctype), wire_type, #ctype}, -struct upb_type_info upb_type_info[] = { +upb_type_info upb_types[] = { TYPE_INFO(DOUBLE, UPB_WIRE_TYPE_64BIT, double) TYPE_INFO(FLOAT, UPB_WIRE_TYPE_32BIT, float) TYPE_INFO(INT64, UPB_WIRE_TYPE_VARINT, int64_t) @@ -36,7 +36,7 @@ struct upb_type_info upb_type_info[] = { TYPE_INFO(BYTES, UPB_WIRE_TYPE_DELIMITED, union upb_string*) }; -void upb_seterr(struct upb_status *status, enum upb_status_code code, +void upb_seterr(upb_status *status, enum upb_status_code code, const char *msg, ...) { if(upb_ok(status)) { // The first error is the most interesting. diff --git a/src/upb.h b/src/upb.h index 4242cdc..d330ad4 100644 --- a/src/upb.h +++ b/src/upb.h @@ -91,15 +91,15 @@ INLINE bool upb_isstringtype(upb_field_type_t type) { } // Info for a given field type. -struct upb_type_info { +typedef struct { uint8_t align; uint8_t size; upb_wire_type_t expected_wire_type; char *ctype; -}; +} upb_type_info; // A static array of info about all of the field types, indexed by type number. -extern struct upb_type_info upb_type_info[]; +extern upb_type_info upb_types[]; // The number of a field, eg. "optional string foo = 3". typedef int32_t upb_field_number_t; @@ -117,10 +117,10 @@ union upb_wire_value { }; // A tag occurs before each value on-the-wire. -struct upb_tag { +typedef struct { upb_field_number_t field_number; upb_wire_type_t wire_type; -}; +} upb_tag; /* Polymorphic values of .proto types *****************************************/ @@ -284,24 +284,24 @@ enum upb_status_code { }; #define UPB_ERRORMSG_MAXLEN 256 -struct upb_status { +typedef struct { enum upb_status_code code; char msg[UPB_ERRORMSG_MAXLEN]; -}; +} upb_status; #define UPB_STATUS_INIT {UPB_STATUS_OK, ""} -INLINE bool upb_ok(struct upb_status *status) { +INLINE bool upb_ok(upb_status *status) { return status->code == UPB_STATUS_OK; } -INLINE void upb_reset(struct upb_status *status) { +INLINE void upb_reset(upb_status *status) { status->code = UPB_STATUS_OK; status->msg[0] = '\0'; } -void upb_seterr(struct upb_status *status, enum upb_status_code code, - const char *msg, ...); +void upb_seterr(upb_status *status, enum upb_status_code code, const char *msg, + ...); #ifdef __cplusplus } /* extern "C" */ diff --git a/src/upb_data.c b/src/upb_data.c index a7027d6..4008df7 100644 --- a/src/upb_data.c +++ b/src/upb_data.c @@ -24,7 +24,7 @@ static uint32_t round_up_to_pow2(uint32_t v) /* upb_data *******************************************************************/ -static void data_elem_unref(union upb_value_ptr p, struct upb_fielddef *f) { +static void data_elem_unref(union upb_value_ptr p, upb_fielddef *f) { if(upb_issubmsg(f)) { upb_msg_unref(*p.msg, upb_downcast_msgdef(f->def)); } else if(upb_isstring(f)) { @@ -34,7 +34,7 @@ static void data_elem_unref(union upb_value_ptr p, struct upb_fielddef *f) { } } -static void data_unref(union upb_value_ptr p, struct upb_fielddef *f) { +static void data_unref(union upb_value_ptr p, upb_fielddef *f) { if(upb_isarray(f)) { upb_array_unref(*p.arr, f); } else { @@ -208,7 +208,7 @@ upb_arrayptr upb_array_new() { } // ONLY handles refcounted arrays for the moment. -void _upb_array_free(upb_arrayptr a, struct upb_fielddef *f) +void _upb_array_free(upb_arrayptr a, upb_fielddef *f) { if(upb_elem_ismm(f)) { for(upb_arraylen_t i = 0; i < a.refcounted->size; i++) { @@ -238,9 +238,9 @@ static void array_set_size(upb_arrayptr a, upb_arraylen_t newsize) { } } -void upb_array_resize(upb_arrayptr a, struct upb_fielddef *f, upb_strlen_t len) { +void upb_array_resize(upb_arrayptr a, upb_fielddef *f, upb_strlen_t len) { check_not_frozen(a.base); - size_t type_size = upb_type_info[f->type].size; + size_t type_size = upb_types[f->type].size; upb_arraylen_t old_size = array_get_size(a); if(old_size < len) { // Need to resize. @@ -257,11 +257,11 @@ void upb_array_resize(upb_arrayptr a, struct upb_fielddef *f, upb_strlen_t len) /* upb_msg ********************************************************************/ -static void upb_msg_sethas(upb_msg *msg, struct upb_fielddef *f) { +static void upb_msg_sethas(upb_msg *msg, upb_fielddef *f) { msg->data[f->field_index/8] |= (1 << (f->field_index % 8)); } -upb_msg *upb_msg_new(struct upb_msgdef *md) { +upb_msg *upb_msg_new(upb_msgdef *md) { upb_msg *msg = malloc(md->size); memset(msg, 0, md->size); data_init(&msg->base, UPB_DATA_HEAPALLOCATED | UPB_DATA_REFCOUNTED); @@ -270,10 +270,10 @@ upb_msg *upb_msg_new(struct upb_msgdef *md) { } // ONLY handles refcounted messages for the moment. -void _upb_msg_free(upb_msg *msg, struct upb_msgdef *md) +void _upb_msg_free(upb_msg *msg, upb_msgdef *md) { for(int i = 0; i < md->num_fields; i++) { - struct upb_fielddef *f = &md->fields[i]; + upb_fielddef *f = &md->fields[i]; union upb_value_ptr p = _upb_msg_getptr(msg, f); if(!upb_field_ismm(f) || !*p.data) continue; data_unref(p, f); @@ -282,8 +282,8 @@ void _upb_msg_free(upb_msg *msg, struct upb_msgdef *md) free(msg); } -void upb_msg_decodestr(upb_msg *msg, struct upb_msgdef *md, upb_strptr str, - struct upb_status *status) +void upb_msg_decodestr(upb_msg *msg, upb_msgdef *md, upb_strptr str, + upb_status *status) { upb_decoder *d = upb_decoder_new(md); upb_msgsink *s = upb_msgsink_new(md); @@ -300,7 +300,7 @@ void upb_msg_decodestr(upb_msg *msg, struct upb_msgdef *md, upb_strptr str, /* upb_msgsrc ****************************************************************/ -static void _upb_msgsrc_produceval(union upb_value v, struct upb_fielddef *f, +static void _upb_msgsrc_produceval(union upb_value v, upb_fielddef *f, upb_sink *sink, bool reverse) { if(upb_issubmsg(f)) { @@ -314,11 +314,11 @@ static void _upb_msgsrc_produceval(union upb_value v, struct upb_fielddef *f, } } -void upb_msgsrc_produce(upb_msg *msg, struct upb_msgdef *md, upb_sink *sink, +void upb_msgsrc_produce(upb_msg *msg, upb_msgdef *md, upb_sink *sink, bool reverse) { for(int i = 0; i < md->num_fields; i++) { - struct upb_fielddef *f = &md->fields[reverse ? md->num_fields - i - 1 : i]; + upb_fielddef *f = &md->fields[reverse ? md->num_fields - i - 1 : i]; if(!upb_msg_has(msg, f)) continue; union upb_value v = upb_msg_get(msg, f); if(upb_isarray(f)) { @@ -337,21 +337,21 @@ void upb_msgsrc_produce(upb_msg *msg, struct upb_msgdef *md, upb_sink *sink, /* upb_msgsink ***************************************************************/ -struct upb_msgsink_frame { +typedef struct { upb_msg *msg; - struct upb_msgdef *md; -}; + upb_msgdef *md; +} upb_msgsink_frame; struct upb_msgsink { upb_sink base; - struct upb_msgdef *toplevel_msgdef; - struct upb_msgsink_frame stack[UPB_MAX_NESTING], *top; + upb_msgdef *toplevel_msgdef; + upb_msgsink_frame stack[UPB_MAX_NESTING], *top; }; /* Helper function that returns a pointer to where the next value for field "f" * should be stored, taking into account whether f is an array that may need to * be allocated or resized. */ -static union upb_value_ptr get_value_ptr(upb_msg *msg, struct upb_fielddef *f) +static union upb_value_ptr get_value_ptr(upb_msg *msg, upb_fielddef *f) { union upb_value_ptr p = _upb_msg_getptr(msg, f); if(upb_isarray(f)) { @@ -376,7 +376,7 @@ static union upb_value_ptr get_value_ptr(upb_msg *msg, struct upb_fielddef *f) // Callbacks for upb_sink. // TODO: implement these in terms of public interfaces. -static upb_sink_status _upb_msgsink_valuecb(upb_sink *s, struct upb_fielddef *f, +static upb_sink_status _upb_msgsink_valuecb(upb_sink *s, upb_fielddef *f, union upb_value val) { upb_msgsink *ms = (upb_msgsink*)s; @@ -387,7 +387,7 @@ static upb_sink_status _upb_msgsink_valuecb(upb_sink *s, struct upb_fielddef *f, return UPB_SINK_CONTINUE; } -static upb_sink_status _upb_msgsink_strcb(upb_sink *s, struct upb_fielddef *f, +static upb_sink_status _upb_msgsink_strcb(upb_sink *s, upb_fielddef *f, upb_strptr str, int32_t start, uint32_t end) { @@ -405,14 +405,14 @@ static upb_sink_status _upb_msgsink_strcb(upb_sink *s, struct upb_fielddef *f, return UPB_SINK_CONTINUE; } -static upb_sink_status _upb_msgsink_startcb(upb_sink *s, struct upb_fielddef *f) +static upb_sink_status _upb_msgsink_startcb(upb_sink *s, upb_fielddef *f) { upb_msgsink *ms = (upb_msgsink*)s; upb_msg *oldmsg = ms->top->msg; union upb_value_ptr p = get_value_ptr(oldmsg, f); if(upb_isarray(f) || !upb_msg_has(oldmsg, f)) { - struct upb_msgdef *md = upb_downcast_msgdef(f->def); + upb_msgdef *md = upb_downcast_msgdef(f->def); if(!*p.msg || !upb_data_only(*p.data)) { if(*p.msg) upb_msg_unref(*p.msg, md); @@ -427,7 +427,7 @@ static upb_sink_status _upb_msgsink_startcb(upb_sink *s, struct upb_fielddef *f) return UPB_SINK_CONTINUE; } -static upb_sink_status _upb_msgsink_endcb(upb_sink *s, struct upb_fielddef *f) +static upb_sink_status _upb_msgsink_endcb(upb_sink *s, upb_fielddef *f) { (void)f; // Unused. upb_msgsink *ms = (upb_msgsink*)s; @@ -446,7 +446,7 @@ static upb_sink_callbacks _upb_msgsink_vtbl = { // External upb_msgsink interface. // -upb_msgsink *upb_msgsink_new(struct upb_msgdef *md) +upb_msgsink *upb_msgsink_new(upb_msgdef *md) { upb_msgsink *ms = malloc(sizeof(*ms)); upb_sink_init(&ms->base, &_upb_msgsink_vtbl); diff --git a/src/upb_data.h b/src/upb_data.h index a11b84a..8c050a2 100644 --- a/src/upb_data.h +++ b/src/upb_data.h @@ -32,9 +32,6 @@ extern "C" { #endif -struct upb_msgdef; -struct upb_fielddef; - /* upb_data *******************************************************************/ // The "base class" of strings, arrays, and messages. Contains a few flags and @@ -398,7 +395,7 @@ INLINE size_t upb_array_len(upb_arrayptr a) { // INTERNAL-ONLY: // Frees the given message and releases references on members. -void _upb_array_free(upb_arrayptr a, struct upb_fielddef *f); +void _upb_array_free(upb_arrayptr a, upb_fielddef *f); // INTERNAL-ONLY: // Returns a pointer to the given elem. @@ -414,12 +411,12 @@ INLINE union upb_value_ptr _upb_array_getptr_raw(upb_arrayptr a, } INLINE union upb_value_ptr _upb_array_getptr(upb_arrayptr a, - struct upb_fielddef *f, + upb_fielddef *f, upb_arraylen_t elem) { - return _upb_array_getptr_raw(a, elem, upb_type_info[f->type].size); + return _upb_array_getptr_raw(a, elem, upb_types[f->type].size); } -INLINE union upb_value upb_array_get(upb_arrayptr a, struct upb_fielddef *f, +INLINE union upb_value upb_array_get(upb_arrayptr a, upb_fielddef *f, upb_arraylen_t elem) { assert(elem < upb_array_len(a)); return upb_value_read(_upb_array_getptr(a, f, elem), f->type); @@ -427,7 +424,7 @@ INLINE union upb_value upb_array_get(upb_arrayptr a, struct upb_fielddef *f, // The caller releases a ref on the given array, which it must previously have // owned a ref on. -INLINE void upb_array_unref(upb_arrayptr a, struct upb_fielddef *f) { +INLINE void upb_array_unref(upb_arrayptr a, upb_fielddef *f) { if(_upb_data_unref(a.base)) _upb_array_free(a, f); } @@ -440,15 +437,15 @@ INLINE upb_arrayptr upb_array_getref(upb_arrayptr src, int ref_flags); // Sets the given element in the array to val. The current length of the array // must be greater than elem. If the field type is dynamic, the array will // take a ref on val and release a ref on what was previously in the array. -INLINE void upb_array_set(upb_arrayptr a, struct upb_fielddef *f, int elem, +INLINE void upb_array_set(upb_arrayptr a, upb_fielddef *f, int elem, union upb_value val); // Note that array_append will attempt to take a reference on the given value, // so to avoid a copy use append_default and get. -INLINE void upb_array_append(upb_arrayptr a, struct upb_fielddef *f, +INLINE void upb_array_append(upb_arrayptr a, upb_fielddef *f, union upb_value val); -INLINE void upb_array_append_default(upb_arrayptr a, struct upb_fielddef *f, +INLINE void upb_array_append_default(upb_arrayptr a, upb_fielddef *f, union upb_value val); #endif @@ -471,34 +468,34 @@ struct _upb_msg { }; // Creates a new msg of the given type. -upb_msg *upb_msg_new(struct upb_msgdef *md); +upb_msg *upb_msg_new(upb_msgdef *md); // INTERNAL-ONLY: // Frees the given message and releases references on members. -void _upb_msg_free(upb_msg *msg, struct upb_msgdef *md); +void _upb_msg_free(upb_msg *msg, upb_msgdef *md); // INTERNAL-ONLY: // Returns a pointer to the given field. -INLINE union upb_value_ptr _upb_msg_getptr(upb_msg *msg, struct upb_fielddef *f) { +INLINE union upb_value_ptr _upb_msg_getptr(upb_msg *msg, upb_fielddef *f) { union upb_value_ptr p; p._void = &msg->data[f->byte_offset]; return p; } // Releases a references on msg. -INLINE void upb_msg_unref(upb_msg *msg, struct upb_msgdef *md) { +INLINE void upb_msg_unref(upb_msg *msg, upb_msgdef *md) { if(_upb_data_unref(&msg->base)) _upb_msg_free(msg, md); } // Tests whether the given field is explicitly set, or whether it will return // a default. -INLINE bool upb_msg_has(upb_msg *msg, struct upb_fielddef *f) { +INLINE bool upb_msg_has(upb_msg *msg, upb_fielddef *f) { return (msg->data[f->field_index/8] & (1 << (f->field_index % 8))) != 0; } // Returns the current value if set, or the default value if not set, of the // specified field. The caller does *not* own a ref. -INLINE union upb_value upb_msg_get(upb_msg *msg, struct upb_fielddef *f) { +INLINE union upb_value upb_msg_get(upb_msg *msg, upb_fielddef *f) { if(upb_msg_has(msg, f)) { return upb_value_read(_upb_msg_getptr(msg, f), f->type); } else { @@ -508,23 +505,23 @@ INLINE union upb_value upb_msg_get(upb_msg *msg, struct upb_fielddef *f) { // Sets the given field to the given value. The msg will take a ref on val, // and will drop a ref on whatever was there before. -void upb_msg_set(upb_msg *msg, struct upb_fielddef *f, union upb_value val); +void upb_msg_set(upb_msg *msg, upb_fielddef *f, union upb_value val); -INLINE void upb_msg_clear(upb_msg *msg, struct upb_msgdef *md) { +INLINE void upb_msg_clear(upb_msg *msg, upb_msgdef *md) { memset(msg->data, 0, md->set_flags_bytes); } // A convenience function for parsing an entire protobuf all at once, without // having to worry about setting up the appropriate objects. -void upb_msg_decodestr(upb_msg *msg, struct upb_msgdef *md, upb_strptr str, - struct upb_status *status); +void upb_msg_decodestr(upb_msg *msg, upb_msgdef *md, upb_strptr str, + upb_status *status); /* upb_msgsrc *****************************************************************/ // A nonresumable, non-interruptable (but simple and fast) source for pushing // the data of a upb_msg to a upb_sink. -void upb_msgsrc_produce(upb_msg *msg, struct upb_msgdef *md, upb_sink *sink, +void upb_msgsrc_produce(upb_msg *msg, upb_msgdef *md, upb_sink *sink, bool reverse); @@ -535,7 +532,7 @@ struct upb_msgsink; typedef struct upb_msgsink upb_msgsink; // Allocate and free a msgsink, respectively. -upb_msgsink *upb_msgsink_new(struct upb_msgdef *md); +upb_msgsink *upb_msgsink_new(upb_msgdef *md); void upb_msgsink_free(upb_msgsink *sink); // Returns the upb_sink (like an upcast). diff --git a/src/upb_decoder.c b/src/upb_decoder.c index ff9753d..464e2b9 100644 --- a/src/upb_decoder.c +++ b/src/upb_decoder.c @@ -19,13 +19,11 @@ // The same applies to the functions to read .proto values below. const uint8_t *upb_get_v_uint64_t_full(const uint8_t *buf, const uint8_t *end, - uint64_t *val, - struct upb_status *status); + uint64_t *val, upb_status *status); // Gets a varint (wire type: UPB_WIRE_TYPE_VARINT). INLINE const uint8_t *upb_get_v_uint64_t(const uint8_t *buf, const uint8_t *end, - uint64_t *val, - struct upb_status *status) + uint64_t *val, upb_status *status) { // We inline this common case (1-byte varints), if that fails we dispatch to // the full (non-inlined) version. @@ -40,8 +38,7 @@ INLINE const uint8_t *upb_get_v_uint64_t(const uint8_t *buf, const uint8_t *end, // Gets a varint -- called when we only need 32 bits of it. Note that a 32-bit // varint is not a true wire type. INLINE const uint8_t *upb_get_v_uint32_t(const uint8_t *buf, const uint8_t *end, - uint32_t *val, - struct upb_status *status) + uint32_t *val, upb_status *status) { uint64_t val64; const uint8_t *ret = upb_get_v_uint64_t(buf, end, &val64, status); @@ -51,7 +48,7 @@ INLINE const uint8_t *upb_get_v_uint32_t(const uint8_t *buf, const uint8_t *end, // Gets a fixed-length 32-bit integer (wire type: UPB_WIRE_TYPE_32BIT). INLINE const uint8_t *upb_get_f_uint32_t(const uint8_t *buf, const uint8_t *end, - uint32_t *val, struct upb_status *status) + uint32_t *val, upb_status *status) { const uint8_t *uint32_end = buf + sizeof(uint32_t); if(uint32_end > end) { @@ -70,8 +67,7 @@ INLINE const uint8_t *upb_get_f_uint32_t(const uint8_t *buf, const uint8_t *end, // Gets a fixed-length 64-bit integer (wire type: UPB_WIRE_TYPE_64BIT). INLINE const uint8_t *upb_get_f_uint64_t(const uint8_t *buf, const uint8_t *end, - uint64_t *val, - struct upb_status *status) + uint64_t *val, upb_status *status) { const uint8_t *uint64_end = buf + sizeof(uint64_t); if(uint64_end > end) { @@ -91,7 +87,7 @@ INLINE const uint8_t *upb_get_f_uint64_t(const uint8_t *buf, const uint8_t *end, INLINE const uint8_t *upb_skip_v_uint64_t(const uint8_t *buf, const uint8_t *end, - struct upb_status *status) + upb_status *status) { const uint8_t *const maxend = buf + 10; uint8_t last = 0x80; @@ -110,7 +106,7 @@ INLINE const uint8_t *upb_skip_v_uint64_t(const uint8_t *buf, INLINE const uint8_t *upb_skip_f_uint32_t(const uint8_t *buf, const uint8_t *end, - struct upb_status *status) + upb_status *status) { const uint8_t *uint32_end = buf + sizeof(uint32_t); if(uint32_end > end) { @@ -122,7 +118,7 @@ INLINE const uint8_t *upb_skip_f_uint32_t(const uint8_t *buf, INLINE const uint8_t *upb_skip_f_uint64_t(const uint8_t *buf, const uint8_t *end, - struct upb_status *status) + upb_status *status) { const uint8_t *uint64_end = buf + sizeof(uint64_t); if(uint64_end > end) { @@ -146,7 +142,7 @@ INLINE int64_t upb_zzdec_64(uint64_t n) { return (n >> 1) ^ -(int64_t)(n & 1); } // // On success, a pointer will be returned to the first byte that was // // not consumed. // const uint8_t *upb_get_INT32(const uint8_t *buf, const uint8_t *end, -// int32_t *d, struct upb_status *status); +// int32_t *d, upb_status *status); // // // Given an already read wire value s (source), convert it to a .proto // // value and return it. @@ -160,7 +156,7 @@ INLINE int64_t upb_zzdec_64(uint64_t n) { return (n >> 1) ^ -(int64_t)(n & 1); } #define GET(type, v_or_f, wire_t, val_t, member_name) \ INLINE const uint8_t *upb_get_ ## type(const uint8_t *buf, const uint8_t *end, \ - val_t *d, struct upb_status *status) { \ + val_t *d, upb_status *status) { \ wire_t tmp = 0; \ const uint8_t *ret = upb_get_ ## v_or_f ## _ ## wire_t(buf, end, &tmp, status); \ *d = upb_wvtov_ ## type(tmp); \ @@ -201,7 +197,7 @@ T(FLOAT, f, uint32_t, float, _float) { // Parses a tag, places the result in *tag. INLINE const uint8_t *decode_tag(const uint8_t *buf, const uint8_t *end, - struct upb_tag *tag, struct upb_status *status) + upb_tag *tag, upb_status *status) { uint32_t tag_int; const uint8_t *ret = upb_get_v_uint32_t(buf, end, &tag_int, status); @@ -216,7 +212,7 @@ INLINE const uint8_t *decode_tag(const uint8_t *buf, const uint8_t *end, * handles 1 and 2 byte varints). */ const uint8_t *upb_get_v_uint64_t_full(const uint8_t *buf, const uint8_t *end, - uint64_t *val, struct upb_status *status) + uint64_t *val, upb_status *status) { const uint8_t *const maxend = buf + 10; uint8_t last = 0x80; @@ -242,7 +238,7 @@ const uint8_t *upb_get_v_uint64_t_full(const uint8_t *buf, const uint8_t *end, const uint8_t *upb_decode_wire_value(uint8_t *buf, uint8_t *end, upb_wire_type_t wt, union upb_wire_value *wv, - struct upb_status *status) + upb_status *status) { switch(wt) { case UPB_WIRE_TYPE_VARINT: @@ -262,8 +258,7 @@ const uint8_t *upb_decode_wire_value(uint8_t *buf, uint8_t *end, * outbuf. */ static const uint8_t *skip_wire_value(const uint8_t *buf, const uint8_t *end, - upb_wire_type_t wt, - struct upb_status *status) + upb_wire_type_t wt, upb_status *status) { switch(wt) { case UPB_WIRE_TYPE_VARINT: @@ -285,7 +280,7 @@ static const uint8_t *skip_wire_value(const uint8_t *buf, const uint8_t *end, static const uint8_t *upb_decode_value(const uint8_t *buf, const uint8_t *end, upb_field_type_t ft, union upb_value_ptr v, - struct upb_status *status) + upb_status *status) { #define CASE(t, member_name) \ case UPB_TYPE(t): return upb_get_ ## t(buf, end, v.member_name, status); @@ -311,25 +306,27 @@ static const uint8_t *upb_decode_value(const uint8_t *buf, const uint8_t *end, #undef CASE } -struct upb_decoder_frame { - struct upb_msgdef *msgdef; - struct upb_fielddef *field; +// The decoder keeps a stack with one entry per level of recursion. +// upb_decoder_frame is one frame of that stack. +typedef struct { + upb_msgdef *msgdef; + upb_fielddef *field; size_t end_offset; // For groups, 0. -}; +} upb_decoder_frame; struct upb_decoder { // Immutable state of the decoder. - struct upb_msgdef *toplevel_msgdef; + upb_msgdef *toplevel_msgdef; upb_sink *sink; // State pertaining to a particular decode (resettable). // Stack entries store the offset where the submsg ends (for groups, 0). - struct upb_decoder_frame stack[UPB_MAX_NESTING], *top, *limit; + upb_decoder_frame stack[UPB_MAX_NESTING], *top, *limit; size_t completed_offset; void *udata; }; -upb_decoder *upb_decoder_new(struct upb_msgdef *msgdef) +upb_decoder *upb_decoder_new(upb_msgdef *msgdef) { upb_decoder *d = malloc(sizeof(*d)); d->toplevel_msgdef = msgdef; @@ -370,7 +367,7 @@ extern upb_wire_type_t upb_expected_wire_types[]; // Returns true if wt is the correct on-the-wire type for ft. INLINE bool upb_check_type(upb_wire_type_t wt, upb_field_type_t ft) { // This doesn't currently support packed arrays. - return upb_type_info[ft].expected_wire_type == wt; + return upb_types[ft].expected_wire_type == wt; } @@ -379,8 +376,8 @@ INLINE bool upb_check_type(upb_wire_type_t wt, upb_field_type_t ft) { * be zero if the submessage is a group). */ static const uint8_t *push(upb_decoder *d, const uint8_t *start, - uint32_t submsg_len, struct upb_fielddef *f, - struct upb_status *status) + uint32_t submsg_len, upb_fielddef *f, + upb_status *status) { d->top->field = f; d->top++; @@ -390,7 +387,7 @@ static const uint8_t *push(upb_decoder *d, const uint8_t *start, UPB_MAX_NESTING); return NULL; } - struct upb_decoder_frame *frame = d->top; + upb_decoder_frame *frame = d->top; frame->end_offset = d->completed_offset + submsg_len; frame->msgdef = upb_downcast_msgdef(f->def); @@ -410,7 +407,7 @@ static const void *pop(upb_decoder *d, const uint8_t *start) } -size_t upb_decoder_decode(upb_decoder *d, upb_strptr str, struct upb_status *status) +size_t upb_decoder_decode(upb_decoder *d, upb_strptr str, upb_status *status) { // buf is our current offset, moves from start to end. const uint8_t *buf = (uint8_t*)upb_string_getrobuf(str); @@ -421,7 +418,7 @@ size_t upb_decoder_decode(upb_decoder *d, upb_strptr str, struct upb_status *sta const uint8_t *completed = buf; const uint8_t *submsg_end = get_msgend(d, start); - struct upb_msgdef *msgdef = d->top->msgdef; + upb_msgdef *msgdef = d->top->msgdef; upb_sink_status sink_status = UPB_SINK_CONTINUE; // We need to check the status of operations that can fail, but we do so as @@ -433,7 +430,7 @@ size_t upb_decoder_decode(upb_decoder *d, upb_strptr str, struct upb_status *sta // Main loop: executed once per tag/field pair. while(sink_status == UPB_SINK_CONTINUE && buf < end) { // Parse/handle tag. - struct upb_tag tag; + upb_tag tag; buf = decode_tag(buf, end, &tag, status); if(tag.wire_type == UPB_WIRE_TYPE_END_GROUP) { CHECK_STATUS(); @@ -450,7 +447,7 @@ size_t upb_decoder_decode(upb_decoder *d, upb_strptr str, struct upb_status *sta } // Look up field by tag number. - struct upb_fielddef *f = upb_msg_itof(msgdef, tag.field_number); + upb_fielddef *f = upb_msg_itof(msgdef, tag.field_number); // Parse/handle field. if(tag.wire_type == UPB_WIRE_TYPE_DELIMITED) { diff --git a/src/upb_decoder.h b/src/upb_decoder.h index 4251fda..b84c149 100644 --- a/src/upb_decoder.h +++ b/src/upb_decoder.h @@ -32,7 +32,7 @@ struct upb_decoder; typedef struct upb_decoder upb_decoder; // Allocates and frees a upb_decoder, respectively. -upb_decoder *upb_decoder_new(struct upb_msgdef *md); +upb_decoder *upb_decoder_new(upb_msgdef *md); void upb_decoder_free(upb_decoder *p); // Resets the internal state of an already-allocated decoder. This puts it in a @@ -47,8 +47,7 @@ void upb_decoder_reset(upb_decoder *p, upb_sink *sink); // // TODO: provide the following guarantee: // retval will always be >= len. -size_t upb_decoder_decode(upb_decoder *p, upb_strptr str, - struct upb_status *status); +size_t upb_decoder_decode(upb_decoder *p, upb_strptr str, upb_status *status); #ifdef __cplusplus } /* extern "C" */ diff --git a/src/upb_def.c b/src/upb_def.c index 2ab1d11..7c9777d 100644 --- a/src/upb_def.c +++ b/src/upb_def.c @@ -56,11 +56,11 @@ static int div_round_up(int numerator, int denominator) { // This algorithm is relatively cheap, since it only requires extra work when // the external refcount on a cyclic type transitions from 0->1 or 1->0. -static void msgdef_free(struct upb_msgdef *m); -static void enumdef_free(struct upb_enumdef *e); -static void unresolveddef_free(struct upb_unresolveddef *u); +static void msgdef_free(upb_msgdef *m); +static void enumdef_free(upb_enumdef *e); +static void unresolveddef_free(struct _upb_unresolveddef *u); -static void def_free(struct upb_def *def) +static void def_free(upb_def *def) { switch(def->type) { case UPB_DEF_MSG: @@ -91,9 +91,8 @@ static void def_free(struct upb_def *def) // search so we can stop the search if we detect a cycles that do not involve // cycle_base. We can't color the nodes as we go by writing to a member of the // def, because another thread could be performing the search concurrently. -static int cycle_ref_or_unref(struct upb_msgdef *m, - struct upb_msgdef *cycle_base, - struct upb_msgdef **open_defs, int num_open_defs, +static int cycle_ref_or_unref(upb_msgdef *m, upb_msgdef *cycle_base, + upb_msgdef **open_defs, int num_open_defs, bool ref) { bool found = false; for(int i = 0; i < num_open_defs; i++) { @@ -116,10 +115,10 @@ static int cycle_ref_or_unref(struct upb_msgdef *m, open_defs[num_open_defs++] = m; } for(int i = 0; i < m->num_fields; i++) { - struct upb_fielddef *f = &m->fields[i]; - struct upb_def *def = f->def; + upb_fielddef *f = &m->fields[i]; + upb_def *def = f->def; if(upb_issubmsg(f) && def->is_cyclic) { - struct upb_msgdef *sub_m = upb_downcast_msgdef(def); + upb_msgdef *sub_m = upb_downcast_msgdef(def); path_count += cycle_ref_or_unref(sub_m, cycle_base, open_defs, num_open_defs, ref); } @@ -134,22 +133,22 @@ static int cycle_ref_or_unref(struct upb_msgdef *m, } } -void _upb_def_reftozero(struct upb_def *def) { +void _upb_def_reftozero(upb_def *def) { if(def->is_cyclic) { - struct upb_msgdef *m = upb_downcast_msgdef(def); - struct upb_msgdef *open_defs[UPB_MAX_TYPE_CYCLE_LEN]; + upb_msgdef *m = upb_downcast_msgdef(def); + upb_msgdef *open_defs[UPB_MAX_TYPE_CYCLE_LEN]; cycle_ref_or_unref(m, NULL, open_defs, 0, false); } else { def_free(def); } } -void _upb_def_cyclic_ref(struct upb_def *def) { - struct upb_msgdef *open_defs[UPB_MAX_TYPE_CYCLE_LEN]; +void _upb_def_cyclic_ref(upb_def *def) { + upb_msgdef *open_defs[UPB_MAX_TYPE_CYCLE_LEN]; cycle_ref_or_unref(upb_downcast_msgdef(def), NULL, open_defs, 0, true); } -static void upb_def_init(struct upb_def *def, enum upb_def_type type, +static void upb_def_init(upb_def *def, enum upb_def_type type, upb_strptr fqname) { def->type = type; def->is_cyclic = 0; // We detect this later, after resolving refs. @@ -158,26 +157,26 @@ static void upb_def_init(struct upb_def *def, enum upb_def_type type, upb_atomic_refcount_init(&def->refcount, 1); } -static void upb_def_uninit(struct upb_def *def) { +static void upb_def_uninit(upb_def *def) { upb_string_unref(def->fqname); } /* upb_unresolveddef **********************************************************/ -struct upb_unresolveddef { - struct upb_def base; +typedef struct _upb_unresolveddef { + upb_def base; upb_strptr name; -}; +} upb_unresolveddef; -static struct upb_unresolveddef *upb_unresolveddef_new(upb_strptr str) { - struct upb_unresolveddef *def = malloc(sizeof(*def)); +static upb_unresolveddef *upb_unresolveddef_new(upb_strptr str) { + upb_unresolveddef *def = malloc(sizeof(*def)); upb_strptr name = upb_string_getref(str, UPB_REF_THREADUNSAFE_READONLY); upb_def_init(&def->base, UPB_DEF_UNRESOLVED, name); def->name = name; return def; } -static void unresolveddef_free(struct upb_unresolveddef *def) { +static void unresolveddef_free(struct _upb_unresolveddef *def) { upb_string_unref(def->name); upb_def_uninit(&def->base); free(def); @@ -185,7 +184,7 @@ static void unresolveddef_free(struct upb_unresolveddef *def) { /* upb_fielddef ***************************************************************/ -static void fielddef_init(struct upb_fielddef *f, +static void fielddef_init(upb_fielddef *f, google_protobuf_FieldDescriptorProto *fd) { f->type = fd->type; @@ -201,14 +200,14 @@ static void fielddef_init(struct upb_fielddef *f, } } -static struct upb_fielddef *fielddef_new( - google_protobuf_FieldDescriptorProto *fd) { - struct upb_fielddef *f = malloc(sizeof(*f)); +static upb_fielddef *fielddef_new(google_protobuf_FieldDescriptorProto *fd) +{ + upb_fielddef *f = malloc(sizeof(*f)); fielddef_init(f, fd); return f; } -static void fielddef_uninit(struct upb_fielddef *f) +static void fielddef_uninit(upb_fielddef *f) { upb_string_unref(f->name); if(upb_hasdef(f) && f->owned) { @@ -216,12 +215,12 @@ static void fielddef_uninit(struct upb_fielddef *f) } } -static void fielddef_free(struct upb_fielddef *f) { +static void fielddef_free(upb_fielddef *f) { fielddef_uninit(f); free(f); } -static void fielddef_copy(struct upb_fielddef *dst, struct upb_fielddef *src) +static void fielddef_copy(upb_fielddef *dst, upb_fielddef *src) { *dst = *src; dst->name = upb_string_getref(src->name, UPB_REF_FROZEN); @@ -232,7 +231,7 @@ static void fielddef_copy(struct upb_fielddef *dst, struct upb_fielddef *src) } // Callback for sorting fields. -static int compare_fields(struct upb_fielddef *f1, struct upb_fielddef *f2) { +static int compare_fields(upb_fielddef *f1, upb_fielddef *f2) { // Required fields go before non-required. bool req1 = f1->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REQUIRED; bool req2 = f2->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REQUIRED; @@ -250,7 +249,7 @@ static int compare_fielddefs(const void *e1, const void *e2) { } static int compare_fds(const void *e1, const void *e2) { - struct upb_fielddef f1, f2; + upb_fielddef f1, f2; fielddef_init(&f1, *(void**)e1); fielddef_init(&f2, *(void**)e2); int ret = compare_fields(&f1, &f2); @@ -264,17 +263,15 @@ void upb_fielddef_sortfds(google_protobuf_FieldDescriptorProto **fds, size_t num qsort(fds, num, sizeof(*fds), compare_fds); } -static void fielddef_sort(struct upb_fielddef **defs, size_t num) +static void fielddef_sort(upb_fielddef **defs, size_t num) { qsort(defs, num, sizeof(*defs), compare_fielddefs); } /* upb_msgdef *****************************************************************/ -static struct upb_msgdef *msgdef_new(struct upb_fielddef **fields, - int num_fields, - upb_strptr fqname, - struct upb_status *status) +static upb_msgdef *msgdef_new(upb_fielddef **fields, int num_fields, + upb_strptr fqname, upb_status *status) { if(num_fields > UPB_MAX_FIELDS) { upb_seterr(status, UPB_STATUS_ERROR, @@ -282,23 +279,23 @@ static struct upb_msgdef *msgdef_new(struct upb_fielddef **fields, free(fields); return NULL; } - struct upb_msgdef *m = malloc(sizeof(*m)); + upb_msgdef *m = malloc(sizeof(*m)); upb_def_init(&m->base, UPB_DEF_MSG, fqname); upb_atomic_refcount_init(&m->cycle_refcount, 0); - upb_inttable_init(&m->itof, num_fields, sizeof(struct upb_itof_ent)); - upb_strtable_init(&m->ntof, num_fields, sizeof(struct upb_ntof_ent)); + upb_inttable_init(&m->itof, num_fields, sizeof(upb_itof_ent)); + upb_strtable_init(&m->ntof, num_fields, sizeof(upb_ntof_ent)); m->num_fields = num_fields; m->set_flags_bytes = div_round_up(m->num_fields, 8); // These are incremented in the loop. m->num_required_fields = 0; m->size = m->set_flags_bytes + 4; // 4 for the refcount. - m->fields = malloc(sizeof(struct upb_fielddef) * num_fields); + m->fields = malloc(sizeof(upb_fielddef) * num_fields); size_t max_align = 0; for(int i = 0; i < num_fields; i++) { - struct upb_fielddef *f = &m->fields[i]; - struct upb_type_info *type_info = &upb_type_info[fields[i]->type]; + upb_fielddef *f = &m->fields[i]; + upb_type_info *type_info = &upb_types[fields[i]->type]; fielddef_copy(f, fields[i]); // General alignment rules are: each member must be at an address that is a @@ -316,8 +313,8 @@ static struct upb_msgdef *msgdef_new(struct upb_fielddef **fields, } // Insert into the tables. - struct upb_itof_ent itof_ent = {{f->number, 0}, f}; - struct upb_ntof_ent ntof_ent = {{f->name, 0}, f}; + upb_itof_ent itof_ent = {{f->number, 0}, f}; + upb_ntof_ent ntof_ent = {{f->name, 0}, f}; upb_inttable_insert(&m->itof, &itof_ent.e); upb_strtable_insert(&m->ntof, &ntof_ent.e); } @@ -326,7 +323,7 @@ static struct upb_msgdef *msgdef_new(struct upb_fielddef **fields, return m; } -static void msgdef_free(struct upb_msgdef *m) +static void msgdef_free(upb_msgdef *m) { for (upb_field_count_t i = 0; i < m->num_fields; i++) fielddef_uninit(&m->fields[i]); @@ -337,8 +334,7 @@ static void msgdef_free(struct upb_msgdef *m) free(m); } -static void upb_msgdef_resolve(struct upb_msgdef *m, struct upb_fielddef *f, - struct upb_def *def) { +static void upb_msgdef_resolve(upb_msgdef *m, upb_fielddef *f, upb_def *def) { (void)m; if(f->owned) upb_def_unref(f->def); f->def = def; @@ -349,75 +345,75 @@ static void upb_msgdef_resolve(struct upb_msgdef *m, struct upb_fielddef *f, /* upb_enumdef ****************************************************************/ -struct ntoi_ent { - struct upb_strtable_entry e; +typedef struct { + upb_strtable_entry e; uint32_t value; -}; +} ntoi_ent; -struct iton_ent { - struct upb_inttable_entry e; +typedef struct { + upb_inttable_entry e; upb_strptr string; -}; +} iton_ent; -static struct upb_enumdef *enumdef_new(google_protobuf_EnumDescriptorProto *ed, - upb_strptr fqname) +static upb_enumdef *enumdef_new(google_protobuf_EnumDescriptorProto *ed, + upb_strptr fqname) { - struct upb_enumdef *e = malloc(sizeof(*e)); + upb_enumdef *e = malloc(sizeof(*e)); upb_def_init(&e->base, UPB_DEF_ENUM, fqname); int num_values = ed->set_flags.has.value ? google_protobuf_EnumValueDescriptorProto_array_len(ed->value) : 0; - upb_strtable_init(&e->ntoi, num_values, sizeof(struct ntoi_ent)); - upb_inttable_init(&e->iton, num_values, sizeof(struct iton_ent)); + upb_strtable_init(&e->ntoi, num_values, sizeof(ntoi_ent)); + upb_inttable_init(&e->iton, num_values, sizeof(iton_ent)); for(int i = 0; i < num_values; i++) { google_protobuf_EnumValueDescriptorProto *value = google_protobuf_EnumValueDescriptorProto_array_get(ed->value, i); - struct ntoi_ent ntoi_ent = {{value->name, 0}, value->number}; - struct iton_ent iton_ent = {{value->number, 0}, value->name}; + ntoi_ent ntoi_ent = {{value->name, 0}, value->number}; + iton_ent iton_ent = {{value->number, 0}, value->name}; upb_strtable_insert(&e->ntoi, &ntoi_ent.e); upb_inttable_insert(&e->iton, &iton_ent.e); } return e; } -static void enumdef_free(struct upb_enumdef *e) { +static void enumdef_free(upb_enumdef *e) { upb_strtable_free(&e->ntoi); upb_inttable_free(&e->iton); upb_def_uninit(&e->base); free(e); } -static void fill_iter(struct upb_enum_iter *iter, struct ntoi_ent *ent) { +static void fill_iter(upb_enum_iter *iter, ntoi_ent *ent) { iter->state = ent; iter->name = ent->e.key; iter->val = ent->value; } -void upb_enum_begin(struct upb_enum_iter *iter, struct upb_enumdef *e) { +void upb_enum_begin(upb_enum_iter *iter, upb_enumdef *e) { // We could iterate over either table here; the choice is arbitrary. - struct ntoi_ent *ent = upb_strtable_begin(&e->ntoi); + ntoi_ent *ent = upb_strtable_begin(&e->ntoi); iter->e = e; fill_iter(iter, ent); } -void upb_enum_next(struct upb_enum_iter *iter) { - struct ntoi_ent *ent = iter->state; +void upb_enum_next(upb_enum_iter *iter) { + ntoi_ent *ent = iter->state; assert(ent); ent = upb_strtable_next(&iter->e->ntoi, &ent->e); iter->state = ent; if(ent) fill_iter(iter, ent); } -bool upb_enum_done(struct upb_enum_iter *iter) { +bool upb_enum_done(upb_enum_iter *iter) { return iter->state == NULL; } /* symtab internal ***********************************************************/ -struct symtab_ent { - struct upb_strtable_entry e; - struct upb_def *def; -}; +typedef struct { + upb_strtable_entry e; + upb_def *def; +} symtab_ent; /* Search for a character in a string, in reverse. */ static int my_memrchr(char *data, char c, size_t len) @@ -429,9 +425,7 @@ static int my_memrchr(char *data, char c, size_t len) /* Given a symbol and the base symbol inside which it is defined, find the * symbol's definition in t. */ -static struct symtab_ent *resolve(struct upb_strtable *t, - upb_strptr base, - upb_strptr symbol) +static symtab_ent *resolve(upb_strtable *t, upb_strptr base, upb_strptr symbol) { if(upb_strlen(base) + upb_strlen(symbol) + 1 >= UPB_SYMBOL_MAXLEN || upb_strlen(symbol) == 0) return NULL; @@ -440,7 +434,7 @@ static struct symtab_ent *resolve(struct upb_strtable *t, // Symbols starting with '.' are absolute, so we do a single lookup. // Slice to omit the leading '.' upb_strptr sym_str = upb_strslice(symbol, 1, INT_MAX); - struct symtab_ent *e = upb_strtable_lookup(t, sym_str); + symtab_ent *e = upb_strtable_lookup(t, sym_str); upb_string_unref(sym_str); return e; } else { @@ -455,7 +449,7 @@ static struct symtab_ent *resolve(struct upb_strtable *t, buf[baselen] = UPB_SYMBOL_SEPARATOR; memcpy(buf + baselen + 1, upb_string_getrobuf(symbol), upb_strlen(symbol)); - struct symtab_ent *e = upb_strtable_lookup(t, sym_str); + symtab_ent *e = upb_strtable_lookup(t, sym_str); if (e) return e; else if(baselen == 0) return NULL; /* No more scopes to try. */ @@ -478,8 +472,8 @@ static upb_strptr join(upb_strptr base, upb_strptr name) { return joined; } -static upb_strptr try_define(struct upb_strtable *t, upb_strptr base, - upb_strptr name, struct upb_status *status) +static upb_strptr try_define(upb_strtable *t, upb_strptr base, + upb_strptr name, upb_status *status) { if(upb_string_isnull(name)) { upb_seterr(status, UPB_STATUS_ERROR, @@ -498,26 +492,23 @@ static upb_strptr try_define(struct upb_strtable *t, upb_strptr base, return fqname; } -static void insert_enum(struct upb_strtable *t, +static void insert_enum(upb_strtable *t, google_protobuf_EnumDescriptorProto *ed, - upb_strptr base, - struct upb_status *status) + upb_strptr base, upb_status *status) { upb_strptr name = ed->set_flags.has.name ? ed->name : UPB_STRING_NULL; upb_strptr fqname = try_define(t, base, name, status); if(upb_string_isnull(fqname)) return; - struct symtab_ent e; + symtab_ent e; e.e.key = fqname; e.def = UPB_UPCAST(enumdef_new(ed, fqname)); upb_strtable_insert(t, &e.e); upb_string_unref(fqname); } -static void insert_message(struct upb_strtable *t, - google_protobuf_DescriptorProto *d, - upb_strptr base, bool sort, - struct upb_status *status) +static void insert_message(upb_strtable *t, google_protobuf_DescriptorProto *d, + upb_strptr base, bool sort, upb_status *status) { upb_strptr name = d->set_flags.has.name ? d->name : UPB_STRING_NULL; upb_strptr fqname = try_define(t, base, name, status); @@ -525,11 +516,11 @@ static void insert_message(struct upb_strtable *t, int num_fields = d->set_flags.has.field ? google_protobuf_FieldDescriptorProto_array_len(d->field) : 0; - struct symtab_ent e; + symtab_ent e; e.e.key = fqname; // Gather our list of fields, sorting if necessary. - struct upb_fielddef **fielddefs = malloc(sizeof(*fielddefs) * num_fields); + upb_fielddef **fielddefs = malloc(sizeof(*fielddefs) * num_fields); for (int i = 0; i < num_fields; i++) { google_protobuf_FieldDescriptorProto *fd = google_protobuf_FieldDescriptorProto_array_get(d->field, i); @@ -562,8 +553,7 @@ error: upb_string_unref(fqname); } -static bool find_cycles(struct upb_msgdef *m, int search_depth, - struct upb_status *status) +static bool find_cycles(upb_msgdef *m, int search_depth, upb_status *status) { if(search_depth > UPB_MAX_TYPE_DEPTH) { // There are many situations in upb where we recurse over the type tree @@ -592,10 +582,10 @@ static bool find_cycles(struct upb_msgdef *m, int search_depth, UPB_UPCAST(m)->search_depth = ++search_depth; bool cycle_found = false; for(upb_field_count_t i = 0; i < m->num_fields; i++) { - struct upb_fielddef *f = &m->fields[i]; + upb_fielddef *f = &m->fields[i]; if(!upb_issubmsg(f)) continue; - struct upb_def *sub_def = f->def; - struct upb_msgdef *sub_m = upb_downcast_msgdef(sub_def); + upb_def *sub_def = f->def; + upb_msgdef *sub_m = upb_downcast_msgdef(sub_def); if(find_cycles(sub_m, search_depth, status)) { cycle_found = true; UPB_UPCAST(m)->is_cyclic = true; @@ -610,9 +600,9 @@ static bool find_cycles(struct upb_msgdef *m, int search_depth, } } -static void addfd(struct upb_strtable *addto, struct upb_strtable *existingdefs, +static void addfd(upb_strtable *addto, upb_strtable *existingdefs, google_protobuf_FileDescriptorProto *fd, bool sort, - struct upb_status *status) + upb_status *status) { upb_strptr pkg; if(fd->set_flags.has.package) { @@ -639,16 +629,16 @@ static void addfd(struct upb_strtable *addto, struct upb_strtable *existingdefs, /* TODO: handle extensions and services. */ // Attempt to resolve all references. - struct symtab_ent *e; + symtab_ent *e; for(e = upb_strtable_begin(addto); e; e = upb_strtable_next(addto, &e->e)) { - struct upb_msgdef *m = upb_dyncast_msgdef(e->def); + upb_msgdef *m = upb_dyncast_msgdef(e->def); if(!m) continue; upb_strptr base = e->e.key; for(upb_field_count_t i = 0; i < m->num_fields; i++) { - struct upb_fielddef *f = &m->fields[i]; + upb_fielddef *f = &m->fields[i]; if(!upb_hasdef(f)) continue; // No resolving necessary. upb_strptr name = upb_downcast_unresolveddef(f->def)->name; - struct symtab_ent *found = resolve(existingdefs, base, name); + symtab_ent *found = resolve(existingdefs, base, name); if(!found) found = resolve(addto, base, name); upb_field_type_t expected = upb_issubmsg(f) ? UPB_DEF_MSG : UPB_DEF_ENUM; if(!found) { @@ -667,33 +657,33 @@ static void addfd(struct upb_strtable *addto, struct upb_strtable *existingdefs, // Deal with type cycles. for(e = upb_strtable_begin(addto); e; e = upb_strtable_next(addto, &e->e)) { - struct upb_msgdef *m = upb_dyncast_msgdef(e->def); + upb_msgdef *m = upb_dyncast_msgdef(e->def); if(!m) continue; // Do an initial pass over the graph to check that there are no cycles // longer than the maximum length. We also mark all cyclic defs as such, // and decrement refs on cyclic defs. find_cycles(m, 0, status); - struct upb_msgdef *open_defs[UPB_MAX_TYPE_CYCLE_LEN]; + upb_msgdef *open_defs[UPB_MAX_TYPE_CYCLE_LEN]; cycle_ref_or_unref(m, NULL, open_defs, 0, true); } } /* upb_symtab *****************************************************************/ -struct upb_symtab *upb_symtab_new() +upb_symtab *upb_symtab_new() { - struct upb_symtab *s = malloc(sizeof(*s)); + upb_symtab *s = malloc(sizeof(*s)); upb_atomic_refcount_init(&s->refcount, 1); upb_rwlock_init(&s->lock); - upb_strtable_init(&s->symtab, 16, sizeof(struct symtab_ent)); - upb_strtable_init(&s->psymtab, 16, sizeof(struct symtab_ent)); + upb_strtable_init(&s->symtab, 16, sizeof(symtab_ent)); + upb_strtable_init(&s->psymtab, 16, sizeof(symtab_ent)); // Add descriptor.proto types to private symtable so we can parse descriptors. // We know there is only 1. google_protobuf_FileDescriptorProto *fd = google_protobuf_FileDescriptorProto_array_get(upb_file_descriptor_set->file, 0); - struct upb_status status = UPB_STATUS_INIT; + upb_status status = UPB_STATUS_INIT; addfd(&s->psymtab, &s->symtab, fd, false, &status); if(!upb_ok(&status)) { fprintf(stderr, "Failed to initialize upb: %s.\n", status.msg); @@ -703,21 +693,21 @@ struct upb_symtab *upb_symtab_new() upb_static_string name = UPB_STATIC_STRING_INIT("google.protobuf.FileDescriptorSet"); upb_strptr nameptr = UPB_STATIC_STRING_PTR_INIT(name); - struct symtab_ent *e = upb_strtable_lookup(&s->psymtab, nameptr); + symtab_ent *e = upb_strtable_lookup(&s->psymtab, nameptr); assert(e); s->fds_msgdef = upb_downcast_msgdef(e->def); return s; } -static void free_symtab(struct upb_strtable *t) +static void free_symtab(upb_strtable *t) { - struct symtab_ent *e = upb_strtable_begin(t); - for(; e; e = upb_strtable_next(t, &e->e)) + symtab_ent *e; + for(e = upb_strtable_begin(t); e; e = upb_strtable_next(t, &e->e)) upb_def_unref(e->def); upb_strtable_free(t); } -void _upb_symtab_free(struct upb_symtab *s) +void _upb_symtab_free(upb_symtab *s) { free_symtab(&s->symtab); free_symtab(&s->psymtab); @@ -725,18 +715,17 @@ void _upb_symtab_free(struct upb_symtab *s) free(s); } -struct upb_def **upb_symtab_getdefs(struct upb_symtab *s, int *count, - upb_def_type_t type) +upb_def **upb_symtab_getdefs(upb_symtab *s, int *count, upb_def_type_t type) { upb_rwlock_rdlock(&s->lock); int total = upb_strtable_count(&s->symtab); // We may only use part of this, depending on how many symbols are of the // correct type. - struct upb_def **defs = malloc(sizeof(*defs) * total); - struct symtab_ent *e = upb_strtable_begin(&s->symtab); + upb_def **defs = malloc(sizeof(*defs) * total); + symtab_ent *e = upb_strtable_begin(&s->symtab); int i = 0; for(; e; e = upb_strtable_next(&s->symtab, &e->e)) { - struct upb_def *def = e->def; + upb_def *def = e->def; assert(def); if(type == UPB_DEF_ANY || def->type == type) defs[i++] = def; @@ -748,11 +737,11 @@ struct upb_def **upb_symtab_getdefs(struct upb_symtab *s, int *count, return defs; } -struct upb_def *upb_symtab_lookup(struct upb_symtab *s, upb_strptr sym) +upb_def *upb_symtab_lookup(upb_symtab *s, upb_strptr sym) { upb_rwlock_rdlock(&s->lock); - struct symtab_ent *e = upb_strtable_lookup(&s->symtab, sym); - struct upb_def *ret = NULL; + symtab_ent *e = upb_strtable_lookup(&s->symtab, sym); + upb_def *ret = NULL; if(e) { ret = e->def; upb_def_ref(ret); @@ -762,11 +751,10 @@ struct upb_def *upb_symtab_lookup(struct upb_symtab *s, upb_strptr sym) } -struct upb_def *upb_symtab_resolve(struct upb_symtab *s, upb_strptr base, - upb_strptr symbol) { +upb_def *upb_symtab_resolve(upb_symtab *s, upb_strptr base, upb_strptr symbol) { upb_rwlock_rdlock(&s->lock); - struct symtab_ent *e = resolve(&s->symtab, base, symbol); - struct upb_def *ret = NULL; + symtab_ent *e = resolve(&s->symtab, base, symbol); + upb_def *ret = NULL; if(e) { ret = e->def; upb_def_ref(ret); @@ -775,15 +763,14 @@ struct upb_def *upb_symtab_resolve(struct upb_symtab *s, upb_strptr base, return ret; } -void upb_symtab_addfds(struct upb_symtab *s, - google_protobuf_FileDescriptorSet *fds, - struct upb_status *status) +void upb_symtab_addfds(upb_symtab *s, google_protobuf_FileDescriptorSet *fds, + upb_status *status) { if(fds->set_flags.has.file) { // Insert new symbols into a temporary table until we have verified that // the descriptor is valid. - struct upb_strtable tmp; - upb_strtable_init(&tmp, 0, sizeof(struct symtab_ent)); + upb_strtable tmp; + upb_strtable_init(&tmp, 0, sizeof(symtab_ent)); { // Read lock scope upb_rwlock_rdlock(&s->lock); @@ -801,7 +788,7 @@ void upb_symtab_addfds(struct upb_symtab *s, // Everything was successfully added, copy from the tmp symtable. { // Write lock scope upb_rwlock_wrlock(&s->lock); - struct symtab_ent *e; + symtab_ent *e; for(e = upb_strtable_begin(&tmp); e; e = upb_strtable_next(&tmp, &e->e)) { // We checked for duplicates when we had only the read lock, but it is // theoretically possible that a duplicate symbol when we dropped the @@ -825,8 +812,7 @@ void upb_symtab_addfds(struct upb_symtab *s, return; } -void upb_symtab_add_desc(struct upb_symtab *s, upb_strptr desc, - struct upb_status *status) +void upb_symtab_add_desc(upb_symtab *s, upb_strptr desc, upb_status *status) { upb_msg *fds = upb_msg_new(s->fds_msgdef); upb_msg_decodestr(fds, s->fds_msgdef, desc, status); diff --git a/src/upb_def.h b/src/upb_def.h index 15af25a..ac17a6f 100644 --- a/src/upb_def.h +++ b/src/upb_def.h @@ -52,7 +52,7 @@ enum upb_def_type { // This typedef is more space-efficient than declaring an enum var directly. typedef int8_t upb_def_type_t; -struct upb_def { +typedef struct { upb_strptr fqname; // Fully qualified. upb_atomic_refcount_t refcount; upb_def_type_t type; @@ -65,58 +65,27 @@ struct upb_def { // is_cyclic flag. bool is_cyclic; uint16_t search_depth; // Used during initialization dfs. -}; +} upb_def; // These must not be called directly! -void _upb_def_cyclic_ref(struct upb_def *def); -void _upb_def_reftozero(struct upb_def *def); +void _upb_def_cyclic_ref(upb_def *def); +void _upb_def_reftozero(upb_def *def); // Call to ref/deref a def. -INLINE void upb_def_ref(struct upb_def *def) { +INLINE void upb_def_ref(upb_def *def) { if(upb_atomic_ref(&def->refcount) && def->is_cyclic) _upb_def_cyclic_ref(def); } -INLINE void upb_def_unref(struct upb_def *def) { +INLINE void upb_def_unref(upb_def *def) { if(upb_atomic_unref(&def->refcount)) _upb_def_reftozero(def); } -// Dynamic casts, for determining if a def is of a particular type at runtime. -#define UPB_DYNAMIC_CAST_DEF(lower, upper) \ - struct upb_ ## lower; /* Forward-declare. */ \ - INLINE struct upb_ ## lower *upb_dyncast_ ## lower(struct upb_def *def) { \ - if(def->type != UPB_DEF_ ## upper) return NULL; \ - return (struct upb_ ## lower*)def; \ - } -UPB_DYNAMIC_CAST_DEF(msgdef, MSG); -UPB_DYNAMIC_CAST_DEF(enumdef, ENUM); -UPB_DYNAMIC_CAST_DEF(svcdef, SVC); -UPB_DYNAMIC_CAST_DEF(extdef, EXT); -UPB_DYNAMIC_CAST_DEF(unresolveddef, UNRESOLVED); -#undef UPB_DYNAMIC_CAST_DEF - -// Downcasts, for when some wants to assert that a def is of a particular type. -// These are only checked if we are building debug. -#define UPB_DOWNCAST_DEF(lower, upper) \ - struct upb_ ## lower; /* Forward-declare. */ \ - INLINE struct upb_ ## lower *upb_downcast_ ## lower(struct upb_def *def) { \ - assert(def->type == UPB_DEF_ ## upper); \ - return (struct upb_ ## lower*)def; \ - } -UPB_DOWNCAST_DEF(msgdef, MSG); -UPB_DOWNCAST_DEF(enumdef, ENUM); -UPB_DOWNCAST_DEF(svcdef, SVC); -UPB_DOWNCAST_DEF(extdef, EXT); -UPB_DOWNCAST_DEF(unresolveddef, UNRESOLVED); -#undef UPB_DOWNCAST_DEF - -#define UPB_UPCAST(ptr) (&(ptr)->base) - /* upb_fielddef ***************************************************************/ // A upb_fielddef describes a single field in a message. It isn't a full def // in the sense that it derives from upb_def. It cannot stand on its own; it // is either a field of a upb_msgdef or contained inside a upb_extensiondef. // It is also reference-counted. -struct upb_fielddef { +typedef struct _upb_fielddef { upb_atomic_refcount_t refcount; upb_field_type_t type; upb_label_t label; @@ -131,29 +100,29 @@ struct upb_fielddef { // For the case of an enum or a submessage, points to the def for that type. // We own a ref on this def. bool owned; - struct upb_def *def; -}; + upb_def *def; +} upb_fielddef; // A variety of tests about the type of a field. -INLINE bool upb_issubmsg(struct upb_fielddef *f) { +INLINE bool upb_issubmsg(upb_fielddef *f) { return upb_issubmsgtype(f->type); } -INLINE bool upb_isstring(struct upb_fielddef *f) { +INLINE bool upb_isstring(upb_fielddef *f) { return upb_isstringtype(f->type); } -INLINE bool upb_isarray(struct upb_fielddef *f) { +INLINE bool upb_isarray(upb_fielddef *f) { return f->label == UPB_LABEL(REPEATED); } // Does the type of this field imply that it should contain an associated def? -INLINE bool upb_hasdef(struct upb_fielddef *f) { +INLINE bool upb_hasdef(upb_fielddef *f) { return upb_issubmsg(f) || f->type == UPB_TYPE(ENUM); } -INLINE bool upb_field_ismm(struct upb_fielddef *f) { +INLINE bool upb_field_ismm(upb_fielddef *f) { return upb_isarray(f) || upb_isstring(f) || upb_issubmsg(f); } -INLINE bool upb_elem_ismm(struct upb_fielddef *f) { +INLINE bool upb_elem_ismm(upb_fielddef *f) { return upb_isstring(f) || upb_issubmsg(f); } @@ -170,102 +139,96 @@ struct google_protobuf_EnumDescriptorProto; struct google_protobuf_DescriptorProto; // Structure that describes a single .proto message type. -struct upb_msgdef { - struct upb_def base; +typedef struct _upb_msgdef { + upb_def base; upb_atomic_refcount_t cycle_refcount; upb_msg *default_msg; // Message with all default values set. size_t size; upb_field_count_t num_fields; uint32_t set_flags_bytes; uint32_t num_required_fields; // Required fields have the lowest set bytemasks. - struct upb_fielddef *fields; // We have exclusive ownership of these. + upb_fielddef *fields; // We have exclusive ownership of these. // Tables for looking up fields by number and name. - struct upb_inttable itof; // int to field - struct upb_strtable ntof; // name to field -}; + upb_inttable itof; // int to field + upb_strtable ntof; // name to field +} upb_msgdef; // Hash table entries for looking up fields by name or number. -struct upb_itof_ent { - struct upb_inttable_entry e; - struct upb_fielddef *f; -}; -struct upb_ntof_ent { - struct upb_strtable_entry e; - struct upb_fielddef *f; -}; +typedef struct { + upb_inttable_entry e; + upb_fielddef *f; +} upb_itof_ent; +typedef struct { + upb_strtable_entry e; + upb_fielddef *f; +} upb_ntof_ent; // Looks up a field by name or number. While these are written to be as fast // as possible, it will still be faster to cache the results of this lookup if // possible. These return NULL if no such field is found. -INLINE struct upb_fielddef *upb_msg_itof(struct upb_msgdef *m, uint32_t num) { - struct upb_itof_ent *e; - e = (struct upb_itof_ent*)upb_inttable_fast_lookup( - &m->itof, num, sizeof(struct upb_itof_ent)); +INLINE upb_fielddef *upb_msg_itof(upb_msgdef *m, uint32_t num) { + upb_itof_ent *e = + (upb_itof_ent*)upb_inttable_fastlookup(&m->itof, num, sizeof(*e)); return e ? e->f : NULL; } -INLINE struct upb_fielddef *upb_msg_ntof(struct upb_msgdef *m, - upb_strptr name) { - struct upb_ntof_ent *e; - e = (struct upb_ntof_ent*)upb_strtable_lookup(&m->ntof, name); +INLINE upb_fielddef *upb_msg_ntof(upb_msgdef *m, upb_strptr name) { + upb_ntof_ent *e = (upb_ntof_ent*)upb_strtable_lookup(&m->ntof, name); return e ? e->f : NULL; } /* upb_enumdef ****************************************************************/ -struct upb_enumdef { - struct upb_def base; - struct upb_strtable ntoi; - struct upb_inttable iton; -}; +typedef struct _upb_enumdef { + upb_def base; + upb_strtable ntoi; + upb_inttable iton; +} upb_enumdef; typedef int32_t upb_enumval_t; // Lookups from name to integer and vice-versa. -bool upb_enumdef_ntoi(struct upb_enumdef *e, upb_strptr name, - upb_enumval_t *num); -upb_strptr upb_enumdef_iton(struct upb_enumdef *e, upb_enumval_t num); +bool upb_enumdef_ntoi(upb_enumdef *e, upb_strptr name, upb_enumval_t *num); +upb_strptr upb_enumdef_iton(upb_enumdef *e, upb_enumval_t num); // Iteration over name/value pairs. The order is undefined. -// struct upb_enumd_iter i; +// upb_enum_iter i; // for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) { // // ... // } -struct upb_enum_iter { - struct upb_enumdef *e; +typedef struct { + upb_enumdef *e; void *state; // Internal iteration state. upb_strptr name; upb_enumval_t val; -}; -void upb_enum_begin(struct upb_enum_iter *iter, struct upb_enumdef *e); -void upb_enum_next(struct upb_enum_iter *iter); -bool upb_enum_done(struct upb_enum_iter *iter); +} upb_enum_iter; +void upb_enum_begin(upb_enum_iter *iter, upb_enumdef *e); +void upb_enum_next(upb_enum_iter *iter); +bool upb_enum_done(upb_enum_iter *iter); /* upb_symtab *****************************************************************/ // A SymbolTable is where upb_defs live. It is empty when first constructed. // Clients add definitions to the symtab by supplying unserialized or // serialized descriptors (as defined in descriptor.proto). -struct upb_symtab { +typedef struct { upb_atomic_refcount_t refcount; - upb_rwlock_t lock; // Protects all members except the refcount. - struct upb_msgdef *fds_msgdef; // In psymtab, ptr here for convenience. + upb_rwlock_t lock; // Protects all members except the refcount. + upb_msgdef *fds_msgdef; // In psymtab, ptr here for convenience. // Our symbol tables; we own refs to the defs therein. - struct upb_strtable symtab; // The main symbol table. - struct upb_strtable psymtab; // Private symbols, for internal use. -}; + upb_strtable symtab; // The main symbol table. + upb_strtable psymtab; // Private symbols, for internal use. +} upb_symtab; // Initializes a upb_symtab. Contexts are not freed explicitly, but unref'd // when the caller is done with them. -struct upb_symtab *upb_symtab_new(void); -void _upb_symtab_free(struct upb_symtab *s); // Must not be called directly! +upb_symtab *upb_symtab_new(void); +void _upb_symtab_free(upb_symtab *s); // Must not be called directly! -INLINE void upb_symtab_ref(struct upb_symtab *s) { - upb_atomic_ref(&s->refcount); -} -INLINE void upb_symtab_unref(struct upb_symtab *s) { +INLINE void upb_symtab_ref(upb_symtab *s) { upb_atomic_ref(&s->refcount); } +INLINE void upb_symtab_unref(upb_symtab *s) { if(upb_atomic_unref(&s->refcount)) _upb_symtab_free(s); } @@ -279,27 +242,58 @@ INLINE void upb_symtab_unref(struct upb_symtab *s) { // // If a def is found, the caller owns one ref on the returned def. Otherwise // returns NULL. -struct upb_def *upb_symtab_resolve(struct upb_symtab *s, upb_strptr base, - upb_strptr symbol); +upb_def *upb_symtab_resolve(upb_symtab *s, upb_strptr base, upb_strptr symbol); // Find an entry in the symbol table with this exact name. If a def is found, // the caller owns one ref on the returned def. Otherwise returns NULL. -struct upb_def *upb_symtab_lookup(struct upb_symtab *s, upb_strptr sym); +upb_def *upb_symtab_lookup(upb_symtab *s, upb_strptr sym); // Gets an array of pointers to all currently active defs in this symtab. The // caller owns the returned array (which is of length *count) as well as a ref // to each symbol inside. If type is UPB_DEF_ANY then defs of all types are // returned, otherwise only defs of the required type are returned. -struct upb_def **upb_symtab_getdefs(struct upb_symtab *s, int *count, - upb_def_type_t type); +upb_def **upb_symtab_getdefs(upb_symtab *s, int *count, upb_def_type_t type); // Adds the definitions in the given serialized descriptor to this symtab. All // types that are referenced from desc must have previously been defined (or be // defined in desc). desc may not attempt to define any names that are already // defined in this symtab. Caller retains ownership of desc. status indicates // whether the operation was successful or not, and the error message (if any). -void upb_symtab_add_desc(struct upb_symtab *s, upb_strptr desc, - struct upb_status *status); +void upb_symtab_add_desc(upb_symtab *s, upb_strptr desc, upb_status *status); + + +/* upb_def casts **************************************************************/ + +// Dynamic casts, for determining if a def is of a particular type at runtime. +#define UPB_DYNAMIC_CAST_DEF(lower, upper) \ + struct _upb_ ## lower; /* Forward-declare. */ \ + INLINE struct _upb_ ## lower *upb_dyncast_ ## lower(upb_def *def) { \ + if(def->type != UPB_DEF_ ## upper) return NULL; \ + return (struct _upb_ ## lower*)def; \ + } +UPB_DYNAMIC_CAST_DEF(msgdef, MSG); +UPB_DYNAMIC_CAST_DEF(enumdef, ENUM); +UPB_DYNAMIC_CAST_DEF(svcdef, SVC); +UPB_DYNAMIC_CAST_DEF(extdef, EXT); +UPB_DYNAMIC_CAST_DEF(unresolveddef, UNRESOLVED); +#undef UPB_DYNAMIC_CAST_DEF + +// Downcasts, for when some wants to assert that a def is of a particular type. +// These are only checked if we are building debug. +#define UPB_DOWNCAST_DEF(lower, upper) \ + struct _upb_ ## lower; /* Forward-declare. */ \ + INLINE struct _upb_ ## lower *upb_downcast_ ## lower(upb_def *def) { \ + assert(def->type == UPB_DEF_ ## upper); \ + return (struct _upb_ ## lower*)def; \ + } +UPB_DOWNCAST_DEF(msgdef, MSG); +UPB_DOWNCAST_DEF(enumdef, ENUM); +UPB_DOWNCAST_DEF(svcdef, SVC); +UPB_DOWNCAST_DEF(extdef, EXT); +UPB_DOWNCAST_DEF(unresolveddef, UNRESOLVED); +#undef UPB_DOWNCAST_DEF + +#define UPB_UPCAST(ptr) (&(ptr)->base) #ifdef __cplusplus } /* extern "C" */ diff --git a/src/upb_encoder.c b/src/upb_encoder.c index f67d39d..3e60a02 100644 --- a/src/upb_encoder.c +++ b/src/upb_encoder.c @@ -227,21 +227,19 @@ static upb_sink_status _upb_encoder_push_buf(upb_encoder *s, const uint8_t *buf, } } -static upb_sink_status _upb_encoder_valuecb(upb_sink *sink, - struct upb_fielddef *f, +static upb_sink_status _upb_encoder_valuecb(upb_sink *sink, upb_fielddef *f, union upb_value val) { upb_encoder *s = (upb_encoder*)sink; uint8_t buf[UPB_ENCODER_BUFSIZE], *ptr = buf; - upb_wire_type_t wt = upb_type_info[f->type].expected_wire_type; + upb_wire_type_t wt = upb_types[f->type].expected_wire_type; // TODO: handle packed encoding. ptr = _upb_put_tag(ptr, f->number, wt); ptr = upb_encode_value(ptr, f->type, val); return _upb_encoder_push_buf(s, buf, ptr - buf); } -static upb_sink_status _upb_encoder_strcb(upb_sink *sink, - struct upb_fielddef *f, +static upb_sink_status _upb_encoder_strcb(upb_sink *sink, upb_fielddef *f, upb_strptr str, int32_t start, uint32_t end) { @@ -257,8 +255,7 @@ static upb_sink_status _upb_encoder_strcb(upb_sink *sink, return _upb_encoder_push_buf(s, (uint8_t*)upb_string_getrobuf(str), end - start); } -static upb_sink_status _upb_encoder_startcb(upb_sink *sink, - struct upb_fielddef *f) +static upb_sink_status _upb_encoder_startcb(upb_sink *sink, upb_fielddef *f) { upb_encoder *s = (upb_encoder*)sink; uint8_t buf[UPB_ENCODER_BUFSIZE], *ptr = buf; @@ -271,8 +268,7 @@ static upb_sink_status _upb_encoder_startcb(upb_sink *sink, return _upb_encoder_push_buf(s, buf, ptr - buf); } -static upb_sink_status _upb_encoder_endcb(upb_sink *sink, - struct upb_fielddef *f) +static upb_sink_status _upb_encoder_endcb(upb_sink *sink, upb_fielddef *f) { upb_encoder *s = (upb_encoder*)sink; uint8_t buf[UPB_ENCODER_BUFSIZE], *ptr = buf; @@ -291,7 +287,7 @@ upb_sink_callbacks _upb_encoder_sink_vtbl = { /* Public Interface ***********************************************************/ -size_t upb_get_encoded_size(union upb_value v, struct upb_fielddef *f) +size_t upb_get_encoded_size(union upb_value v, upb_fielddef *f) { #define CASE(t, member_name) \ case UPB_TYPE(t): return upb_get_ ## t ## _size(v.member_name); diff --git a/src/upb_encoder.h b/src/upb_encoder.h index 3abd706..1322e21 100644 --- a/src/upb_encoder.h +++ b/src/upb_encoder.h @@ -20,9 +20,6 @@ extern "C" { #endif -size_t upb_get_encoded_tag_size(uint32_t fieldnum); -size_t upb_get_encoded_value_size(union upb_value v, struct upb_fielddef *f); - struct upb_encoder; typedef struct upb_encoder upb_encoder; diff --git a/src/upb_sink.h b/src/upb_sink.h index d864512..5d34000 100644 --- a/src/upb_sink.h +++ b/src/upb_sink.h @@ -34,12 +34,12 @@ #ifndef UPB_SINK_H #define UPB_SINK_H +#include "upb_def.h" + #ifdef __cplusplus extern "C" { #endif -struct upb_fielddef; - // Each of the upb_sink callbacks returns a status of this type. typedef enum { // The normal case, where the consumer wants to continue consuming. @@ -65,7 +65,7 @@ typedef struct { // The value callback is called for a regular value (ie. not a string or // submessage). -typedef upb_sink_status (*upb_value_cb)(upb_sink *s, struct upb_fielddef *f, +typedef upb_sink_status (*upb_value_cb)(upb_sink *s, upb_fielddef *f, union upb_value val); // The string callback is called for string data. "str" is the string in which @@ -79,14 +79,14 @@ typedef upb_sink_status (*upb_value_cb)(upb_sink *s, struct upb_fielddef *f, // The data is supplied this way to give you the opportunity to reference this // data instead of copying it (perhaps using upb_strslice), or to minimize // copying if it is unavoidable. -typedef upb_sink_status (*upb_str_cb)(upb_sink *s, struct upb_fielddef *f, +typedef upb_sink_status (*upb_str_cb)(upb_sink *s, upb_fielddef *f, upb_strptr str, int32_t start, uint32_t end); // The start and end callbacks are called when a submessage begins and ends, // respectively. -typedef upb_sink_status (*upb_start_cb)(upb_sink *s, struct upb_fielddef *f); -typedef upb_sink_status (*upb_end_cb)(upb_sink *s, struct upb_fielddef *f); +typedef upb_sink_status (*upb_start_cb)(upb_sink *s, upb_fielddef *f); +typedef upb_sink_status (*upb_end_cb)(upb_sink *s, upb_fielddef *f); /* upb_sink implementation ****************************************************/ diff --git a/src/upb_table.c b/src/upb_table.c index 14902df..a477121 100644 --- a/src/upb_table.c +++ b/src/upb_table.c @@ -17,14 +17,14 @@ static const double MAX_LOAD = 0.85; static uint32_t MurmurHash2(const void *key, size_t len, uint32_t seed); /* We use 1-based indexes into the table so that 0 can be "NULL". */ -static struct upb_inttable_entry *intent(struct upb_inttable *t, int32_t i) { +static upb_inttable_entry *intent(upb_inttable *t, int32_t i) { return UPB_INDEX(t->t.entries, i-1, t->t.entry_size); } -static struct upb_strtable_entry *strent(struct upb_strtable *t, int32_t i) { +static upb_strtable_entry *strent(upb_strtable *t, int32_t i) { return UPB_INDEX(t->t.entries, i-1, t->t.entry_size); } -void upb_table_init(struct upb_table *t, uint32_t size, uint16_t entry_size) +void upb_table_init(upb_table *t, uint32_t size, uint16_t entry_size) { t->count = 0; t->entry_size = entry_size; @@ -36,37 +36,37 @@ void upb_table_init(struct upb_table *t, uint32_t size, uint16_t entry_size) memset(t->entries, 0, bytes); /* Both tables consider 0's an empty entry. */ } -void upb_inttable_init(struct upb_inttable *t, uint32_t size, uint16_t entsize) +void upb_inttable_init(upb_inttable *t, uint32_t size, uint16_t entsize) { upb_table_init(&t->t, size, entsize); } -void upb_strtable_init(struct upb_strtable *t, uint32_t size, uint16_t entsize) +void upb_strtable_init(upb_strtable *t, uint32_t size, uint16_t entsize) { upb_table_init(&t->t, size, entsize); } -void upb_table_free(struct upb_table *t) { free(t->entries); } -void upb_inttable_free(struct upb_inttable *t) { upb_table_free(&t->t); } -void upb_strtable_free(struct upb_strtable *t) { +void upb_table_free(upb_table *t) { free(t->entries); } +void upb_inttable_free(upb_inttable *t) { upb_table_free(&t->t); } +void upb_strtable_free(upb_strtable *t) { // Free refs from the strtable. - struct upb_strtable_entry *e = upb_strtable_begin(t); + upb_strtable_entry *e = upb_strtable_begin(t); for(; e; e = upb_strtable_next(t, e)) { upb_string_unref(e->key); } upb_table_free(&t->t); } -static uint32_t strtable_bucket(struct upb_strtable *t, upb_strptr key) +static uint32_t strtable_bucket(upb_strtable *t, upb_strptr key) { uint32_t hash = MurmurHash2(upb_string_getrobuf(key), upb_strlen(key), 0); return (hash & (upb_strtable_size(t)-1)) + 1; } -void *upb_strtable_lookup(struct upb_strtable *t, upb_strptr key) +void *upb_strtable_lookup(upb_strtable *t, upb_strptr key) { uint32_t bucket = strtable_bucket(t, key); - struct upb_strtable_entry *e; + upb_strtable_entry *e; do { e = strent(t, bucket); if(!upb_string_isnull(e->key) && upb_streql(e->key, key)) return e; @@ -74,11 +74,11 @@ void *upb_strtable_lookup(struct upb_strtable *t, upb_strptr key) return NULL; } -static uint32_t empty_intbucket(struct upb_inttable *table) +static uint32_t empty_intbucket(upb_inttable *table) { /* TODO: does it matter that this is biased towards the front of the table? */ for(uint32_t i = 1; i <= upb_inttable_size(table); i++) { - struct upb_inttable_entry *e = intent(table, i); + upb_inttable_entry *e = intent(table, i); if(e->key == EMPTYENT) return i; } assert(false); @@ -88,12 +88,12 @@ static uint32_t empty_intbucket(struct upb_inttable *table) /* The insert routines have a lot more code duplication between int/string * variants than I would like, but there's just a bit too much that varies to * parameterize them. */ -static void intinsert(struct upb_inttable *t, struct upb_inttable_entry *e) +static void intinsert(upb_inttable *t, upb_inttable_entry *e) { assert(upb_inttable_lookup(t, e->key) == NULL); t->t.count++; uint32_t bucket = upb_inttable_bucket(t, e->key); - struct upb_inttable_entry *table_e = intent(t, bucket); + upb_inttable_entry *table_e = intent(t, bucket); if(table_e->key != EMPTYENT) { /* Collision. */ if(bucket == upb_inttable_bucket(t, table_e->key)) { /* Existing element is in its main posisiton. Find an empty slot to @@ -109,7 +109,7 @@ static void intinsert(struct upb_inttable *t, struct upb_inttable_entry *e) uint32_t empty_bucket = empty_intbucket(t); uint32_t evictee_bucket = upb_inttable_bucket(t, table_e->key); memcpy(intent(t, empty_bucket), table_e, t->t.entry_size); /* copies next */ - struct upb_inttable_entry *evictee_e = intent(t, evictee_bucket); + upb_inttable_entry *evictee_e = intent(t, evictee_bucket); while(1) { assert(evictee_e->key != UPB_EMPTY_ENTRY); assert(evictee_e->next != UPB_END_OF_CHAIN); @@ -127,15 +127,15 @@ static void intinsert(struct upb_inttable *t, struct upb_inttable_entry *e) assert(upb_inttable_lookup(t, e->key) == table_e); } -void upb_inttable_insert(struct upb_inttable *t, struct upb_inttable_entry *e) +void upb_inttable_insert(upb_inttable *t, upb_inttable_entry *e) { assert(e->key != 0); if((double)(t->t.count + 1) / upb_inttable_size(t) > MAX_LOAD) { /* Need to resize. New table of double the size, add old elements to it. */ - struct upb_inttable new_table; + upb_inttable new_table; upb_inttable_init(&new_table, upb_inttable_size(t)*2, t->t.entry_size); new_table.t.count = t->t.count; - struct upb_inttable_entry *old_e; + upb_inttable_entry *old_e; for(old_e = upb_inttable_begin(t); old_e; old_e = upb_inttable_next(t, old_e)) intinsert(&new_table, old_e); upb_inttable_free(t); @@ -144,24 +144,24 @@ void upb_inttable_insert(struct upb_inttable *t, struct upb_inttable_entry *e) intinsert(t, e); } -static uint32_t empty_strbucket(struct upb_strtable *table) +static uint32_t empty_strbucket(upb_strtable *table) { /* TODO: does it matter that this is biased towards the front of the table? */ for(uint32_t i = 1; i <= upb_strtable_size(table); i++) { - struct upb_strtable_entry *e = strent(table, i); + upb_strtable_entry *e = strent(table, i); if(upb_string_isnull(e->key)) return i; } assert(false); return 0; } -static void strinsert(struct upb_strtable *t, struct upb_strtable_entry *e) +static void strinsert(upb_strtable *t, upb_strtable_entry *e) { assert(upb_strtable_lookup(t, e->key) == NULL); e->key = upb_string_getref(e->key, UPB_REF_FROZEN); t->t.count++; uint32_t bucket = strtable_bucket(t, e->key); - struct upb_strtable_entry *table_e = strent(t, bucket); + upb_strtable_entry *table_e = strent(t, bucket); if(!upb_string_isnull(table_e->key)) { /* Collision. */ if(bucket == strtable_bucket(t, table_e->key)) { /* Existing element is in its main posisiton. Find an empty slot to @@ -177,7 +177,7 @@ static void strinsert(struct upb_strtable *t, struct upb_strtable_entry *e) uint32_t empty_bucket = empty_strbucket(t); uint32_t evictee_bucket = strtable_bucket(t, table_e->key); memcpy(strent(t, empty_bucket), table_e, t->t.entry_size); /* copies next */ - struct upb_strtable_entry *evictee_e = strent(t, evictee_bucket); + upb_strtable_entry *evictee_e = strent(t, evictee_bucket); while(1) { assert(!upb_string_isnull(evictee_e->key)); assert(evictee_e->next != UPB_END_OF_CHAIN); @@ -195,13 +195,13 @@ static void strinsert(struct upb_strtable *t, struct upb_strtable_entry *e) assert(upb_strtable_lookup(t, e->key) == table_e); } -void upb_strtable_insert(struct upb_strtable *t, struct upb_strtable_entry *e) +void upb_strtable_insert(upb_strtable *t, upb_strtable_entry *e) { if((double)(t->t.count + 1) / upb_strtable_size(t) > MAX_LOAD) { /* Need to resize. New table of double the size, add old elements to it. */ - struct upb_strtable new_table; + upb_strtable new_table; upb_strtable_init(&new_table, upb_strtable_size(t)*2, t->t.entry_size); - struct upb_strtable_entry *old_e; + upb_strtable_entry *old_e; for(old_e = upb_strtable_begin(t); old_e; old_e = upb_strtable_next(t, old_e)) strinsert(&new_table, old_e); upb_strtable_free(t); @@ -210,12 +210,12 @@ void upb_strtable_insert(struct upb_strtable *t, struct upb_strtable_entry *e) strinsert(t, e); } -void *upb_inttable_begin(struct upb_inttable *t) { +void *upb_inttable_begin(upb_inttable *t) { return upb_inttable_next(t, intent(t, 0)); } -void *upb_inttable_next(struct upb_inttable *t, struct upb_inttable_entry *cur) { - struct upb_inttable_entry *end = intent(t, upb_inttable_size(t)+1); +void *upb_inttable_next(upb_inttable *t, upb_inttable_entry *cur) { + upb_inttable_entry *end = intent(t, upb_inttable_size(t)+1); do { cur = (void*)((char*)cur + t->t.entry_size); if(cur == end) return NULL; @@ -223,12 +223,12 @@ void *upb_inttable_next(struct upb_inttable *t, struct upb_inttable_entry *cur) return cur; } -void *upb_strtable_begin(struct upb_strtable *t) { +void *upb_strtable_begin(upb_strtable *t) { return upb_strtable_next(t, strent(t, 0)); } -void *upb_strtable_next(struct upb_strtable *t, struct upb_strtable_entry *cur) { - struct upb_strtable_entry *end = strent(t, upb_strtable_size(t)+1); +void *upb_strtable_next(upb_strtable *t, upb_strtable_entry *cur) { + upb_strtable_entry *end = strent(t, upb_strtable_size(t)+1); do { cur = (void*)((char*)cur + t->t.entry_size); if(cur == end) return NULL; diff --git a/src/upb_table.h b/src/upb_table.h index f7f548b..122aed3 100644 --- a/src/upb_table.h +++ b/src/upb_table.h @@ -28,59 +28,57 @@ typedef uint32_t upb_inttable_key_t; #define UPB_END_OF_CHAIN (uint32_t)0 #define UPB_EMPTY_ENTRY (uint32_t)0 -struct upb_inttable_entry { +typedef struct { upb_inttable_key_t key; uint32_t next; /* Internal chaining. */ -}; +} upb_inttable_entry; // TODO: consider storing the hash in the entry. This would avoid the need to // rehash on table resizes, but more importantly could possibly improve lookup // performance by letting us compare hashes before comparing lengths or the // strings themselves. -struct upb_strtable_entry { +typedef struct { upb_strptr key; // We own a frozen ref. uint32_t next; // Internal chaining. -}; +} upb_strtable_entry; -struct upb_table { +typedef struct { void *entries; uint32_t count; /* How many elements are currently in the table? */ uint16_t entry_size; /* How big is each entry? */ uint8_t size_lg2; /* The table is 2^size_lg2 in size. */ uint32_t mask; -}; +} upb_table; -struct upb_strtable { - struct upb_table t; -}; +typedef struct { + upb_table t; +} upb_strtable; -struct upb_inttable { - struct upb_table t; -}; +typedef struct { + upb_table t; +} upb_inttable; /* Initialize and free a table, respectively. Specify the initial size * with 'size' (the size will be increased as necessary). Entry size * specifies how many bytes each entry in the table is. */ -void upb_inttable_init(struct upb_inttable *table, - uint32_t size, uint16_t entry_size); -void upb_inttable_free(struct upb_inttable *table); -void upb_strtable_init(struct upb_strtable *table, - uint32_t size, uint16_t entry_size); -void upb_strtable_free(struct upb_strtable *table); - -INLINE uint32_t upb_table_size(struct upb_table *t) { return 1 << t->size_lg2; } -INLINE uint32_t upb_inttable_size(struct upb_inttable *t) { +void upb_inttable_init(upb_inttable *table, uint32_t size, uint16_t entry_size); +void upb_inttable_free(upb_inttable *table); +void upb_strtable_init(upb_strtable *table, uint32_t size, uint16_t entry_size); +void upb_strtable_free(upb_strtable *table); + +INLINE uint32_t upb_table_size(upb_table *t) { return 1 << t->size_lg2; } +INLINE uint32_t upb_inttable_size(upb_inttable *t) { return upb_table_size(&t->t); } -INLINE uint32_t upb_strtable_size(struct upb_strtable *t) { +INLINE uint32_t upb_strtable_size(upb_strtable *t) { return upb_table_size(&t->t); } -INLINE uint32_t upb_table_count(struct upb_table *t) { return t->count; } -INLINE uint32_t upb_inttable_count(struct upb_inttable *t) { +INLINE uint32_t upb_table_count(upb_table *t) { return t->count; } +INLINE uint32_t upb_inttable_count(upb_inttable *t) { return upb_table_count(&t->t); } -INLINE uint32_t upb_strtable_count(struct upb_strtable *t) { +INLINE uint32_t upb_strtable_count(upb_strtable *t) { return upb_table_count(&t->t); } @@ -89,43 +87,43 @@ INLINE uint32_t upb_strtable_count(struct upb_strtable *t) { * the hashtable (the amount of data copied comes from entry_size when the * table was constructed). Therefore the data at val may be freed once the * call returns. */ -void upb_inttable_insert(struct upb_inttable *t, struct upb_inttable_entry *e); -void upb_strtable_insert(struct upb_strtable *t, struct upb_strtable_entry *e); +void upb_inttable_insert(upb_inttable *t, upb_inttable_entry *e); +void upb_strtable_insert(upb_strtable *t, upb_strtable_entry *e); -INLINE uint32_t upb_inttable_bucket(struct upb_inttable *t, upb_inttable_key_t k) { +INLINE uint32_t upb_inttable_bucket(upb_inttable *t, upb_inttable_key_t k) { return (k & t->t.mask) + 1; /* Identity hash for ints. */ } -/* Looks up key in this table. Inlined because this is in the critical path - * of parsing. We have the caller specify the entry_size because fixing - * this as a literal (instead of reading table->entry_size) gives the - * compiler more ability to optimize. */ -INLINE void *upb_inttable_fast_lookup(struct upb_inttable *t, - uint32_t key, uint32_t entry_size) { +/* Looks up key in this table. Inlined because this is in the critical path of + * decoding. We have the caller specify the entry_size because fixing this as + * a literal (instead of reading table->entry_size) gives the compiler more + * ability to optimize. */ +INLINE void *upb_inttable_fastlookup(upb_inttable *t, uint32_t key, + uint32_t entry_size) { assert(key != 0); uint32_t bucket = upb_inttable_bucket(t, key); - struct upb_inttable_entry *e; + upb_inttable_entry *e; do { - e = (struct upb_inttable_entry*)UPB_INDEX(t->t.entries, bucket-1, entry_size); + e = (upb_inttable_entry*)UPB_INDEX(t->t.entries, bucket-1, entry_size); if(e->key == key) return e; } while((bucket = e->next) != UPB_END_OF_CHAIN); return NULL; /* Not found. */ } -INLINE void *upb_inttable_lookup(struct upb_inttable *t, uint32_t key) { - return upb_inttable_fast_lookup(t, key, t->t.entry_size); +INLINE void *upb_inttable_lookup(upb_inttable *t, uint32_t key) { + return upb_inttable_fastlookup(t, key, t->t.entry_size); } -void *upb_strtable_lookup(struct upb_strtable *t, upb_strptr key); +void *upb_strtable_lookup(upb_strtable *t, upb_strptr key); /* Provides iteration over the table. The order in which the entries are * returned is undefined. Insertions invalidate iterators. The _next * functions return NULL when the end has been reached. */ -void *upb_inttable_begin(struct upb_inttable *t); -void *upb_inttable_next(struct upb_inttable *t, struct upb_inttable_entry *cur); +void *upb_inttable_begin(upb_inttable *t); +void *upb_inttable_next(upb_inttable *t, upb_inttable_entry *cur); -void *upb_strtable_begin(struct upb_strtable *t); -void *upb_strtable_next(struct upb_strtable *t, struct upb_strtable_entry *cur); +void *upb_strtable_begin(upb_strtable *t); +void *upb_strtable_next(upb_strtable *t, upb_strtable_entry *cur); #ifdef __cplusplus } /* extern "C" */ diff --git a/src/upb_text.c b/src/upb_text.c index 3981715..bef60a2 100644 --- a/src/upb_text.c +++ b/src/upb_text.c @@ -41,15 +41,14 @@ void upb_text_printval(upb_field_type_t type, union upb_value val, FILE *file) } } -static void print_indent(struct upb_text_printer *p, FILE *stream) +static void print_indent(upb_text_printer *p, FILE *stream) { if(!p->single_line) for(int i = 0; i < p->indent_depth; i++) fprintf(stream, " "); } -void upb_text_printfield(struct upb_text_printer *p, - upb_strptr name, +void upb_text_printfield(upb_text_printer *p, upb_strptr name, upb_field_type_t valtype, union upb_value val, FILE *stream) { @@ -62,9 +61,7 @@ void upb_text_printfield(struct upb_text_printer *p, fputc('\n', stream); } -void upb_text_push(struct upb_text_printer *p, - upb_strptr submsg_type, - FILE *stream) +void upb_text_push(upb_text_printer *p, upb_strptr submsg_type, FILE *stream) { print_indent(p, stream); fprintf(stream, UPB_STRFMT " {", UPB_STRARG(submsg_type)); @@ -72,23 +69,22 @@ void upb_text_push(struct upb_text_printer *p, p->indent_depth++; } -void upb_text_pop(struct upb_text_printer *p, - FILE *stream) +void upb_text_pop(upb_text_printer *p, FILE *stream) { p->indent_depth--; print_indent(p, stream); fprintf(stream, "}\n"); } -static void printval(struct upb_text_printer *printer, union upb_value v, - struct upb_fielddef *f, +static void printval(upb_text_printer *printer, union upb_value v, + upb_fielddef *f, FILE *stream); -static void printmsg(struct upb_text_printer *printer, - upb_msg *msg, struct upb_msgdef *md, FILE *stream) +static void printmsg(upb_text_printer *printer, upb_msg *msg, upb_msgdef *md, + FILE *stream) { for(upb_field_count_t i = 0; i < md->num_fields; i++) { - struct upb_fielddef *f = &md->fields[i]; + upb_fielddef *f = &md->fields[i]; if(!upb_msg_has(msg, f)) continue; union upb_value v = upb_msg_get(msg, f); if(upb_isarray(f)) { @@ -103,8 +99,8 @@ static void printmsg(struct upb_text_printer *printer, } } -static void printval(struct upb_text_printer *printer, union upb_value v, - struct upb_fielddef *f, +static void printval(upb_text_printer *printer, union upb_value v, + upb_fielddef *f, FILE *stream) { if(upb_issubmsg(f)) { @@ -117,10 +113,10 @@ static void printval(struct upb_text_printer *printer, union upb_value v, } -void upb_msg_print(upb_msg *msg, struct upb_msgdef *md, bool single_line, +void upb_msg_print(upb_msg *msg, upb_msgdef *md, bool single_line, FILE *stream) { - struct upb_text_printer printer; + upb_text_printer printer; upb_text_printer_init(&printer, single_line); printmsg(&printer, msg, md, stream); } diff --git a/src/upb_text.h b/src/upb_text.h index bc15c3f..86d69a2 100644 --- a/src/upb_text.h +++ b/src/upb_text.h @@ -13,22 +13,22 @@ extern "C" { #endif -struct upb_text_printer { +typedef struct { int indent_depth; bool single_line; -}; +} upb_text_printer; -INLINE void upb_text_printer_init(struct upb_text_printer *p, bool single_line) { +INLINE void upb_text_printer_init(upb_text_printer *p, bool single_line) { p->indent_depth = 0; p->single_line = single_line; } void upb_text_printval(upb_field_type_t type, union upb_value p, FILE *file); -void upb_text_printfield(struct upb_text_printer *p, upb_strptr name, +void upb_text_printfield(upb_text_printer *p, upb_strptr name, upb_field_type_t valtype, union upb_value val, FILE *stream); -void upb_text_push(struct upb_text_printer *p, upb_strptr submsg_type, +void upb_text_push(upb_text_printer *p, upb_strptr submsg_type, FILE *stream); -void upb_text_pop(struct upb_text_printer *p, FILE *stream); +void upb_text_pop(upb_text_printer *p, FILE *stream); #ifdef __cplusplus } /* extern "C" */ -- cgit v1.2.3