Skip to content

Commit a303ccb

Browse files
ericsalocopybara-github
authored andcommitted
upb: use upb_alloc functions internally instead of stdlib malloc/free
PiperOrigin-RevId: 589864832
1 parent 1ac8c04 commit a303ccb

File tree

7 files changed

+34
-13
lines changed

7 files changed

+34
-13
lines changed

upb/message/internal/map_sorter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <stdlib.h>
1414

15+
#include "upb/mem/alloc.h"
1516
#include "upb/message/internal/extension.h"
1617
#include "upb/message/internal/map.h"
1718
#include "upb/message/internal/map_entry.h"
@@ -46,7 +47,7 @@ UPB_INLINE void _upb_mapsorter_init(_upb_mapsorter* s) {
4647
}
4748

4849
UPB_INLINE void _upb_mapsorter_destroy(_upb_mapsorter* s) {
49-
if (s->entries) free(s->entries);
50+
if (s->entries) upb_gfree(s->entries);
5051
}
5152

5253
UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s, const upb_Map* map,

upb/message/map_sorter.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77

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

10+
#include <stdint.h>
11+
#include <string.h>
12+
13+
#include "upb/base/descriptor_constants.h"
1014
#include "upb/base/internal/log2.h"
15+
#include "upb/base/string_view.h"
16+
#include "upb/mem/alloc.h"
17+
#include "upb/message/map.h"
1118
#include "upb/mini_table/extension.h"
1219

1320
// Must be last.
@@ -91,8 +98,10 @@ static bool _upb_mapsorter_resize(_upb_mapsorter* s, _upb_sortedmap* sorted,
9198
sorted->end = sorted->start + size;
9299

93100
if (sorted->end > s->cap) {
101+
const int oldsize = s->cap * sizeof(*s->entries);
94102
s->cap = upb_Log2CeilingSize(sorted->end);
95-
s->entries = realloc(s->entries, s->cap * sizeof(*s->entries));
103+
const int newsize = s->cap * sizeof(*s->entries);
104+
s->entries = upb_grealloc(s->entries, oldsize, newsize);
96105
if (!s->entries) return false;
97106
}
98107

upb/reflection/def_pool.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "upb/base/status.h"
1111
#include "upb/hash/int_table.h"
1212
#include "upb/hash/str_table.h"
13+
#include "upb/mem/alloc.h"
14+
#include "upb/mem/arena.h"
1315
#include "upb/reflection/def_type.h"
1416
#include "upb/reflection/file_def.h"
1517
#include "upb/reflection/internal/def_builder.h"
@@ -504,7 +506,7 @@ const upb_FieldDef** upb_DefPool_GetAllExtensions(const upb_DefPool* s,
504506
const upb_FieldDef* f = upb_value_getconstptr(val);
505507
if (upb_FieldDef_ContainingType(f) == m) n++;
506508
}
507-
const upb_FieldDef** exts = malloc(n * sizeof(*exts));
509+
const upb_FieldDef** exts = upb_gmalloc(n * sizeof(*exts));
508510
iter = UPB_INTTABLE_BEGIN;
509511
size_t i = 0;
510512
while (upb_inttable_next(&s->exts, &key, &val, &iter)) {

upb/reflection/internal/def_builder.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string.h>
1111

1212
#include "upb/base/internal/log2.h"
13+
#include "upb/mem/alloc.h"
1314
#include "upb/message/copy.h"
1415
#include "upb/reflection/def_pool.h"
1516
#include "upb/reflection/def_type.h"
@@ -116,15 +117,15 @@ const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx,
116117
if (sym.size == 0) goto notfound;
117118
upb_value v;
118119
if (sym.data[0] == '.') {
119-
/* Symbols starting with '.' are absolute, so we do a single lookup.
120-
* Slice to omit the leading '.' */
120+
// Symbols starting with '.' are absolute, so we do a single lookup.
121+
// Slice to omit the leading '.'
121122
if (!_upb_DefPool_LookupSym(ctx->symtab, sym.data + 1, sym.size - 1, &v)) {
122123
goto notfound;
123124
}
124125
} else {
125-
/* Remove components from base until we find an entry or run out. */
126+
// Remove components from base until we find an entry or run out.
126127
size_t baselen = base ? strlen(base) : 0;
127-
char* tmp = malloc(sym.size + baselen + 1);
128+
char* tmp = upb_gmalloc(sym.size + baselen + 1);
128129
while (1) {
129130
char* p = tmp;
130131
if (baselen) {
@@ -138,11 +139,11 @@ const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx,
138139
break;
139140
}
140141
if (!remove_component(tmp, &baselen)) {
141-
free(tmp);
142+
upb_gfree(tmp);
142143
goto notfound;
143144
}
144145
}
145-
free(tmp);
146+
upb_gfree(tmp);
146147
}
147148

148149
*type = _upb_DefType_Type(v);

upb/util/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ cc_library(
9898
visibility = ["//visibility:public"],
9999
deps = [
100100
"//upb:base",
101+
"//upb:mem",
101102
"//upb:message",
102103
"//upb:port",
103104
"//upb:reflection",
@@ -149,6 +150,7 @@ cc_library(
149150
deps = [
150151
"//upb:base",
151152
"//upb:eps_copy_input_stream",
153+
"//upb:mem",
152154
"//upb:port",
153155
"//upb:wire_reader",
154156
"//upb:wire_types",

upb/util/compare.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdlib.h>
1111

1212
#include "upb/base/string_view.h"
13+
#include "upb/mem/alloc.h"
1314
#include "upb/wire/eps_copy_input_stream.h"
1415
#include "upb/wire/reader.h"
1516
#include "upb/wire/types.h"
@@ -108,9 +109,11 @@ static void upb_UnknownFields_SortRecursive(upb_UnknownField* arr, size_t start,
108109
static void upb_UnknownFields_Sort(upb_UnknownField_Context* ctx,
109110
upb_UnknownFields* fields) {
110111
if (ctx->tmp_size < fields->size) {
112+
const int oldsize = ctx->tmp_size * sizeof(*ctx->tmp);
111113
ctx->tmp_size = UPB_MAX(8, ctx->tmp_size);
112114
while (ctx->tmp_size < fields->size) ctx->tmp_size *= 2;
113-
ctx->tmp = realloc(ctx->tmp, ctx->tmp_size * sizeof(*ctx->tmp));
115+
const int newsize = ctx->tmp_size * sizeof(*ctx->tmp);
116+
ctx->tmp = upb_grealloc(ctx->tmp, oldsize, newsize);
114117
}
115118
upb_UnknownFields_SortRecursive(fields->fields, 0, fields->size, ctx->tmp);
116119
}
@@ -261,7 +264,7 @@ static upb_UnknownCompareResult upb_UnknownField_Compare(
261264
}
262265

263266
upb_Arena_Free(ctx->arena);
264-
free(ctx->tmp);
267+
upb_gfree(ctx->tmp);
265268
return ret;
266269
}
267270

upb/util/required_fields.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <string.h>
1818

1919
#include "upb/base/descriptor_constants.h"
20+
#include "upb/mem/alloc.h"
2021
#include "upb/message/array.h"
2122
#include "upb/message/map.h"
2223
#include "upb/message/message.h"
@@ -168,10 +169,12 @@ static void upb_FieldPathVector_Reserve(upb_FindContext* ctx,
168169
upb_FieldPathVector* vec,
169170
size_t elems) {
170171
if (vec->cap - vec->size < elems) {
172+
const int oldsize = vec->cap * sizeof(*vec->path);
171173
size_t need = vec->size + elems;
172174
vec->cap = UPB_MAX(4, vec->cap);
173175
while (vec->cap < need) vec->cap *= 2;
174-
vec->path = realloc(vec->path, vec->cap * sizeof(*vec->path));
176+
const int newsize = vec->cap * sizeof(*vec->path);
177+
vec->path = upb_grealloc(vec->path, oldsize, newsize);
175178
if (!vec->path) {
176179
UPB_LONGJMP(ctx->err, 1);
177180
}
@@ -289,7 +292,7 @@ bool upb_util_HasUnsetRequired(const upb_Message* msg, const upb_MessageDef* m,
289292
upb_FieldPathVector_Init(&ctx.stack);
290293
upb_FieldPathVector_Init(&ctx.out_fields);
291294
upb_util_FindUnsetRequiredInternal(&ctx, msg, m);
292-
free(ctx.stack.path);
295+
upb_gfree(ctx.stack.path);
293296

294297
if (ctx.has_unset_required && fields) {
295298
upb_FieldPathVector_Reserve(&ctx, &ctx.out_fields, 1);

0 commit comments

Comments
 (0)