Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use the global allocator for scratch space when building minitables #20035

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions upb/mini_descriptor/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "upb/base/descriptor_constants.h"
#include "upb/base/status.h"
#include "upb/base/string_view.h"
#include "upb/mem/alloc.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/map_entry.h"
#include "upb/mini_descriptor/internal/base92.h"
Expand Down Expand Up @@ -55,7 +56,7 @@ typedef struct {
typedef struct {
upb_OneOfLayoutItem* data;
size_t size;
size_t capacity;
size_t buf_capacity_bytes;
} upb_OneOfLayoutItemVector;

typedef struct {
Expand Down Expand Up @@ -258,11 +259,13 @@ static void upb_MtDecoder_PushOneof(upb_MtDecoder* d,
if (item.field_index == kUpb_OneOfLayoutItem_IndexSentinel) {
upb_MdDecoder_ErrorJmp(&d->base, "Empty oneof");
}
if (d->oneofs.size == d->oneofs.capacity) {
size_t new_cap = UPB_MAX(8, d->oneofs.size * 2);
d->oneofs.data = realloc(d->oneofs.data, new_cap * sizeof(*d->oneofs.data));
if ((d->oneofs.size + 1) * sizeof(*d->oneofs.data) >
d->oneofs.buf_capacity_bytes) {
size_t new_cap = UPB_MAX(8, d->oneofs.size * 2) * sizeof(*d->oneofs.data);
d->oneofs.data =
upb_grealloc(d->oneofs.data, d->oneofs.buf_capacity_bytes, new_cap);
upb_MdDecoder_CheckOutOfMemory(&d->base, d->oneofs.data);
d->oneofs.capacity = new_cap;
d->oneofs.buf_capacity_bytes = new_cap;
}
item.field_index -= kOneofBase;

Expand Down Expand Up @@ -733,7 +736,7 @@ static upb_MiniTable* upb_MtDecoder_DoBuildMiniTableWithBuf(

done:
*buf = decoder->oneofs.data;
*buf_size = decoder->oneofs.capacity * sizeof(*decoder->oneofs.data);
*buf_size = decoder->oneofs.buf_capacity_bytes;
return decoder->table;
}

Expand All @@ -742,7 +745,7 @@ static upb_MiniTable* upb_MtDecoder_BuildMiniTableWithBuf(
void** const buf, size_t* const buf_size) {
if (UPB_SETJMP(decoder->base.err) != 0) {
*buf = decoder->oneofs.data;
*buf_size = decoder->oneofs.capacity * sizeof(*decoder->oneofs.data);
*buf_size = decoder->oneofs.buf_capacity_bytes;
return NULL;
}

Expand All @@ -761,7 +764,7 @@ upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len,
.oneofs =
{
.data = *buf,
.capacity = *buf_size / sizeof(*decoder.oneofs.data),
.buf_capacity_bytes = *buf_size,
.size = 0,
},
.arena = arena,
Expand Down Expand Up @@ -859,6 +862,6 @@ upb_MiniTable* _upb_MiniTable_Build(const char* data, size_t len,
size_t size = 0;
upb_MiniTable* ret = upb_MiniTable_BuildWithBuf(data, len, platform, arena,
&buf, &size, status);
free(buf);
upb_gfree(buf);
return ret;
}
7 changes: 4 additions & 3 deletions upb/mini_descriptor/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildEnum(
}

// Like upb_MiniTable_Build(), but the user provides a buffer of layout data so
// it can be reused from call to call, avoiding repeated realloc()/free().
// it can be reused from call to call, avoiding repeated
// upb_grealloc()/upb_gfree().
//
// The caller owns `*buf` both before and after the call, and must free() it
// when it is no longer in use. The function will realloc() `*buf` as
// The caller owns `*buf` both before and after the call, and must upb_gfree()
// it when it is no longer in use. The function will upb_grealloc() `*buf` as
// necessary, updating `*size` accordingly.
upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len,
upb_MiniTablePlatform platform,
Expand Down
Loading