From c4b98ddfb5f9cb925ffb556f45c33e2f83c9578a Mon Sep 17 00:00:00 2001 From: Eric Salo Date: Mon, 27 Feb 2023 10:34:01 -0800 Subject: [PATCH] implement a function to register a single extension PiperOrigin-RevId: 512667332 --- upb/mini_table/extension_registry.c | 22 +++++++++++----------- upb/mini_table/extension_registry.h | 7 +++++-- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/upb/mini_table/extension_registry.c b/upb/mini_table/extension_registry.c index de8e05ed7a..3d3e4bab9f 100644 --- a/upb/mini_table/extension_registry.c +++ b/upb/mini_table/extension_registry.c @@ -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); } diff --git a/upb/mini_table/extension_registry.h b/upb/mini_table/extension_registry.h index fb7ba053c2..619e1d91d5 100644 --- a/upb/mini_table/extension_registry.h +++ b/upb/mini_table/extension_registry.h @@ -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