From 47da2afd52b0f108085439e3dc8ad5236809fbae Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 21 Jan 2017 10:47:58 -0800 Subject: Make upb::SymbolTable no longer reference-counted. This transitions it from shared ownership to unique ownership. --- upb/bindings/lua/def.c | 57 ++++++++++++++++++-------------------------------- upb/bindings/lua/upb.h | 2 +- upb/def.c | 22 ++----------------- upb/def.h | 26 ++++++++--------------- upb/msg.c | 2 -- 5 files changed, 32 insertions(+), 77 deletions(-) (limited to 'upb') 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 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::New() { - upb_symtab *s = upb_symtab_new(&s); - return reffed_ptr(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); } -- cgit v1.2.3