diff --git a/upb/message/internal/map_sorter.h b/upb/message/internal/map_sorter.h index 12f7e7a81add..64d6d14d44b7 100644 --- a/upb/message/internal/map_sorter.h +++ b/upb/message/internal/map_sorter.h @@ -12,6 +12,7 @@ #include +#include "upb/mem/alloc.h" #include "upb/message/internal/extension.h" #include "upb/message/internal/map.h" #include "upb/message/internal/map_entry.h" @@ -46,7 +47,7 @@ UPB_INLINE void _upb_mapsorter_init(_upb_mapsorter* s) { } UPB_INLINE void _upb_mapsorter_destroy(_upb_mapsorter* s) { - if (s->entries) free(s->entries); + if (s->entries) upb_gfree(s->entries); } UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s, const upb_Map* map, diff --git a/upb/message/map_sorter.c b/upb/message/map_sorter.c index 42dc4b5ec34e..3536e35f66f6 100644 --- a/upb/message/map_sorter.c +++ b/upb/message/map_sorter.c @@ -7,7 +7,14 @@ #include "upb/message/internal/map_sorter.h" +#include +#include + +#include "upb/base/descriptor_constants.h" #include "upb/base/internal/log2.h" +#include "upb/base/string_view.h" +#include "upb/mem/alloc.h" +#include "upb/message/map.h" #include "upb/mini_table/extension.h" // Must be last. @@ -91,8 +98,10 @@ static bool _upb_mapsorter_resize(_upb_mapsorter* s, _upb_sortedmap* sorted, sorted->end = sorted->start + size; if (sorted->end > s->cap) { + const int oldsize = s->cap * sizeof(*s->entries); s->cap = upb_Log2CeilingSize(sorted->end); - s->entries = realloc(s->entries, s->cap * sizeof(*s->entries)); + const int newsize = s->cap * sizeof(*s->entries); + s->entries = upb_grealloc(s->entries, oldsize, newsize); if (!s->entries) return false; } diff --git a/upb/reflection/def_pool.c b/upb/reflection/def_pool.c index 83759d0a3975..0250d1b0d27a 100644 --- a/upb/reflection/def_pool.c +++ b/upb/reflection/def_pool.c @@ -10,6 +10,8 @@ #include "upb/base/status.h" #include "upb/hash/int_table.h" #include "upb/hash/str_table.h" +#include "upb/mem/alloc.h" +#include "upb/mem/arena.h" #include "upb/reflection/def_type.h" #include "upb/reflection/file_def.h" #include "upb/reflection/internal/def_builder.h" @@ -504,7 +506,7 @@ const upb_FieldDef** upb_DefPool_GetAllExtensions(const upb_DefPool* s, const upb_FieldDef* f = upb_value_getconstptr(val); if (upb_FieldDef_ContainingType(f) == m) n++; } - const upb_FieldDef** exts = malloc(n * sizeof(*exts)); + const upb_FieldDef** exts = upb_gmalloc(n * sizeof(*exts)); iter = UPB_INTTABLE_BEGIN; size_t i = 0; while (upb_inttable_next(&s->exts, &key, &val, &iter)) { diff --git a/upb/reflection/internal/def_builder.c b/upb/reflection/internal/def_builder.c index d6737982caa3..7558c9ebe9e2 100644 --- a/upb/reflection/internal/def_builder.c +++ b/upb/reflection/internal/def_builder.c @@ -10,6 +10,7 @@ #include #include "upb/base/internal/log2.h" +#include "upb/mem/alloc.h" #include "upb/message/copy.h" #include "upb/reflection/def_pool.h" #include "upb/reflection/def_type.h" @@ -116,15 +117,15 @@ const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx, if (sym.size == 0) goto notfound; upb_value v; if (sym.data[0] == '.') { - /* Symbols starting with '.' are absolute, so we do a single lookup. - * Slice to omit the leading '.' */ + // Symbols starting with '.' are absolute, so we do a single lookup. + // Slice to omit the leading '.' if (!_upb_DefPool_LookupSym(ctx->symtab, sym.data + 1, sym.size - 1, &v)) { goto notfound; } } else { - /* Remove components from base until we find an entry or run out. */ + // Remove components from base until we find an entry or run out. size_t baselen = base ? strlen(base) : 0; - char* tmp = malloc(sym.size + baselen + 1); + char* tmp = upb_gmalloc(sym.size + baselen + 1); while (1) { char* p = tmp; if (baselen) { @@ -138,11 +139,11 @@ const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx, break; } if (!remove_component(tmp, &baselen)) { - free(tmp); + upb_gfree(tmp); goto notfound; } } - free(tmp); + upb_gfree(tmp); } *type = _upb_DefType_Type(v); diff --git a/upb/util/BUILD b/upb/util/BUILD index 8efb0b1f68b0..d2279918da5f 100644 --- a/upb/util/BUILD +++ b/upb/util/BUILD @@ -98,6 +98,7 @@ cc_library( visibility = ["//visibility:public"], deps = [ "//upb:base", + "//upb:mem", "//upb:message", "//upb:port", "//upb:reflection", @@ -149,6 +150,7 @@ cc_library( deps = [ "//upb:base", "//upb:eps_copy_input_stream", + "//upb:mem", "//upb:port", "//upb:wire_reader", "//upb:wire_types", diff --git a/upb/util/compare.c b/upb/util/compare.c index 2a774d04e3e4..9d2907597414 100644 --- a/upb/util/compare.c +++ b/upb/util/compare.c @@ -10,6 +10,7 @@ #include #include "upb/base/string_view.h" +#include "upb/mem/alloc.h" #include "upb/wire/eps_copy_input_stream.h" #include "upb/wire/reader.h" #include "upb/wire/types.h" @@ -108,9 +109,11 @@ static void upb_UnknownFields_SortRecursive(upb_UnknownField* arr, size_t start, static void upb_UnknownFields_Sort(upb_UnknownField_Context* ctx, upb_UnknownFields* fields) { if (ctx->tmp_size < fields->size) { + const int oldsize = ctx->tmp_size * sizeof(*ctx->tmp); ctx->tmp_size = UPB_MAX(8, ctx->tmp_size); while (ctx->tmp_size < fields->size) ctx->tmp_size *= 2; - ctx->tmp = realloc(ctx->tmp, ctx->tmp_size * sizeof(*ctx->tmp)); + const int newsize = ctx->tmp_size * sizeof(*ctx->tmp); + ctx->tmp = upb_grealloc(ctx->tmp, oldsize, newsize); } upb_UnknownFields_SortRecursive(fields->fields, 0, fields->size, ctx->tmp); } @@ -261,7 +264,7 @@ static upb_UnknownCompareResult upb_UnknownField_Compare( } upb_Arena_Free(ctx->arena); - free(ctx->tmp); + upb_gfree(ctx->tmp); return ret; } diff --git a/upb/util/required_fields.c b/upb/util/required_fields.c index b93adc0d9958..c3723863c030 100644 --- a/upb/util/required_fields.c +++ b/upb/util/required_fields.c @@ -17,6 +17,7 @@ #include #include "upb/base/descriptor_constants.h" +#include "upb/mem/alloc.h" #include "upb/message/array.h" #include "upb/message/map.h" #include "upb/message/message.h" @@ -168,10 +169,12 @@ static void upb_FieldPathVector_Reserve(upb_FindContext* ctx, upb_FieldPathVector* vec, size_t elems) { if (vec->cap - vec->size < elems) { + const int oldsize = vec->cap * sizeof(*vec->path); size_t need = vec->size + elems; vec->cap = UPB_MAX(4, vec->cap); while (vec->cap < need) vec->cap *= 2; - vec->path = realloc(vec->path, vec->cap * sizeof(*vec->path)); + const int newsize = vec->cap * sizeof(*vec->path); + vec->path = upb_grealloc(vec->path, oldsize, newsize); if (!vec->path) { UPB_LONGJMP(ctx->err, 1); } @@ -289,7 +292,7 @@ bool upb_util_HasUnsetRequired(const upb_Message* msg, const upb_MessageDef* m, upb_FieldPathVector_Init(&ctx.stack); upb_FieldPathVector_Init(&ctx.out_fields); upb_util_FindUnsetRequiredInternal(&ctx, msg, m); - free(ctx.stack.path); + upb_gfree(ctx.stack.path); if (ctx.has_unset_required && fields) { upb_FieldPathVector_Reserve(&ctx, &ctx.out_fields, 1);