summaryrefslogtreecommitdiff
path: root/upb/pb
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/pb
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/pb')
-rw-r--r--upb/pb/compile_decoder.c17
-rw-r--r--upb/pb/decoder.h3
-rw-r--r--upb/pb/decoder.int.h1
-rw-r--r--upb/pb/encoder.c5
-rw-r--r--upb/pb/encoder.h1
-rw-r--r--upb/pb/glue.c5
-rw-r--r--upb/pb/glue.h3
-rw-r--r--upb/pb/textprinter.c5
-rw-r--r--upb/pb/textprinter.h1
9 files changed, 17 insertions, 24 deletions
diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c
index a46e644..7724113 100644
--- a/upb/pb/compile_decoder.c
+++ b/upb/pb/compile_decoder.c
@@ -31,8 +31,8 @@ static void freegroup(upb_refcounted *r) {
#ifdef UPB_USE_JIT_X64
upb_pbdecoder_freejit(g);
#endif
- free(g->bytecode);
- free(g);
+ upb_gfree(g->bytecode);
+ upb_gfree(g);
}
static void visitgroup(const upb_refcounted *r, upb_refcounted_visit *visit,
@@ -47,7 +47,7 @@ static void visitgroup(const upb_refcounted *r, upb_refcounted_visit *visit,
}
mgroup *newgroup(const void *owner) {
- mgroup *g = malloc(sizeof(*g));
+ mgroup *g = upb_gmalloc(sizeof(*g));
static const struct upb_refcounted_vtbl vtbl = {visitgroup, freegroup};
upb_refcounted_init(mgroup_upcast_mutable(g), &vtbl, owner);
upb_inttable_init(&g->methods, UPB_CTYPE_PTR);
@@ -67,7 +67,7 @@ static void freemethod(upb_refcounted *r) {
}
upb_inttable_uninit(&method->dispatch);
- free(method);
+ upb_gfree(method);
}
static void visitmethod(const upb_refcounted *r, upb_refcounted_visit *visit,
@@ -79,7 +79,7 @@ static void visitmethod(const upb_refcounted *r, upb_refcounted_visit *visit,
static upb_pbdecodermethod *newmethod(const upb_handlers *dest_handlers,
mgroup *group) {
static const struct upb_refcounted_vtbl vtbl = {visitmethod, freemethod};
- upb_pbdecodermethod *ret = malloc(sizeof(*ret));
+ upb_pbdecodermethod *ret = upb_gmalloc(sizeof(*ret));
upb_refcounted_init(upb_pbdecodermethod_upcast_mutable(ret), &vtbl, &ret);
upb_byteshandler_init(&ret->input_handler_);
@@ -142,7 +142,7 @@ typedef struct {
} compiler;
static compiler *newcompiler(mgroup *group, bool lazy) {
- compiler *ret = malloc(sizeof(*ret));
+ compiler *ret = upb_gmalloc(sizeof(*ret));
int i;
ret->group = group;
@@ -155,7 +155,7 @@ static compiler *newcompiler(mgroup *group, bool lazy) {
}
static void freecompiler(compiler *c) {
- free(c);
+ upb_gfree(c);
}
const size_t ptr_words = sizeof(void*) / sizeof(uint32_t);
@@ -259,7 +259,8 @@ static void put32(compiler *c, uint32_t v) {
size_t oldsize = g->bytecode_end - g->bytecode;
size_t newsize = UPB_MAX(oldsize * 2, 64);
/* TODO(haberman): handle OOM. */
- g->bytecode = realloc(g->bytecode, newsize * sizeof(uint32_t));
+ g->bytecode = upb_grealloc(g->bytecode, oldsize * sizeof(uint32_t),
+ newsize * sizeof(uint32_t));
g->bytecode_end = g->bytecode + newsize;
c->pc = g->bytecode + ofs;
}
diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h
index 8172a99..7c1877a 100644
--- a/upb/pb/decoder.h
+++ b/upb/pb/decoder.h
@@ -15,7 +15,6 @@
#ifndef UPB_DECODER_H_
#define UPB_DECODER_H_
-#include "upb/env.h"
#include "upb/sink.h"
#ifdef __cplusplus
@@ -99,7 +98,7 @@ class upb::pb::DecoderMethod {
* constructed. This hint may be an overestimate for some build configurations.
* But if the decoder library is upgraded without recompiling the application,
* it may be an underestimate. */
-#define UPB_PB_DECODER_SIZE 4408
+#define UPB_PB_DECODER_SIZE 4416
#ifdef __cplusplus
diff --git a/upb/pb/decoder.int.h b/upb/pb/decoder.int.h
index f2bf242..4032570 100644
--- a/upb/pb/decoder.int.h
+++ b/upb/pb/decoder.int.h
@@ -5,7 +5,6 @@
#ifndef UPB_DECODER_INT_H_
#define UPB_DECODER_INT_H_
-#include <stdlib.h>
#include "upb/def.h"
#include "upb/handlers.h"
#include "upb/pb/decoder.h"
diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c
index cf4df9e..8f974a3 100644
--- a/upb/pb/encoder.c
+++ b/upb/pb/encoder.c
@@ -57,7 +57,6 @@
#include "upb/pb/encoder.h"
#include "upb/pb/varint.int.h"
-#include <stdlib.h>
/* The output buffer is divided into segments; a segment is a string of data
* that is "ready to go" -- it does not need any varint lengths inserted into
@@ -302,12 +301,12 @@ static void new_tag(upb_handlers *h, const upb_fielddef *f, upb_wiretype_t wt,
upb_handlerattr *attr) {
uint32_t n = upb_fielddef_number(f);
- tag_t *tag = malloc(sizeof(tag_t));
+ tag_t *tag = upb_gmalloc(sizeof(tag_t));
tag->bytes = upb_vencode64((n << 3) | wt, tag->tag);
upb_handlerattr_init(attr);
upb_handlerattr_sethandlerdata(attr, tag);
- upb_handlers_addcleanup(h, tag, free);
+ upb_handlers_addcleanup(h, tag, upb_gfree);
}
static bool encode_tag(upb_pb_encoder *e, const tag_t *tag) {
diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h
index e8f7425..41b7e7b 100644
--- a/upb/pb/encoder.h
+++ b/upb/pb/encoder.h
@@ -12,7 +12,6 @@
#ifndef UPB_ENCODER_H_
#define UPB_ENCODER_H_
-#include "upb/env.h"
#include "upb/sink.h"
#ifdef __cplusplus
diff --git a/upb/pb/glue.c b/upb/pb/glue.c
index 5583f6e..fb2b769 100644
--- a/upb/pb/glue.c
+++ b/upb/pb/glue.c
@@ -1,9 +1,6 @@
#include "upb/pb/glue.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
#include "upb/descriptor/reader.h"
#include "upb/pb/decoder.h"
@@ -36,7 +33,7 @@ upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner,
goto cleanup;
}
- ret = malloc(sizeof (*ret) * (upb_descreader_filecount(reader) + 1));
+ ret = upb_gmalloc(sizeof (*ret) * (upb_descreader_filecount(reader) + 1));
if (!ret) {
goto cleanup;
diff --git a/upb/pb/glue.h b/upb/pb/glue.h
index 014562b..8f570bc 100644
--- a/upb/pb/glue.h
+++ b/upb/pb/glue.h
@@ -31,7 +31,8 @@ extern "C" {
#endif
/* Loads a binary descriptor and returns a NULL-terminated array of unfrozen
- * filedefs. The caller owns the returned array. */
+ * filedefs. The caller owns the returned array, which must be freed with
+ * upb_gfree(). */
upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner,
upb_status *status);
diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c
index 3785d83..b9ff8d4 100644
--- a/upb/pb/textprinter.c
+++ b/upb/pb/textprinter.c
@@ -12,7 +12,6 @@
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include "upb/sink.h"
@@ -109,14 +108,14 @@ bool putf(upb_textprinter *p, const char *fmt, ...) {
va_end(args_copy);
/* + 1 for NULL terminator (vsprintf() requires it even if we don't). */
- str = malloc(len + 1);
+ str = upb_gmalloc(len + 1);
if (!str) return false;
written = vsprintf(str, fmt, args);
va_end(args);
UPB_ASSERT_VAR(written, written == len);
ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL);
- free(str);
+ upb_gfree(str);
return ok;
}
diff --git a/upb/pb/textprinter.h b/upb/pb/textprinter.h
index b6ad9c5..2f40ed8 100644
--- a/upb/pb/textprinter.h
+++ b/upb/pb/textprinter.h
@@ -7,7 +7,6 @@
#ifndef UPB_TEXT_H_
#define UPB_TEXT_H_
-#include "upb/env.h"
#include "upb/sink.h"
#ifdef __cplusplus
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback