summaryrefslogtreecommitdiff
path: root/upb/handlers.c
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2016-04-19 14:57:31 -0700
committerJoshua Haberman <jhaberman@gmail.com>2016-04-19 14:57:31 -0700
commit68bc62a7fa5febbf5c8ab2fe8f6171121d18690f (patch)
treecd40e0a0151a977627559b1fbbbac9250c8bba64 /upb/handlers.c
parent04786dc2b3c68c8449b19fa2d12bd929f9813155 (diff)
Split upb::Arena/upb::Allocator from upb::Environment. (#58)
* Split upb::Arena/upb::Allocator from upb::Environment. This will allow arenas and allocators to be used independently of environments, which will be important for an upcoming change (a message representation). Overall this design feels cleaner that the previous Environment/SeededAllocator design. As part of this change, moved all allocations in upb to use a global allocator instead of hard-coding malloc/free. This will allow injecting OOM faults for more robust testing. One place that doesn't use the global allocator is the tracked ref code. Instead of its previous approach of CHECK_OOM() after every malloc() or table insert, it simply uses an allocator that does this automatically. I moved Allocator/Arena/Environment into upb.h. This seems principled since these are the only types in upb whose size is directly exposed to users, since they form the basis of memory allocation strategy. * Cleaned up some header includes and fixed more malloc -> upb_gmalloc(). * Changes from PR review. * Don't use UINTPTR_MAX or UINT64_MAX. * Punt on adding line/file for now. * We actually can't store (uint64_t)-1, update comment and test.
Diffstat (limited to 'upb/handlers.c')
-rw-r--r--upb/handlers.c24
1 files changed, 18 insertions, 6 deletions
diff --git a/upb/handlers.c b/upb/handlers.c
index ead0403..055eadf 100644
--- a/upb/handlers.c
+++ b/upb/handlers.c
@@ -6,11 +6,17 @@
#include "upb/handlers.h"
#include "upb/structdefs.int.h"
-#include <stdlib.h>
#include <string.h>
#include "upb/sink.h"
+static void *upb_calloc(size_t size) {
+ void *mem = upb_gmalloc(size);
+ if (mem) {
+ memset(mem, 0, size);
+ }
+ return mem;
+}
/* Defined for the sole purpose of having a unique pointer value for
* UPB_NO_CLOSURE. */
@@ -30,8 +36,8 @@ static void freehandlers(upb_refcounted *r) {
upb_inttable_uninit(&h->cleanup_);
upb_msgdef_unref(h->msg, h);
- free(h->sub);
- free(h);
+ upb_gfree(h->sub);
+ upb_gfree(h);
}
static void visithandlers(const upb_refcounted *r, upb_refcounted_visit *visit,
@@ -281,14 +287,20 @@ upb_handlers *upb_handlers_new(const upb_msgdef *md, const void *owner) {
assert(upb_msgdef_isfrozen(md));
extra = sizeof(upb_handlers_tabent) * (md->selector_count - 1);
- h = calloc(sizeof(*h) + extra, 1);
+ h = upb_calloc(sizeof(*h) + extra);
if (!h) return NULL;
h->msg = md;
upb_msgdef_ref(h->msg, h);
upb_status_clear(&h->status_);
- h->sub = calloc(md->submsg_field_count, sizeof(*h->sub));
- if (!h->sub) goto oom;
+
+ if (md->submsg_field_count > 0) {
+ h->sub = upb_calloc(md->submsg_field_count * sizeof(*h->sub));
+ if (!h->sub) goto oom;
+ } else {
+ h->sub = 0;
+ }
+
if (!upb_refcounted_init(upb_handlers_upcast_mutable(h), &vtbl, owner))
goto oom;
if (!upb_inttable_init(&h->cleanup_, UPB_CTYPE_FPTR)) goto oom;
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback