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/parser.rl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'upb/json/parser.rl') diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 3e1ea94..b67f08d 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -19,12 +19,11 @@ ** - handling of keys/escape-sequences/etc that span input buffers. */ -#include -#include #include -#include -#include #include +#include +#include +#include #include "upb/json/parser.h" @@ -1332,12 +1331,12 @@ static void free_json_parsermethod(upb_refcounted *r) { upb_value val = upb_inttable_iter_value(&i); upb_strtable *t = upb_value_getptr(val); upb_strtable_uninit(t); - free(t); + upb_gfree(t); } upb_inttable_uninit(&method->name_tables); - free(r); + upb_gfree(r); } static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { @@ -1354,7 +1353,7 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { } /* TODO(haberman): handle malloc failure. */ - t = malloc(sizeof(*t)); + t = upb_gmalloc(sizeof(*t)); upb_strtable_init(t, UPB_CTYPE_CONSTPTR); upb_inttable_insertptr(&m->name_tables, md, upb_value_ptr(t)); @@ -1367,7 +1366,7 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { size_t field_len = upb_fielddef_getjsonname(f, buf, len); if (field_len > len) { size_t len2; - buf = realloc(buf, field_len); + buf = upb_grealloc(buf, 0, field_len); len = field_len; len2 = upb_fielddef_getjsonname(f, buf, len); UPB_ASSERT_VAR(len2, len == len2); @@ -1386,7 +1385,7 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { } } - free(buf); + upb_gfree(buf); } /* Public API *****************************************************************/ @@ -1426,7 +1425,7 @@ upb_json_parsermethod *upb_json_parsermethod_new(const upb_msgdef* md, const void* owner) { static const struct upb_refcounted_vtbl vtbl = {visit_json_parsermethod, free_json_parsermethod}; - upb_json_parsermethod *ret = malloc(sizeof(*ret)); + upb_json_parsermethod *ret = upb_gmalloc(sizeof(*ret)); upb_refcounted_init(upb_json_parsermethod_upcast_mutable(ret), &vtbl, owner); ret->msg = md; -- cgit v1.2.3