From f7713ebc391d247617b0dff788a76e5c3383ae94 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 6 Sep 2018 12:57:16 -0700 Subject: Changed C API to use inline functions and computed offsets. --- upb/decode.c | 12 +++++++++++- upb/encode.c | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'upb') diff --git a/upb/decode.c b/upb/decode.c index 1e5a6bc..1f3e4d8 100644 --- a/upb/decode.c +++ b/upb/decode.c @@ -200,9 +200,19 @@ static void *upb_array_add(upb_array *arr, size_t elements) { return ret; } +static size_t get_field_offset(const upb_decframe *frame, + const upb_msglayout_field *field) { + if (field->oneof_index == UPB_NOT_IN_ONEOF) { + return field->offset; + } else { + return frame->m->oneofs[field->oneof_index].data_offset; + } +} + static upb_array *upb_getarr(upb_decframe *frame, const upb_msglayout_field *field) { UPB_ASSERT(field->label == UPB_LABEL_REPEATED); + UPB_ASSERT(field->oneof_index == UPB_NOT_IN_ONEOF); return *(upb_array**)&frame->msg[field->offset]; } @@ -237,7 +247,7 @@ static void upb_setoneofcase(upb_decframe *frame, static char *upb_decode_prepareslot(upb_decstate *d, upb_decframe *frame, const upb_msglayout_field *field) { - char *field_mem = frame->msg + field->offset; + char *field_mem = frame->msg + get_field_offset(frame, field); upb_array *arr; if (field->label == UPB_LABEL_REPEATED) { diff --git a/upb/encode.c b/upb/encode.c index d38676d..034a90d 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -340,6 +340,15 @@ bool upb_encode_hasscalarfield(const char *msg, const upb_msglayout *m, } } +static size_t get_field_offset2(const upb_msglayout *m, + const upb_msglayout_field *field) { + if (field->oneof_index == UPB_NOT_IN_ONEOF) { + return field->offset; + } else { + return m->oneofs[field->oneof_index].data_offset; + } +} + bool upb_encode_message(upb_encstate *e, const char *msg, const upb_msglayout *m, size_t *size) { int i; @@ -351,13 +360,14 @@ bool upb_encode_message(upb_encstate *e, const char *msg, for (i = m->field_count - 1; i >= 0; i--) { const upb_msglayout_field *f = &m->fields[i]; + size_t offset = get_field_offset2(m, f); if (f->label == UPB_LABEL_REPEATED) { - CHK(upb_encode_array(e, msg + f->offset, m, f)); + CHK(upb_encode_array(e, msg + offset, m, f)); } else { if (upb_encode_hasscalarfield(msg, m, f)) { if (f->oneof_index == UPB_NOT_IN_ONEOF) { - CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, !m->is_proto2)); + CHK(upb_encode_scalarfield(e, msg + offset, m, f, !m->is_proto2)); } else { const upb_msglayout_oneof *o = &m->oneofs[f->oneof_index]; CHK(upb_encode_scalarfield(e, msg + o->data_offset, -- cgit v1.2.3