diff options
-rw-r--r-- | upb/bindings/lua/msg.c | 11 | ||||
-rw-r--r-- | upb/bindings/lua/upb.h | 1 | ||||
-rw-r--r-- | upb/decode.c | 50 | ||||
-rw-r--r-- | upb/encode.c | 25 | ||||
-rw-r--r-- | upb/generated_util.h | 2 | ||||
-rw-r--r-- | upb/legacy_msg_reflection.c | 16 | ||||
-rw-r--r-- | upb/legacy_msg_reflection.h | 5 | ||||
-rw-r--r-- | upb/msg.c | 7 | ||||
-rw-r--r-- | upb/msg.h | 4 |
9 files changed, 46 insertions, 75 deletions
diff --git a/upb/bindings/lua/msg.c b/upb/bindings/lua/msg.c index 4b8a854..5e769b2 100644 --- a/upb/bindings/lua/msg.c +++ b/upb/bindings/lua/msg.c @@ -129,7 +129,7 @@ char lupb_arena_cache_key; * Callers can be guaranteed that it will be alive as long as |L| is. * TODO(haberman): we shouldn't use a global arena! We should have * one arena for a parse, or per independently-created message. */ -static upb_arena *lupb_arena_get(lua_State *L) { +upb_arena *lupb_arena_get(lua_State *L) { upb_arena *arena; lua_pushlightuserdata(L, &lupb_arena_cache_key); @@ -473,6 +473,7 @@ typedef struct { * case but simplifies the code. Could optimize away if desired. */ const lupb_msgclass *lmsgclass; upb_array *arr; + upb_fieldtype_t type; } lupb_array; #define ARRAY_MSGCLASS_INDEX 0 @@ -540,8 +541,9 @@ static int lupb_array_new(lua_State *L) { } larray = lupb_newuserdata(L, sizeof(*larray), LUPB_ARRAY); + larray->type = type; larray->lmsgclass = lmsgclass; - larray->arr = upb_array_new(type, lupb_arena_get(L)); + larray->arr = upb_array_new(lupb_arena_get(L)); return 1; } @@ -552,7 +554,7 @@ static int lupb_array_newindex(lua_State *L) { uint32_t n = lupb_array_checkindex(L, 2, upb_array_size(larray->arr) + 1); upb_msgval msgval = lupb_tomsgval(L, type, 3, larray->lmsgclass); - upb_array_set(larray->arr, n, msgval); + upb_array_set(larray->arr, larray->type, n, msgval, lupb_arena_get(L)); if (lupb_istypewrapped(type)) { lupb_uservalseti(L, 1, n, 3); @@ -570,7 +572,8 @@ static int lupb_array_index(lua_State *L) { if (lupb_istypewrapped(type)) { lupb_uservalgeti(L, 1, n); } else { - lupb_pushmsgval(L, upb_array_type(array), upb_array_get(array, n)); + lupb_pushmsgval(L, upb_array_type(array), + upb_array_get(array, larray->type, n)); } return 1; diff --git a/upb/bindings/lua/upb.h b/upb/bindings/lua/upb.h index 6861286..51d8acf 100644 --- a/upb/bindings/lua/upb.h +++ b/upb/bindings/lua/upb.h @@ -111,6 +111,7 @@ typedef struct lupb_msgclass lupb_msgclass; upb_arena *lupb_arena_check(lua_State *L, int narg); int lupb_arena_new(lua_State *L); +upb_arena *lupb_arena_get(lua_State *L); int lupb_msg_pushref(lua_State *L, int msgclass, void *msg); const upb_msg *lupb_msg_checkmsg(lua_State *L, int narg, const lupb_msgclass *lmsgclass); diff --git a/upb/decode.c b/upb/decode.c index eb3306f..559e640 100644 --- a/upb/decode.c +++ b/upb/decode.c @@ -174,13 +174,14 @@ static bool upb_skip_unknowngroup(upb_decstate *d, int field_number) { return true; } -static bool upb_array_grow(upb_array *arr, size_t elements, size_t elem_size) { +static bool upb_array_grow(upb_array *arr, size_t elements, size_t elem_size, + upb_arena *arena) { size_t needed = arr->len + elements; size_t new_size = UPB_MAX(arr->size, 8); size_t new_bytes; size_t old_bytes; void *new_data; - upb_alloc *alloc = upb_arena_alloc(arr->arena); + upb_alloc *alloc = upb_arena_alloc(arena); while (new_size < needed) { new_size *= 2; @@ -197,16 +198,16 @@ static bool upb_array_grow(upb_array *arr, size_t elements, size_t elem_size) { } static void *upb_array_reserve(upb_array *arr, size_t elements, - size_t elem_size) { + size_t elem_size, upb_arena *arena) { if (arr->size - arr->len < elements) { - CHK(upb_array_grow(arr, elements, elem_size)); + CHK(upb_array_grow(arr, elements, elem_size, arena)); } return (char*)arr->data + (arr->len * elem_size); } bool upb_array_add(upb_array *arr, size_t elements, size_t elem_size, - const void *data) { - void *dest = upb_array_reserve(arr, elements, elem_size); + const void *data, upb_arena *arena) { + void *dest = upb_array_reserve(arr, elements, elem_size, arena); CHK(dest); arr->len += elements; @@ -226,8 +227,7 @@ static upb_array *upb_getorcreatearr(upb_decframe *frame, upb_array *arr = upb_getarr(frame, field); if (!arr) { - upb_fieldtype_t type = upb_desctype_to_fieldtype[field->descriptortype]; - arr = upb_array_new(type, frame->state->arena); + arr = upb_array_new(frame->state->arena); CHK(arr); *(upb_array**)&frame->msg[field->offset] = arr; } @@ -260,7 +260,7 @@ static upb_msg *upb_addmsg(upb_decframe *frame, *subm = frame->layout->submsgs[field->submsg_index]; submsg = upb_msg_new(*subm, frame->state->arena); CHK(submsg); - upb_array_add(arr, 1, sizeof(submsg), &submsg); + upb_array_add(arr, 1, sizeof(submsg), &submsg, frame->state->arena); return submsg; } @@ -287,7 +287,7 @@ static bool upb_decode_addval(upb_decframe *frame, if (field->label == UPB_LABEL_REPEATED) { arr = upb_getorcreatearr(frame, field); CHK(arr); - field_mem = upb_array_reserve(arr, 1, size); + field_mem = upb_array_reserve(arr, 1, size, frame->state->arena); CHK(field_mem); } @@ -414,7 +414,7 @@ static bool upb_decode_fixedpacked(upb_decstate *d, upb_array *arr, size_t elements = len / elem_size; CHK((size_t)(elements * elem_size) == len); - CHK(upb_array_add(arr, elements, elem_size, d->ptr)); + CHK(upb_array_add(arr, elements, elem_size, d->ptr, d->arena)); d->ptr += len; return true; @@ -433,26 +433,26 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, upb_array *arr = upb_getorcreatearr(frame, field); CHK(arr); -#define VARINT_CASE(ctype, decode) \ - { \ - const char *ptr = d->ptr; \ - const char *limit = ptr + len; \ - while (ptr < limit) { \ - uint64_t val; \ - ctype decoded; \ - CHK(upb_decode_varint(&ptr, limit, &val)); \ - decoded = (decode)(val); \ - CHK(upb_array_add(arr, 1, sizeof(decoded), &decoded)); \ - } \ - d->ptr = ptr; \ - return true; \ +#define VARINT_CASE(ctype, decode) \ + { \ + const char *ptr = d->ptr; \ + const char *limit = ptr + len; \ + while (ptr < limit) { \ + uint64_t val; \ + ctype decoded; \ + CHK(upb_decode_varint(&ptr, limit, &val)); \ + decoded = (decode)(val); \ + CHK(upb_array_add(arr, 1, sizeof(decoded), &decoded, d->arena)); \ + } \ + d->ptr = ptr; \ + return true; \ } switch (field->descriptortype) { case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_BYTES: { upb_strview str = upb_decode_strfield(d, len); - return upb_array_add(arr, 1, sizeof(str), &str); + return upb_array_add(arr, 1, sizeof(str), &str, d->arena); } case UPB_DESCRIPTOR_TYPE_FLOAT: case UPB_DESCRIPTOR_TYPE_FIXED32: diff --git a/upb/encode.c b/upb/encode.c index 576d238..0f15803 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -12,29 +12,6 @@ #define UPB_PB_VARINT_MAX_LEN 10 #define CHK(x) do { if (!(x)) { return false; } } while(0) -/* Maps descriptor type -> upb field type. */ -static const uint8_t upb_desctype_to_fieldtype2[] = { - UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */ - UPB_TYPE_DOUBLE, /* DOUBLE */ - UPB_TYPE_FLOAT, /* FLOAT */ - UPB_TYPE_INT64, /* INT64 */ - UPB_TYPE_UINT64, /* UINT64 */ - UPB_TYPE_INT32, /* INT32 */ - UPB_TYPE_UINT64, /* FIXED64 */ - UPB_TYPE_UINT32, /* FIXED32 */ - UPB_TYPE_BOOL, /* BOOL */ - UPB_TYPE_STRING, /* STRING */ - UPB_TYPE_MESSAGE, /* GROUP */ - UPB_TYPE_MESSAGE, /* MESSAGE */ - UPB_TYPE_BYTES, /* BYTES */ - UPB_TYPE_UINT32, /* UINT32 */ - UPB_TYPE_ENUM, /* ENUM */ - UPB_TYPE_INT32, /* SFIXED32 */ - UPB_TYPE_INT64, /* SFIXED64 */ - UPB_TYPE_INT32, /* SINT32 */ - UPB_TYPE_INT64, /* SINT64 */ -}; - static size_t upb_encode_varint(uint64_t val, char *buf) { size_t i; if (val < 128) { buf[0] = val; return 1; } @@ -166,8 +143,6 @@ static bool upb_encode_array(upb_encstate *e, const char *field_mem, return true; } - UPB_ASSERT(arr->type == upb_desctype_to_fieldtype2[f->descriptortype]); - #define VARINT_CASE(ctype, encode) { \ ctype *start = arr->data; \ ctype *ptr = start + arr->len; \ diff --git a/upb/generated_util.h b/upb/generated_util.h index 314a6d5..a0b3e8d 100644 --- a/upb/generated_util.h +++ b/upb/generated_util.h @@ -46,7 +46,7 @@ UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size, upb_array *arr = *PTR_AT(msg, ofs, upb_array*); if (!arr) { - arr = upb_array_new(type, arena); + arr = upb_array_new(arena); if (!arr) return NULL; *PTR_AT(msg, ofs, upb_array*) = arr; } diff --git a/upb/legacy_msg_reflection.c b/upb/legacy_msg_reflection.c index 0683dde..81a878b 100644 --- a/upb/legacy_msg_reflection.c +++ b/upb/legacy_msg_reflection.c @@ -167,18 +167,15 @@ size_t upb_array_size(const upb_array *arr) { return arr->len; } -upb_fieldtype_t upb_array_type(const upb_array *arr) { - return arr->type; -} - -upb_msgval upb_array_get(const upb_array *arr, size_t i) { - size_t element_size = upb_msgval_sizeof(arr->type); +upb_msgval upb_array_get(const upb_array *arr, upb_fieldtype_t type, size_t i) { + size_t element_size = upb_msgval_sizeof(type); UPB_ASSERT(i < arr->len); return upb_msgval_read(arr->data, i * element_size, element_size); } -bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) { - size_t element_size = upb_msgval_sizeof(arr->type); +bool upb_array_set(upb_array *arr, upb_fieldtype_t type, size_t i, + upb_msgval val, upb_arena *arena) { + size_t element_size = upb_msgval_sizeof(type); UPB_ASSERT(i <= arr->len); if (i == arr->len) { @@ -189,7 +186,7 @@ bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) { size_t new_size = UPB_MAX(arr->size * 2, 8); size_t new_bytes = new_size * element_size; size_t old_bytes = arr->size * element_size; - upb_alloc *alloc = upb_arena_alloc(arr->arena); + upb_alloc *alloc = upb_arena_alloc(arena); upb_msgval *new_data = upb_realloc(alloc, arr->data, old_bytes, new_bytes); @@ -208,7 +205,6 @@ bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) { return true; } - /** upb_map *******************************************************************/ struct upb_map { diff --git a/upb/legacy_msg_reflection.h b/upb/legacy_msg_reflection.h index 32a621b..b33ec68 100644 --- a/upb/legacy_msg_reflection.h +++ b/upb/legacy_msg_reflection.h @@ -126,12 +126,13 @@ upb_fieldtype_t upb_array_type(const upb_array *arr); /* Read-only interface. Safe for anyone to call. */ size_t upb_array_size(const upb_array *arr); -upb_msgval upb_array_get(const upb_array *arr, size_t i); +upb_msgval upb_array_get(const upb_array *arr, upb_fieldtype_t type, size_t i); /* Write interface. May only be called by the message's owner who can enforce * its memory management invariants. */ -bool upb_array_set(upb_array *arr, size_t i, upb_msgval val); +bool upb_array_set(upb_array *arr, upb_fieldtype_t type, size_t i, + upb_msgval val, upb_arena *arena); /** upb_map *******************************************************************/ @@ -74,19 +74,16 @@ upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a) { return msg; } -upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a) { - upb_alloc *alloc = upb_arena_alloc(a); - upb_array *ret = upb_malloc(alloc, sizeof(upb_array)); +upb_array *upb_array_new(upb_arena *a) { + upb_array *ret = upb_arena_malloc(a, sizeof(upb_array)); if (!ret) { return NULL; } - ret->type = type; ret->data = NULL; ret->len = 0; ret->size = 0; - ret->arena = a; return ret; } @@ -48,11 +48,9 @@ typedef struct upb_msglayout { /* Our internal representation for repeated fields. */ typedef struct { - upb_fieldtype_t type; void *data; /* Each element is element_size. */ size_t len; /* Measured in elements. */ size_t size; /* Measured in elements. */ - upb_arena *arena; } upb_array; upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a); @@ -62,7 +60,7 @@ void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len, upb_arena *arena); const char *upb_msg_getunknown(const upb_msg *msg, size_t *len); -upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a); +upb_array *upb_array_new(upb_arena *a); #ifdef __cplusplus } /* extern "C" */ |