summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/bindings/lua/test_upb.lua11
-rw-r--r--tests/test_cpp.cc6
-rw-r--r--tests/test_def.c27
-rw-r--r--upb/bindings/lua/def.c57
-rw-r--r--upb/bindings/lua/upb.h2
-rw-r--r--upb/def.c22
-rw-r--r--upb/def.h26
-rw-r--r--upb/msg.c2
8 files changed, 46 insertions, 107 deletions
diff --git a/tests/bindings/lua/test_upb.lua b/tests/bindings/lua/test_upb.lua
index 65439bc..261328d 100644
--- a/tests/bindings/lua/test_upb.lua
+++ b/tests/bindings/lua/test_upb.lua
@@ -401,17 +401,6 @@ function test_symtab()
local msgdef3 = symtab:lookup("ContainingMessage2")
assert_not_nil(msgdef3)
assert_equal(msgdef3:field("field5"):subdef(), msgdef2)
-
- -- Freeze the symtab and verify that mutating operations are not allowed.
- assert_false(symtab:is_frozen())
- symtab:freeze()
- assert_true(symtab:is_frozen())
- assert_error_match("frozen", function() symtab:freeze() end)
- assert_error_match("frozen", function()
- symtab:add{
- upb.MessageDef{full_name = "Foo"}
- }
- end)
end
function test_symtab_add_extension()
diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc
index 220e69f..2cd9802 100644
--- a/tests/test_cpp.cc
+++ b/tests/test_cpp.cc
@@ -154,15 +154,12 @@ static void TestSymbolTable(const char *descriptor_file) {
exit(1);
}
- upb::reffed_ptr<upb::SymbolTable> s(upb::SymbolTable::New());
+ upb::SymbolTable* s = upb::SymbolTable::New();
for (size_t i = 0; i < files.size(); i++) {
ASSERT(s->AddFile(files[i].get(), &status));
}
- ASSERT(!s->IsFrozen());
- s->Freeze();
- ASSERT(s->IsFrozen());
upb::reffed_ptr<const upb::MessageDef> md(s->LookupMessage("C"));
ASSERT(md.get());
@@ -180,6 +177,7 @@ static void TestSymbolTable(const char *descriptor_file) {
#endif
ASSERT(md.get());
+ upb::SymbolTable::Free(s);
}
static void TestCasts1() {
diff --git a/tests/test_def.c b/tests/test_def.c
index 93622c1..29c459a 100644
--- a/tests/test_def.c
+++ b/tests/test_def.c
@@ -13,13 +13,13 @@
const char *descriptor_file;
static void test_empty_symtab() {
- upb_symtab *s = upb_symtab_new(&s);
+ upb_symtab *s = upb_symtab_new();
upb_symtab_iter i;
for (upb_symtab_begin(&i, s, UPB_DEF_ANY); !upb_symtab_done(&i);
upb_symtab_next(&i)) {
ASSERT(false); /* Should not get here. */
}
- upb_symtab_unref(s, &s);
+ upb_symtab_free(s);
}
static void test_noreftracking() {
@@ -39,8 +39,8 @@ static void test_noreftracking() {
upb_msgdef_unref(md, &md);
}
-static upb_symtab *load_test_proto(void *owner) {
- upb_symtab *s = upb_symtab_new(owner);
+static upb_symtab *load_test_proto() {
+ upb_symtab *s = upb_symtab_new();
upb_status status = UPB_STATUS_INIT;
size_t len;
char *data = upb_readfile(descriptor_file, &len);
@@ -64,15 +64,12 @@ static upb_symtab *load_test_proto(void *owner) {
upb_gfree(files);
- ASSERT(!upb_symtab_isfrozen(s));
- upb_symtab_freeze(s);
- ASSERT(upb_symtab_isfrozen(s));
return s;
}
static void test_cycles() {
bool ok;
- upb_symtab *s = load_test_proto(&s);
+ upb_symtab *s = load_test_proto();
const upb_msgdef *m;
const upb_fielddef *f;
const upb_def *def;
@@ -84,7 +81,7 @@ static void test_cycles() {
upb_def_ref(def, &def);
ASSERT(def);
ASSERT(upb_def_isfrozen(def));
- upb_symtab_unref(s, &s);
+ upb_symtab_free(s);
/* Message A has only one subfield: "optional B b = 1". */
m = upb_downcast_msgdef(def);
@@ -162,7 +159,7 @@ static void test_symbol_resolution() {
upb_msgdef_unref(m3, &m3);
upb_msgdef_unref(m2, &m2);
upb_msgdef_unref(m1, &m1);
- upb_symtab_unref(symtab, &symtab);
+ upb_symtab_free(symtab);
}
static void test_fielddef_unref() {
@@ -173,7 +170,7 @@ static void test_fielddef_unref() {
upb_fielddef_ref(f, &f);
/* Unref symtab; now fielddef is the only thing keeping the msgdef alive. */
- upb_symtab_unref(s, &s);
+ upb_symtab_free(s);
/* Check that md is still alive. */
ok = strcmp(upb_msgdef_fullname(md), "A") == 0;
ASSERT(ok);
@@ -256,7 +253,7 @@ static void test_replacement() {
* are reachable from it. */
ASSERT(m3 == m2);
- upb_symtab_unref(s, &s);
+ upb_symtab_free(s);
}
static void test_cycles_in_replacement() {
@@ -269,7 +266,7 @@ static void test_cycles_in_replacement() {
&s, NULL);
ASSERT_STATUS(upb_symtab_add(s, (upb_def**)&m, 1, &s, &status), &status);
ASSERT_STATUS(upb_symtab_add(s, NULL, 0, &s, &status), &status);
- upb_symtab_unref(s, &s);
+ upb_symtab_free(s);
}
static void test_freeze_free() {
@@ -413,7 +410,7 @@ static void test_mapentry_check() {
upb_symtab_add(symtab, defs, 2, NULL, &s);
ASSERT(upb_ok(&s));
- upb_symtab_unref(symtab, &symtab);
+ upb_symtab_free(symtab);
upb_msgdef_unref(subm, &subm);
upb_msgdef_unref(m, &m);
}
@@ -470,7 +467,7 @@ static void test_oneofs() {
lookup_field = upb_oneofdef_ntofz(o, "field1");
ASSERT(lookup_field != NULL && upb_fielddef_number(lookup_field) == 1);
- upb_symtab_unref(symtab, &symtab);
+ upb_symtab_free(symtab);
upb_oneofdef_unref(o, &o);
}
diff --git a/upb/bindings/lua/def.c b/upb/bindings/lua/def.c
index 638abb6..5133831 100644
--- a/upb/bindings/lua/def.c
+++ b/upb/bindings/lua/def.c
@@ -1090,50 +1090,35 @@ static const struct luaL_Reg lupb_filedef_m[] = {
/* lupb_symtab ****************************************************************/
-/* Inherits a ref on the symtab.
- * Checks that narg is a proper lupb_symtab object. If it is, leaves its
- * metatable on the stack for cache lookups/updates. */
-const upb_symtab *lupb_symtab_check(lua_State *L, int narg) {
- return lupb_refcounted_check(L, narg, LUPB_SYMTAB);
-}
-
-static upb_symtab *lupb_symtab_checkmutable(lua_State *L, int narg) {
- const upb_symtab *s = lupb_symtab_check(L, narg);
- if (upb_symtab_isfrozen(s))
- luaL_error(L, "not allowed on frozen value");
- return (upb_symtab*)s;
-}
-
-void lupb_symtab_pushwrapper(lua_State *L, const upb_symtab *s,
- const void *ref_donor) {
- lupb_refcounted_pushwrapper(L, upb_symtab_upcast(s), LUPB_SYMTAB, ref_donor,
- sizeof(void *));
-}
+typedef struct {
+ upb_symtab *symtab;
+} lupb_symtab;
-void lupb_symtab_pushnewrapper(lua_State *L, const upb_symtab *s,
- const void *ref_donor) {
- lupb_refcounted_pushnewrapper(L, upb_symtab_upcast(s), LUPB_SYMTAB,
- ref_donor);
+upb_symtab *lupb_symtab_check(lua_State *L, int narg) {
+ lupb_symtab *lsymtab = luaL_checkudata(L, narg, LUPB_SYMTAB);
+ if (!lsymtab->symtab) {
+ luaL_error(L, "called into dead object");
+ }
+ return lsymtab->symtab;
}
static int lupb_symtab_new(lua_State *L) {
- upb_symtab *s = upb_symtab_new(&s);
- lupb_symtab_pushnewrapper(L, s, &s);
+ lupb_symtab *lsymtab = lua_newuserdata(L, sizeof(*lsymtab));
+ lsymtab->symtab = upb_symtab_new();
+ luaL_getmetatable(L, LUPB_SYMTAB);
+ lua_setmetatable(L, -2);
return 1;
}
-static int lupb_symtab_freeze(lua_State *L) {
- upb_symtab_freeze(lupb_symtab_checkmutable(L, 1));
+static int lupb_symtab_gc(lua_State *L) {
+ lupb_symtab *lsymtab = luaL_checkudata(L, 1, LUPB_SYMTAB);
+ upb_symtab_free(lsymtab->symtab);
+ lsymtab->symtab = NULL;
return 0;
}
-static int lupb_symtab_isfrozen(lua_State *L) {
- lua_pushboolean(L, upb_symtab_isfrozen(lupb_symtab_check(L, 1)));
- return 1;
-}
-
static int lupb_symtab_add(lua_State *L) {
- upb_symtab *s = lupb_symtab_checkmutable(L, 1);
+ upb_symtab *s = lupb_symtab_check(L, 1);
int n;
upb_def **defs;
@@ -1161,7 +1146,7 @@ static int lupb_symtab_add(lua_State *L) {
}
static int lupb_symtab_addfile(lua_State *L) {
- upb_symtab *s = lupb_symtab_checkmutable(L, 1);
+ upb_symtab *s = lupb_symtab_check(L, 1);
upb_filedef *f = lupb_filedef_checkmutable(L, 2);
CHK(upb_symtab_addfile(s, f, &status));
return 0;
@@ -1201,14 +1186,12 @@ static const struct luaL_Reg lupb_symtab_m[] = {
{"add", lupb_symtab_add},
{"add_file", lupb_symtab_addfile},
{"defs", lupb_symtab_defs},
- {"freeze", lupb_symtab_freeze},
- {"is_frozen", lupb_symtab_isfrozen},
{"lookup", lupb_symtab_lookup},
{NULL, NULL}
};
static const struct luaL_Reg lupb_symtab_mm[] = {
- {"__gc", lupb_refcounted_gc},
+ {"__gc", lupb_symtab_gc},
{NULL, NULL}
};
diff --git a/upb/bindings/lua/upb.h b/upb/bindings/lua/upb.h
index 84212c5..88a201c 100644
--- a/upb/bindings/lua/upb.h
+++ b/upb/bindings/lua/upb.h
@@ -101,7 +101,7 @@ void *lupb_refcounted_check(lua_State *L, int narg, const char *type);
const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg);
const upb_enumdef *lupb_enumdef_check(lua_State *L, int narg);
const upb_fielddef *lupb_fielddef_check(lua_State *L, int narg);
-const upb_symtab *lupb_symtab_check(lua_State *L, int narg);
+upb_symtab *lupb_symtab_check(lua_State *L, int narg);
void lupb_refcounted_pushnewrapper(lua_State *L, const upb_refcounted *obj,
const char *type, const void *ref_donor);
diff --git a/upb/def.c b/upb/def.c
index 45cd362..39d8c08 100644
--- a/upb/def.c
+++ b/upb/def.c
@@ -2142,8 +2142,7 @@ bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep) {
}
}
-static void upb_symtab_free(upb_refcounted *r) {
- upb_symtab *s = (upb_symtab*)r;
+void upb_symtab_free(upb_symtab *s) {
upb_strtable_iter i;
upb_strtable_begin(&i, &s->symtab);
for (; !upb_strtable_done(&i); upb_strtable_next(&i)) {
@@ -2154,32 +2153,16 @@ static void upb_symtab_free(upb_refcounted *r) {
upb_gfree(s);
}
-upb_symtab *upb_symtab_new(const void *owner) {
- static const struct upb_refcounted_vtbl vtbl = {NULL, &upb_symtab_free};
-
+upb_symtab *upb_symtab_new() {
upb_symtab *s = upb_gmalloc(sizeof(*s));
if (!s) {
return NULL;
}
- upb_refcounted_init(upb_symtab_upcast_mutable(s), &vtbl, owner);
upb_strtable_init(&s->symtab, UPB_CTYPE_PTR);
return s;
}
-void upb_symtab_freeze(upb_symtab *s) {
- upb_refcounted *r;
- bool ok;
-
- UPB_ASSERT(!upb_symtab_isfrozen(s));
- r = upb_symtab_upcast_mutable(s);
- /* The symtab does not take ref2's (see refcounted.h) on the defs, because
- * defs cannot refer back to the table and therefore cannot create cycles. So
- * 0 will suffice for maxdepth here. */
- ok = upb_refcounted_freeze(&r, 1, NULL, 0);
- UPB_ASSERT(ok);
-}
-
const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym) {
upb_value v;
upb_def *ret = upb_strtable_lookup(&s->symtab, sym, &v) ?
@@ -2352,7 +2335,6 @@ static bool symtab_add(upb_symtab *s, upb_def *const*defs, size_t n,
return true;
}
- UPB_ASSERT(!upb_symtab_isfrozen(s));
if (!upb_strtable_init(&addtab, UPB_CTYPE_PTR)) {
upb_status_seterrmsg(status, "out of memory");
return false;
diff --git a/upb/def.h b/upb/def.h
index b2c42a6..b99dcb0 100644
--- a/upb/def.h
+++ b/upb/def.h
@@ -44,8 +44,7 @@ UPB_DECLARE_DERIVED_TYPE(upb::OneofDef, upb::RefCounted, upb_oneofdef,
upb_refcounted)
UPB_DECLARE_DERIVED_TYPE(upb::FileDef, upb::RefCounted, upb_filedef,
upb_refcounted)
-UPB_DECLARE_DERIVED_TYPE(upb::SymbolTable, upb::RefCounted,
- upb_symtab, upb_refcounted)
+UPB_DECLARE_TYPE(upb::SymbolTable, upb_symtab)
/* The maximum message depth that the type graph can have. This is a resource
@@ -1434,10 +1433,8 @@ class upb::SymbolTable {
public:
/* Returns a new symbol table with a single ref owned by "owner."
* Returns NULL if memory allocation failed. */
- static reffed_ptr<SymbolTable> New();
-
- /* Include RefCounted base methods. */
- UPB_REFCOUNTED_CPPMETHODS
+ static SymbolTable* New();
+ static void Free(upb::SymbolTable* table);
/* For all lookup functions, the returned pointer is not owned by the
* caller; it may be invalidated by any non-const call or unref of the
@@ -1527,11 +1524,8 @@ UPB_BEGIN_EXTERN_C
/* Native C API. */
-/* Include refcounted methods like upb_symtab_ref(). */
-UPB_REFCOUNTED_CMETHODS(upb_symtab, upb_symtab_upcast)
-
-upb_symtab *upb_symtab_new(const void *owner);
-void upb_symtab_freeze(upb_symtab *s);
+upb_symtab *upb_symtab_new();
+void upb_symtab_free(upb_symtab* s);
const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base,
const char *sym);
const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym);
@@ -1562,13 +1556,11 @@ UPB_END_EXTERN_C
#ifdef __cplusplus
/* C++ inline wrappers. */
namespace upb {
-inline reffed_ptr<SymbolTable> SymbolTable::New() {
- upb_symtab *s = upb_symtab_new(&s);
- return reffed_ptr<SymbolTable>(s, &s);
+inline SymbolTable* SymbolTable::New() {
+ return upb_symtab_new();
}
-
-inline void SymbolTable::Freeze() {
- return upb_symtab_freeze(this);
+inline void SymbolTable::Free(SymbolTable* s) {
+ upb_symtab_free(s);
}
inline const Def *SymbolTable::Resolve(const char *base,
const char *sym) const {
diff --git a/upb/msg.c b/upb/msg.c
index 517b814..5fa9bc8 100644
--- a/upb/msg.c
+++ b/upb/msg.c
@@ -350,7 +350,6 @@ upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab) {
upb_msgfactory *ret = upb_gmalloc(sizeof(*ret));
ret->symtab = symtab;
- upb_symtab_ref(ret->symtab, &ret->symtab);
upb_inttable_init(&ret->layouts, UPB_CTYPE_PTR);
upb_inttable_init(&ret->mergehandlers, UPB_CTYPE_CONSTPTR);
@@ -373,7 +372,6 @@ void upb_msgfactory_free(upb_msgfactory *f) {
upb_inttable_uninit(&f->layouts);
upb_inttable_uninit(&f->mergehandlers);
- upb_symtab_unref(f->symtab, &f->symtab);
upb_gfree(f);
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback