Added some comments and reversed upb_arena_cleanup() args.

pull/13171/head
Joshua Haberman 6 years ago
parent 9349b703a3
commit 2c26f60dbb
  1. 21
      upb/handlers.c
  2. 4
      upb/handlers.h
  3. 3
      upb/json/parser.h
  4. 2
      upb/json/printer.h
  5. 3
      upb/pb/decoder.h
  6. 2
      upb/pb/encoder.h
  7. 2
      upb/upb.c
  8. 6
      upb/upb.h

@ -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 ***********************************************************/

@ -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

@ -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;

@ -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

@ -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;

@ -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

@ -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. */

@ -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

Loading…
Cancel
Save