summaryrefslogtreecommitdiff
path: root/upb
diff options
context:
space:
mode:
Diffstat (limited to 'upb')
-rw-r--r--upb/bindings/lua/upb/pb.c2
-rw-r--r--upb/decode.c14
-rw-r--r--upb/decode.h2
-rw-r--r--upb/encode.c6
-rw-r--r--upb/generated_util.h102
-rw-r--r--upb/msg.c2
-rw-r--r--upb/msg.h20
-rw-r--r--upb/msgfactory.c2
-rw-r--r--upb/structs.int.h3
9 files changed, 130 insertions, 23 deletions
diff --git a/upb/bindings/lua/upb/pb.c b/upb/bindings/lua/upb/pb.c
index bca2ee8..b5284df 100644
--- a/upb/bindings/lua/upb/pb.c
+++ b/upb/bindings/lua/upb/pb.c
@@ -16,7 +16,7 @@ static int lupb_pb_decode(lua_State *L) {
const upb_msglayout *layout;
upb_msg *msg = lupb_msg_checkmsg2(L, 1, &layout);
const char *pb = lua_tolstring(L, 2, &len);
- upb_stringview buf = upb_stringview_make(pb, len);
+ upb_strview buf = upb_strview_make(pb, len);
upb_decode(buf, msg, layout);
/* TODO(haberman): check for error. */
diff --git a/upb/decode.c b/upb/decode.c
index fc0e644..c54ee3c 100644
--- a/upb/decode.c
+++ b/upb/decode.c
@@ -112,14 +112,14 @@ static int64_t upb_zzdecode_64(uint64_t n) {
}
static bool upb_decode_string(const char **ptr, const char *limit,
- upb_stringview *val) {
+ upb_strview *val) {
uint32_t len;
CHK(upb_decode_varint32(ptr, limit, &len) &&
len < INT32_MAX &&
limit - *ptr >= (int32_t)len);
- *val = upb_stringview_make(*ptr, len);
+ *val = upb_strview_make(*ptr, len);
*ptr += len;
return true;
}
@@ -150,7 +150,7 @@ static bool upb_skip_unknownfielddata(upb_decstate *d, upb_decframe *frame,
return upb_decode_64bit(&d->ptr, frame->limit, &val);
}
case UPB_WIRE_TYPE_DELIMITED: {
- upb_stringview val;
+ upb_strview val;
return upb_decode_string(&d->ptr, frame->limit, &val);
}
case UPB_WIRE_TYPE_START_GROUP:
@@ -374,7 +374,7 @@ static bool upb_decode_32bitfield(upb_decstate *d, upb_decframe *frame,
return true;
}
-static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data,
+static bool upb_decode_fixedpacked(upb_array *arr, upb_strview data,
int elem_size) {
int elements = data.size / elem_size;
void *field_mem;
@@ -389,7 +389,7 @@ static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data,
static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame,
const char *field_start,
const upb_msglayout_field *field,
- upb_stringview val) {
+ upb_strview val) {
upb_array *arr = upb_getorcreatearr(frame, field);
#define VARINT_CASE(ctype, decode) { \
@@ -470,7 +470,7 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame,
static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame,
const char *field_start,
const upb_msglayout_field *field) {
- upb_stringview val;
+ upb_strview val;
CHK(upb_decode_string(&d->ptr, frame->limit, &val));
@@ -584,7 +584,7 @@ static bool upb_decode_message(upb_decstate *d, const char *limit,
return true;
}
-bool upb_decode(upb_stringview buf, void *msg, const upb_msglayout *l) {
+bool upb_decode(upb_strview buf, void *msg, const upb_msglayout *l) {
upb_decstate state;
state.ptr = buf.data;
diff --git a/upb/decode.h b/upb/decode.h
index 79774ed..dee4c0f 100644
--- a/upb/decode.h
+++ b/upb/decode.h
@@ -9,7 +9,7 @@
UPB_BEGIN_EXTERN_C
-bool upb_decode(upb_stringview buf, upb_msg *msg, const upb_msglayout *l);
+bool upb_decode(upb_strview buf, upb_msg *msg, const upb_msglayout *l);
UPB_END_EXTERN_C
diff --git a/upb/encode.c b/upb/encode.c
index bff8262..b728353 100644
--- a/upb/encode.c
+++ b/upb/encode.c
@@ -207,8 +207,8 @@ do { ; } while(0)
VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
case UPB_DESCRIPTOR_TYPE_STRING:
case UPB_DESCRIPTOR_TYPE_BYTES: {
- upb_stringview *start = arr->data;
- upb_stringview *ptr = start + arr->len;
+ upb_strview *start = arr->data;
+ upb_strview *ptr = start + arr->len;
do {
ptr--;
CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
@@ -292,7 +292,7 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem,
CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
case UPB_DESCRIPTOR_TYPE_STRING:
case UPB_DESCRIPTOR_TYPE_BYTES: {
- upb_stringview view = *(upb_stringview*)field_mem;
+ upb_strview view = *(upb_strview*)field_mem;
if (skip_zero_value && view.size == 0) {
return true;
}
diff --git a/upb/generated_util.h b/upb/generated_util.h
new file mode 100644
index 0000000..bd47389
--- /dev/null
+++ b/upb/generated_util.h
@@ -0,0 +1,102 @@
+/*
+** Functions for use by generated code. These are not public and users must
+** not call them directly.
+*/
+
+#ifndef UPB_GENERATED_UTIL_H_
+#define UPB_GENERATED_UTIL_H_
+
+#include <stdint.h>
+#include "upb/structs.int.h"
+
+#define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs)
+
+UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs,
+ size_t *size) {
+ const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*);
+ if (arr) {
+ if (size) *size = arr->size;
+ return arr->data;
+ } else {
+ if (size) *size = 0;
+ return NULL;
+ }
+}
+
+UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
+ size_t *size) {
+ upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
+ if (arr) {
+ if (size) *size = arr->size;
+ return arr->data;
+ } else {
+ if (size) *size = 0;
+ return NULL;
+ }
+}
+
+/* TODO(haberman): this is a mess. It will improve when upb_array no longer
+ * carries reflective state (type, elem_size). */
+UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
+ size_t elem_size,
+ upb_fieldtype_t type,
+ upb_arena *arena) {
+ upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
+
+ if (!arr) {
+ arr = upb_array_new(type, arena);
+ if (!arr) return NULL;
+ *PTR_AT(msg, ofs, upb_array*) = arr;
+ }
+
+ if (size > arr->size) {
+ size_t new_size = UPB_MAX(arr->size, 4);
+ size_t old_bytes = arr->size * elem_size;
+ size_t new_bytes;
+ upb_alloc *alloc = upb_arena_alloc(arr->arena);
+ while (new_size < size) new_size *= 2;
+ new_bytes = new_size * elem_size;
+ arr->data = upb_realloc(alloc, arr->data, old_bytes, new_bytes);
+ if (!arr->data) {
+ return NULL;
+ }
+ arr->size = new_size;
+ }
+
+ arr->len = size;
+ return arr->data;
+}
+
+UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
+ size_t elem_size,
+ upb_fieldtype_t type,
+ const void *value,
+ upb_arena *arena) {
+ upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
+ size_t i = arr ? arr->len : 0;
+ void *data =
+ _upb_array_resize_accessor(msg, ofs, i + 1, elem_size, type, arena);
+ if (!data) return false;
+ memcpy(PTR_AT(data, i * elem_size, char), value, elem_size);
+ return true;
+}
+
+UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) {
+ return (*PTR_AT(msg, idx / 8, const char) & (idx % 8)) != 0;
+}
+
+UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) {
+ return (*PTR_AT(msg, idx / 8, char)) |= (1 << (idx % 8));
+}
+
+UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) {
+ return (*PTR_AT(msg, idx / 8, char)) &= ~(1 << (idx % 8));
+}
+
+UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) {
+ return *PTR_AT(msg, case_ofs, int32_t) == num;
+}
+
+#undef PTR_AT
+
+#endif /* UPB_GENERATED_UTIL_H_ */
diff --git a/upb/msg.c b/upb/msg.c
index 29ba2e1..c6f0c17 100644
--- a/upb/msg.c
+++ b/upb/msg.c
@@ -51,7 +51,7 @@ static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
return sizeof(void*);
case UPB_TYPE_BYTES:
case UPB_TYPE_STRING:
- return sizeof(upb_stringview);
+ return sizeof(upb_strview);
}
UPB_UNREACHABLE();
}
diff --git a/upb/msg.h b/upb/msg.h
index c72e9f0..058f4a9 100644
--- a/upb/msg.h
+++ b/upb/msg.h
@@ -36,10 +36,12 @@ class MessageLayout;
#endif
-UPB_DECLARE_TYPE(upb::Array, upb_array)
UPB_DECLARE_TYPE(upb::Map, upb_map)
UPB_DECLARE_TYPE(upb::MapIterator, upb_mapiter)
+struct upb_array;
+typedef struct upb_array upb_array;
+
/* TODO(haberman): C++ accessors */
UPB_BEGIN_EXTERN_C
@@ -73,21 +75,21 @@ typedef struct upb_msglayout {
} upb_msglayout;
-/** upb_stringview ************************************************************/
+/** upb_strview ************************************************************/
typedef struct {
const char *data;
size_t size;
-} upb_stringview;
+} upb_strview;
-UPB_INLINE upb_stringview upb_stringview_make(const char *data, size_t size) {
- upb_stringview ret;
+UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) {
+ upb_strview ret;
ret.data = data;
ret.size = size;
return ret;
}
-#define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len}
+#define UPB_STRVIEW_INIT(ptr, len) {ptr, len}
/** upb_msgval ****************************************************************/
@@ -107,7 +109,7 @@ typedef union {
const upb_msg* msg;
const upb_array* arr;
const void* ptr;
- upb_stringview str;
+ upb_strview str;
} upb_msgval;
#define ACCESSORS(name, membername, ctype) \
@@ -134,12 +136,12 @@ ACCESSORS(map, map, const upb_map*)
ACCESSORS(msg, msg, const upb_msg*)
ACCESSORS(ptr, ptr, const void*)
ACCESSORS(arr, arr, const upb_array*)
-ACCESSORS(str, str, upb_stringview)
+ACCESSORS(str, str, upb_strview)
#undef ACCESSORS
UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
- return upb_msgval_str(upb_stringview_make(data, size));
+ return upb_msgval_str(upb_strview_make(data, size));
}
diff --git a/upb/msgfactory.c b/upb/msgfactory.c
index 593c9dc..05f5975 100644
--- a/upb/msgfactory.c
+++ b/upb/msgfactory.c
@@ -32,7 +32,7 @@ static size_t upb_msgval_sizeof2(upb_fieldtype_t type) {
return sizeof(void*);
case UPB_TYPE_BYTES:
case UPB_TYPE_STRING:
- return sizeof(upb_stringview);
+ return sizeof(upb_strview);
}
UPB_UNREACHABLE();
}
diff --git a/upb/structs.int.h b/upb/structs.int.h
index 6bc502e..1394dd4 100644
--- a/upb/structs.int.h
+++ b/upb/structs.int.h
@@ -5,6 +5,9 @@
#ifndef UPB_STRUCTS_H_
#define UPB_STRUCTS_H_
+#include "upb/def.h"
+#include "upb/msg.h"
+
struct upb_array {
upb_fieldtype_t type;
uint8_t element_size;
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback