Skip to content

Commit

Permalink
upb: implement upb_Message
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 597662892
  • Loading branch information
ericsalo authored and copybara-github committed Jan 11, 2024
1 parent 81ce699 commit 5c5f092
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 161 deletions.
1 change: 0 additions & 1 deletion upb/message/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ cc_library(
"internal/array.h",
"internal/extension.h",
"internal/map.h",
"internal/map_entry.h",
"internal/map_sorter.h",
"internal/message.h",
"internal/tagged_ptr.h",
Expand Down
4 changes: 1 addition & 3 deletions upb/message/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ extern "C" {

UPB_API_INLINE void upb_Message_Clear(upb_Message* msg,
const upb_MiniTable* m) {
// Note: Can't use UPB_PTR_AT() here because we are doing pointer subtraction.
char* mem = (char*)msg - sizeof(upb_Message_Internal);
memset(mem, 0, upb_msg_sizeof(m));
UPB_PRIVATE(_upb_Message_Clear)(msg, m);
}

UPB_API_INLINE void upb_Message_ClearBaseField(upb_Message* msg,
Expand Down
2 changes: 1 addition & 1 deletion upb/message/copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src,
upb_Arena* arena) {
upb_StringView empty_string = upb_StringView_FromDataAndSize(NULL, 0);
// Only copy message area skipping upb_Message_Internal.
memcpy(dst, src, mini_table->UPB_PRIVATE(size));
memcpy(dst + 1, src + 1, mini_table->UPB_PRIVATE(size) - sizeof(upb_Message));
for (size_t i = 0; i < mini_table->UPB_PRIVATE(field_count); ++i) {
const upb_MiniTableField* field = &mini_table->UPB_PRIVATE(fields)[i];
if (upb_MiniTableField_IsScalar(field)) {
Expand Down
18 changes: 17 additions & 1 deletion upb/message/internal/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdint.h>
#include <string.h>

#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/extension.h"
Expand All @@ -21,6 +22,7 @@
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/message.h"

// Must be last.
#include "upb/port/def.inc"
Expand Down Expand Up @@ -111,6 +113,15 @@ UPB_INLINE bool UPB_PRIVATE(_upb_Message_ClearOneofCase)(

// LINT.ThenChange(GoogleInternalName2)

// Returns false if the message is missing any of its required fields.
UPB_INLINE bool UPB_PRIVATE(_upb_Message_IsInitializedShallow)(
const struct upb_Message* msg, const upb_MiniTable* m) {
uint64_t bits;
memcpy(&bits, msg + 1, sizeof(bits));
bits = upb_BigEndian64(bits);
return (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~bits) == 0;
}

UPB_INLINE void* UPB_PRIVATE(_upb_Message_MutableDataPtr)(
struct upb_Message* msg, const upb_MiniTableField* f) {
return (char*)msg + f->UPB_ONLYBITS(offset);
Expand Down Expand Up @@ -278,6 +289,11 @@ UPB_INLINE bool _upb_Message_SetExtensionField(
return true;
}

UPB_INLINE void UPB_PRIVATE(_upb_Message_Clear)(struct upb_Message* msg,
const upb_MiniTable* m) {
memset(msg, 0, m->UPB_PRIVATE(size));
}

UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearBaseField)(
struct upb_Message* msg, const upb_MiniTableField* f) {
if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
Expand All @@ -294,7 +310,7 @@ UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearBaseField)(

UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearExtension)(
struct upb_Message* msg, const upb_MiniTableExtension* e) {
upb_Message_InternalData* in = upb_Message_GetInternalData(msg);
upb_Message_Internal* in = msg->internal;
if (!in) return;
const struct upb_Extension* base =
UPB_PTR_AT(in, in->ext_begin, struct upb_Extension);
Expand Down
4 changes: 2 additions & 2 deletions upb/message/internal/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const struct upb_Extension* _upb_Message_Getext(

const struct upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(
const struct upb_Message* msg, size_t* count) {
upb_Message_InternalData* in = upb_Message_GetInternalData(msg);
upb_Message_Internal* in = msg->internal;
if (in) {
*count = (in->size - in->ext_begin) / sizeof(struct upb_Extension);
return UPB_PTR_AT(in, in->ext_begin, void);
Expand All @@ -53,7 +53,7 @@ struct upb_Extension* _upb_Message_GetOrCreateExtension(
if (ext) return ext;
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, sizeof(struct upb_Extension), a))
return NULL;
upb_Message_InternalData* in = upb_Message_GetInternalData(msg);
upb_Message_Internal* in = msg->internal;
in->ext_begin -= sizeof(struct upb_Extension);
ext = UPB_PTR_AT(in, in->ext_begin, void);
memset(ext, 0, sizeof(struct upb_Extension));
Expand Down
6 changes: 3 additions & 3 deletions upb/message/internal/map_sorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "upb/mem/alloc.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/map_entry.h"
#include "upb/mini_table/internal/map_entry.h"

// Must be last.
#include "upb/port/def.inc"
Expand Down Expand Up @@ -58,9 +58,9 @@ UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s,
if (sorted->pos == sorted->end) return false;
const upb_tabent* tabent = (const upb_tabent*)s->entries[sorted->pos++];
upb_StringView key = upb_tabstrview(tabent->key);
_upb_map_fromkey(key, &ent->data.k, map->key_size);
_upb_map_fromkey(key, &ent->k, map->key_size);
upb_value val = {tabent->val.val};
_upb_map_fromvalue(val, &ent->data.v, map->val_size);
_upb_map_fromvalue(val, &ent->v, map->val_size);
return true;
}

Expand Down
9 changes: 4 additions & 5 deletions upb/message/internal/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ const double kUpb_NaN = NAN;

bool UPB_PRIVATE(_upb_Message_Realloc)(struct upb_Message* msg, size_t need,
upb_Arena* a) {
const size_t overhead = sizeof(upb_Message_InternalData);
const size_t overhead = sizeof(upb_Message_Internal);

upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
upb_Message_Internal* in = msg->internal;
if (!in) {
// No internal data, allocate from scratch.
size_t size = UPB_MAX(128, upb_Log2CeilingSize(need + overhead));
Expand All @@ -35,7 +34,7 @@ bool UPB_PRIVATE(_upb_Message_Realloc)(struct upb_Message* msg, size_t need,
in->size = size;
in->unknown_end = overhead;
in->ext_begin = size;
owner->internal = in;
msg->internal = in;
} else if (in->ext_begin - in->unknown_end < need) {
// Internal data is too small, reallocate.
size_t new_size = upb_Log2CeilingSize(in->size + need);
Expand All @@ -51,7 +50,7 @@ bool UPB_PRIVATE(_upb_Message_Realloc)(struct upb_Message* msg, size_t need,
}
in->ext_begin = new_ext_begin;
in->size = new_size;
owner->internal = in;
msg->internal = in;
}

UPB_ASSERT(in->ext_begin - in->unknown_end >= need);
Expand Down
61 changes: 14 additions & 47 deletions upb/message/internal/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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

// Must be last.
Expand All @@ -33,14 +34,12 @@ extern const float kUpb_FltInfinity;
extern const double kUpb_Infinity;
extern const double kUpb_NaN;

/* Internal members of a upb_Message that track unknown fields and/or
* extensions. We can change this without breaking binary compatibility. We put
* these before the user's data. The user's upb_Message* points after the
* upb_Message_Internal. */
// Internal members of a upb_Message that track unknown fields and/or
// extensions. We can change this without breaking binary compatibility.

typedef struct {
/* Total size of this structure, including the data that follows.
* Must be aligned to 8, which is alignof(upb_Extension) */
typedef struct upb_Message_Internal {
// Total size of this structure, including the data that follows.
// Must be aligned to 8, which is alignof(upb_Extension)
uint32_t size;

/* Offsets relative to the beginning of this structure.
Expand All @@ -50,59 +49,27 @@ typedef struct {
* When the two meet, we're out of data and have to realloc.
*
* If we imagine that the final member of this struct is:
* char data[size - overhead]; // overhead =
* sizeof(upb_Message_InternalData)
* char data[size - overhead]; // overhead = sizeof(upb_Message_Internal)
*
* Then we have:
* unknown data: data[0 .. (unknown_end - overhead)]
* extensions data: data[(ext_begin - overhead) .. (size - overhead)] */
uint32_t unknown_end;
uint32_t ext_begin;
/* Data follows, as if there were an array:
* char data[size - sizeof(upb_Message_InternalData)]; */
} upb_Message_InternalData;

typedef struct {
union {
upb_Message_InternalData* internal;

// Force 8-byte alignment, since the data members may contain members that
// require 8-byte alignment.
double d;
};
* char data[size - sizeof(upb_Message_Internal)]; */
} upb_Message_Internal;

struct upb_Message {
int unused; // Placeholder cuz Windows won't compile an empty struct.
};

UPB_INLINE size_t upb_msg_sizeof(const upb_MiniTable* m) {
return m->UPB_PRIVATE(size) + sizeof(upb_Message_Internal);
}

// Inline version upb_Message_New(), for internal use.
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;
struct upb_Message* msg =
UPB_PTR_AT(mem, sizeof(upb_Message_Internal), struct upb_Message);
memset(mem, 0, size);
UPB_INLINE struct upb_Message* _upb_Message_New(const upb_MiniTable* m,
upb_Arena* a) {
const int size = m->UPB_PRIVATE(size);
struct upb_Message* msg = (struct upb_Message*)upb_Arena_Malloc(a, size);
if (UPB_UNLIKELY(!msg)) return NULL;
memset(msg, 0, size);
return msg;
}

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

UPB_INLINE upb_Message_InternalData* upb_Message_GetInternalData(
const struct upb_Message* msg) {
return upb_Message_Getinternal(msg)->internal;
}

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

Expand Down
14 changes: 5 additions & 9 deletions upb/message/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// Must be last.
#include "upb/port/def.inc"

static const size_t message_overhead = sizeof(upb_Message_InternalData);
static const size_t message_overhead = sizeof(upb_Message_Internal);

upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* a) {
return _upb_Message_New(m, a);
Expand All @@ -27,24 +27,21 @@ upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* a) {
bool UPB_PRIVATE(_upb_Message_AddUnknown)(upb_Message* msg, const char* data,
size_t len, upb_Arena* arena) {
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, len, arena)) return false;
upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
upb_Message_Internal* in = msg->internal;
memcpy(UPB_PTR_AT(in, in->unknown_end, char), data, len);
in->unknown_end += len;
return true;
}

void _upb_Message_DiscardUnknown_shallow(upb_Message* msg) {
upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
upb_Message_Internal* in = msg->internal;
if (in) {
in->unknown_end = message_overhead;
}
}

const char* upb_Message_GetUnknown(const upb_Message* msg, size_t* len) {
upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
upb_Message_Internal* in = msg->internal;
if (in) {
*len = in->unknown_end - message_overhead;
return (char*)(in + 1);
Expand All @@ -55,8 +52,7 @@ const char* upb_Message_GetUnknown(const upb_Message* msg, size_t* len) {
}

void upb_Message_DeleteUnknown(upb_Message* msg, const char* data, size_t len) {
upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
upb_Message_Internal* in = msg->internal;
const char* internal_unknown_end = UPB_PTR_AT(in, in->unknown_end, char);

#ifndef NDEBUG
Expand Down
2 changes: 1 addition & 1 deletion upb/message/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ TEST(MessageTest, MapField) {
// TEST(FuzzTest, TooManyRequiredFields) {
// DecodeEncodeArbitrarySchemaAndPayload(
// {{"$ N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N "
// "N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N"},
// "N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N"},
// {},
// "",
// {}},
Expand Down
Loading

0 comments on commit 5c5f092

Please sign in to comment.