Skip to content

Commit

Permalink
upb: clean up some of the includes in upb/message/
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 594103742
  • Loading branch information
ericsalo authored and copybara-github committed Dec 27, 2023
1 parent 36bb6fb commit c122e18
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 64 deletions.
22 changes: 10 additions & 12 deletions upb/message/internal/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@

#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/types.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
Expand Down Expand Up @@ -232,7 +229,7 @@ static UPB_FORCEINLINE void _upb_Message_GetNonExtensionField(
UPB_INLINE void _upb_Message_GetExtensionField(
const upb_Message* msg, const upb_MiniTableExtension* mt_ext,
const void* default_val, void* val) {
const upb_Extension* ext = _upb_Message_Getext(msg, mt_ext);
const struct upb_Extension* ext = _upb_Message_Getext(msg, mt_ext);
const upb_MiniTableField* f = &mt_ext->UPB_PRIVATE(field);
UPB_ASSUME(upb_MiniTableField_IsExtension(f));

Expand Down Expand Up @@ -282,7 +279,7 @@ UPB_INLINE bool _upb_Message_SetExtensionField(
upb_Message* msg, const upb_MiniTableExtension* mt_ext, const void* val,
upb_Arena* a) {
UPB_ASSERT(a);
upb_Extension* ext = _upb_Message_GetOrCreateExtension(msg, mt_ext, a);
struct upb_Extension* ext = _upb_Message_GetOrCreateExtension(msg, mt_ext, a);
if (!ext) return false;
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(&mt_ext->UPB_PRIVATE(field), &ext->data, val);
Expand All @@ -293,12 +290,13 @@ UPB_INLINE void _upb_Message_ClearExtensionField(
upb_Message* msg, const upb_MiniTableExtension* ext_l) {
upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (!in->internal) return;
const upb_Extension* base =
UPB_PTR_AT(in->internal, in->internal->ext_begin, upb_Extension);
upb_Extension* ext = (upb_Extension*)_upb_Message_Getext(msg, ext_l);
const struct upb_Extension* base =
UPB_PTR_AT(in->internal, in->internal->ext_begin, struct upb_Extension);
struct upb_Extension* ext =
(struct upb_Extension*)_upb_Message_Getext(msg, ext_l);
if (ext) {
*ext = *base;
in->internal->ext_begin += sizeof(upb_Extension);
in->internal->ext_begin += sizeof(struct upb_Extension);
}
}

Expand Down Expand Up @@ -328,13 +326,13 @@ UPB_INLINE void _upb_Message_AssertMapIsUntagged(
#endif
}

UPB_INLINE upb_Map* _upb_Message_GetOrCreateMutableMap(
UPB_INLINE struct upb_Map* _upb_Message_GetOrCreateMutableMap(
upb_Message* msg, const upb_MiniTableField* field, size_t key_size,
size_t val_size, upb_Arena* arena) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
_upb_Message_AssertMapIsUntagged(msg, field);
upb_Map* map = NULL;
upb_Map* default_map_value = NULL;
struct upb_Map* map = NULL;
struct upb_Map* default_map_value = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_map_value, &map);
if (!map) {
map = _upb_Map_New(arena, key_size, val_size);
Expand Down
34 changes: 17 additions & 17 deletions upb/message/internal/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include <string.h>

#include "upb/message/array.h"

// Must be last.
#include "upb/port/def.inc"

Expand Down Expand Up @@ -39,37 +37,39 @@ struct upb_Array {
size_t UPB_PRIVATE(capacity); // Allocated storage. Measured in elements.
};

UPB_INLINE void UPB_PRIVATE(_upb_Array_SetTaggedPtr)(upb_Array* array,
UPB_INLINE void UPB_PRIVATE(_upb_Array_SetTaggedPtr)(struct upb_Array* array,
void* data, size_t lg2) {
UPB_ASSERT(lg2 != 1);
UPB_ASSERT(lg2 <= 4);
const size_t bits = lg2 - (lg2 != 0);
array->data = (uintptr_t)data | bits;
}

UPB_INLINE size_t UPB_PRIVATE(_upb_Array_ElemSizeLg2)(const upb_Array* array) {
UPB_INLINE size_t
UPB_PRIVATE(_upb_Array_ElemSizeLg2)(const struct upb_Array* array) {
const size_t bits = array->data & _UPB_ARRAY_MASK_LG2;
const size_t lg2 = bits + (bits != 0);
return lg2;
}

UPB_INLINE const void* _upb_array_constptr(const upb_Array* array) {
UPB_INLINE const void* _upb_array_constptr(const struct upb_Array* array) {
UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array); // Check assertions.
return (void*)(array->data & ~(uintptr_t)_UPB_ARRAY_MASK_ALL);
}

UPB_INLINE void* _upb_array_ptr(upb_Array* array) {
UPB_INLINE void* _upb_array_ptr(struct upb_Array* array) {
return (void*)_upb_array_constptr(array);
}

UPB_INLINE upb_Array* UPB_PRIVATE(_upb_Array_New)(upb_Arena* arena,
size_t init_capacity,
int elem_size_lg2) {
UPB_INLINE struct upb_Array* UPB_PRIVATE(_upb_Array_New)(upb_Arena* arena,
size_t init_capacity,
int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 != 1);
UPB_ASSERT(elem_size_lg2 <= 4);
const size_t array_size = UPB_ALIGN_UP(sizeof(upb_Array), UPB_MALLOC_ALIGN);
const size_t array_size =
UPB_ALIGN_UP(sizeof(struct upb_Array), UPB_MALLOC_ALIGN);
const size_t bytes = array_size + (init_capacity << elem_size_lg2);
upb_Array* array = (upb_Array*)upb_Arena_Malloc(arena, bytes);
struct upb_Array* array = (struct upb_Array*)upb_Arena_Malloc(arena, bytes);
if (!array) return NULL;
UPB_PRIVATE(_upb_Array_SetTaggedPtr)
(array, UPB_PTR_AT(array, array_size, void), elem_size_lg2);
Expand All @@ -79,19 +79,19 @@ UPB_INLINE upb_Array* UPB_PRIVATE(_upb_Array_New)(upb_Arena* arena,
}

// Resizes the capacity of the array to be at least min_size.
bool UPB_PRIVATE(_upb_Array_Realloc)(upb_Array* array, size_t min_size,
bool UPB_PRIVATE(_upb_Array_Realloc)(struct upb_Array* array, size_t min_size,
upb_Arena* arena);

UPB_INLINE bool UPB_PRIVATE(_upb_Array_Reserve)(upb_Array* array, size_t size,
upb_Arena* arena) {
UPB_INLINE bool UPB_PRIVATE(_upb_Array_Reserve)(struct upb_Array* array,
size_t size, upb_Arena* arena) {
if (array->UPB_PRIVATE(capacity) < size)
return UPB_PRIVATE(_upb_Array_Realloc)(array, size, arena);
return true;
}

// Resize without initializing new elements.
UPB_INLINE bool _upb_Array_ResizeUninitialized(upb_Array* array, size_t size,
upb_Arena* arena) {
UPB_INLINE bool _upb_Array_ResizeUninitialized(struct upb_Array* array,
size_t size, upb_Arena* arena) {
UPB_ASSERT(size <= array->UPB_ONLYBITS(size) ||
arena); // Allow NULL arena when shrinking.
if (!UPB_PRIVATE(_upb_Array_Reserve)(array, size, arena)) return false;
Expand All @@ -102,7 +102,7 @@ UPB_INLINE bool _upb_Array_ResizeUninitialized(upb_Array* array, size_t size,
// This function is intended for situations where elem_size is compile-time
// constant or a known expression of the form (1 << lg2), so that the expression
// i*elem_size does not result in an actual multiplication.
UPB_INLINE void UPB_PRIVATE(_upb_Array_Set)(upb_Array* array, size_t i,
UPB_INLINE void UPB_PRIVATE(_upb_Array_Set)(struct upb_Array* array, size_t i,
const void* data,
size_t elem_size) {
UPB_ASSERT(i < array->UPB_ONLYBITS(size));
Expand Down
15 changes: 8 additions & 7 deletions upb/message/internal/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/message/internal/message.h"
#include "upb/mini_table/extension.h"

// Must be last.
Expand Down Expand Up @@ -40,18 +40,19 @@ extern "C" {
// Adds the given extension data to the given message.
// |ext| is copied into the message instance.
// This logically replaces any previously-added extension with this number.
upb_Extension* _upb_Message_GetOrCreateExtension(
upb_Message* msg, const upb_MiniTableExtension* ext, upb_Arena* arena);
struct upb_Extension* _upb_Message_GetOrCreateExtension(
struct upb_Message* msg, const upb_MiniTableExtension* ext,
upb_Arena* arena);

// Returns an array of extensions for this message.
// Note: the array is ordered in reverse relative to the order of creation.
const upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(const upb_Message* msg,
size_t* count);
const struct upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(
const struct upb_Message* msg, size_t* count);

// Returns an extension for a message with a given mini table,
// or NULL if no extension exists with this mini table.
const upb_Extension* _upb_Message_Getext(const upb_Message* msg,
const upb_MiniTableExtension* ext);
const struct upb_Extension* _upb_Message_Getext(
const struct upb_Message* msg, const upb_MiniTableExtension* ext);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
21 changes: 11 additions & 10 deletions upb/message/internal/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) {
}
}

UPB_INLINE void* _upb_map_next(const upb_Map* map, size_t* iter) {
UPB_INLINE void* _upb_map_next(const struct upb_Map* map, size_t* iter) {
upb_strtable_iter it;
it.t = &map->table;
it.index = *iter;
Expand All @@ -91,17 +91,17 @@ UPB_INLINE void* _upb_map_next(const upb_Map* map, size_t* iter) {
return (void*)str_tabent(&it);
}

UPB_INLINE void _upb_Map_Clear(upb_Map* map) {
UPB_INLINE void _upb_Map_Clear(struct upb_Map* map) {
upb_strtable_clear(&map->table);
}

UPB_INLINE bool _upb_Map_Delete(upb_Map* map, const void* key, size_t key_size,
upb_value* val) {
UPB_INLINE bool _upb_Map_Delete(struct upb_Map* map, const void* key,
size_t key_size, upb_value* val) {
upb_StringView k = _upb_map_tokey(key, key_size);
return upb_strtable_remove2(&map->table, k.data, k.size, val);
}

UPB_INLINE bool _upb_Map_Get(const upb_Map* map, const void* key,
UPB_INLINE bool _upb_Map_Get(const struct upb_Map* map, const void* key,
size_t key_size, void* val, size_t val_size) {
upb_value tabval;
upb_StringView k = _upb_map_tokey(key, key_size);
Expand All @@ -112,9 +112,10 @@ UPB_INLINE bool _upb_Map_Get(const upb_Map* map, const void* key,
return ret;
}

UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(upb_Map* map, const void* key,
size_t key_size, void* val,
size_t val_size, upb_Arena* a) {
UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(struct upb_Map* map,
const void* key, size_t key_size,
void* val, size_t val_size,
upb_Arena* a) {
upb_StringView strkey = _upb_map_tokey(key, key_size);
upb_value tabval = {0};
if (!_upb_map_tovalue(val, val_size, &tabval, a)) {
Expand All @@ -131,7 +132,7 @@ UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(upb_Map* map, const void* key,
: kUpb_MapInsertStatus_Inserted;
}

UPB_INLINE size_t _upb_Map_Size(const upb_Map* map) {
UPB_INLINE size_t _upb_Map_Size(const struct upb_Map* map) {
return map->table.t.count;
}

Expand All @@ -143,7 +144,7 @@ UPB_INLINE size_t _upb_Map_CTypeSize(upb_CType ctype) {
}

// Creates a new map on the given arena with this key/value type.
upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);
struct upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
9 changes: 5 additions & 4 deletions upb/message/internal/map_sorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s, const upb_Map* map,

UPB_INLINE bool _upb_sortedmap_nextext(_upb_mapsorter* s,
_upb_sortedmap* sorted,
const upb_Extension** ext) {
const struct upb_Extension** ext) {
if (sorted->pos == sorted->end) return false;
*ext = (const upb_Extension*)s->entries[sorted->pos++];
*ext = (const struct upb_Extension*)s->entries[sorted->pos++];
return true;
}

Expand All @@ -77,8 +77,9 @@ UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter* s,
bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
const upb_Map* map, _upb_sortedmap* sorted);

bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Extension* exts,
size_t count, _upb_sortedmap* sorted);
bool _upb_mapsorter_pushexts(_upb_mapsorter* s,
const struct upb_Extension* exts, size_t count,
_upb_sortedmap* sorted);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
5 changes: 2 additions & 3 deletions upb/message/internal/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

#include "upb/message/message.h"
#include "upb/message/internal/message.h"

#include <math.h>
#include <string.h>

#include "upb/base/internal/log2.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/types.h"

// Must be last.
Expand All @@ -24,7 +23,7 @@ const double kUpb_NaN = NAN;

static const size_t realloc_overhead = sizeof(upb_Message_InternalData);

bool UPB_PRIVATE(_upb_Message_Realloc)(upb_Message* msg, size_t need,
bool UPB_PRIVATE(_upb_Message_Realloc)(struct upb_Message* msg, size_t need,
upb_Arena* arena) {
upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (!in->internal) {
Expand Down
18 changes: 10 additions & 8 deletions upb/message/internal/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,33 @@ UPB_INLINE size_t upb_msg_sizeof(const upb_MiniTable* m) {
}

// Inline version upb_Message_New(), for internal use.
UPB_INLINE upb_Message* _upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena) {
UPB_INLINE struct upb_Message* _upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena) {
size_t size = upb_msg_sizeof(mini_table);
void* mem = upb_Arena_Malloc(arena, size + sizeof(upb_Message_Internal));
if (UPB_UNLIKELY(!mem)) return NULL;
upb_Message* msg = UPB_PTR_AT(mem, sizeof(upb_Message_Internal), upb_Message);
struct upb_Message* msg =
UPB_PTR_AT(mem, sizeof(upb_Message_Internal), struct upb_Message);
memset(mem, 0, size);
return msg;
}

UPB_INLINE upb_Message_Internal* upb_Message_Getinternal(
const upb_Message* msg) {
const struct upb_Message* msg) {
ptrdiff_t size = sizeof(upb_Message_Internal);
return (upb_Message_Internal*)((char*)msg - size);
}

// Discards the unknown fields for this message only.
void _upb_Message_DiscardUnknown_shallow(upb_Message* msg);
void _upb_Message_DiscardUnknown_shallow(struct upb_Message* msg);

// Adds unknown data (serialized protobuf data) to the given message.
// The data is copied into the message instance.
bool UPB_PRIVATE(_upb_Message_AddUnknown)(upb_Message* msg, const char* data,
size_t len, upb_Arena* arena);
bool UPB_PRIVATE(_upb_Message_AddUnknown)(struct upb_Message* msg,
const char* data, size_t len,
upb_Arena* arena);

bool UPB_PRIVATE(_upb_Message_Realloc)(upb_Message* msg, size_t need,
bool UPB_PRIVATE(_upb_Message_Realloc)(struct upb_Message* msg, size_t need,
upb_Arena* arena);

#ifdef __cplusplus
Expand Down
5 changes: 2 additions & 3 deletions upb/message/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include "upb/base/string_view.h"
#include "upb/message/tagged_ptr.h"
#include "upb/message/types.h"

typedef union {
bool bool_val;
Expand All @@ -27,7 +26,7 @@ typedef union {
uint64_t uint64_val;
const struct upb_Array* array_val;
const struct upb_Map* map_val;
const upb_Message* msg_val;
const struct upb_Message* msg_val;
upb_StringView str_val;

// EXPERIMENTAL: A tagged upb_Message*. Users must use this instead of
Expand All @@ -40,7 +39,7 @@ typedef union {
typedef union {
struct upb_Array* array;
struct upb_Map* map;
upb_Message* msg;
struct upb_Message* msg;
} upb_MutableMessageValue;

#endif /* UPB_MESSAGE_VALUE_H_ */

0 comments on commit c122e18

Please sign in to comment.