rename the upb_Array 'size' field as 'capacity'

The current field/function names for upb_Array are quite confusing.
We will fix them in two steps, this being the first step.

PiperOrigin-RevId: 456687224
pull/13171/head
Protobuf Team Bot 3 years ago committed by Copybara-Service
parent a6cbda7b7b
commit 83f4988561
  1. 2
      upb/array.h
  2. 2
      upb/decode.c
  3. 6
      upb/decode_fast.c
  4. 12
      upb/msg.c
  5. 18
      upb/msg_internal.h

@ -41,7 +41,7 @@ extern "C" {
/* Creates a new array on the given arena that holds elements of this type. */
upb_Array* upb_Array_New(upb_Arena* a, upb_CType type);
/* Returns the size of the array. */
/* Returns the number of elements in the array. */
size_t upb_Array_Size(const upb_Array* arr);
/* Returns the given element, which must be within the array's current size. */

@ -201,7 +201,7 @@ 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->size - arr->len < elem;
bool need_realloc = arr->capacity - arr->len < elem;
if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, &d->arena)) {
decode_err(d, kUpb_DecodeStatus_OutOfMemory);
}

@ -217,14 +217,14 @@ UPB_FORCEINLINE
static void* fastdecode_resizearr(upb_Decoder* d, void* dst,
fastdecode_arr* farr, int valbytes) {
if (UPB_UNLIKELY(dst == farr->end)) {
size_t old_size = farr->arr->size;
size_t old_size = farr->arr->capacity;
size_t old_bytes = old_size * valbytes;
size_t new_size = old_size * 2;
size_t new_bytes = new_size * valbytes;
char* old_ptr = _upb_array_ptr(farr->arr);
char* new_ptr = upb_Arena_Realloc(&d->arena, old_ptr, old_bytes, new_bytes);
uint8_t elem_size_lg2 = __builtin_ctz(valbytes);
farr->arr->size = new_size;
farr->arr->capacity = new_size;
farr->arr->data = _upb_array_tagptr(new_ptr, elem_size_lg2);
dst = (void*)(new_ptr + (old_size * valbytes));
farr->end = (void*)(new_ptr + (new_size * valbytes));
@ -313,7 +313,7 @@ static void* fastdecode_getfield(upb_Decoder* d, const char* ptr,
farr->arr = *arr_p;
}
begin = _upb_array_ptr(farr->arr);
farr->end = begin + (farr->arr->size * valbytes);
farr->end = begin + (farr->arr->capacity * valbytes);
*data = fastdecode_loadtag(ptr);
return begin + (farr->arr->len * valbytes);
}

@ -175,17 +175,17 @@ size_t upb_Message_ExtensionCount(const upb_Message* msg) {
/** upb_Array *****************************************************************/
bool _upb_array_realloc(upb_Array* arr, size_t min_size, upb_Arena* arena) {
size_t new_size = UPB_MAX(arr->size, 4);
bool _upb_array_realloc(upb_Array* arr, size_t min_capacity, upb_Arena* arena) {
size_t new_capacity = UPB_MAX(arr->capacity, 4);
int elem_size_lg2 = arr->data & 7;
size_t old_bytes = arr->size << elem_size_lg2;
size_t old_bytes = arr->capacity << elem_size_lg2;
size_t new_bytes;
void* ptr = _upb_array_ptr(arr);
/* Log2 ceiling of size. */
while (new_size < min_size) new_size *= 2;
while (new_capacity < min_capacity) new_capacity *= 2;
new_bytes = new_size << elem_size_lg2;
new_bytes = new_capacity << elem_size_lg2;
ptr = upb_Arena_Realloc(arena, ptr, old_bytes, new_bytes);
if (!ptr) {
@ -193,7 +193,7 @@ bool _upb_array_realloc(upb_Array* arr, size_t min_size, upb_Arena* arena) {
}
arr->data = _upb_tag_arrptr(ptr, elem_size_lg2);
arr->size = new_size;
arr->capacity = new_capacity;
return true;
}

@ -411,9 +411,9 @@ 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; /* Measured in elements. */
uintptr_t data; /* Tagged ptr: low 3 bits of ptr are lg2(elem size). */
size_t len; /* Measured in elements. */
size_t capacity; /* Allocated storage. Measured in elements. */
uint64_t junk;
} upb_Array;
@ -437,15 +437,15 @@ UPB_INLINE uintptr_t _upb_tag_arrptr(void* ptr, int elem_size_lg2) {
return (uintptr_t)ptr | (unsigned)elem_size_lg2;
}
UPB_INLINE upb_Array* _upb_Array_New(upb_Arena* a, size_t init_size,
UPB_INLINE upb_Array* _upb_Array_New(upb_Arena* a, size_t init_capacity,
int elem_size_lg2) {
const size_t arr_size = UPB_ALIGN_UP(sizeof(upb_Array), 8);
const size_t bytes = sizeof(upb_Array) + (init_size << elem_size_lg2);
const size_t bytes = sizeof(upb_Array) + (init_capacity << elem_size_lg2);
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 = init_size;
arr->capacity = init_capacity;
return arr;
}
@ -460,7 +460,7 @@ bool _upb_Array_Append_fallback(upb_Array** arr_ptr, const void* value,
UPB_INLINE bool _upb_array_reserve(upb_Array* arr, size_t size,
upb_Arena* arena) {
if (arr->size < size) return _upb_array_realloc(arr, size, arena);
if (arr->capacity < size) return _upb_array_realloc(arr, size, arena);
return true;
}
@ -504,7 +504,7 @@ UPB_INLINE void* _upb_Array_Resize_accessor2(void* msg, size_t ofs, size_t size,
upb_Arena* arena) {
upb_Array** arr_ptr = UPB_PTR_AT(msg, ofs, upb_Array*);
upb_Array* arr = *arr_ptr;
if (!arr || arr->size < size) {
if (!arr || arr->capacity < size) {
return _upb_Array_Resize_fallback(arr_ptr, size, elem_size_lg2, arena);
}
arr->len = size;
@ -519,7 +519,7 @@ 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->size) {
if (!arr || arr->len == arr->capacity) {
return _upb_Array_Append_fallback(arr_ptr, value, elem_size_lg2, arena);
}
ptr = _upb_array_ptr(arr);

Loading…
Cancel
Save