From 2c26f60dbbc49bca6233cc20a15ff4b32454c6e8 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 15 Jan 2019 14:48:09 -0800 Subject: [PATCH] Added some comments and reversed upb_arena_cleanup() args. --- upb/handlers.c | 21 +-------------------- upb/handlers.h | 4 ++++ upb/json/parser.h | 3 +++ upb/json/printer.h | 2 ++ upb/pb/decoder.h | 3 +++ upb/pb/encoder.h | 2 ++ upb/upb.c | 2 +- upb/upb.h | 6 +++--- 8 files changed, 19 insertions(+), 24 deletions(-) diff --git a/upb/handlers.c b/upb/handlers.c index aa23b4651f..7abf94814c 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -392,7 +392,6 @@ uint32_t upb_handlers_selectorcount(const upb_fielddef *f) { struct upb_handlercache { upb_arena *arena; upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ - upb_inttable cleanup_; upb_handlers_callback *callback; const void *closure; }; @@ -448,7 +447,6 @@ upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, cache->closure = closure; if (!upb_inttable_init(&cache->tab, UPB_CTYPE_PTR)) goto oom; - if (!upb_inttable_init(&cache->cleanup_, UPB_CTYPE_FPTR)) goto oom; return cache; @@ -458,31 +456,14 @@ oom: } void upb_handlercache_free(upb_handlercache *cache) { - upb_inttable_iter i; - - upb_inttable_begin(&i, &cache->cleanup_); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - void *val = (void*)upb_inttable_iter_key(&i); - upb_value func_val = upb_inttable_iter_value(&i); - upb_handlerfree *func = upb_value_getfptr(func_val); - func(val); - } - upb_inttable_uninit(&cache->tab); - upb_inttable_uninit(&cache->cleanup_); upb_arena_free(cache->arena); upb_gfree(cache); } bool upb_handlercache_addcleanup(upb_handlercache *c, void *p, upb_handlerfree *func) { - bool ok; - if (upb_inttable_lookupptr(&c->cleanup_, p, NULL)) { - return false; - } - ok = upb_inttable_insertptr(&c->cleanup_, p, upb_value_fptr(func)); - UPB_ASSERT(ok); - return true; + return upb_arena_addcleanup(c->arena, p, func); } /* upb_byteshandler ***********************************************************/ diff --git a/upb/handlers.h b/upb/handlers.h index 764e83e1be..e267f126e7 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -594,6 +594,10 @@ class upb::HandlersPtr { /* upb_handlercache ***********************************************************/ +/* A upb_handlercache lazily builds and caches upb_handlers. You pass it a + * function (with optional closure) that can build handlers for a given + * message on-demand, and the cache maintains a map of msgdef->handlers. */ + #ifdef __cplusplus extern "C" { #endif diff --git a/upb/json/parser.h b/upb/json/parser.h index 6f3eaa75d9..c063a77ef7 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -103,6 +103,9 @@ class upb::json::ParserPtr { /* upb_json_codecache *********************************************************/ +/* Lazily builds and caches decoder methods that will push data to the given + * handlers. The upb_symtab object(s) must outlive this object. */ + struct upb_json_codecache; typedef struct upb_json_codecache upb_json_codecache; diff --git a/upb/json/printer.h b/upb/json/printer.h index 857ae4708f..85b9b128f9 100644 --- a/upb/json/printer.h +++ b/upb/json/printer.h @@ -36,6 +36,8 @@ const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, bool preserve_fieldnames, const void *owner); +/* Lazily builds and caches handlers that will push encoded data to a bytessink. + * Any msgdef objects used with this object must outlive it. */ upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames); #ifdef __cplusplus diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 5adfba83b3..86e51dfd57 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -171,6 +171,9 @@ class upb::pb::DecoderPtr { /* upb_pbcodecache ************************************************************/ +/* Lazily builds and caches decoder methods that will push data to the given + * handlers. The destination handlercache must outlive this object. */ + struct upb_pbcodecache; typedef struct upb_pbcodecache upb_pbcodecache; diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h index 780f60f9d1..1113c3ac2d 100644 --- a/upb/pb/encoder.h +++ b/upb/pb/encoder.h @@ -43,6 +43,8 @@ upb_sink upb_pb_encoder_input(upb_pb_encoder *p); upb_pb_encoder* upb_pb_encoder_create(upb_arena* a, const upb_handlers* h, upb_bytessink output); +/* Lazily builds and caches handlers that will push encoded data to a bytessink. + * Any msgdef objects used with this object must outlive it. */ upb_handlercache *upb_pb_encoder_newcache(); #ifdef __cplusplus diff --git a/upb/upb.c b/upb/upb.c index f56f6c4764..d8d27236b8 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -231,7 +231,7 @@ void upb_arena_free(upb_arena *a) { } } -bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud) { +bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func) { cleanup_ent *ent = upb_malloc(&a->alloc, sizeof(cleanup_ent)); if (!ent) { return false; /* Out of memory. */ diff --git a/upb/upb.h b/upb/upb.h index 1c13dd9b3f..fc83902034 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -256,7 +256,7 @@ extern "C" { * is a fixed-size arena and cannot grow. */ upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc); void upb_arena_free(upb_arena *a); -bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud); +bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func); size_t upb_arena_bytesallocated(const upb_arena *a); UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; } @@ -295,8 +295,8 @@ class upb::Arena { /* Add a cleanup function to run when the arena is destroyed. * Returns false on out-of-memory. */ - bool AddCleanup(upb_cleanup_func *func, void *ud) { - return upb_arena_addcleanup(ptr_.get(), func, ud); + bool AddCleanup(void *ud, upb_cleanup_func* func) { + return upb_arena_addcleanup(ptr_.get(), ud, func); } /* Total number of bytes that have been allocated. It is undefined what