rename the upb_Array 'len' field as 'size'

Now that 'size' has been renamed as 'capacity' we are free to rename 'len' as
'size', so upb_Array_Size() is actually returning the 'size' field.

PiperOrigin-RevId: 456865972
pull/13171/head
Protobuf Team Bot 3 years ago committed by Copybara-Service
parent e153b52394
commit 1695cb2788
  1. 26
      upb/array.c
  2. 38
      upb/decode.c
  3. 6
      upb/decode_fast.c
  4. 12
      upb/encode.c
  5. 2
      upb/msg.c
  6. 18
      upb/msg_internal.h

@ -53,13 +53,13 @@ upb_Array* upb_Array_New(upb_Arena* a, upb_CType type) {
return _upb_Array_New(a, 4, _upb_CTypeo_sizelg2[type]);
}
size_t upb_Array_Size(const upb_Array* arr) { return arr->len; }
size_t upb_Array_Size(const upb_Array* arr) { return arr->size; }
upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i) {
upb_MessageValue ret;
const char* data = _upb_array_constptr(arr);
int lg2 = arr->data & 7;
UPB_ASSERT(i < arr->len);
UPB_ASSERT(i < arr->size);
memcpy(&ret, data + (i << lg2), 1 << lg2);
return ret;
}
@ -67,15 +67,15 @@ upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i) {
void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val) {
char* data = _upb_array_ptr(arr);
int lg2 = arr->data & 7;
UPB_ASSERT(i < arr->len);
UPB_ASSERT(i < arr->size);
memcpy(data + (i << lg2), &val, 1 << lg2);
}
bool upb_Array_Append(upb_Array* arr, upb_MessageValue val, upb_Arena* arena) {
if (!upb_Array_Resize(arr, arr->len + 1, arena)) {
if (!upb_Array_Resize(arr, arr->size + 1, arena)) {
return false;
}
upb_Array_Set(arr, arr->len - 1, val);
upb_Array_Set(arr, arr->size - 1, val);
return true;
}
@ -88,10 +88,10 @@ void upb_Array_Move(upb_Array* arr, size_t dst_idx, size_t src_idx,
bool upb_Array_Insert(upb_Array* arr, size_t i, size_t count,
upb_Arena* arena) {
UPB_ASSERT(i <= arr->len);
UPB_ASSERT(count + arr->len >= count);
size_t oldsize = arr->len;
if (!upb_Array_Resize(arr, arr->len + count, arena)) {
UPB_ASSERT(i <= arr->size);
UPB_ASSERT(count + arr->size >= count);
size_t oldsize = arr->size;
if (!upb_Array_Resize(arr, arr->size + count, arena)) {
return false;
}
upb_Array_Move(arr, i + count, i, oldsize - i);
@ -99,15 +99,15 @@ bool upb_Array_Insert(upb_Array* arr, size_t i, size_t count,
}
/*
* i end arr->len
* i end arr->size
* |------------|XXXXXXXX|--------|
*/
void upb_Array_Delete(upb_Array* arr, size_t i, size_t count) {
size_t end = i + count;
UPB_ASSERT(i <= end);
UPB_ASSERT(end <= arr->len);
upb_Array_Move(arr, i, end, arr->len - end);
arr->len -= count;
UPB_ASSERT(end <= arr->size);
upb_Array_Move(arr, i, end, arr->size - end);
arr->size -= count;
}
bool upb_Array_Resize(upb_Array* arr, size_t size, upb_Arena* arena) {

@ -201,8 +201,8 @@ static void decode_verifyutf8(upb_Decoder* d, const char* buf, int len) {
}
static bool decode_reserve(upb_Decoder* d, upb_Array* arr, size_t elem) {
bool need_realloc = arr->capacity - arr->len < elem;
if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, &d->arena)) {
bool need_realloc = arr->capacity - arr->size < elem;
if (need_realloc && !_upb_array_realloc(arr, arr->size + elem, &d->arena)) {
decode_err(d, kUpb_DecodeStatus_OutOfMemory);
}
return need_realloc;
@ -445,8 +445,8 @@ static const char* decode_enum_toarray(upb_Decoder* d, const char* ptr,
wireval* val) {
const upb_MiniTable_Enum* e = subs[field->submsg_index].subenum;
if (!decode_checkenum(d, ptr, msg, e, field, val)) return ptr;
void* mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len * 4, void);
arr->len++;
void* mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->size * 4, void);
arr->size++;
memcpy(mem, val, 4);
return ptr;
}
@ -463,8 +463,8 @@ static const char* decode_fixed_packed(upb_Decoder* d, const char* ptr,
return decode_err(d, kUpb_DecodeStatus_Malformed);
}
decode_reserve(d, arr, count);
void* mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
arr->len += count;
void* mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->size << lg2, void);
arr->size += count;
// Note: if/when the decoder supports multi-buffer input, we will need to
// handle buffer seams here.
if (_upb_IsLittleEndian()) {
@ -501,15 +501,15 @@ static const char* decode_varint_packed(upb_Decoder* d, const char* ptr,
int lg2) {
int scale = 1 << lg2;
int saved_limit = decode_pushlimit(d, ptr, val->size);
char* out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
char* out = UPB_PTR_AT(_upb_array_ptr(arr), arr->size << lg2, void);
while (!decode_isdone(d, &ptr)) {
wireval elem;
ptr = decode_varint64(d, ptr, &elem.uint64_val);
decode_munge(field->descriptortype, &elem);
if (decode_reserve(d, arr, 1)) {
out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
out = UPB_PTR_AT(_upb_array_ptr(arr), arr->size << lg2, void);
}
arr->len++;
arr->size++;
memcpy(out, &elem, scale);
out += scale;
}
@ -525,7 +525,7 @@ static const char* decode_enum_packed(upb_Decoder* d, const char* ptr,
wireval* val) {
const upb_MiniTable_Enum* e = subs[field->submsg_index].subenum;
int saved_limit = decode_pushlimit(d, ptr, val->size);
char* out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len * 4, void);
char* out = UPB_PTR_AT(_upb_array_ptr(arr), arr->size * 4, void);
while (!decode_isdone(d, &ptr)) {
wireval elem;
ptr = decode_varint64(d, ptr, &elem.uint64_val);
@ -534,9 +534,9 @@ static const char* decode_enum_packed(upb_Decoder* d, const char* ptr,
continue;
}
if (decode_reserve(d, arr, 1)) {
out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len * 4, void);
out = UPB_PTR_AT(_upb_array_ptr(arr), arr->size * 4, void);
}
arr->len++;
arr->size++;
memcpy(out, &elem, 4);
out += 4;
}
@ -567,8 +567,8 @@ static const char* decode_toarray(upb_Decoder* d, const char* ptr,
case OP_SCALAR_LG2(2):
case OP_SCALAR_LG2(3):
/* Append scalar value. */
mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << op, void);
arr->len++;
mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->size << op, void);
arr->size++;
memcpy(mem, val, 1 << op);
return ptr;
case OP_STRING:
@ -576,16 +576,16 @@ static const char* decode_toarray(upb_Decoder* d, const char* ptr,
/* Fallthrough. */
case OP_BYTES: {
/* Append bytes. */
upb_StringView* str = (upb_StringView*)_upb_array_ptr(arr) + arr->len;
arr->len++;
upb_StringView* str = (upb_StringView*)_upb_array_ptr(arr) + arr->size;
arr->size++;
return decode_readstr(d, ptr, val->size, str);
}
case OP_SUBMSG: {
/* Append submessage / group. */
upb_Message* submsg = decode_newsubmsg(d, subs, field);
*UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(void*), upb_Message*) =
submsg;
arr->len++;
*UPB_PTR_AT(_upb_array_ptr(arr), arr->size * sizeof(void*),
upb_Message*) = submsg;
arr->size++;
if (UPB_UNLIKELY(field->descriptortype == kUpb_FieldType_Group)) {
return decode_togroup(d, ptr, submsg, subs, field);
} else {

@ -244,7 +244,7 @@ static bool fastdecode_tagmatch(uint32_t tag, uint64_t data, int tagbytes) {
UPB_FORCEINLINE
static void fastdecode_commitarr(void* dst, fastdecode_arr* farr,
int valbytes) {
farr->arr->len =
farr->arr->size =
(size_t)((char*)dst - (char*)_upb_array_ptr(farr->arr)) / valbytes;
}
@ -315,7 +315,7 @@ static void* fastdecode_getfield(upb_Decoder* d, const char* ptr,
begin = _upb_array_ptr(farr->arr);
farr->end = begin + (farr->arr->capacity * valbytes);
*data = fastdecode_loadtag(ptr);
return begin + (farr->arr->len * valbytes);
return begin + (farr->arr->size * valbytes);
}
default:
UPB_UNREACHABLE();
@ -600,7 +600,7 @@ TAGBYTES(p)
\
char* dst = _upb_array_ptr(arr); \
memcpy(dst, ptr, size); \
arr->len = elems; \
arr->size = elems; \
\
ptr += size; \
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);

@ -173,7 +173,7 @@ static void encode_tag(upb_encstate* e, uint32_t field_number,
static void encode_fixedarray(upb_encstate* e, const upb_Array* arr,
size_t elem_size, uint32_t tag) {
size_t bytes = arr->len * elem_size;
size_t bytes = arr->size * elem_size;
const char* data = _upb_array_constptr(arr);
const char* ptr = data + bytes - elem_size;
@ -294,14 +294,14 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
bool packed = f->mode & kUpb_LabelFlags_IsPacked;
size_t pre_len = e->limit - e->ptr;
if (arr == NULL || arr->len == 0) {
if (arr == NULL || arr->size == 0) {
return;
}
#define VARINT_CASE(ctype, encode) \
{ \
const ctype* start = _upb_array_constptr(arr); \
const ctype* ptr = start + arr->len; \
const ctype* ptr = start + arr->size; \
uint32_t tag = packed ? 0 : (f->number << 3) | kUpb_WireType_Varint; \
do { \
ptr--; \
@ -345,7 +345,7 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
case kUpb_FieldType_String:
case kUpb_FieldType_Bytes: {
const upb_StringView* start = _upb_array_constptr(arr);
const upb_StringView* ptr = start + arr->len;
const upb_StringView* ptr = start + arr->size;
do {
ptr--;
encode_bytes(e, ptr->data, ptr->size);
@ -356,7 +356,7 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
}
case kUpb_FieldType_Group: {
const void* const* start = _upb_array_constptr(arr);
const void* const* ptr = start + arr->len;
const void* const* ptr = start + arr->size;
const upb_MiniTable* subm = subs[f->submsg_index].submsg;
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
do {
@ -371,7 +371,7 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
}
case kUpb_FieldType_Message: {
const void* const* start = _upb_array_constptr(arr);
const void* const* ptr = start + arr->len;
const void* const* ptr = start + arr->size;
const upb_MiniTable* subm = subs[f->submsg_index].submsg;
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
do {

@ -220,7 +220,7 @@ bool _upb_Array_Append_fallback(upb_Array** arr_ptr, const void* value,
upb_Array* arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
if (!arr) return false;
size_t elems = arr->len;
size_t elems = arr->size;
if (!_upb_Array_Resize(arr, elems + 1, arena)) {
return false;

@ -412,7 +412,7 @@ UPB_INLINE bool _upb_has_submsg_nohasbit(const upb_Message* msg, size_t ofs) {
/* Our internal representation for repeated fields. */
typedef struct {
uintptr_t data; /* Tagged ptr: low 3 bits of ptr are lg2(elem size). */
size_t len; /* Measured in elements. */
size_t size; /* The number of elements in the array. */
size_t capacity; /* Allocated storage. Measured in elements. */
uint64_t junk;
} upb_Array;
@ -444,7 +444,7 @@ UPB_INLINE upb_Array* _upb_Array_New(upb_Arena* a, size_t init_capacity,
upb_Array* arr = (upb_Array*)upb_Arena_Malloc(a, bytes);
if (!arr) return NULL;
arr->data = _upb_tag_arrptr(UPB_PTR_AT(arr, arr_size, void), elem_size_lg2);
arr->len = 0;
arr->size = 0;
arr->capacity = init_capacity;
return arr;
}
@ -467,7 +467,7 @@ UPB_INLINE bool _upb_array_reserve(upb_Array* arr, size_t size,
UPB_INLINE bool _upb_Array_Resize(upb_Array* arr, size_t size,
upb_Arena* arena) {
if (!_upb_array_reserve(arr, size, arena)) return false;
arr->len = size;
arr->size = size;
return true;
}
@ -479,7 +479,7 @@ UPB_INLINE const void* _upb_array_accessor(const void* msg, size_t ofs,
size_t* size) {
const upb_Array* arr = *UPB_PTR_AT(msg, ofs, const upb_Array*);
if (arr) {
if (size) *size = arr->len;
if (size) *size = arr->size;
return _upb_array_constptr(arr);
} else {
if (size) *size = 0;
@ -491,7 +491,7 @@ UPB_INLINE void* _upb_array_mutable_accessor(void* msg, size_t ofs,
size_t* size) {
upb_Array* arr = *UPB_PTR_AT(msg, ofs, upb_Array*);
if (arr) {
if (size) *size = arr->len;
if (size) *size = arr->size;
return _upb_array_ptr(arr);
} else {
if (size) *size = 0;
@ -507,7 +507,7 @@ UPB_INLINE void* _upb_Array_Resize_accessor2(void* msg, size_t ofs, size_t size,
if (!arr || arr->capacity < size) {
return _upb_Array_Resize_fallback(arr_ptr, size, elem_size_lg2, arena);
}
arr->len = size;
arr->size = size;
return _upb_array_ptr(arr);
}
@ -519,12 +519,12 @@ UPB_INLINE bool _upb_Array_Append_accessor2(void* msg, size_t ofs,
size_t elem_size = 1 << elem_size_lg2;
upb_Array* arr = *arr_ptr;
void* ptr;
if (!arr || arr->len == arr->capacity) {
if (!arr || arr->size == arr->capacity) {
return _upb_Array_Append_fallback(arr_ptr, value, elem_size_lg2, arena);
}
ptr = _upb_array_ptr(arr);
memcpy(UPB_PTR_AT(ptr, arr->len * elem_size, char), value, elem_size);
arr->len++;
memcpy(UPB_PTR_AT(ptr, arr->size * elem_size, char), value, elem_size);
arr->size++;
return true;
}

Loading…
Cancel
Save