Skip to content

Commit

Permalink
upb: use upb_alloc functions internally instead of stdlib malloc/free
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 589864832
  • Loading branch information
ericsalo authored and copybara-github committed Dec 11, 2023
1 parent 1ac8c04 commit a303ccb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
3 changes: 2 additions & 1 deletion upb/message/internal/map_sorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <stdlib.h>

#include "upb/mem/alloc.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/map_entry.h"
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion upb/message/map_sorter.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

#include "upb/message/internal/map_sorter.h"

#include <stdint.h>
#include <string.h>

#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.
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion upb/reflection/def_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)) {
Expand Down
13 changes: 7 additions & 6 deletions upb/reflection/internal/def_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string.h>

#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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions upb/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//upb:base",
"//upb:mem",
"//upb:message",
"//upb:port",
"//upb:reflection",
Expand Down Expand Up @@ -149,6 +150,7 @@ cc_library(
deps = [
"//upb:base",
"//upb:eps_copy_input_stream",
"//upb:mem",
"//upb:port",
"//upb:wire_reader",
"//upb:wire_types",
Expand Down
7 changes: 5 additions & 2 deletions upb/util/compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdlib.h>

#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"
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -261,7 +264,7 @@ static upb_UnknownCompareResult upb_UnknownField_Compare(
}

upb_Arena_Free(ctx->arena);
free(ctx->tmp);
upb_gfree(ctx->tmp);
return ret;
}

Expand Down
7 changes: 5 additions & 2 deletions upb/util/required_fields.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string.h>

#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"
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a303ccb

Please sign in to comment.