Skip to content

Commit

Permalink
split apart mini_table.c into a new subdir
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 484352293
  • Loading branch information
ericsalo authored and copybara-github committed Oct 27, 2022
1 parent 6c4dc6f commit c033eff
Show file tree
Hide file tree
Showing 21 changed files with 813 additions and 648 deletions.
31 changes: 23 additions & 8 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,13 @@ cc_library(

cc_library(
name = "mini_table_internal",
hdrs = [
srcs = [
"upb/mini_table/common.h",
"upb/msg_internal.h",
],
hdrs = [
"upb/mini_table/common_internal.h",
],
visibility = ["//:__subpackages__"],
deps = [
":extension_registry",
Expand All @@ -189,30 +193,39 @@ cc_library(
cc_library(
name = "mini_table",
srcs = [
"upb/mini_table.c",
"upb/mini_table/common.c",
"upb/mini_table/common.h",
"upb/mini_table/common_internal.h",
"upb/mini_table/decode.c",
"upb/mini_table/encode.c",
"upb/msg_internal.h",
],
hdrs = [
"upb/mini_table.h",
"upb/mini_table.hpp",
"upb/mini_table/decode.h",
"upb/mini_table/encode.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":extension_registry",
":mini_table_internal",
":port",
":table_internal",
":upb",
],
)

cc_library(
name = "mini_table_accessors",
srcs = [
"upb/internal/mini_table_accessors.h",
"upb/mini_table_accessors.c",
"upb/mini_table/accessors.c",
"upb/mini_table/common.h",
"upb/msg_internal.h",
],
hdrs = [
"upb/mini_table_accessors.h",
"upb/mini_table/accessors.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
Expand All @@ -222,15 +235,16 @@ cc_library(
":mini_table",
":mini_table_internal",
":port",
":table_internal",
":upb",
],
)

cc_test(
name = "mini_table_test",
name = "mini_table_encode_test",
srcs = [
"upb/internal/table.h",
"upb/mini_table_test.cc",
"upb/mini_table/encode_test.cc",
],
deps = [
":extension_registry",
Expand All @@ -246,7 +260,7 @@ cc_test(

cc_test(
name = "mini_table_accessors_test",
srcs = ["upb/mini_table_accessors_test.cc"],
srcs = ["upb/mini_table/accessors_test.cc"],
deps = [
":collections",
":mini_table",
Expand Down Expand Up @@ -920,6 +934,7 @@ cc_library(
hdrs = ["upb/fuzz_test_util.h"],
deps = [
":mini_table",
":mini_table_internal",
":upb",
],
)
Expand Down
173 changes: 4 additions & 169 deletions upb/mini_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,177 +25,12 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// This header is deprecated, use the header(s) below instead.

#ifndef UPB_MINI_TABLE_H_
#define UPB_MINI_TABLE_H_

#include "upb/msg_internal.h"

// Must be last.
#include "upb/port_def.inc"

#ifdef __cplusplus
extern "C" {
#endif

const upb_MiniTable_Field* upb_MiniTable_FindFieldByNumber(
const upb_MiniTable* table, uint32_t number);

upb_FieldType upb_MiniTableField_Type(const upb_MiniTable_Field* field);

UPB_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable(
const upb_MiniTable* mini_table, const upb_MiniTable_Field* field) {
return mini_table->subs[field->submsg_index].submsg;
}

UPB_INLINE const upb_MiniTable_Enum* upb_MiniTable_GetSubEnumTable(
const upb_MiniTable* mini_table, const upb_MiniTable_Field* field) {
return mini_table->subs[field->submsg_index].subenum;
}

/** upb_MtDataEncoder *********************************************************/

// Functions to encode a string in a format that can be loaded by
// upb_MiniTable_Build().

typedef enum {
kUpb_MessageModifier_ValidateUtf8 = 1 << 0,
kUpb_MessageModifier_DefaultIsPacked = 1 << 1,
kUpb_MessageModifier_IsExtendable = 1 << 2,
} kUpb_MessageModifier;

typedef enum {
kUpb_FieldModifier_IsRepeated = 1 << 0,
kUpb_FieldModifier_IsPacked = 1 << 1,
kUpb_FieldModifier_IsClosedEnum = 1 << 2,
kUpb_FieldModifier_IsProto3Singular = 1 << 3,
kUpb_FieldModifier_IsRequired = 1 << 4,
} kUpb_FieldModifier;

typedef struct {
char* end; // Limit of the buffer passed as a parameter.
// Aliased to internal-only members in .cc.
char internal[32];
} upb_MtDataEncoder;

// If the input buffer has at least this many bytes available, the encoder call
// is guaranteed to succeed (as long as field number order is maintained).
#define kUpb_MtDataEncoder_MinSize 16

// Encodes field/oneof information for a given message. The sequence of calls
// should look like:
//
// upb_MtDataEncoder e;
// char buf[256];
// char* ptr = buf;
// e.end = ptr + sizeof(buf);
// unit64_t msg_mod = ...; // bitwise & of kUpb_MessageModifiers or zero
// ptr = upb_MtDataEncoder_StartMessage(&e, ptr, msg_mod);
// // Fields *must* be in field number order.
// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
//
// // If oneofs are present. Oneofs must be encoded after regular fields.
// ptr = upb_MiniTable_StartOneof(&e, ptr)
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
//
// ptr = upb_MiniTable_StartOneof(&e, ptr);
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
//
// Oneofs must be encoded after all regular fields.
char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr,
uint64_t msg_mod);
char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod);
char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr);
char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr,
uint32_t field_num);

// Encodes the set of values for a given enum. The values must be given in
// order (after casting to uint32_t), and repeats are not allowed.
char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr);
char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr,
uint32_t val);
char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr);

// Encodes an entire mini descriptor for an extension.
char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod);

// Encodes an entire mini descriptor for a map.
char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr,
upb_FieldType key_type,
upb_FieldType value_type, uint64_t value_mod);

// Encodes an entire mini descriptor for a message set.
char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr);

/** upb_MiniTable *************************************************************/

typedef enum {
kUpb_MiniTablePlatform_32Bit,
kUpb_MiniTablePlatform_64Bit,
kUpb_MiniTablePlatform_Native =
UPB_SIZE(kUpb_MiniTablePlatform_32Bit, kUpb_MiniTablePlatform_64Bit),
} upb_MiniTablePlatform;

// Builds a mini table from the data encoded in the buffer [data, len]. If any
// errors occur, returns NULL and sets a status message. In the success case,
// the caller must call upb_MiniTable_SetSub*() for all message or proto2 enum
// fields to link the table to the appropriate sub-tables.
upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len,
upb_MiniTablePlatform platform,
upb_Arena* arena, upb_Status* status);

// Links a sub-message field to a MiniTable for that sub-message. If a
// sub-message field is not linked, it will be treated as an unknown field
// during parsing, and setting the field will not be allowed. It is possible
// to link the message field later, at which point it will no longer be treated
// as unknown. However there is no synchronization for this operation, which
// means parallel mutation requires external synchronization.
void upb_MiniTable_SetSubMessage(upb_MiniTable* table,
upb_MiniTable_Field* field,
const upb_MiniTable* sub);

// Links an enum field to a MiniTable for that enum. All enum fields must
// be linked prior to parsing.
void upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTable_Field* field,
const upb_MiniTable_Enum* sub);

const char* upb_MiniTable_BuildExtension(const char* data, size_t len,
upb_MiniTable_Extension* ext,
const upb_MiniTable* extendee,
upb_MiniTable_Sub sub,
upb_Status* status);

upb_MiniTable_Enum* upb_MiniTable_BuildEnum(const char* data, size_t len,
upb_Arena* arena,
upb_Status* status);

// 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().
//
// 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
// necessary, updating `*size` accordingly.
upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len,
upb_MiniTablePlatform platform,
upb_Arena* arena, void** buf,
size_t* buf_size, upb_Status* status);

// For testing only.
char upb_ToBase92(int8_t ch);
char upb_FromBase92(uint8_t ch);
bool upb_IsTypePackable(upb_FieldType type);

#ifdef __cplusplus
} /* extern "C" */
#endif

#include "upb/port_undef.inc"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode.h"

#endif /* UPB_MINI_TABLE_H_ */
5 changes: 2 additions & 3 deletions upb/mini_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
#ifndef UPB_MINI_TABLE_HPP_
#define UPB_MINI_TABLE_HPP_

#include <assert.h>

#include <string>

#include "upb/mini_table.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode.h"

namespace upb {

Expand Down
11 changes: 5 additions & 6 deletions upb/mini_table_accessors.c → upb/mini_table/accessors.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "upb/mini_table_accessors.h"
#include "upb/mini_table/accessors.h"

#include "upb/decode.h"
#include "upb/encode.h"
#include "upb/internal/array.h"
#include "upb/mini_table.h"

// Must be last.
#include "upb/msg.h"
#include "upb/port_def.inc"

size_t upb_MiniTable_Field_GetSize(const upb_MiniTable_Field* f) {
static size_t _upb_MiniTable_Field_GetSize(const upb_MiniTable_Field* f) {
static unsigned char sizes[] = {
0, /* 0 */
8, /* kUpb_FieldType_Double */
Expand All @@ -62,7 +61,7 @@ size_t upb_MiniTable_Field_GetSize(const upb_MiniTable_Field* f) {
}

// Maps descriptor type to elem_size_lg2.
int upb_MiniTable_Field_CTypeLg2Size(const upb_MiniTable_Field* f) {
static int _upb_MiniTable_Field_CTypeLg2Size(const upb_MiniTable_Field* f) {
static const uint8_t sizes[] = {
-1, /* invalid descriptor type */
3, /* DOUBLE */
Expand Down Expand Up @@ -110,14 +109,14 @@ void upb_MiniTable_ClearField(upb_Message* msg,
if (*oneof_case != field->number) return;
*oneof_case = 0;
}
memset(mem, 0, upb_MiniTable_Field_GetSize(field));
memset(mem, 0, _upb_MiniTable_Field_GetSize(field));
}

void* upb_MiniTable_ResizeArray(upb_Message* msg,
const upb_MiniTable_Field* field, size_t len,
upb_Arena* arena) {
return _upb_Array_Resize_accessor2(
msg, field->offset, len, upb_MiniTable_Field_CTypeLg2Size(field), arena);
msg, field->offset, len, _upb_MiniTable_Field_CTypeLg2Size(field), arena);
}

typedef struct {
Expand Down
18 changes: 16 additions & 2 deletions upb/mini_table_accessors.h → upb/mini_table/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
#define UPB_MINI_TABLE_ACCESSORS_H_

#include "upb/array.h"
#include "upb/internal/mini_table_accessors.h"
#include "upb/mini_table.h"
#include "upb/mini_table/common.h"

// Must be last.
#include "upb/port_def.inc"
Expand All @@ -39,6 +38,21 @@
extern "C" {
#endif

UPB_INLINE bool _upb_MiniTable_Field_InOneOf(const upb_MiniTable_Field* field) {
return field->presence < 0;
}

UPB_INLINE void _upb_MiniTable_SetPresence(upb_Message* msg,
const upb_MiniTable_Field* field) {
if (field->presence > 0) {
_upb_sethas_field(msg, field);
} else if (_upb_MiniTable_Field_InOneOf(field)) {
*_upb_oneofcase_field(msg, field) = field->number;
}
}

// EVERYTHING ABOVE THIS LINE IS INTERNAL - DO NOT USE /////////////////////////

bool upb_MiniTable_HasField(const upb_Message* msg,
const upb_MiniTable_Field* field);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
* accessed through reflective APIs exposed through mini table accessors.
*/

#include "upb/mini_table_accessors.h"
#include "upb/mini_table/accessors.h"

#include "gtest/gtest.h"
#include "google/protobuf/test_messages_proto2.upb.h"
#include "google/protobuf/test_messages_proto3.upb.h"
#include "upb/array.h"
#include "upb/decode.h"
#include "upb/mini_table.h"
#include "upb/msg_internal.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode.h"
#include "upb/test.upb.h"
#include "upb/upb.h"

Expand Down Expand Up @@ -522,7 +522,7 @@ TEST(GeneratedCode, PromoteUnknownMessage) {
&serialized_size);

upb_MiniTable* mini_table = CreateMiniTableWithEmptySubTables(arena);
upb_Message* msg = upb_Message_New(mini_table, arena);
upb_Message* msg = _upb_Message_New(mini_table, arena);
upb_DecodeStatus decode_status = upb_Decode(serialized, serialized_size, msg,
mini_table, nullptr, 0, arena);
EXPECT_EQ(decode_status, kUpb_DecodeStatus_Ok);
Expand Down
Loading

0 comments on commit c033eff

Please sign in to comment.