make upb_ExtensionRegistry_AddArray() and upb_ExtensionRegistry_Lookup() public

Previously these were internal _upb functions but it turns out that they are also useful outside of upb.

PiperOrigin-RevId: 480997511
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent fc73215f86
commit df34b04658
  1. 2
      python/extension_dict.c
  2. 28
      upb/decode.c
  3. 12
      upb/extension_registry.c
  4. 26
      upb/extension_registry.h
  5. 7
      upb/fuzz_test_util.cc
  6. 5
      upb/reflection/def_pool.c
  7. 4
      upb/reflection/file_def.c

@ -74,7 +74,7 @@ static PyObject* PyUpb_ExtensionDict_FindExtensionByNumber(PyObject* _self,
const upb_ExtensionRegistry* reg = upb_DefPool_ExtensionRegistry(symtab);
int64_t number = PyLong_AsLong(arg);
const upb_MiniTable_Extension* ext =
(upb_MiniTable_Extension*)_upb_extreg_get(reg, l, number);
(upb_MiniTable_Extension*)upb_ExtensionRegistry_Lookup(reg, l, number);
if (ext) {
const upb_FieldDef* f = upb_DefPool_FindExtensionByMiniTable(symtab, ext);
return PyUpb_FieldDescriptor_Get(f);

@ -795,11 +795,11 @@ static void upb_Decoder_AddUnknownMessageSetItem(upb_Decoder* d,
}
static void upb_Decoder_AddMessageSetItem(upb_Decoder* d, upb_Message* msg,
const upb_MiniTable* layout,
const upb_MiniTable* t,
uint32_t type_id, const char* data,
uint32_t size) {
const upb_MiniTable_Extension* item_mt =
_upb_extreg_get(d->extreg, layout, type_id);
upb_ExtensionRegistry_Lookup(d->extreg, t, type_id);
if (item_mt) {
upb_Decoder_AddKnownMessageSetItem(d, msg, item_mt, data, size);
} else {
@ -861,40 +861,40 @@ static const char* upb_Decoder_DecodeMessageSetItem(
}
static const upb_MiniTable_Field* _upb_Decoder_FindField(
upb_Decoder* d, const upb_MiniTable* l, uint32_t field_number,
upb_Decoder* d, const upb_MiniTable* t, uint32_t field_number,
int* last_field_index) {
static upb_MiniTable_Field none = {
0, 0, 0, 0, kUpb_FakeFieldType_FieldNotFound, 0};
if (l == NULL) return &none;
if (t == NULL) return &none;
size_t idx = ((size_t)field_number) - 1; // 0 wraps to SIZE_MAX
if (idx < l->dense_below) {
if (idx < t->dense_below) {
/* Fastest case: index into dense fields. */
goto found;
}
if (l->dense_below < l->field_count) {
if (t->dense_below < t->field_count) {
/* Linear search non-dense fields. Resume scanning from last_field_index
* since fields are usually in order. */
int last = *last_field_index;
for (idx = last; idx < l->field_count; idx++) {
if (l->fields[idx].number == field_number) {
for (idx = last; idx < t->field_count; idx++) {
if (t->fields[idx].number == field_number) {
goto found;
}
}
for (idx = l->dense_below; idx < last; idx++) {
if (l->fields[idx].number == field_number) {
for (idx = t->dense_below; idx < last; idx++) {
if (t->fields[idx].number == field_number) {
goto found;
}
}
}
if (d->extreg) {
switch (l->ext) {
switch (t->ext) {
case kUpb_ExtMode_Extendable: {
const upb_MiniTable_Extension* ext =
_upb_extreg_get(d->extreg, l, field_number);
upb_ExtensionRegistry_Lookup(d->extreg, t, field_number);
if (ext) return &ext->field;
break;
}
@ -911,9 +911,9 @@ static const upb_MiniTable_Field* _upb_Decoder_FindField(
return &none; /* Unknown field. */
found:
UPB_ASSERT(l->fields[idx].number == field_number);
UPB_ASSERT(t->fields[idx].number == field_number);
*last_field_index = idx;
return &l->fields[idx];
return &t->fields[idx];
}
int _upb_Decoder_GetVarintOp(const upb_MiniTable_Field* field) {

@ -54,8 +54,9 @@ upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena) {
return r;
}
bool _upb_extreg_add(upb_ExtensionRegistry* r,
const upb_MiniTable_Extension** e, size_t count) {
bool upb_ExtensionRegistry_AddArray(upb_ExtensionRegistry* r,
const upb_MiniTable_Extension** e,
size_t count) {
char buf[EXTREG_KEY_SIZE];
const upb_MiniTable_Extension** start = e;
const upb_MiniTable_Extension** end = UPB_PTRADD(e, count);
@ -81,12 +82,11 @@ failure:
return false;
}
const upb_MiniTable_Extension* _upb_extreg_get(const upb_ExtensionRegistry* r,
const upb_MiniTable* l,
uint32_t num) {
const upb_MiniTable_Extension* upb_ExtensionRegistry_Lookup(
const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num) {
char buf[EXTREG_KEY_SIZE];
upb_value v;
extreg_key(buf, l, num);
extreg_key(buf, t, num);
if (upb_strtable_lookup2(&r->exts, buf, EXTREG_KEY_SIZE, &v)) {
return upb_value_getconstptr(v);
} else {

@ -73,27 +73,23 @@ extern "C" {
*/
typedef struct upb_ExtensionRegistry upb_ExtensionRegistry;
typedef struct upb_MiniTable_Extension upb_MiniTable_Extension;
// Creates a upb_ExtensionRegistry in the given arena.
// The arena must outlive any use of the extreg.
upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena);
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
typedef struct upb_MiniTable_Extension upb_MiniTable_Extension;
// Adds the given extension info for message type |l| and field number |num|
// into the registry. Returns false if this message type and field number were
// already in the map, or if memory allocation fails.
bool _upb_extreg_add(upb_ExtensionRegistry* r,
const upb_MiniTable_Extension** e, size_t count);
// Adds the given extension info for the array |e| of size |count| into the
// registry. If there are any errors, the entire array is backed out.
// The extensions must outlive the registry.
bool upb_ExtensionRegistry_AddArray(upb_ExtensionRegistry* r,
const upb_MiniTable_Extension** e,
size_t count);
// Looks up the extension (if any) defined for message type |l| and field
// number |num|. If an extension was found, copies the field info into |*ext|
// and returns true. Otherwise returns false.
const upb_MiniTable_Extension* _upb_extreg_get(const upb_ExtensionRegistry* r,
const upb_MiniTable* l,
uint32_t num);
// Looks up the extension (if any) defined for message type |t| and field
// number |num|. Returns the extension if found, otherwise NULL.
const upb_MiniTable_Extension* upb_ExtensionRegistry_Lookup(
const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num);
#ifdef __cplusplus
} /* extern "C" */

@ -144,9 +144,10 @@ void Builder::BuildExtensions(upb_ExtensionRegistry** exts) {
status.ptr());
if (!ptr) break;
if (!LinkExtension(ext)) continue;
if (_upb_extreg_get(*exts, ext->extendee, ext->field.number)) continue;
_upb_extreg_add(*exts, const_cast<const upb_MiniTable_Extension**>(&ext),
1);
if (upb_ExtensionRegistry_Lookup(*exts, ext->extendee, ext->field.number))
continue;
upb_ExtensionRegistry_AddArray(
*exts, const_cast<const upb_MiniTable_Extension**>(&ext), 1);
}
}
}

@ -411,8 +411,9 @@ const upb_FieldDef* upb_DefPool_FindExtensionByMiniTable(
const upb_FieldDef* upb_DefPool_FindExtensionByNumber(const upb_DefPool* s,
const upb_MessageDef* m,
int32_t fieldnum) {
const upb_MiniTable* l = upb_MessageDef_MiniTable(m);
const upb_MiniTable_Extension* ext = _upb_extreg_get(s->extreg, l, fieldnum);
const upb_MiniTable* t = upb_MessageDef_MiniTable(m);
const upb_MiniTable_Extension* ext =
upb_ExtensionRegistry_Lookup(s->extreg, t, fieldnum);
return ext ? upb_DefPool_FindExtensionByMiniTable(s, ext) : NULL;
}

@ -338,8 +338,8 @@ void _upb_FileDef_Create(upb_DefBuilder* ctx,
}
if (file->ext_count) {
bool ok = _upb_extreg_add(_upb_DefPool_ExtReg(ctx->symtab),
file->ext_layouts, file->ext_count);
bool ok = upb_ExtensionRegistry_AddArray(
_upb_DefPool_ExtReg(ctx->symtab), file->ext_layouts, file->ext_count);
if (!ok) _upb_DefBuilder_OomErr(ctx);
}
}

Loading…
Cancel
Save