pull/13171/head
Joshua Haberman 3 years ago
parent 32038c1c59
commit 74bc67bbbc
  1. 2
      upb/upb.h
  2. 5
      upbc/message_layout.cc
  3. 3
      upbc/message_layout.h
  4. 58
      upbc/protoc-gen-upb.cc

@ -230,7 +230,7 @@ UPB_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize,
oldsize = UPB_ALIGN_MALLOC(size);
size = UPB_ALIGN_MALLOC(size);
if (ch_ptr + oldsize == h->ptr) {
if (h->end - ch_ptr >= size) {
if ((size_t)(h->end - ch_ptr) >= size) {
h->ptr = ch_ptr + size;
return ptr;
}

@ -43,8 +43,9 @@ MessageLayout::Size MessageLayout::Place(
offset.AlignUp(size_and_align.align);
size_ = offset;
size_.Add(size_and_align.size);
// maxalign_.MaxFrom(size_and_align.align);
maxalign_.MaxFrom(size_and_align.size);
ABSL_ASSERT(IsPowerOfTwo(size_and_align.align.size32));
ABSL_ASSERT(IsPowerOfTwo(size_and_align.align.size64));
maxalign_.MaxFrom(size_and_align.align);
return offset;
}

@ -116,6 +116,9 @@ class MessageLayout {
static bool IsPowerOfTwo(size_t val) { return (val & (val - 1)) == 0; }
static size_t Align(size_t val, size_t align) {
if (!IsPowerOfTwo(align)) {
fprintf(stderr, "YO! Align is: %d\n", (int)align);
}
ABSL_ASSERT(IsPowerOfTwo(align));
return (val + align - 1) & ~(align - 1);
}

@ -375,26 +375,34 @@ class FilePlatformLayout {
FilePlatformLayout(const protobuf::FileDescriptor* fd,
upb_MiniTablePlatform platform)
: platform_(platform) {
ComputeLayout(fd);
BuildMiniTables(fd);
BuildExtensions(fd);
}
upb_MiniTable* GetTable(const protobuf::Descriptor* m) const {
upb_MiniTable* GetMiniTable(const protobuf::Descriptor* m) const {
auto it = table_map_.find(m);
assert(it != table_map_.end());
return it->second;
}
const upb_MiniTable_Extension* GetExtension(
const protobuf::FieldDescriptor* fd) const {
auto it = extension_map_.find(fd);
assert(it != extension_map_.end());
return it->second;
}
private:
void ComputeLayout(const protobuf::FileDescriptor* fd) {
void BuildMiniTables(const protobuf::FileDescriptor* fd) {
for (const auto& m : SortedMessages(fd)) {
table_map_[m] = MakeMiniTable(m);
}
for (const auto& m : SortedMessages(fd)) {
upb_MiniTable* mt = GetTable(m);
upb_MiniTable* mt = GetMiniTable(m);
for (const auto& f : FieldNumberOrder(m)) {
if (f->message_type() && f->message_type()->file() == f->file()) {
upb_MiniTable_Field* mt_f = FindField(mt, f->number());
upb_MiniTable* sub_mt = GetTable(f->message_type());
upb_MiniTable* sub_mt = GetMiniTable(f->message_type());
upb_MiniTable_SetSubMessage(mt, mt_f, sub_mt);
}
// TODO: proto2 enums.
@ -402,6 +410,34 @@ class FilePlatformLayout {
}
}
void BuildExtensions(const protobuf::FileDescriptor* fd) {
auto sorted = SortedExtensions(fd);
upb::MtDataEncoder e;
e.StartMessage(0);
for (const auto& f : sorted) {
fprintf(stderr, "Field number: %d\n", (int)f->number());
e.PutField(static_cast<upb_FieldType>(f->type()), f->number(),
GetFieldModifiers(f));
}
upb::Status status;
size_t ext_count;
upb_MiniTable_Extension* exts =
upb_MiniTable_BuildExtensions(e.data().data(), e.data().size(),
&ext_count, arena_.ptr(), status.ptr());
if (!exts) {
fprintf(stderr, "Error building mini-table: %s\n", status.error_message());
}
ABSL_ASSERT(exts);
if (ext_count != sorted.size()) {
fprintf(stderr, "ext_count: %zu, sorted.size(): %zu, data(): %s\n", ext_count,
sorted.size(), e.data().c_str());
}
ABSL_ASSERT(ext_count == sorted.size());
for (size_t i = 0; i < ext_count; i++) {
extension_map_[sorted[i]] = &exts[i];
}
}
upb_MiniTable* MakeMiniTable(const protobuf::Descriptor* m) {
if (m->options().message_set_wire_format()) {
return upb_MiniTable_BuildMessageSet(platform_, arena_.ptr());
@ -474,8 +510,11 @@ class FilePlatformLayout {
private:
typedef absl::flat_hash_map<const protobuf::Descriptor*, upb_MiniTable*>
TableMap;
typedef absl::flat_hash_map<const protobuf::FieldDescriptor*, upb_MiniTable_Extension*>
ExtensionMap;
upb::Arena arena_;
TableMap table_map_;
ExtensionMap extension_map_;
upb_MiniTablePlatform platform_;
};
@ -489,11 +528,16 @@ class FileLayout {
const protobuf::FileDescriptor* descriptor() const { return descriptor_; }
const upb_MiniTable* GetMiniTable32(const protobuf::Descriptor* m) const {
return layout32_.GetTable(m);
return layout32_.GetMiniTable(m);
}
const upb_MiniTable* GetMiniTable64(const protobuf::Descriptor* m) const {
return layout64_.GetTable(m);
return layout64_.GetMiniTable(m);
}
const upb_MiniTable_Extension* GetExtension(
const protobuf::FieldDescriptor* f) const {
return layout64_.GetExtension(f);
}
private:

Loading…
Cancel
Save