Skip to content

Commit

Permalink
upb: factor out message internal data pointers
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 594537482
  • Loading branch information
ericsalo authored and copybara-github committed Dec 30, 2023
1 parent 499c748 commit 3f5081d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 56 deletions.
9 changes: 5 additions & 4 deletions upb/message/internal/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,16 @@ UPB_INLINE bool _upb_Message_SetExtensionField(

UPB_INLINE void _upb_Message_ClearExtensionField(
struct upb_Message* msg, const upb_MiniTableExtension* ext_l) {
upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (!in->internal) return;
upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
if (!in) return;
const struct upb_Extension* base =
UPB_PTR_AT(in->internal, in->internal->ext_begin, struct upb_Extension);
UPB_PTR_AT(in, in->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(struct upb_Extension);
in->ext_begin += sizeof(struct upb_Extension);
}
}

Expand Down
23 changes: 11 additions & 12 deletions upb/message/internal/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,28 @@ const struct upb_Extension* _upb_Message_Getext(

const struct upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(
const struct upb_Message* msg, size_t* count) {
const upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (in->internal) {
*count = (in->internal->size - in->internal->ext_begin) /
sizeof(struct upb_Extension);
return UPB_PTR_AT(in->internal, in->internal->ext_begin, void);
const upb_Message_Internal* owner = upb_Message_Getinternal(msg);
const upb_Message_InternalData* in = owner->internal;
if (in) {
*count = (in->size - in->ext_begin) / sizeof(struct upb_Extension);
return UPB_PTR_AT(in, in->ext_begin, void);
} else {
*count = 0;
return NULL;
}
}

struct upb_Extension* _upb_Message_GetOrCreateExtension(
struct upb_Message* msg, const upb_MiniTableExtension* e,
upb_Arena* arena) {
struct upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* a) {
struct upb_Extension* ext =
(struct upb_Extension*)_upb_Message_Getext(msg, e);
if (ext) return ext;
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, sizeof(struct upb_Extension),
arena))
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, sizeof(struct upb_Extension), a))
return NULL;
upb_Message_Internal* in = upb_Message_Getinternal(msg);
in->internal->ext_begin -= sizeof(struct upb_Extension);
ext = UPB_PTR_AT(in->internal, in->internal->ext_begin, void);
upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
in->ext_begin -= sizeof(struct upb_Extension);
ext = UPB_PTR_AT(in, in->ext_begin, void);
memset(ext, 0, sizeof(struct upb_Extension));
ext->ext = e;
return ext;
Expand Down
51 changes: 27 additions & 24 deletions upb/message/internal/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,40 @@ const float kUpb_FltInfinity = INFINITY;
const double kUpb_Infinity = INFINITY;
const double kUpb_NaN = NAN;

static const size_t realloc_overhead = sizeof(upb_Message_InternalData);

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) {
upb_Arena* a) {
const size_t overhead = sizeof(upb_Message_InternalData);

upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
if (!in) {
// No internal data, allocate from scratch.
size_t size = UPB_MAX(128, upb_Log2CeilingSize(need + realloc_overhead));
upb_Message_InternalData* internal = upb_Arena_Malloc(arena, size);
if (!internal) return false;
internal->size = size;
internal->unknown_end = realloc_overhead;
internal->ext_begin = size;
in->internal = internal;
} else if (in->internal->ext_begin - in->internal->unknown_end < need) {
size_t size = UPB_MAX(128, upb_Log2CeilingSize(need + overhead));
in = upb_Arena_Malloc(a, size);
if (!in) return false;

in->size = size;
in->unknown_end = overhead;
in->ext_begin = size;
owner->internal = in;
} else if (in->ext_begin - in->unknown_end < need) {
// Internal data is too small, reallocate.
size_t new_size = upb_Log2CeilingSize(in->internal->size + need);
size_t ext_bytes = in->internal->size - in->internal->ext_begin;
size_t new_size = upb_Log2CeilingSize(in->size + need);
size_t ext_bytes = in->size - in->ext_begin;
size_t new_ext_begin = new_size - ext_bytes;
upb_Message_InternalData* internal =
upb_Arena_Realloc(arena, in->internal, in->internal->size, new_size);
if (!internal) return false;
in = upb_Arena_Realloc(a, in, in->size, new_size);
if (!in) return false;

if (ext_bytes) {
// Need to move extension data to the end.
char* ptr = (char*)internal;
memmove(ptr + new_ext_begin, ptr + internal->ext_begin, ext_bytes);
char* ptr = (char*)in;
memmove(ptr + new_ext_begin, ptr + in->ext_begin, ext_bytes);
}
internal->ext_begin = new_ext_begin;
internal->size = new_size;
in->internal = internal;
in->ext_begin = new_ext_begin;
in->size = new_size;
owner->internal = in;
}
UPB_ASSERT(in->internal->ext_begin - in->internal->unknown_end >= need);

UPB_ASSERT(in->ext_begin - in->unknown_end >= need);
return true;
}
37 changes: 21 additions & 16 deletions upb/message/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,45 @@

static const size_t message_overhead = sizeof(upb_Message_InternalData);

upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* arena) {
return _upb_Message_New(m, arena);
upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* a) {
return _upb_Message_New(m, 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* in = upb_Message_Getinternal(msg);
memcpy(UPB_PTR_AT(in->internal, in->internal->unknown_end, char), data, len);
in->internal->unknown_end += len;
upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->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* in = upb_Message_Getinternal(msg);
if (in->internal) {
in->internal->unknown_end = message_overhead;
upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
if (in) {
in->unknown_end = message_overhead;
}
}

const char* upb_Message_GetUnknown(const upb_Message* msg, size_t* len) {
const upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (in->internal) {
*len = in->internal->unknown_end - message_overhead;
return (char*)(in->internal + 1);
upb_Message_Internal* owner = upb_Message_Getinternal(msg);
upb_Message_InternalData* in = owner->internal;
if (in) {
*len = in->unknown_end - message_overhead;
return (char*)(in + 1);
} else {
*len = 0;
return NULL;
}
}

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

#ifndef NDEBUG
size_t full_unknown_size;
const char* full_unknown = upb_Message_GetUnknown(msg, &full_unknown_size);
Expand All @@ -63,10 +67,11 @@ void upb_Message_DeleteUnknown(upb_Message* msg, const char* data, size_t len) {
UPB_ASSERT((uintptr_t)(data + len) > (uintptr_t)data);
UPB_ASSERT((uintptr_t)(data + len) <= (uintptr_t)internal_unknown_end);
#endif

if ((data + len) != internal_unknown_end) {
memmove((char*)data, data + len, internal_unknown_end - data - len);
}
in->internal->unknown_end -= len;
in->unknown_end -= len;
}

size_t upb_Message_ExtensionCount(const upb_Message* msg) {
Expand Down

0 comments on commit 3f5081d

Please sign in to comment.