implement a function to register a single extension

PiperOrigin-RevId: 512667332
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 19c4e62875
commit c4b98ddfb5
  1. 22
      upb/mini_table/extension_registry.c
  2. 7
      upb/mini_table/extension_registry.h

@ -53,23 +53,22 @@ upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena) {
return r;
}
UPB_API bool upb_ExtensionRegistry_Add(upb_ExtensionRegistry* r,
const upb_MiniTableExtension* e) {
char buf[EXTREG_KEY_SIZE];
extreg_key(buf, e->extendee, e->field.number);
if (upb_strtable_lookup2(&r->exts, buf, EXTREG_KEY_SIZE, NULL)) return false;
return upb_strtable_insert(&r->exts, buf, EXTREG_KEY_SIZE,
upb_value_constptr(e), r->arena);
}
bool upb_ExtensionRegistry_AddArray(upb_ExtensionRegistry* r,
const upb_MiniTableExtension** e,
size_t count) {
char buf[EXTREG_KEY_SIZE];
const upb_MiniTableExtension** start = e;
const upb_MiniTableExtension** end = UPB_PTRADD(e, count);
for (; e < end; e++) {
const upb_MiniTableExtension* ext = *e;
extreg_key(buf, ext->extendee, ext->field.number);
upb_value v;
if (upb_strtable_lookup2(&r->exts, buf, EXTREG_KEY_SIZE, &v)) {
goto failure;
}
if (!upb_strtable_insert(&r->exts, buf, EXTREG_KEY_SIZE,
upb_value_constptr(ext), r->arena)) {
goto failure;
}
if (!upb_ExtensionRegistry_Add(r, *e)) goto failure;
}
return true;
@ -77,6 +76,7 @@ failure:
// Back out the entries previously added.
for (end = e, e = start; e < end; e++) {
const upb_MiniTableExtension* ext = *e;
char buf[EXTREG_KEY_SIZE];
extreg_key(buf, ext->extendee, ext->field.number);
upb_strtable_remove2(&r->exts, buf, EXTREG_KEY_SIZE, NULL);
}

@ -78,18 +78,21 @@ typedef struct upb_ExtensionRegistry upb_ExtensionRegistry;
// The arena must outlive any use of the extreg.
UPB_API upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena);
UPB_API bool upb_ExtensionRegistry_Add(upb_ExtensionRegistry* r,
const upb_MiniTableExtension* e);
// 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.
// Possible errors include OOM or an extension number that already exists.
// TODO: There is currently no way to determine the exact reason for failure.
// TODO(salo): There is currently no way to know the exact reason for failure.
bool upb_ExtensionRegistry_AddArray(upb_ExtensionRegistry* r,
const upb_MiniTableExtension** e,
size_t count);
// Looks up the extension (if any) defined for message type |t| and field
// number |num|. Returns the extension if found, otherwise NULL.
const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(
UPB_API const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(
const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num);
#ifdef __cplusplus

Loading…
Cancel
Save