Skip to content

Commit

Permalink
Always use the global allocator for scratch space when building minit…
Browse files Browse the repository at this point in the history
…ables

PiperOrigin-RevId: 718469089
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 22, 2025
1 parent 1ebba0d commit 1f42fcb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
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

0 comments on commit 1f42fcb

Please sign in to comment.