From 68bc62a7fa5febbf5c8ab2fe8f6171121d18690f Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 19 Apr 2016 14:57:31 -0700 Subject: 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. --- upb/json/printer.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'upb/json/printer.c') diff --git a/upb/json/printer.c b/upb/json/printer.c index 230aa0d..cc50417 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -5,8 +5,6 @@ #include "upb/json/printer.h" -#include -#include #include #include @@ -39,22 +37,22 @@ typedef struct { void freestrpc(void *ptr) { strpc *pc = ptr; - free(pc->ptr); - free(pc); + upb_gfree(pc->ptr); + upb_gfree(pc); } /* Convert fielddef name to JSON name and return as a string piece. */ strpc *newstrpc(upb_handlers *h, const upb_fielddef *f, bool preserve_fieldnames) { /* TODO(haberman): handle malloc failure. */ - strpc *ret = malloc(sizeof(*ret)); + strpc *ret = upb_gmalloc(sizeof(*ret)); if (preserve_fieldnames) { - ret->ptr = upb_strdup(upb_fielddef_name(f)); + ret->ptr = upb_gstrdup(upb_fielddef_name(f)); ret->len = strlen(ret->ptr); } else { size_t len; ret->len = upb_fielddef_getjsonname(f, NULL, 0); - ret->ptr = malloc(ret->len); + ret->ptr = upb_gmalloc(ret->len); len = upb_fielddef_getjsonname(f, ret->ptr, ret->len); UPB_ASSERT_VAR(len, len == ret->len); ret->len--; /* NULL */ @@ -566,10 +564,10 @@ static void set_enum_hd(upb_handlers *h, const upb_fielddef *f, bool preserve_fieldnames, upb_handlerattr *attr) { - EnumHandlerData *hd = malloc(sizeof(EnumHandlerData)); + EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData)); hd->enumdef = (const upb_enumdef *)upb_fielddef_subdef(f); hd->keyname = newstrpc(h, f, preserve_fieldnames); - upb_handlers_addcleanup(h, hd, free); + upb_handlers_addcleanup(h, hd, upb_gfree); upb_handlerattr_sethandlerdata(attr, hd); } -- cgit v1.2.3