Skip to content

Commit ad4ffd2

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Remove upb_ExtensionRegistry_AddAllLinkedExtensions API.
This is now an implementation detail of the public GeneratedRegistry. PiperOrigin-RevId: 827277782
1 parent a531523 commit ad4ffd2

File tree

7 files changed

+73
-77
lines changed

7 files changed

+73
-77
lines changed

hpb/extension.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "hpb/ptr.h"
2222
#include "upb/message/accessors.h"
2323
#include "upb/mini_table/extension_registry.h"
24+
#include "upb/mini_table/generated_registry.h"
2425

2526
namespace hpb {
2627
// upb has a notion of an ExtensionRegistry. We expect most callers to use
@@ -66,17 +67,21 @@ class ExtensionRegistry {
6667

6768
private:
6869
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
70+
explicit ExtensionRegistry(upb_ExtensionRegistry* registry)
71+
: registry_(registry) {}
72+
6973
friend upb_ExtensionRegistry* ::hpb::internal::GetUpbExtensions(
7074
const ExtensionRegistry& extension_registry);
7175
upb_ExtensionRegistry* registry_;
7276
#endif
7377
// TODO: b/379100963 - Introduce ShutdownHpbLibrary
7478
static const ExtensionRegistry* NewGeneratedRegistry() {
7579
#if HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_UPB
76-
static hpb::Arena* global_arena = new hpb::Arena();
77-
ExtensionRegistry* registry = new ExtensionRegistry(*global_arena);
78-
upb_ExtensionRegistry_AddAllLinkedExtensions(registry->registry_);
79-
return registry;
80+
static const upb_GeneratedRegistryRef* registry_ref =
81+
upb_GeneratedRegistry_Load();
82+
// Const cast is safe because we're returning a const wrapper.
83+
return new ExtensionRegistry(const_cast<upb_ExtensionRegistry*>(
84+
upb_GeneratedRegistry_Get(registry_ref)));
8085
#elif HPB_INTERNAL_BACKEND == HPB_INTERNAL_BACKEND_CPP
8186
ExtensionRegistry* registry = new ExtensionRegistry();
8287
return registry;

upb/mini_table/extension_registry.c

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include "upb/hash/str_table.h"
1616
#include "upb/mem/arena.h"
1717
#include "upb/mini_table/extension.h"
18-
#include "upb/mini_table/generated_registry.h"
19-
#include "upb/mini_table/internal/generated_registry.h"
2018
#include "upb/mini_table/message.h"
2119

2220
// Must be last.
@@ -91,47 +89,6 @@ upb_ExtensionRegistryStatus upb_ExtensionRegistry_AddArray(
9189
return status;
9290
}
9391

94-
const UPB_PRIVATE(upb_GeneratedExtensionListEntry) *
95-
UPB_PRIVATE(upb_generated_extension_list) = NULL;
96-
97-
bool upb_ExtensionRegistry_AddAllLinkedExtensions(upb_ExtensionRegistry* r) {
98-
const UPB_PRIVATE(upb_GeneratedExtensionListEntry)* entry =
99-
UPB_PRIVATE(upb_generated_extension_list);
100-
while (entry != NULL) {
101-
// Comparing pointers to different objects is undefined behavior, so we
102-
// convert them to uintptr_t and compare their values.
103-
uintptr_t begin = (uintptr_t)entry->start;
104-
uintptr_t end = (uintptr_t)entry->stop;
105-
uintptr_t current = begin;
106-
while (current < end) {
107-
const upb_MiniTableExtension* ext =
108-
(const upb_MiniTableExtension*)current;
109-
// Sentinels and padding introduced by the linker can result in zeroed
110-
// entries, so simply skip them.
111-
if (upb_MiniTableExtension_Number(ext) == 0) {
112-
// MSVC introduces padding that might not be sized exactly the same as
113-
// upb_MiniTableExtension, so we can't iterate by sizeof. This is a
114-
// valid thing for any linker to do, so it's safer to just always do it.
115-
current += UPB_ALIGN_OF(upb_MiniTableExtension);
116-
continue;
117-
}
118-
119-
if (upb_ExtensionRegistry_Add(r, ext) !=
120-
kUpb_ExtensionRegistryStatus_Ok) {
121-
return false;
122-
}
123-
current += sizeof(upb_MiniTableExtension);
124-
}
125-
entry = entry->next;
126-
}
127-
return true;
128-
}
129-
130-
const upb_ExtensionRegistry* upb_ExtensionRegistry_GetGenerated(
131-
const upb_GeneratedRegistryRef* genreg) {
132-
return genreg != NULL ? genreg->registry : NULL;
133-
}
134-
13592
const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(
13693
const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num) {
13794
char buf[EXTREG_KEY_SIZE];

upb/mini_table/extension_registry.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include "upb/mem/arena.h"
1515
#include "upb/mini_table/extension.h"
16-
#include "upb/mini_table/generated_registry.h"
1716
#include "upb/mini_table/message.h"
1817

1918
// Must be last.
@@ -80,28 +79,6 @@ UPB_API upb_ExtensionRegistryStatus upb_ExtensionRegistry_Add(
8079
upb_ExtensionRegistryStatus upb_ExtensionRegistry_AddArray(
8180
upb_ExtensionRegistry* r, const upb_MiniTableExtension** e, size_t count);
8281

83-
// Adds all extensions linked into the binary into the registry. The set of
84-
// linked extensions is assembled by the linker using linker arrays. This
85-
// will likely not work properly if the extensions are split across multiple
86-
// shared libraries.
87-
//
88-
// Returns true if all extensions were added successfully, false on out of
89-
// memory or if any extensions were already present.
90-
//
91-
// This API is currently not available on MSVC (though it *is* available on
92-
// Windows using clang-cl).
93-
UPB_API bool upb_ExtensionRegistry_AddAllLinkedExtensions(
94-
upb_ExtensionRegistry* r);
95-
96-
// Returns the extension registry contained by a reference to the generated
97-
// registry. The reference must be held for the lifetime of the registry.
98-
//
99-
// TODO This should actually be moved to generated_registry.h, but
100-
// can't because of the current location of
101-
// upb_ExtensionRegistry_AddAllLinkedExtensions.
102-
UPB_API const upb_ExtensionRegistry* upb_ExtensionRegistry_GetGenerated(
103-
const upb_GeneratedRegistryRef* genreg);
104-
10582
// Looks up the extension (if any) defined for message type |t| and field
10683
// number |num|. Returns the extension if found, otherwise NULL.
10784
UPB_API const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(

upb/mini_table/generated_registry.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "upb/mem/alloc.h"
1313
#include "upb/mem/arena.h"
14+
#include "upb/mini_table/extension.h"
1415
#include "upb/mini_table/extension_registry.h"
1516
#include "upb/mini_table/internal/generated_registry.h" // IWYU pragma: keep
1617
#include "upb/port/atomic.h"
@@ -22,6 +23,9 @@
2223
#include <sched.h>
2324
#endif // UPB_TSAN
2425

26+
const UPB_PRIVATE(upb_GeneratedExtensionListEntry) *
27+
UPB_PRIVATE(upb_generated_extension_list) = NULL;
28+
2529
typedef struct upb_GeneratedRegistry {
2630
UPB_ATOMIC(upb_GeneratedRegistryRef*) ref;
2731
UPB_ATOMIC(int32_t) ref_count;
@@ -32,6 +36,40 @@ static upb_GeneratedRegistry* _upb_generated_registry(void) {
3236
return &r;
3337
}
3438

39+
static bool _upb_GeneratedRegistry_AddAllLinkedExtensions(
40+
upb_ExtensionRegistry* r) {
41+
const UPB_PRIVATE(upb_GeneratedExtensionListEntry)* entry =
42+
UPB_PRIVATE(upb_generated_extension_list);
43+
while (entry != NULL) {
44+
// Comparing pointers to different objects is undefined behavior, so we
45+
// convert them to uintptr_t and compare their values.
46+
uintptr_t begin = (uintptr_t)entry->start;
47+
uintptr_t end = (uintptr_t)entry->stop;
48+
uintptr_t current = begin;
49+
while (current < end) {
50+
const upb_MiniTableExtension* ext =
51+
(const upb_MiniTableExtension*)current;
52+
// Sentinels and padding introduced by the linker can result in zeroed
53+
// entries, so simply skip them.
54+
if (upb_MiniTableExtension_Number(ext) == 0) {
55+
// MSVC introduces padding that might not be sized exactly the same as
56+
// upb_MiniTableExtension, so we can't iterate by sizeof. This is a
57+
// valid thing for any linker to do, so it's safer to just always do it.
58+
current += UPB_ALIGN_OF(upb_MiniTableExtension);
59+
continue;
60+
}
61+
62+
if (upb_ExtensionRegistry_Add(r, ext) !=
63+
kUpb_ExtensionRegistryStatus_Ok) {
64+
return false;
65+
}
66+
current += sizeof(upb_MiniTableExtension);
67+
}
68+
entry = entry->next;
69+
}
70+
return true;
71+
}
72+
3573
// Constructs a new GeneratedRegistryRef, adding all linked extensions to the
3674
// registry or returning NULL on failure.
3775
static upb_GeneratedRegistryRef* _upb_GeneratedRegistry_New(void) {
@@ -47,7 +85,7 @@ static upb_GeneratedRegistryRef* _upb_GeneratedRegistry_New(void) {
4785
ref->arena = arena;
4886
ref->registry = extreg;
4987

50-
if (!upb_ExtensionRegistry_AddAllLinkedExtensions(extreg)) goto err;
88+
if (!_upb_GeneratedRegistry_AddAllLinkedExtensions(extreg)) goto err;
5189

5290
return ref;
5391

@@ -138,3 +176,9 @@ void upb_GeneratedRegistry_Release(const upb_GeneratedRegistryRef* r) {
138176
}
139177
}
140178
}
179+
180+
const upb_ExtensionRegistry* upb_GeneratedRegistry_Get(
181+
const upb_GeneratedRegistryRef* r) {
182+
if (r == NULL) return NULL;
183+
return r->registry;
184+
}

upb/mini_table/generated_registry.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef UPB_MINI_TABLE_GENERATED_REGISTRY_H_
99
#define UPB_MINI_TABLE_GENERATED_REGISTRY_H_
1010

11+
#include "upb/mini_table/extension_registry.h"
12+
1113
// Must be last.
1214
#include "upb/port/def.inc"
1315

@@ -47,6 +49,13 @@ UPB_API const upb_GeneratedRegistryRef* upb_GeneratedRegistry_Load(void);
4749
// callers.
4850
UPB_API void upb_GeneratedRegistry_Release(const upb_GeneratedRegistryRef* r);
4951

52+
// Returns the extension registry contained by a reference to the generated
53+
// registry.
54+
//
55+
// The reference must be held for the lifetime of the registry.
56+
UPB_API const upb_ExtensionRegistry* upb_GeneratedRegistry_Get(
57+
const upb_GeneratedRegistryRef* r);
58+
5059
#ifdef __cplusplus
5160
} /* extern "C" */
5261
#endif

upb/mini_table/generated_registry_test.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class GeneratedRegistryTest : public ::testing::Test {
2828
void SetUp() override {
2929
ref_ = upb_GeneratedRegistry_Load();
3030
ASSERT_NE(ref_, nullptr);
31-
reg_ = upb_ExtensionRegistry_GetGenerated(ref_);
31+
reg_ = upb_GeneratedRegistry_Get(ref_);
3232
ASSERT_NE(reg_, nullptr);
3333
}
3434

@@ -92,6 +92,10 @@ TEST_F(GeneratedRegistryTest, ReleaseOnError) {
9292
upb_GeneratedRegistry_Release(nullptr);
9393
}
9494

95+
TEST_F(GeneratedRegistryTest, GetOnError) {
96+
EXPECT_EQ(upb_GeneratedRegistry_Get(nullptr), nullptr);
97+
}
98+
9599
// On 32-bit systems, the stack size is smaller, so we can't run too many
96100
// concurrent threads without overflowing the stack.
97101
constexpr int kRaceIterations = sizeof(void*) < 8 ? 100 : 2000;
@@ -104,7 +108,7 @@ TEST(GeneratedRegistryRaceTest, Load) {
104108
threads.push_back(std::thread([&, i]() {
105109
barrier.Block();
106110
const upb_GeneratedRegistryRef* ref = upb_GeneratedRegistry_Load();
107-
EXPECT_NE(upb_ExtensionRegistry_GetGenerated(ref), nullptr);
111+
EXPECT_NE(upb_GeneratedRegistry_Get(ref), nullptr);
108112
refs[i] = ref;
109113
}));
110114
}
@@ -129,7 +133,7 @@ TEST(GeneratedRegistryRaceTest, Release) {
129133

130134
for (int i = 0; i < kRaceIterations; ++i) {
131135
threads.push_back(std::thread([&, i]() {
132-
EXPECT_NE(upb_ExtensionRegistry_GetGenerated(refs[i]), nullptr);
136+
EXPECT_NE(upb_GeneratedRegistry_Get(refs[i]), nullptr);
133137
barrier.Block();
134138
upb_GeneratedRegistry_Release(refs[i]);
135139
}));
@@ -146,7 +150,7 @@ TEST(GeneratedRegistryRaceTest, LoadRelease) {
146150
threads.push_back(std::thread([&]() {
147151
barrier.Block();
148152
const upb_GeneratedRegistryRef* ref = upb_GeneratedRegistry_Load();
149-
EXPECT_NE(upb_ExtensionRegistry_GetGenerated(ref), nullptr);
153+
EXPECT_NE(upb_GeneratedRegistry_Get(ref), nullptr);
150154
upb_GeneratedRegistry_Release(ref);
151155
}));
152156
}
@@ -174,7 +178,7 @@ TEST(GeneratedRegistryRaceTest, ReleaseLastAndLoadMultiple) {
174178
threads.push_back(std::thread([&]() {
175179
barrier.Block();
176180
const upb_GeneratedRegistryRef* ref2 = upb_GeneratedRegistry_Load();
177-
EXPECT_NE(upb_ExtensionRegistry_GetGenerated(ref2), nullptr);
181+
EXPECT_NE(upb_GeneratedRegistry_Get(ref2), nullptr);
178182
upb_GeneratedRegistry_Release(ref2);
179183
}));
180184
}

upb/reflection/def_pool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,5 +545,5 @@ bool _upb_DefPool_LoadDefInit(upb_DefPool* s, const _upb_DefPool_Init* init) {
545545

546546
const upb_ExtensionRegistry* _upb_DefPool_GeneratedExtensionRegistry(
547547
const upb_DefPool* s) {
548-
return upb_ExtensionRegistry_GetGenerated(s->generated_extreg);
548+
return upb_GeneratedRegistry_Get(s->generated_extreg);
549549
}

0 commit comments

Comments
 (0)