Commit 917f459e authored by Kenton Varda's avatar Kenton Varda

Mass rename 'reference' -> 'pointer'. Been meaning to do this for a while --…

Mass rename 'reference' -> 'pointer'.  Been meaning to do this for a while -- all the documentation talks about 'pointers', not 'references'.
parent e550bee1
......@@ -77,12 +77,12 @@ internal::FieldSize elementSizeFor(schema::Type::Body::Which elementType) {
case schema::Type::Body::FLOAT32_TYPE: return internal::FieldSize::FOUR_BYTES;
case schema::Type::Body::FLOAT64_TYPE: return internal::FieldSize::EIGHT_BYTES;
case schema::Type::Body::TEXT_TYPE: return internal::FieldSize::REFERENCE;
case schema::Type::Body::DATA_TYPE: return internal::FieldSize::REFERENCE;
case schema::Type::Body::LIST_TYPE: return internal::FieldSize::REFERENCE;
case schema::Type::Body::TEXT_TYPE: return internal::FieldSize::POINTER;
case schema::Type::Body::DATA_TYPE: return internal::FieldSize::POINTER;
case schema::Type::Body::LIST_TYPE: return internal::FieldSize::POINTER;
case schema::Type::Body::ENUM_TYPE: return internal::FieldSize::TWO_BYTES;
case schema::Type::Body::STRUCT_TYPE: return internal::FieldSize::INLINE_COMPOSITE;
case schema::Type::Body::INTERFACE_TYPE: return internal::FieldSize::REFERENCE;
case schema::Type::Body::INTERFACE_TYPE: return internal::FieldSize::POINTER;
case schema::Type::Body::OBJECT_TYPE: FAIL_CHECK("List(Object) not supported."); break;
}
FAIL_CHECK("Can't get here.");
......@@ -93,7 +93,7 @@ inline internal::StructSize structSizeFromSchema(StructSchema schema) {
auto node = schema.getProto().getBody().getStructNode();
return internal::StructSize(
node.getDataSectionWordSize() * WORDS,
node.getPointerSectionSize() * REFERENCES,
node.getPointerSectionSize() * POINTERS,
static_cast<internal::FieldSize>(node.getPreferredListEncoding()));
}
......@@ -561,14 +561,14 @@ DynamicValue::Reader DynamicStruct::Reader::getImpl(
case schema::Type::Body::TEXT_TYPE: {
Text::Reader typedDval = dval.getTextValue();
return DynamicValue::Reader(
reader.getBlobField<Text>(field.getOffset() * REFERENCES,
reader.getBlobField<Text>(field.getOffset() * POINTERS,
typedDval.data(), typedDval.size() * BYTES));
}
case schema::Type::Body::DATA_TYPE: {
Data::Reader typedDval = dval.getDataValue();
return DynamicValue::Reader(
reader.getBlobField<Data>(field.getOffset() * REFERENCES,
reader.getBlobField<Data>(field.getOffset() * POINTERS,
typedDval.data(), typedDval.size() * BYTES));
}
......@@ -576,7 +576,7 @@ DynamicValue::Reader DynamicStruct::Reader::getImpl(
auto elementType = type.getListType();
return DynamicValue::Reader(DynamicList::Reader(
ListSchema::of(elementType, member.getContainingStruct()),
reader.getListField(field.getOffset() * REFERENCES,
reader.getListField(field.getOffset() * POINTERS,
elementSizeFor(elementType.getBody().which()),
dval.getListValue<internal::TrustedMessage>())));
}
......@@ -584,13 +584,13 @@ DynamicValue::Reader DynamicStruct::Reader::getImpl(
case schema::Type::Body::STRUCT_TYPE: {
return DynamicValue::Reader(DynamicStruct::Reader(
member.getContainingStruct().getDependency(type.getStructType()).asStruct(),
reader.getStructField(field.getOffset() * REFERENCES,
reader.getStructField(field.getOffset() * POINTERS,
dval.getStructValue<internal::TrustedMessage>())));
}
case schema::Type::Body::OBJECT_TYPE: {
return DynamicValue::Reader(DynamicObject(
reader.getObjectField(field.getOffset() * REFERENCES,
reader.getObjectField(field.getOffset() * POINTERS,
dval.getObjectValue<internal::TrustedMessage>())));
}
......@@ -654,21 +654,21 @@ DynamicValue::Builder DynamicStruct::Builder::getImpl(
case schema::Type::Body::TEXT_TYPE: {
Text::Reader typedDval = dval.getTextValue();
return DynamicValue::Builder(
builder.getBlobField<Text>(field.getOffset() * REFERENCES,
builder.getBlobField<Text>(field.getOffset() * POINTERS,
typedDval.data(), typedDval.size() * BYTES));
}
case schema::Type::Body::DATA_TYPE: {
Data::Reader typedDval = dval.getDataValue();
return DynamicValue::Builder(
builder.getBlobField<Data>(field.getOffset() * REFERENCES,
builder.getBlobField<Data>(field.getOffset() * POINTERS,
typedDval.data(), typedDval.size() * BYTES));
}
case schema::Type::Body::LIST_TYPE:
return DynamicValue::Builder(DynamicList::Builder(
ListSchema::of(type.getListType(), member.getContainingStruct()),
builder.getListField(field.getOffset() * REFERENCES,
builder.getListField(field.getOffset() * POINTERS,
dval.getListValue<internal::TrustedMessage>())));
case schema::Type::Body::STRUCT_TYPE: {
......@@ -677,7 +677,7 @@ DynamicValue::Builder DynamicStruct::Builder::getImpl(
return DynamicValue::Builder(DynamicStruct::Builder(
structSchema,
builder.getStructField(
field.getOffset() * REFERENCES,
field.getOffset() * POINTERS,
structSizeFromSchema(structSchema),
dval.getStructValue<internal::TrustedMessage>())));
}
......@@ -685,7 +685,7 @@ DynamicValue::Builder DynamicStruct::Builder::getImpl(
case schema::Type::Body::OBJECT_TYPE: {
return DynamicValue::Builder(DynamicObject(
builder.asReader().getObjectField(
field.getOffset() * REFERENCES,
field.getOffset() * POINTERS,
dval.getObjectValue<internal::TrustedMessage>())));
}
......@@ -706,25 +706,25 @@ DynamicStruct::Builder DynamicStruct::Builder::getObjectImpl(
internal::StructBuilder builder, StructSchema::Member field, StructSchema type) {
return DynamicStruct::Builder(type,
builder.getStructField(
field.getProto().getBody().getFieldMember().getOffset() * REFERENCES,
field.getProto().getBody().getFieldMember().getOffset() * POINTERS,
structSizeFromSchema(type), nullptr));
}
DynamicList::Builder DynamicStruct::Builder::getObjectImpl(
internal::StructBuilder builder, StructSchema::Member field, ListSchema type) {
return DynamicList::Builder(type,
builder.getListField(
field.getProto().getBody().getFieldMember().getOffset() * REFERENCES,
field.getProto().getBody().getFieldMember().getOffset() * POINTERS,
nullptr));
}
Text::Builder DynamicStruct::Builder::getObjectAsTextImpl(
internal::StructBuilder builder, StructSchema::Member field) {
return builder.getBlobField<Text>(
field.getProto().getBody().getFieldMember().getOffset() * REFERENCES, nullptr, 0 * BYTES);
field.getProto().getBody().getFieldMember().getOffset() * POINTERS, nullptr, 0 * BYTES);
}
Data::Builder DynamicStruct::Builder::getObjectAsDataImpl(
internal::StructBuilder builder, StructSchema::Member field) {
return builder.getBlobField<Data>(
field.getProto().getBody().getFieldMember().getOffset() * REFERENCES, nullptr, 0 * BYTES);
field.getProto().getBody().getFieldMember().getOffset() * POINTERS, nullptr, 0 * BYTES);
}
void DynamicStruct::Builder::setImpl(
......@@ -795,11 +795,11 @@ void DynamicStruct::Builder::setImpl(
}
case schema::Type::Body::TEXT_TYPE:
builder.setBlobField<Text>(field.getOffset() * REFERENCES, value.as<Text>());
builder.setBlobField<Text>(field.getOffset() * POINTERS, value.as<Text>());
return;
case schema::Type::Body::DATA_TYPE:
builder.setBlobField<Data>(field.getOffset() * REFERENCES, value.as<Data>());
builder.setBlobField<Data>(field.getOffset() * POINTERS, value.as<Data>());
return;
case schema::Type::Body::LIST_TYPE: {
......@@ -893,7 +893,7 @@ DynamicStruct::Builder DynamicStruct::Builder::initFieldImpl(
internal::StructBuilder builder, StructSchema::Member field, StructSchema type) {
return DynamicStruct::Builder(
type, builder.initStructField(
field.getProto().getBody().getFieldMember().getOffset() * REFERENCES,
field.getProto().getBody().getFieldMember().getOffset() * POINTERS,
structSizeFromSchema(type)));
}
DynamicList::Builder DynamicStruct::Builder::initFieldImpl(
......@@ -902,12 +902,12 @@ DynamicList::Builder DynamicStruct::Builder::initFieldImpl(
if (type.whichElementType() == schema::Type::Body::STRUCT_TYPE) {
return DynamicList::Builder(
type, builder.initStructListField(
field.getProto().getBody().getFieldMember().getOffset() * REFERENCES, size * ELEMENTS,
field.getProto().getBody().getFieldMember().getOffset() * POINTERS, size * ELEMENTS,
structSizeFromSchema(type.getStructElementType())));
} else {
return DynamicList::Builder(
type, builder.initListField(
field.getProto().getBody().getFieldMember().getOffset() * REFERENCES,
field.getProto().getBody().getFieldMember().getOffset() * POINTERS,
elementSizeFor(type.whichElementType()),
size * ELEMENTS));
}
......@@ -915,12 +915,12 @@ DynamicList::Builder DynamicStruct::Builder::initFieldImpl(
Text::Builder DynamicStruct::Builder::initFieldAsTextImpl(
internal::StructBuilder builder, StructSchema::Member field, uint size) {
return builder.initBlobField<Text>(
field.getProto().getBody().getFieldMember().getOffset() * REFERENCES, size * BYTES);
field.getProto().getBody().getFieldMember().getOffset() * POINTERS, size * BYTES);
}
Data::Builder DynamicStruct::Builder::initFieldAsDataImpl(
internal::StructBuilder builder, StructSchema::Member field, uint size) {
return builder.initBlobField<Data>(
field.getProto().getBody().getFieldMember().getOffset() * REFERENCES, size * BYTES);
field.getProto().getBody().getFieldMember().getOffset() * POINTERS, size * BYTES);
}
// =======================================================================================
......@@ -1362,41 +1362,41 @@ DynamicStruct::Builder MessageBuilder::getRoot<DynamicStruct>(StructSchema schem
namespace internal {
DynamicStruct::Reader PointerHelpers<DynamicStruct, Kind::UNKNOWN>::getDynamic(
StructReader reader, WireReferenceCount index, StructSchema schema) {
StructReader reader, WirePointerCount index, StructSchema schema) {
return DynamicStruct::Reader(schema, reader.getStructField(index, nullptr));
}
DynamicStruct::Builder PointerHelpers<DynamicStruct, Kind::UNKNOWN>::getDynamic(
StructBuilder builder, WireReferenceCount index, StructSchema schema) {
StructBuilder builder, WirePointerCount index, StructSchema schema) {
return DynamicStruct::Builder(schema, builder.getStructField(
index, structSizeFromSchema(schema), nullptr));
}
void PointerHelpers<DynamicStruct, Kind::UNKNOWN>::set(
StructBuilder builder, WireReferenceCount index, DynamicStruct::Reader value) {
StructBuilder builder, WirePointerCount index, DynamicStruct::Reader value) {
// TODO(now): schemaless copy
FAIL_CHECK("Unimplemented: copyFrom()");
}
DynamicStruct::Builder PointerHelpers<DynamicStruct, Kind::UNKNOWN>::init(
StructBuilder builder, WireReferenceCount index, StructSchema schema) {
StructBuilder builder, WirePointerCount index, StructSchema schema) {
return DynamicStruct::Builder(schema,
builder.initStructField(index, structSizeFromSchema(schema)));
}
DynamicList::Reader PointerHelpers<DynamicList, Kind::UNKNOWN>::getDynamic(
StructReader reader, WireReferenceCount index, ListSchema schema) {
StructReader reader, WirePointerCount index, ListSchema schema) {
return DynamicList::Reader(schema,
reader.getListField(index, elementSizeFor(schema.whichElementType()), nullptr));
}
DynamicList::Builder PointerHelpers<DynamicList, Kind::UNKNOWN>::getDynamic(
StructBuilder builder, WireReferenceCount index, ListSchema schema) {
StructBuilder builder, WirePointerCount index, ListSchema schema) {
return DynamicList::Builder(schema, builder.getListField(index, nullptr));
}
void PointerHelpers<DynamicList, Kind::UNKNOWN>::set(
StructBuilder builder, WireReferenceCount index, DynamicList::Reader value) {
StructBuilder builder, WirePointerCount index, DynamicList::Reader value) {
// TODO(now): schemaless copy
FAIL_CHECK("Unimplemented: copyFrom()");
}
DynamicList::Builder PointerHelpers<DynamicList, Kind::UNKNOWN>::init(
StructBuilder builder, WireReferenceCount index, ListSchema schema, uint size) {
StructBuilder builder, WirePointerCount index, ListSchema schema, uint size) {
if (schema.whichElementType() == schema::Type::Body::STRUCT_TYPE) {
return DynamicList::Builder(schema,
builder.initStructListField(index, size * ELEMENTS,
......
......@@ -656,13 +656,13 @@ struct PointerHelpers<DynamicStruct, Kind::UNKNOWN> {
// non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we
// don't want people to accidentally be able to provide their own default value.
static DynamicStruct::Reader getDynamic(
StructReader reader, WireReferenceCount index, StructSchema schema);
StructReader reader, WirePointerCount index, StructSchema schema);
static DynamicStruct::Builder getDynamic(
StructBuilder builder, WireReferenceCount index, StructSchema schema);
StructBuilder builder, WirePointerCount index, StructSchema schema);
static void set(
StructBuilder builder, WireReferenceCount index, DynamicStruct::Reader value);
StructBuilder builder, WirePointerCount index, DynamicStruct::Reader value);
static DynamicStruct::Builder init(
StructBuilder builder, WireReferenceCount index, StructSchema schema);
StructBuilder builder, WirePointerCount index, StructSchema schema);
};
template <>
......@@ -671,13 +671,13 @@ struct PointerHelpers<DynamicList, Kind::UNKNOWN> {
// non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we
// don't want people to accidentally be able to provide their own default value.
static DynamicList::Reader getDynamic(
StructReader reader, WireReferenceCount index, ListSchema schema);
StructReader reader, WirePointerCount index, ListSchema schema);
static DynamicList::Builder getDynamic(
StructBuilder builder, WireReferenceCount index, ListSchema schema);
StructBuilder builder, WirePointerCount index, ListSchema schema);
static void set(
StructBuilder builder, WireReferenceCount index, DynamicList::Reader value);
StructBuilder builder, WirePointerCount index, DynamicList::Reader value);
static DynamicList::Builder init(
StructBuilder builder, WireReferenceCount index, ListSchema schema, uint size);
StructBuilder builder, WirePointerCount index, ListSchema schema, uint size);
};
} // namespace internal
......
......@@ -40,64 +40,64 @@ namespace internal {
template <typename T>
struct PointerHelpers<T, Kind::STRUCT> {
static inline typename T::Reader get(StructReader reader, WireReferenceCount index,
static inline typename T::Reader get(StructReader reader, WirePointerCount index,
const word* defaultValue = nullptr) {
return typename T::Reader(reader.getStructField(index, defaultValue));
}
static inline typename T::Builder get(StructBuilder builder, WireReferenceCount index,
static inline typename T::Builder get(StructBuilder builder, WirePointerCount index,
const word* defaultValue = nullptr) {
return typename T::Builder(builder.getStructField(index, structSize<T>(), defaultValue));
}
static inline void set(StructBuilder builder, WireReferenceCount index,
static inline void set(StructBuilder builder, WirePointerCount index,
typename T::Reader value) {
// TODO(now): schemaless copy
CAPNPROTO_INLINE_PRECOND(false, "Not implemented: set() for struct fields.");
}
static inline typename T::Builder init(StructBuilder builder, WireReferenceCount index) {
static inline typename T::Builder init(StructBuilder builder, WirePointerCount index) {
return typename T::Builder(builder.initStructField(index, structSize<T>()));
}
};
template <typename T>
struct PointerHelpers<List<T>, Kind::LIST> {
static inline typename List<T>::Reader get(StructReader reader, WireReferenceCount index,
static inline typename List<T>::Reader get(StructReader reader, WirePointerCount index,
const word* defaultValue = nullptr) {
return typename List<T>::Reader(List<T>::getAsFieldOf(reader, index, defaultValue));
}
static inline typename List<T>::Builder get(StructBuilder builder, WireReferenceCount index,
static inline typename List<T>::Builder get(StructBuilder builder, WirePointerCount index,
const word* defaultValue = nullptr) {
return typename List<T>::Builder(List<T>::getAsFieldOf(builder, index, defaultValue));
}
static inline void set(StructBuilder builder, WireReferenceCount index,
static inline void set(StructBuilder builder, WirePointerCount index,
typename List<T>::Reader value) {
init(builder, index, value.size()).copyFrom(value);
}
static inline void set(StructBuilder builder, WireReferenceCount index,
static inline void set(StructBuilder builder, WirePointerCount index,
std::initializer_list<ReaderFor<T>> value) {
init(builder, index, value.size()).copyFrom(value);
}
static inline typename List<T>::Builder init(
StructBuilder builder, WireReferenceCount index, uint size) {
StructBuilder builder, WirePointerCount index, uint size) {
return typename List<T>::Builder(List<T>::initAsFieldOf(builder, index, size));
}
};
template <typename T>
struct PointerHelpers<T, Kind::BLOB> {
static inline typename T::Reader get(StructReader reader, WireReferenceCount index,
static inline typename T::Reader get(StructReader reader, WirePointerCount index,
const void* defaultValue = nullptr,
uint defaultBytes = 0) {
return reader.getBlobField<T>(index, defaultValue, defaultBytes * BYTES);
}
static inline typename T::Builder get(StructBuilder builder, WireReferenceCount index,
static inline typename T::Builder get(StructBuilder builder, WirePointerCount index,
const void* defaultValue = nullptr,
uint defaultBytes = 0) {
return builder.getBlobField<T>(index, defaultValue, defaultBytes * BYTES);
}
static inline void set(StructBuilder builder, WireReferenceCount index, typename T::Reader value) {
static inline void set(StructBuilder builder, WirePointerCount index, typename T::Reader value) {
builder.setBlobField<T>(index, value);
}
static inline typename T::Builder init(StructBuilder builder, WireReferenceCount index, uint size) {
static inline typename T::Builder init(StructBuilder builder, WirePointerCount index, uint size) {
return builder.initBlobField<T>(index, size * BYTES);
}
};
......@@ -114,7 +114,7 @@ struct PointerHelpers<TrustedMessage> {
// itself trusted. This hack is currently private. It is used to locate default values within
// encoded schemas.
static inline const word* get(StructReader reader, WireReferenceCount index) {
static inline const word* get(StructReader reader, WirePointerCount index) {
return reader.getTrustedPointer(index);
}
};
......@@ -177,7 +177,7 @@ inline constexpr uint64_t typeId() { return internal::TypeIdFor<T>::typeId; }
template <> struct KindOf<type> { static constexpr Kind kind = Kind::STRUCT; }; \
template <> struct StructSizeFor<type> { \
static constexpr StructSize value = StructSize( \
dataWordSize * WORDS, pointerCount * REFERENCES, FieldSize::preferredElementEncoding); \
dataWordSize * WORDS, pointerCount * POINTERS, FieldSize::preferredElementEncoding); \
}; \
template <> struct TypeIdFor<type> { static constexpr uint64_t typeId = 0x##id; }; \
template <> struct RawSchemaFor<type> { \
......
......@@ -40,7 +40,7 @@ namespace {
TEST(WireFormat, SimpleRawDataStruct) {
AlignedData<2> data = {{
// Struct ref, offset = 1, dataSize = 1, referenceCount = 0
// Struct ref, offset = 1, dataSize = 1, pointerCount = 0
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
// Content for the data segment.
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
......@@ -103,7 +103,7 @@ static const AlignedData<2> STRUCTLIST_ELEMENT_SUBSTRUCT_DEFAULT =
{{0,0,0,0,1,0,0,0, 0,0,0,0,0,0,0,0}};
static constexpr StructSize STRUCTLIST_ELEMENT_SIZE(
1 * WORDS, 1 * REFERENCES, FieldSize::INLINE_COMPOSITE);
1 * WORDS, 1 * POINTERS, FieldSize::INLINE_COMPOSITE);
static void setupStruct(StructBuilder builder) {
builder.setDataField<uint64_t>(0 * ELEMENTS, 0x1011121314151617ull);
......@@ -121,12 +121,12 @@ static void setupStruct(StructBuilder builder) {
{
StructBuilder subStruct = builder.initStructField(
0 * REFERENCES, StructSize(1 * WORDS, 0 * REFERENCES, FieldSize::EIGHT_BYTES));
0 * POINTERS, StructSize(1 * WORDS, 0 * POINTERS, FieldSize::EIGHT_BYTES));
subStruct.setDataField<uint32_t>(0 * ELEMENTS, 123);
}
{
ListBuilder list = builder.initListField(1 * REFERENCES, FieldSize::FOUR_BYTES, 3 * ELEMENTS);
ListBuilder list = builder.initListField(1 * POINTERS, FieldSize::FOUR_BYTES, 3 * ELEMENTS);
EXPECT_EQ(3 * ELEMENTS, list.size());
list.setDataElement<int32_t>(0 * ELEMENTS, 200);
list.setDataElement<int32_t>(1 * ELEMENTS, 201);
......@@ -135,19 +135,19 @@ static void setupStruct(StructBuilder builder) {
{
ListBuilder list = builder.initStructListField(
2 * REFERENCES, 4 * ELEMENTS, STRUCTLIST_ELEMENT_SIZE);
2 * POINTERS, 4 * ELEMENTS, STRUCTLIST_ELEMENT_SIZE);
EXPECT_EQ(4 * ELEMENTS, list.size());
for (int i = 0; i < 4; i++) {
StructBuilder element = list.getStructElement(i * ELEMENTS);
element.setDataField<int32_t>(0 * ELEMENTS, 300 + i);
element.initStructField(0 * REFERENCES,
StructSize(1 * WORDS, 0 * REFERENCES, FieldSize::EIGHT_BYTES))
element.initStructField(0 * POINTERS,
StructSize(1 * WORDS, 0 * POINTERS, FieldSize::EIGHT_BYTES))
.setDataField<int32_t>(0 * ELEMENTS, 400 + i);
}
}
{
ListBuilder list = builder.initListField(3 * REFERENCES, FieldSize::REFERENCE, 5 * ELEMENTS);
ListBuilder list = builder.initListField(3 * POINTERS, FieldSize::POINTER, 5 * ELEMENTS);
EXPECT_EQ(5 * ELEMENTS, list.size());
for (uint i = 0; i < 5; i++) {
ListBuilder element = list.initListElement(
......@@ -176,13 +176,13 @@ static void checkStruct(StructBuilder builder) {
{
StructBuilder subStruct = builder.getStructField(
0 * REFERENCES, StructSize(1 * WORDS, 0 * REFERENCES, FieldSize::EIGHT_BYTES),
0 * POINTERS, StructSize(1 * WORDS, 0 * POINTERS, FieldSize::EIGHT_BYTES),
SUBSTRUCT_DEFAULT.words);
EXPECT_EQ(123u, subStruct.getDataField<uint32_t>(0 * ELEMENTS));
}
{
ListBuilder list = builder.getListField(1 * REFERENCES, nullptr);
ListBuilder list = builder.getListField(1 * POINTERS, nullptr);
ASSERT_EQ(3 * ELEMENTS, list.size());
EXPECT_EQ(200, list.getDataElement<int32_t>(0 * ELEMENTS));
EXPECT_EQ(201, list.getDataElement<int32_t>(1 * ELEMENTS));
......@@ -190,21 +190,21 @@ static void checkStruct(StructBuilder builder) {
}
{
ListBuilder list = builder.getListField(2 * REFERENCES, nullptr);
ListBuilder list = builder.getListField(2 * POINTERS, nullptr);
ASSERT_EQ(4 * ELEMENTS, list.size());
for (int i = 0; i < 4; i++) {
StructBuilder element = list.getStructElement(i * ELEMENTS);
EXPECT_EQ(300 + i, element.getDataField<int32_t>(0 * ELEMENTS));
EXPECT_EQ(400 + i,
element.getStructField(0 * REFERENCES,
StructSize(1 * WORDS, 0 * REFERENCES, FieldSize::EIGHT_BYTES),
element.getStructField(0 * POINTERS,
StructSize(1 * WORDS, 0 * POINTERS, FieldSize::EIGHT_BYTES),
STRUCTLIST_ELEMENT_SUBSTRUCT_DEFAULT.words)
.getDataField<int32_t>(0 * ELEMENTS));
}
}
{
ListBuilder list = builder.getListField(3 * REFERENCES, nullptr);
ListBuilder list = builder.getListField(3 * POINTERS, nullptr);
ASSERT_EQ(5 * ELEMENTS, list.size());
for (uint i = 0; i < 5; i++) {
ListBuilder element = list.getListElement(i * ELEMENTS);
......@@ -231,12 +231,12 @@ static void checkStruct(StructReader reader) {
EXPECT_FALSE(reader.getDataField<bool>(127 * ELEMENTS));
{
StructReader subStruct = reader.getStructField(0 * REFERENCES, SUBSTRUCT_DEFAULT.words);
StructReader subStruct = reader.getStructField(0 * POINTERS, SUBSTRUCT_DEFAULT.words);
EXPECT_EQ(123u, subStruct.getDataField<uint32_t>(0 * ELEMENTS));
}
{
ListReader list = reader.getListField(1 * REFERENCES, FieldSize::FOUR_BYTES, nullptr);
ListReader list = reader.getListField(1 * POINTERS, FieldSize::FOUR_BYTES, nullptr);
ASSERT_EQ(3 * ELEMENTS, list.size());
EXPECT_EQ(200, list.getDataElement<int32_t>(0 * ELEMENTS));
EXPECT_EQ(201, list.getDataElement<int32_t>(1 * ELEMENTS));
......@@ -244,20 +244,20 @@ static void checkStruct(StructReader reader) {
}
{
ListReader list = reader.getListField(2 * REFERENCES, FieldSize::INLINE_COMPOSITE, nullptr);
ListReader list = reader.getListField(2 * POINTERS, FieldSize::INLINE_COMPOSITE, nullptr);
ASSERT_EQ(4 * ELEMENTS, list.size());
for (int i = 0; i < 4; i++) {
StructReader element = list.getStructElement(i * ELEMENTS);
EXPECT_EQ(300 + i, element.getDataField<int32_t>(0 * ELEMENTS));
EXPECT_EQ(400 + i,
element.getStructField(0 * REFERENCES, STRUCTLIST_ELEMENT_SUBSTRUCT_DEFAULT.words)
element.getStructField(0 * POINTERS, STRUCTLIST_ELEMENT_SUBSTRUCT_DEFAULT.words)
.getDataField<int32_t>(0 * ELEMENTS));
}
}
{
// TODO: Use valid default value.
ListReader list = reader.getListField(3 * REFERENCES, FieldSize::REFERENCE, nullptr);
ListReader list = reader.getListField(3 * POINTERS, FieldSize::POINTER, nullptr);
ASSERT_EQ(5 * ELEMENTS, list.size());
for (uint i = 0; i < 5; i++) {
ListReader element = list.getListElement(i * ELEMENTS, FieldSize::TWO_BYTES);
......@@ -276,11 +276,11 @@ TEST(WireFormat, StructRoundTrip_OneSegment) {
word* rootLocation = segment->allocate(1 * WORDS);
StructBuilder builder = StructBuilder::initRoot(
segment, rootLocation, StructSize(2 * WORDS, 4 * REFERENCES, FieldSize::INLINE_COMPOSITE));
segment, rootLocation, StructSize(2 * WORDS, 4 * POINTERS, FieldSize::INLINE_COMPOSITE));
setupStruct(builder);
// word count:
// 1 root reference
// 1 root pointer
// 6 root struct
// 1 sub message
// 2 3-element int32 list
......@@ -288,10 +288,10 @@ TEST(WireFormat, StructRoundTrip_OneSegment) {
// 1 tag
// 12 4x struct
// 1 data segment
// 1 reference segment
// 1 pointer segment
// 1 sub-struct
// 11 list list
// 5 references to sub-lists
// 5 pointers to sub-lists
// 6 sub-lists (4x 1 word, 1x 2 words)
// -----
// 34
......@@ -312,7 +312,7 @@ TEST(WireFormat, StructRoundTrip_OneSegmentPerAllocation) {
word* rootLocation = segment->allocate(1 * WORDS);
StructBuilder builder = StructBuilder::initRoot(
segment, rootLocation, StructSize(2 * WORDS, 4 * REFERENCES, FieldSize::INLINE_COMPOSITE));
segment, rootLocation, StructSize(2 * WORDS, 4 * POINTERS, FieldSize::INLINE_COMPOSITE));
setupStruct(builder);
// Verify that we made 15 segments.
......@@ -320,7 +320,7 @@ TEST(WireFormat, StructRoundTrip_OneSegmentPerAllocation) {
ASSERT_EQ(15u, segments.size());
// Check that each segment has the expected size. Recall that the first word of each segment will
// actually be a reference to the first thing allocated within that segment.
// actually be a pointer to the first thing allocated within that segment.
EXPECT_EQ( 1u, segments[ 0].size()); // root ref
EXPECT_EQ( 7u, segments[ 1].size()); // root struct
EXPECT_EQ( 2u, segments[ 2].size()); // sub-struct
......@@ -349,7 +349,7 @@ TEST(WireFormat, StructRoundTrip_MultipleSegmentsWithMultipleAllocations) {
word* rootLocation = segment->allocate(1 * WORDS);
StructBuilder builder = StructBuilder::initRoot(
segment, rootLocation, StructSize(2 * WORDS, 4 * REFERENCES, FieldSize::INLINE_COMPOSITE));
segment, rootLocation, StructSize(2 * WORDS, 4 * POINTERS, FieldSize::INLINE_COMPOSITE));
setupStruct(builder);
// Verify that we made 6 segments.
......
......@@ -34,28 +34,28 @@ namespace internal {
// =======================================================================================
struct WireReference {
// A reference, in exactly the format in which it appears on the wire.
struct WirePointer {
// A pointer, in exactly the format in which it appears on the wire.
// Copying and moving is not allowed because the offset would become wrong.
WireReference(const WireReference& other) = delete;
WireReference(WireReference&& other) = delete;
WireReference& operator=(const WireReference& other) = delete;
WireReference& operator=(WireReference&& other) = delete;
WirePointer(const WirePointer& other) = delete;
WirePointer(WirePointer&& other) = delete;
WirePointer& operator=(const WirePointer& other) = delete;
WirePointer& operator=(WirePointer&& other) = delete;
// -----------------------------------------------------------------
// Common part of all references: kind + offset
// Common part of all pointers: kind + offset
//
// Actually this is not terribly common. The "offset" could actually be different things
// depending on the context:
// - For a regular (e.g. struct/list) reference, a signed word offset from the word immediately
// following the reference pointer. (The off-by-one means the offset is more often zero, saving
// - For a regular (e.g. struct/list) pointer, a signed word offset from the word immediately
// following the pointer pointer. (The off-by-one means the offset is more often zero, saving
// bytes on the wire when packed.)
// - For an inline composite list tag (not really a reference, but structured similarly), an
// - For an inline composite list tag (not really a pointer, but structured similarly), an
// element count.
// - For a FAR reference, an unsigned offset into the target segment.
// - For a FAR pointer, an unsigned offset into the target segment.
// - For a FAR landing pad, zero indicates that the target value immediately follows the pad while
// 1 indicates that the pad is followed by another FAR reference that actually points at the
// 1 indicates that the pad is followed by another FAR pointer that actually points at the
// value.
enum Kind {
......@@ -103,12 +103,12 @@ struct WireReference {
CAPNPROTO_ALWAYS_INLINE(WordCount farPositionInSegment() const) {
DPRECOND(kind() == FAR,
"positionInSegment() should only be called on FAR references.");
"positionInSegment() should only be called on FAR pointers.");
return (offsetAndKind.get() >> 3) * WORDS;
}
CAPNPROTO_ALWAYS_INLINE(bool isDoubleFar() const) {
DPRECOND(kind() == FAR,
"isDoubleFar() should only be called on FAR references.");
"isDoubleFar() should only be called on FAR pointers.");
return (offsetAndKind.get() >> 2) & 1;
}
CAPNPROTO_ALWAYS_INLINE(void setFar(bool isDoubleFar, WordCount pos)) {
......@@ -117,26 +117,26 @@ struct WireReference {
}
// -----------------------------------------------------------------
// Part of reference that depends on the kind.
// Part of pointer that depends on the kind.
union {
uint32_t upper32Bits;
struct {
WireValue<WordCount16> dataSize;
WireValue<WireReferenceCount16> refCount;
WireValue<WirePointerCount16> ptrCount;
inline WordCount wordSize() const {
return dataSize.get() + refCount.get() * WORDS_PER_REFERENCE;
return dataSize.get() + ptrCount.get() * WORDS_PER_POINTER;
}
CAPNPROTO_ALWAYS_INLINE(void set(WordCount ds, WireReferenceCount rc)) {
CAPNPROTO_ALWAYS_INLINE(void set(WordCount ds, WirePointerCount rc)) {
dataSize.set(ds);
refCount.set(rc);
ptrCount.set(rc);
}
CAPNPROTO_ALWAYS_INLINE(void set(StructSize size)) {
dataSize.set(size.data);
refCount.set(size.pointers);
ptrCount.set(size.pointers);
}
} structRef;
// Also covers capabilities.
......@@ -183,14 +183,14 @@ struct WireReference {
return (offsetAndKind.get() == 0) & (upper32Bits == 0);
}
};
static_assert(sizeof(WireReference) == sizeof(word),
"capnproto::WireReference is not exactly one word. This will probably break everything.");
static_assert(REFERENCES * WORDS_PER_REFERENCE * BYTES_PER_WORD / BYTES == sizeof(WireReference),
"WORDS_PER_REFERENCE is wrong.");
static_assert(REFERENCES * BYTES_PER_REFERENCE / BYTES == sizeof(WireReference),
"BYTES_PER_REFERENCE is wrong.");
static_assert(REFERENCES * BITS_PER_REFERENCE / BITS_PER_BYTE / BYTES == sizeof(WireReference),
"BITS_PER_REFERENCE is wrong.");
static_assert(sizeof(WirePointer) == sizeof(word),
"capnproto::WirePointer is not exactly one word. This will probably break everything.");
static_assert(POINTERS * WORDS_PER_POINTER * BYTES_PER_WORD / BYTES == sizeof(WirePointer),
"WORDS_PER_POINTER is wrong.");
static_assert(POINTERS * BYTES_PER_POINTER / BYTES == sizeof(WirePointer),
"BYTES_PER_POINTER is wrong.");
static_assert(POINTERS * BITS_PER_POINTER / BITS_PER_BYTE / BYTES == sizeof(WirePointer),
"BITS_PER_POINTER is wrong.");
// =======================================================================================
......@@ -210,39 +210,39 @@ struct WireHelpers {
}
static CAPNPROTO_ALWAYS_INLINE(word* allocate(
WireReference*& ref, SegmentBuilder*& segment, WordCount amount,
WireReference::Kind kind)) {
WirePointer*& ref, SegmentBuilder*& segment, WordCount amount,
WirePointer::Kind kind)) {
word* ptr = segment->allocate(amount);
if (ptr == nullptr) {
// Need to allocate in a new segment. We'll need to allocate an extra reference worth of
// space to act as the landing pad for a far reference.
// Need to allocate in a new segment. We'll need to allocate an extra pointer worth of
// space to act as the landing pad for a far pointer.
WordCount amountPlusRef = amount + REFERENCE_SIZE_IN_WORDS;
WordCount amountPlusRef = amount + POINTER_SIZE_IN_WORDS;
segment = segment->getArena()->getSegmentWithAvailable(amountPlusRef);
ptr = segment->allocate(amountPlusRef);
// Set up the original reference to be a far reference to the new segment.
// Set up the original pointer to be a far pointer to the new segment.
ref->setFar(false, segment->getOffsetTo(ptr));
ref->farRef.set(segment->getSegmentId());
// Initialize the landing pad to indicate that the data immediately follows the pad.
ref = reinterpret_cast<WireReference*>(ptr);
ref->setKindAndTarget(kind, ptr + REFERENCE_SIZE_IN_WORDS);
ref = reinterpret_cast<WirePointer*>(ptr);
ref->setKindAndTarget(kind, ptr + POINTER_SIZE_IN_WORDS);
// Allocated space follows new reference.
return ptr + REFERENCE_SIZE_IN_WORDS;
// Allocated space follows new pointer.
return ptr + POINTER_SIZE_IN_WORDS;
} else {
ref->setKindAndTarget(kind, ptr);
return ptr;
}
}
static CAPNPROTO_ALWAYS_INLINE(word* followFars(WireReference*& ref, SegmentBuilder*& segment)) {
if (ref->kind() == WireReference::FAR) {
static CAPNPROTO_ALWAYS_INLINE(word* followFars(WirePointer*& ref, SegmentBuilder*& segment)) {
if (ref->kind() == WirePointer::FAR) {
segment = segment->getArena()->getSegment(ref->farRef.segmentId.get());
WireReference* pad =
reinterpret_cast<WireReference*>(segment->getPtrUnchecked(ref->farPositionInSegment()));
WirePointer* pad =
reinterpret_cast<WirePointer*>(segment->getPtrUnchecked(ref->farPositionInSegment()));
if (!ref->isDoubleFar()) {
ref = pad;
return pad->target();
......@@ -260,8 +260,8 @@ struct WireHelpers {
}
static CAPNPROTO_ALWAYS_INLINE(
const word* followFars(const WireReference*& ref, SegmentReader*& segment)) {
if (ref->kind() == WireReference::FAR) {
const word* followFars(const WirePointer*& ref, SegmentReader*& segment)) {
if (ref->kind() == WirePointer::FAR) {
// Look up the segment containing the landing pad.
segment = segment->getArena()->tryGetSegment(ref->farRef.segmentId.get());
VALIDATE_INPUT(segment != nullptr, "Message contains far pointer to unknown segment.") {
......@@ -270,13 +270,13 @@ struct WireHelpers {
// Find the landing pad and check that it is within bounds.
const word* ptr = segment->getStartPtr() + ref->farPositionInSegment();
WordCount padWords = (1 + ref->isDoubleFar()) * REFERENCE_SIZE_IN_WORDS;
WordCount padWords = (1 + ref->isDoubleFar()) * POINTER_SIZE_IN_WORDS;
VALIDATE_INPUT(segment->containsInterval(ptr, ptr + padWords),
"Message contains out-of-bounds far pointer.") {
return nullptr;
}
const WireReference* pad = reinterpret_cast<const WireReference*>(ptr);
const WirePointer* pad = reinterpret_cast<const WirePointer*>(ptr);
// If this is not a double-far then the landing pad is our final pointer.
if (!ref->isDoubleFar()) {
......@@ -304,40 +304,40 @@ struct WireHelpers {
static CAPNPROTO_ALWAYS_INLINE(
void copyStruct(SegmentBuilder* segment, word* dst, const word* src,
WordCount dataSize, WireReferenceCount referenceCount)) {
WordCount dataSize, WirePointerCount pointerCount)) {
memcpy(dst, src, dataSize * BYTES_PER_WORD / BYTES);
const WireReference* srcRefs = reinterpret_cast<const WireReference*>(src + dataSize);
WireReference* dstRefs = reinterpret_cast<WireReference*>(dst + dataSize);
const WirePointer* srcRefs = reinterpret_cast<const WirePointer*>(src + dataSize);
WirePointer* dstRefs = reinterpret_cast<WirePointer*>(dst + dataSize);
for (uint i = 0; i < referenceCount / REFERENCES; i++) {
for (uint i = 0; i < pointerCount / POINTERS; i++) {
SegmentBuilder* subSegment = segment;
WireReference* dstRef = dstRefs + i;
WirePointer* dstRef = dstRefs + i;
copyMessage(subSegment, dstRef, srcRefs + i);
}
}
static word* copyMessage(
SegmentBuilder*& segment, WireReference*& dst, const WireReference* src) {
SegmentBuilder*& segment, WirePointer*& dst, const WirePointer* src) {
// Not always-inline because it's recursive.
switch (src->kind()) {
case WireReference::STRUCT: {
case WirePointer::STRUCT: {
if (src->isNull()) {
memset(dst, 0, sizeof(WireReference));
memset(dst, 0, sizeof(WirePointer));
return nullptr;
} else {
const word* srcPtr = src->target();
word* dstPtr = allocate(dst, segment, src->structRef.wordSize(), WireReference::STRUCT);
word* dstPtr = allocate(dst, segment, src->structRef.wordSize(), WirePointer::STRUCT);
copyStruct(segment, dstPtr, srcPtr, src->structRef.dataSize.get(),
src->structRef.refCount.get());
src->structRef.ptrCount.get());
dst->structRef.set(src->structRef.dataSize.get(), src->structRef.refCount.get());
dst->structRef.set(src->structRef.dataSize.get(), src->structRef.ptrCount.get());
return dstPtr;
}
}
case WireReference::LIST: {
case WirePointer::LIST: {
switch (src->listRef.elementSize()) {
case FieldSize::VOID:
case FieldSize::BIT:
......@@ -349,52 +349,52 @@ struct WireHelpers {
ElementCount64(src->listRef.elementCount()) *
dataBitsPerElement(src->listRef.elementSize()));
const word* srcPtr = src->target();
word* dstPtr = allocate(dst, segment, wordCount, WireReference::LIST);
word* dstPtr = allocate(dst, segment, wordCount, WirePointer::LIST);
memcpy(dstPtr, srcPtr, wordCount * BYTES_PER_WORD / BYTES);
dst->listRef.set(src->listRef.elementSize(), src->listRef.elementCount());
return dstPtr;
}
case FieldSize::REFERENCE: {
const WireReference* srcRefs = reinterpret_cast<const WireReference*>(src->target());
WireReference* dstRefs = reinterpret_cast<WireReference*>(
case FieldSize::POINTER: {
const WirePointer* srcRefs = reinterpret_cast<const WirePointer*>(src->target());
WirePointer* dstRefs = reinterpret_cast<WirePointer*>(
allocate(dst, segment, src->listRef.elementCount() *
(1 * REFERENCES / ELEMENTS) * WORDS_PER_REFERENCE,
WireReference::LIST));
(1 * POINTERS / ELEMENTS) * WORDS_PER_POINTER,
WirePointer::LIST));
uint n = src->listRef.elementCount() / ELEMENTS;
for (uint i = 0; i < n; i++) {
SegmentBuilder* subSegment = segment;
WireReference* dstRef = dstRefs + i;
WirePointer* dstRef = dstRefs + i;
copyMessage(subSegment, dstRef, srcRefs + i);
}
dst->listRef.set(FieldSize::REFERENCE, src->listRef.elementCount());
dst->listRef.set(FieldSize::POINTER, src->listRef.elementCount());
return reinterpret_cast<word*>(dstRefs);
}
case FieldSize::INLINE_COMPOSITE: {
const word* srcPtr = src->target();
word* dstPtr = allocate(dst, segment,
src->listRef.inlineCompositeWordCount() + REFERENCE_SIZE_IN_WORDS,
WireReference::LIST);
src->listRef.inlineCompositeWordCount() + POINTER_SIZE_IN_WORDS,
WirePointer::LIST);
dst->listRef.setInlineComposite(src->listRef.inlineCompositeWordCount());
const WireReference* srcTag = reinterpret_cast<const WireReference*>(srcPtr);
memcpy(dstPtr, srcTag, sizeof(WireReference));
const WirePointer* srcTag = reinterpret_cast<const WirePointer*>(srcPtr);
memcpy(dstPtr, srcTag, sizeof(WirePointer));
const word* srcElement = srcPtr + REFERENCE_SIZE_IN_WORDS;
word* dstElement = dstPtr + REFERENCE_SIZE_IN_WORDS;
const word* srcElement = srcPtr + POINTER_SIZE_IN_WORDS;
word* dstElement = dstPtr + POINTER_SIZE_IN_WORDS;
CHECK(srcTag->kind() == WireReference::STRUCT,
CHECK(srcTag->kind() == WirePointer::STRUCT,
"INLINE_COMPOSITE of lists is not yet supported.");
uint n = srcTag->inlineCompositeListElementCount() / ELEMENTS;
for (uint i = 0; i < n; i++) {
copyStruct(segment, dstElement, srcElement,
srcTag->structRef.dataSize.get(), srcTag->structRef.refCount.get());
srcTag->structRef.dataSize.get(), srcTag->structRef.ptrCount.get());
srcElement += srcTag->structRef.wordSize();
dstElement += srcTag->structRef.wordSize();
}
......@@ -411,18 +411,18 @@ struct WireHelpers {
return nullptr;
}
static void transferPointer(SegmentBuilder* dstSegment, WireReference* dst,
SegmentBuilder* srcSegment, WireReference* src) {
static void transferPointer(SegmentBuilder* dstSegment, WirePointer* dst,
SegmentBuilder* srcSegment, WirePointer* src) {
// Make *dst point to the same object as *src. Both must reside in the same message, but can
// be in different segments. Not always-inline because this is rarely used.
if (src->isNull()) {
memset(dst, 0, sizeof(WireReference));
} else if (src->kind() == WireReference::FAR) {
memset(dst, 0, sizeof(WirePointer));
} else if (src->kind() == WirePointer::FAR) {
// Far pointers are position-independent, so we can just copy.
memcpy(dst, src, sizeof(WireReference));
memcpy(dst, src, sizeof(WirePointer));
} else if (dstSegment == srcSegment) {
// Same segment, so create a direct reference.
// Same segment, so create a direct pointer.
dst->setKindAndTarget(src->kind(), src->target());
// We can just copy the upper 32 bits. (Use memcpy() to comply with aliasing rules.)
......@@ -431,12 +431,12 @@ struct WireHelpers {
// Need to create a far pointer. Try to allocate it in the same segment as the source, so
// that it doesn't need to be a double-far.
WireReference* landingPad =
reinterpret_cast<WireReference*>(srcSegment->allocate(1 * WORDS));
WirePointer* landingPad =
reinterpret_cast<WirePointer*>(srcSegment->allocate(1 * WORDS));
if (landingPad == nullptr) {
// Darn, need a double-far.
SegmentBuilder* farSegment = srcSegment->getArena()->getSegmentWithAvailable(2 * WORDS);
landingPad = reinterpret_cast<WireReference*>(farSegment->allocate(2 * WORDS));
landingPad = reinterpret_cast<WirePointer*>(farSegment->allocate(2 * WORDS));
DCHECK(landingPad != nullptr,
"getSegmentWithAvailable() returned segment without space available.");
......@@ -461,48 +461,48 @@ struct WireHelpers {
// -----------------------------------------------------------------
static CAPNPROTO_ALWAYS_INLINE(StructBuilder initStructReference(
WireReference* ref, SegmentBuilder* segment, StructSize size)) {
static CAPNPROTO_ALWAYS_INLINE(StructBuilder initStructPointer(
WirePointer* ref, SegmentBuilder* segment, StructSize size)) {
// Allocate space for the new struct. Newly-allocated space is automatically zeroed.
word* ptr = allocate(ref, segment, size.total(), WireReference::STRUCT);
word* ptr = allocate(ref, segment, size.total(), WirePointer::STRUCT);
// Initialize the reference.
// Initialize the pointer.
ref->structRef.set(size);
// Build the StructBuilder.
return StructBuilder(segment, ptr, reinterpret_cast<WireReference*>(ptr + size.data),
return StructBuilder(segment, ptr, reinterpret_cast<WirePointer*>(ptr + size.data),
size.data * BITS_PER_WORD, size.pointers, 0 * BITS);
}
static CAPNPROTO_ALWAYS_INLINE(StructBuilder getWritableStructReference(
WireReference* ref, SegmentBuilder* segment, StructSize size, const word* defaultValue)) {
static CAPNPROTO_ALWAYS_INLINE(StructBuilder getWritableStructPointer(
WirePointer* ref, SegmentBuilder* segment, StructSize size, const word* defaultValue)) {
word* ptr;
if (ref->isNull()) {
useDefault:
if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
ptr = allocate(ref, segment, size.total(), WireReference::STRUCT);
reinterpret_cast<const WirePointer*>(defaultValue)->isNull()) {
ptr = allocate(ref, segment, size.total(), WirePointer::STRUCT);
ref->structRef.set(size);
} else {
ptr = copyMessage(segment, ref, reinterpret_cast<const WireReference*>(defaultValue));
ptr = copyMessage(segment, ref, reinterpret_cast<const WirePointer*>(defaultValue));
}
return StructBuilder(segment, ptr, reinterpret_cast<WireReference*>(ptr + size.data),
return StructBuilder(segment, ptr, reinterpret_cast<WirePointer*>(ptr + size.data),
size.data * BITS_PER_WORD, size.pointers, 0 * BITS);
} else {
WireReference* oldRef = ref;
WirePointer* oldRef = ref;
SegmentBuilder* oldSegment = segment;
word* oldPtr = followFars(oldRef, oldSegment);
VALIDATE_INPUT(oldRef->kind() == WireReference::STRUCT,
"Message contains non-struct reference where struct reference was expected.") {
VALIDATE_INPUT(oldRef->kind() == WirePointer::STRUCT,
"Message contains non-struct pointer where struct pointer was expected.") {
goto useDefault;
}
WordCount oldDataSize = oldRef->structRef.dataSize.get();
WireReferenceCount oldPointerCount = oldRef->structRef.refCount.get();
WireReference* oldPointerSection =
reinterpret_cast<WireReference*>(oldPtr + oldDataSize);
WirePointerCount oldPointerCount = oldRef->structRef.ptrCount.get();
WirePointer* oldPointerSection =
reinterpret_cast<WirePointer*>(oldPtr + oldDataSize);
if (oldDataSize < size.data || oldPointerCount < size.pointers) {
// The space allocated for this struct is too small. Unlike with readers, we can't just
......@@ -510,19 +510,19 @@ struct WireHelpers {
// Instead, we have to copy the struct to a new space now.
WordCount newDataSize = std::max<WordCount>(oldDataSize, size.data);
WireReferenceCount newPointerCount =
std::max<WireReferenceCount>(oldPointerCount, size.pointers);
WordCount totalSize = newDataSize + newPointerCount * WORDS_PER_REFERENCE;
WirePointerCount newPointerCount =
std::max<WirePointerCount>(oldPointerCount, size.pointers);
WordCount totalSize = newDataSize + newPointerCount * WORDS_PER_POINTER;
ptr = allocate(ref, segment, totalSize, WireReference::STRUCT);
ptr = allocate(ref, segment, totalSize, WirePointer::STRUCT);
ref->structRef.set(newDataSize, newPointerCount);
// Copy data section.
memcpy(ptr, oldPtr, oldDataSize * BYTES_PER_WORD / BYTES);
// Copy pointer section.
WireReference* newPointerSection = reinterpret_cast<WireReference*>(ptr + newDataSize);
for (uint i = 0; i < oldPointerCount / REFERENCES; i++) {
WirePointer* newPointerSection = reinterpret_cast<WirePointer*>(ptr + newDataSize);
for (uint i = 0; i < oldPointerCount / POINTERS; i++) {
transferPointer(segment, newPointerSection + i, oldSegment, oldPointerSection + i);
}
......@@ -535,233 +535,233 @@ struct WireHelpers {
}
}
static CAPNPROTO_ALWAYS_INLINE(ListBuilder initListReference(
WireReference* ref, SegmentBuilder* segment, ElementCount elementCount,
static CAPNPROTO_ALWAYS_INLINE(ListBuilder initListPointer(
WirePointer* ref, SegmentBuilder* segment, ElementCount elementCount,
FieldSize elementSize)) {
DPRECOND(elementSize != FieldSize::INLINE_COMPOSITE,
"Should have called initStructListReference() instead.");
"Should have called initStructListPointer() instead.");
BitCount dataSize = dataBitsPerElement(elementSize) * ELEMENTS;
WireReferenceCount referenceCount = pointersPerElement(elementSize) * ELEMENTS;
auto step = (dataSize + referenceCount * BITS_PER_REFERENCE) / ELEMENTS;
WirePointerCount pointerCount = pointersPerElement(elementSize) * ELEMENTS;
auto step = (dataSize + pointerCount * BITS_PER_POINTER) / ELEMENTS;
// Calculate size of the list.
WordCount wordCount = roundUpToWords(ElementCount64(elementCount) * step);
// Allocate the list.
word* ptr = allocate(ref, segment, wordCount, WireReference::LIST);
word* ptr = allocate(ref, segment, wordCount, WirePointer::LIST);
// Initialize the reference.
// Initialize the pointer.
ref->listRef.set(elementSize, elementCount);
// Build the ListBuilder.
return ListBuilder(segment, ptr, step, elementCount, dataSize, referenceCount);
return ListBuilder(segment, ptr, step, elementCount, dataSize, pointerCount);
}
static CAPNPROTO_ALWAYS_INLINE(ListBuilder initStructListReference(
WireReference* ref, SegmentBuilder* segment, ElementCount elementCount,
static CAPNPROTO_ALWAYS_INLINE(ListBuilder initStructListPointer(
WirePointer* ref, SegmentBuilder* segment, ElementCount elementCount,
StructSize elementSize)) {
if (elementSize.preferredListEncoding != FieldSize::INLINE_COMPOSITE) {
// Small data-only struct. Allocate a list of primitives instead.
return initListReference(ref, segment, elementCount, elementSize.preferredListEncoding);
return initListPointer(ref, segment, elementCount, elementSize.preferredListEncoding);
}
auto wordsPerElement = elementSize.total() / ELEMENTS;
// Allocate the list, prefixed by a single WireReference.
// Allocate the list, prefixed by a single WirePointer.
WordCount wordCount = elementCount * wordsPerElement;
word* ptr = allocate(ref, segment, REFERENCE_SIZE_IN_WORDS + wordCount, WireReference::LIST);
word* ptr = allocate(ref, segment, POINTER_SIZE_IN_WORDS + wordCount, WirePointer::LIST);
// Initialize the reference.
// Initialize the pointer.
// INLINE_COMPOSITE lists replace the element count with the word count.
ref->listRef.setInlineComposite(wordCount);
// Initialize the list tag.
reinterpret_cast<WireReference*>(ptr)->setKindAndInlineCompositeListElementCount(
WireReference::STRUCT, elementCount);
reinterpret_cast<WireReference*>(ptr)->structRef.set(elementSize);
ptr += REFERENCE_SIZE_IN_WORDS;
reinterpret_cast<WirePointer*>(ptr)->setKindAndInlineCompositeListElementCount(
WirePointer::STRUCT, elementCount);
reinterpret_cast<WirePointer*>(ptr)->structRef.set(elementSize);
ptr += POINTER_SIZE_IN_WORDS;
// Build the ListBuilder.
return ListBuilder(segment, ptr, wordsPerElement * BITS_PER_WORD, elementCount,
elementSize.data * BITS_PER_WORD, elementSize.pointers);
}
static CAPNPROTO_ALWAYS_INLINE(ListBuilder getWritableListReference(
WireReference* ref, SegmentBuilder* segment, const word* defaultValue)) {
const WireReference* defaultRef = reinterpret_cast<const WireReference*>(defaultValue);
static CAPNPROTO_ALWAYS_INLINE(ListBuilder getWritableListPointer(
WirePointer* ref, SegmentBuilder* segment, const word* defaultValue)) {
const WirePointer* defaultRef = reinterpret_cast<const WirePointer*>(defaultValue);
word* ptr;
if (ref->isNull()) {
if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
reinterpret_cast<const WirePointer*>(defaultValue)->isNull()) {
return ListBuilder();
}
ptr = copyMessage(segment, ref, defaultRef);
} else {
ptr = followFars(ref, segment);
PRECOND(ref->kind() == WireReference::LIST,
"Called getList{Field,Element}() but existing reference is not a list.");
PRECOND(ref->kind() == WirePointer::LIST,
"Called getList{Field,Element}() but existing pointer is not a list.");
}
if (ref->listRef.elementSize() == FieldSize::INLINE_COMPOSITE) {
// Read the tag to get the actual element count.
WireReference* tag = reinterpret_cast<WireReference*>(ptr);
PRECOND(tag->kind() == WireReference::STRUCT,
WirePointer* tag = reinterpret_cast<WirePointer*>(ptr);
PRECOND(tag->kind() == WirePointer::STRUCT,
"INLINE_COMPOSITE list with non-STRUCT elements not supported.");
// First list element is at tag + 1 reference.
// First list element is at tag + 1 pointer.
return ListBuilder(segment, tag + 1, tag->structRef.wordSize() * BITS_PER_WORD / ELEMENTS,
tag->inlineCompositeListElementCount(),
tag->structRef.dataSize.get() * BITS_PER_WORD,
tag->structRef.refCount.get());
tag->structRef.ptrCount.get());
} else {
BitCount dataSize = dataBitsPerElement(ref->listRef.elementSize()) * ELEMENTS;
WireReferenceCount referenceCount = pointersPerElement(ref->listRef.elementSize()) * ELEMENTS;
auto step = (dataSize + referenceCount * BITS_PER_REFERENCE) / ELEMENTS;
return ListBuilder(segment, ptr, step, ref->listRef.elementCount(), dataSize, referenceCount);
WirePointerCount pointerCount = pointersPerElement(ref->listRef.elementSize()) * ELEMENTS;
auto step = (dataSize + pointerCount * BITS_PER_POINTER) / ELEMENTS;
return ListBuilder(segment, ptr, step, ref->listRef.elementCount(), dataSize, pointerCount);
}
}
static CAPNPROTO_ALWAYS_INLINE(Text::Builder initTextReference(
WireReference* ref, SegmentBuilder* segment, ByteCount size)) {
static CAPNPROTO_ALWAYS_INLINE(Text::Builder initTextPointer(
WirePointer* ref, SegmentBuilder* segment, ByteCount size)) {
// The byte list must include a NUL terminator.
ByteCount byteSize = size + 1 * BYTES;
// Allocate the space.
word* ptr = allocate(ref, segment, roundUpToWords(byteSize), WireReference::LIST);
word* ptr = allocate(ref, segment, roundUpToWords(byteSize), WirePointer::LIST);
// Initialize the reference.
// Initialize the pointer.
ref->listRef.set(FieldSize::BYTE, byteSize * (1 * ELEMENTS / BYTES));
// Build the Text::Builder. This will initialize the NUL terminator.
return Text::Builder(reinterpret_cast<char*>(ptr), size / BYTES);
}
static CAPNPROTO_ALWAYS_INLINE(void setTextReference(
WireReference* ref, SegmentBuilder* segment, Text::Reader value)) {
initTextReference(ref, segment, value.size() * BYTES).copyFrom(value);
static CAPNPROTO_ALWAYS_INLINE(void setTextPointer(
WirePointer* ref, SegmentBuilder* segment, Text::Reader value)) {
initTextPointer(ref, segment, value.size() * BYTES).copyFrom(value);
}
static CAPNPROTO_ALWAYS_INLINE(Text::Builder getWritableTextReference(
WireReference* ref, SegmentBuilder* segment,
static CAPNPROTO_ALWAYS_INLINE(Text::Builder getWritableTextPointer(
WirePointer* ref, SegmentBuilder* segment,
const void* defaultValue, ByteCount defaultSize)) {
if (ref->isNull()) {
Text::Builder builder = initTextReference(ref, segment, defaultSize);
Text::Builder builder = initTextPointer(ref, segment, defaultSize);
builder.copyFrom(defaultValue);
return builder;
} else {
word* ptr = followFars(ref, segment);
PRECOND(ref->kind() == WireReference::LIST,
"Called getText{Field,Element}() but existing reference is not a list.");
PRECOND(ref->kind() == WirePointer::LIST,
"Called getText{Field,Element}() but existing pointer is not a list.");
PRECOND(ref->listRef.elementSize() == FieldSize::BYTE,
"Called getText{Field,Element}() but existing list reference is not byte-sized.");
"Called getText{Field,Element}() but existing list pointer is not byte-sized.");
// Subtract 1 from the size for the NUL terminator.
return Text::Builder(reinterpret_cast<char*>(ptr), ref->listRef.elementCount() / ELEMENTS - 1);
}
}
static CAPNPROTO_ALWAYS_INLINE(Data::Builder initDataReference(
WireReference* ref, SegmentBuilder* segment, ByteCount size)) {
static CAPNPROTO_ALWAYS_INLINE(Data::Builder initDataPointer(
WirePointer* ref, SegmentBuilder* segment, ByteCount size)) {
// Allocate the space.
word* ptr = allocate(ref, segment, roundUpToWords(size), WireReference::LIST);
word* ptr = allocate(ref, segment, roundUpToWords(size), WirePointer::LIST);
// Initialize the reference.
// Initialize the pointer.
ref->listRef.set(FieldSize::BYTE, size * (1 * ELEMENTS / BYTES));
// Build the Data::Builder.
return Data::Builder(reinterpret_cast<char*>(ptr), size / BYTES);
}
static CAPNPROTO_ALWAYS_INLINE(void setDataReference(
WireReference* ref, SegmentBuilder* segment, Data::Reader value)) {
initDataReference(ref, segment, value.size() * BYTES).copyFrom(value);
static CAPNPROTO_ALWAYS_INLINE(void setDataPointer(
WirePointer* ref, SegmentBuilder* segment, Data::Reader value)) {
initDataPointer(ref, segment, value.size() * BYTES).copyFrom(value);
}
static CAPNPROTO_ALWAYS_INLINE(Data::Builder getWritableDataReference(
WireReference* ref, SegmentBuilder* segment,
static CAPNPROTO_ALWAYS_INLINE(Data::Builder getWritableDataPointer(
WirePointer* ref, SegmentBuilder* segment,
const void* defaultValue, ByteCount defaultSize)) {
if (ref->isNull()) {
Data::Builder builder = initDataReference(ref, segment, defaultSize);
Data::Builder builder = initDataPointer(ref, segment, defaultSize);
builder.copyFrom(defaultValue);
return builder;
} else {
word* ptr = followFars(ref, segment);
PRECOND(ref->kind() == WireReference::LIST,
"Called getData{Field,Element}() but existing reference is not a list.");
PRECOND(ref->kind() == WirePointer::LIST,
"Called getData{Field,Element}() but existing pointer is not a list.");
PRECOND(ref->listRef.elementSize() == FieldSize::BYTE,
"Called getData{Field,Element}() but existing list reference is not byte-sized.");
"Called getData{Field,Element}() but existing list pointer is not byte-sized.");
return Data::Builder(reinterpret_cast<char*>(ptr), ref->listRef.elementCount() / ELEMENTS);
}
}
static CAPNPROTO_ALWAYS_INLINE(ObjectBuilder getWritableObjectReference(
SegmentBuilder* segment, WireReference* ref, const word* defaultValue)) {
static CAPNPROTO_ALWAYS_INLINE(ObjectBuilder getWritableObjectPointer(
SegmentBuilder* segment, WirePointer* ref, const word* defaultValue)) {
word* ptr;
if (ref->isNull()) {
if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
reinterpret_cast<const WirePointer*>(defaultValue)->isNull()) {
return ObjectBuilder();
} else {
ptr = copyMessage(segment, ref, reinterpret_cast<const WireReference*>(defaultValue));
ptr = copyMessage(segment, ref, reinterpret_cast<const WirePointer*>(defaultValue));
}
} else {
ptr = followFars(ref, segment);
}
if (ref->kind() == WireReference::LIST) {
if (ref->kind() == WirePointer::LIST) {
if (ref->listRef.elementSize() == FieldSize::INLINE_COMPOSITE) {
// Read the tag to get the actual element count.
WireReference* tag = reinterpret_cast<WireReference*>(ptr);
PRECOND(tag->kind() == WireReference::STRUCT,
WirePointer* tag = reinterpret_cast<WirePointer*>(ptr);
PRECOND(tag->kind() == WirePointer::STRUCT,
"INLINE_COMPOSITE list with non-STRUCT elements not supported.");
// First list element is at tag + 1 reference.
// First list element is at tag + 1 pointer.
return ObjectBuilder(
ListBuilder(segment, tag + 1, tag->structRef.wordSize() * BITS_PER_WORD / ELEMENTS,
tag->inlineCompositeListElementCount(),
tag->structRef.dataSize.get() * BITS_PER_WORD,
tag->structRef.refCount.get()));
tag->structRef.ptrCount.get()));
} else {
BitCount dataSize = dataBitsPerElement(ref->listRef.elementSize()) * ELEMENTS;
WireReferenceCount referenceCount =
WirePointerCount pointerCount =
pointersPerElement(ref->listRef.elementSize()) * ELEMENTS;
auto step = (dataSize + referenceCount * BITS_PER_REFERENCE) / ELEMENTS;
auto step = (dataSize + pointerCount * BITS_PER_POINTER) / ELEMENTS;
return ObjectBuilder(ListBuilder(
segment, ptr, step, ref->listRef.elementCount(), dataSize, referenceCount));
segment, ptr, step, ref->listRef.elementCount(), dataSize, pointerCount));
}
} else {
return ObjectBuilder(StructBuilder(
segment, ptr,
reinterpret_cast<WireReference*>(ptr + ref->structRef.dataSize.get()),
reinterpret_cast<WirePointer*>(ptr + ref->structRef.dataSize.get()),
ref->structRef.dataSize.get() * BITS_PER_WORD,
ref->structRef.refCount.get(),
ref->structRef.ptrCount.get(),
0 * BITS));
}
}
// -----------------------------------------------------------------
static CAPNPROTO_ALWAYS_INLINE(StructReader readStructReference(
SegmentReader* segment, const WireReference* ref, const word* defaultValue,
static CAPNPROTO_ALWAYS_INLINE(StructReader readStructPointer(
SegmentReader* segment, const WirePointer* ref, const word* defaultValue,
int nestingLimit)) {
const word* ptr;
if (ref == nullptr || ref->isNull()) {
useDefault:
if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
return StructReader(nullptr, nullptr, nullptr, 0 * BITS, 0 * REFERENCES, 0 * BITS,
reinterpret_cast<const WirePointer*>(defaultValue)->isNull()) {
return StructReader(nullptr, nullptr, nullptr, 0 * BITS, 0 * POINTERS, 0 * BITS,
std::numeric_limits<int>::max());
}
segment = nullptr;
ref = reinterpret_cast<const WireReference*>(defaultValue);
ref = reinterpret_cast<const WirePointer*>(defaultValue);
ptr = ref->target();
} else if (segment != nullptr) {
VALIDATE_INPUT(nestingLimit > 0,
......@@ -775,13 +775,13 @@ struct WireHelpers {
goto useDefault;
}
VALIDATE_INPUT(ref->kind() == WireReference::STRUCT,
"Message contains non-struct reference where struct reference was expected.") {
VALIDATE_INPUT(ref->kind() == WirePointer::STRUCT,
"Message contains non-struct pointer where struct pointer was expected.") {
goto useDefault;
}
VALIDATE_INPUT(segment->containsInterval(ptr, ptr + ref->structRef.wordSize()),
"Message contained out-of-bounds struct reference.") {
"Message contained out-of-bounds struct pointer.") {
goto useDefault;
}
} else {
......@@ -790,24 +790,24 @@ struct WireHelpers {
}
return StructReader(
segment, ptr, reinterpret_cast<const WireReference*>(ptr + ref->structRef.dataSize.get()),
segment, ptr, reinterpret_cast<const WirePointer*>(ptr + ref->structRef.dataSize.get()),
ref->structRef.dataSize.get() * BITS_PER_WORD,
ref->structRef.refCount.get(),
ref->structRef.ptrCount.get(),
0 * BITS, nestingLimit - 1);
}
static CAPNPROTO_ALWAYS_INLINE(ListReader readListReference(
SegmentReader* segment, const WireReference* ref, const word* defaultValue,
static CAPNPROTO_ALWAYS_INLINE(ListReader readListPointer(
SegmentReader* segment, const WirePointer* ref, const word* defaultValue,
FieldSize expectedElementSize, int nestingLimit)) {
const word* ptr;
if (ref == nullptr || ref->isNull()) {
useDefault:
if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
reinterpret_cast<const WirePointer*>(defaultValue)->isNull()) {
return ListReader();
}
segment = nullptr;
ref = reinterpret_cast<const WireReference*>(defaultValue);
ref = reinterpret_cast<const WirePointer*>(defaultValue);
ptr = ref->target();
} else if (segment != nullptr) {
VALIDATE_INPUT(nestingLimit > 0,
......@@ -821,8 +821,8 @@ struct WireHelpers {
goto useDefault;
}
VALIDATE_INPUT(ref->kind() == WireReference::LIST,
"Message contains non-list reference where list reference was expected.") {
VALIDATE_INPUT(ref->kind() == WirePointer::LIST,
"Message contains non-list pointer where list pointer was expected.") {
goto useDefault;
}
} else {
......@@ -836,17 +836,17 @@ struct WireHelpers {
WordCount wordCount = ref->listRef.inlineCompositeWordCount();
// An INLINE_COMPOSITE list points to a tag, which is formatted like a reference.
const WireReference* tag = reinterpret_cast<const WireReference*>(ptr);
ptr += REFERENCE_SIZE_IN_WORDS;
// An INLINE_COMPOSITE list points to a tag, which is formatted like a pointer.
const WirePointer* tag = reinterpret_cast<const WirePointer*>(ptr);
ptr += POINTER_SIZE_IN_WORDS;
if (segment != nullptr) {
VALIDATE_INPUT(segment->containsInterval(ptr - REFERENCE_SIZE_IN_WORDS, ptr + wordCount),
"Message contains out-of-bounds list reference.") {
VALIDATE_INPUT(segment->containsInterval(ptr - POINTER_SIZE_IN_WORDS, ptr + wordCount),
"Message contains out-of-bounds list pointer.") {
goto useDefault;
}
VALIDATE_INPUT(tag->kind() == WireReference::STRUCT,
VALIDATE_INPUT(tag->kind() == WirePointer::STRUCT,
"INLINE_COMPOSITE lists of non-STRUCT type are not supported.") {
goto useDefault;
}
......@@ -885,12 +885,12 @@ struct WireHelpers {
}
break;
case FieldSize::REFERENCE:
// We expected a list of references but got a list of structs. Assuming the first field
// in the struct is the reference we were looking for, we want to munge the pointer to
// point at the first element's reference segment.
case FieldSize::POINTER:
// We expected a list of pointers but got a list of structs. Assuming the first field
// in the struct is the pointer we were looking for, we want to munge the pointer to
// point at the first element's pointer segment.
ptr += tag->structRef.dataSize.get();
VALIDATE_INPUT(tag->structRef.refCount.get() > 0 * REFERENCES,
VALIDATE_INPUT(tag->structRef.ptrCount.get() > 0 * POINTERS,
"Expected a pointer list, but got a list of data-only structs.") {
goto useDefault;
}
......@@ -906,7 +906,7 @@ struct WireHelpers {
size = tag->inlineCompositeListElementCount();
wordsPerElement = tag->structRef.wordSize() / ELEMENTS;
if (expectedElementSize == FieldSize::REFERENCE) {
if (expectedElementSize == FieldSize::POINTER) {
ptr += tag->structRef.dataSize.get();
}
}
......@@ -914,20 +914,20 @@ struct WireHelpers {
return ListReader(
segment, ptr, size, wordsPerElement * BITS_PER_WORD,
tag->structRef.dataSize.get() * BITS_PER_WORD,
tag->structRef.refCount.get(), nestingLimit - 1);
tag->structRef.ptrCount.get(), nestingLimit - 1);
} else {
// This is a primitive or pointer list, but all such lists can also be interpreted as struct
// lists. We need to compute the data size and reference count for such structs.
// lists. We need to compute the data size and pointer count for such structs.
BitCount dataSize = dataBitsPerElement(ref->listRef.elementSize()) * ELEMENTS;
WireReferenceCount referenceCount =
WirePointerCount pointerCount =
pointersPerElement(ref->listRef.elementSize()) * ELEMENTS;
auto step = (dataSize + referenceCount * BITS_PER_REFERENCE) / ELEMENTS;
auto step = (dataSize + pointerCount * BITS_PER_POINTER) / ELEMENTS;
if (segment != nullptr) {
VALIDATE_INPUT(segment->containsInterval(ptr, ptr +
roundUpToWords(ElementCount64(ref->listRef.elementCount()) * step)),
"Message contains out-of-bounds list reference.") {
"Message contains out-of-bounds list pointer.") {
goto useDefault;
}
}
......@@ -940,31 +940,31 @@ struct WireHelpers {
BitCount expectedDataBitsPerElement =
dataBitsPerElement(expectedElementSize) * ELEMENTS;
WireReferenceCount expectedPointersPerElement =
WirePointerCount expectedPointersPerElement =
pointersPerElement(expectedElementSize) * ELEMENTS;
VALIDATE_INPUT(expectedDataBitsPerElement <= dataSize,
"Message contained list with incompatible element type.") {
goto useDefault;
}
VALIDATE_INPUT(expectedPointersPerElement <= referenceCount,
VALIDATE_INPUT(expectedPointersPerElement <= pointerCount,
"Message contained list with incompatible element type.") {
goto useDefault;
}
}
return ListReader(segment, ptr, ref->listRef.elementCount(), step,
dataSize, referenceCount, nestingLimit - 1);
dataSize, pointerCount, nestingLimit - 1);
}
}
static CAPNPROTO_ALWAYS_INLINE(Text::Reader readTextReference(
SegmentReader* segment, const WireReference* ref,
static CAPNPROTO_ALWAYS_INLINE(Text::Reader readTextPointer(
SegmentReader* segment, const WirePointer* ref,
const void* defaultValue, ByteCount defaultSize)) {
if (ref == nullptr || ref->isNull()) {
useDefault:
if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
reinterpret_cast<const WirePointer*>(defaultValue)->isNull()) {
defaultValue = "";
}
return Text::Reader(reinterpret_cast<const char*>(defaultValue), defaultSize / BYTES);
......@@ -982,19 +982,19 @@ struct WireHelpers {
uint size = ref->listRef.elementCount() / ELEMENTS;
VALIDATE_INPUT(ref->kind() == WireReference::LIST,
"Message contains non-list reference where text was expected.") {
VALIDATE_INPUT(ref->kind() == WirePointer::LIST,
"Message contains non-list pointer where text was expected.") {
goto useDefault;
}
VALIDATE_INPUT(ref->listRef.elementSize() == FieldSize::BYTE,
"Message contains list reference of non-bytes where text was expected.") {
"Message contains list pointer of non-bytes where text was expected.") {
goto useDefault;
}
VALIDATE_INPUT(segment->containsInterval(ptr, ptr +
roundUpToWords(ref->listRef.elementCount() * (1 * BYTES / ELEMENTS))),
"Message contained out-of-bounds text reference.") {
"Message contained out-of-bounds text pointer.") {
goto useDefault;
}
......@@ -1013,8 +1013,8 @@ struct WireHelpers {
}
}
static CAPNPROTO_ALWAYS_INLINE(Data::Reader readDataReference(
SegmentReader* segment, const WireReference* ref,
static CAPNPROTO_ALWAYS_INLINE(Data::Reader readDataPointer(
SegmentReader* segment, const WirePointer* ref,
const void* defaultValue, ByteCount defaultSize)) {
if (ref == nullptr || ref->isNull()) {
useDefault:
......@@ -1033,19 +1033,19 @@ struct WireHelpers {
uint size = ref->listRef.elementCount() / ELEMENTS;
VALIDATE_INPUT(ref->kind() == WireReference::LIST,
"Message contains non-list reference where data was expected.") {
VALIDATE_INPUT(ref->kind() == WirePointer::LIST,
"Message contains non-list pointer where data was expected.") {
goto useDefault;
}
VALIDATE_INPUT(ref->listRef.elementSize() == FieldSize::BYTE,
"Message contains list reference of non-bytes where data was expected.") {
"Message contains list pointer of non-bytes where data was expected.") {
goto useDefault;
}
VALIDATE_INPUT(segment->containsInterval(ptr, ptr +
roundUpToWords(ref->listRef.elementCount() * (1 * BYTES / ELEMENTS))),
"Message contained out-of-bounds data reference.") {
"Message contained out-of-bounds data pointer.") {
goto useDefault;
}
......@@ -1053,10 +1053,10 @@ struct WireHelpers {
}
}
static CAPNPROTO_ALWAYS_INLINE(ObjectReader readObjectReference(
SegmentReader* segment, const WireReference* ref,
static CAPNPROTO_ALWAYS_INLINE(ObjectReader readObjectPointer(
SegmentReader* segment, const WirePointer* ref,
const word* defaultValue, int nestingLimit)) {
// We can't really reuse readStructReference() and readListReference() because they are designed
// We can't really reuse readStructPointer() and readListPointer() because they are designed
// for the case where we are expecting a specific type, and they do validation around that,
// whereas this method is for the case where we accept any pointer.
......@@ -1064,11 +1064,11 @@ struct WireHelpers {
if (ref == nullptr || ref->isNull()) {
useDefault:
if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
reinterpret_cast<const WirePointer*>(defaultValue)->isNull()) {
return ObjectReader();
}
segment = nullptr;
ref = reinterpret_cast<const WireReference*>(defaultValue);
ref = reinterpret_cast<const WirePointer*>(defaultValue);
ptr = ref->target();
} else if (segment != nullptr) {
ptr = WireHelpers::followFars(ref, segment);
......@@ -1081,7 +1081,7 @@ struct WireHelpers {
}
switch (ref->kind()) {
case WireReference::STRUCT:
case WirePointer::STRUCT:
if (segment != nullptr) {
VALIDATE_INPUT(nestingLimit > 0,
"Message is too deeply-nested or contains cycles. See capnproto::ReadOptions.") {
......@@ -1089,17 +1089,17 @@ struct WireHelpers {
}
VALIDATE_INPUT(segment->containsInterval(ptr, ptr + ref->structRef.wordSize()),
"Message contained out-of-bounds struct reference.") {
"Message contained out-of-bounds struct pointer.") {
goto useDefault;
}
}
return ObjectReader(
StructReader(segment, ptr,
reinterpret_cast<const WireReference*>(ptr + ref->structRef.dataSize.get()),
reinterpret_cast<const WirePointer*>(ptr + ref->structRef.dataSize.get()),
ref->structRef.dataSize.get() * BITS_PER_WORD,
ref->structRef.refCount.get(),
ref->structRef.ptrCount.get(),
0 * BITS, nestingLimit - 1));
case WireReference::LIST: {
case WirePointer::LIST: {
FieldSize elementSize = ref->listRef.elementSize();
if (segment != nullptr) {
......@@ -1111,17 +1111,17 @@ struct WireHelpers {
if (elementSize == FieldSize::INLINE_COMPOSITE) {
WordCount wordCount = ref->listRef.inlineCompositeWordCount();
const WireReference* tag = reinterpret_cast<const WireReference*>(ptr);
ptr += REFERENCE_SIZE_IN_WORDS;
const WirePointer* tag = reinterpret_cast<const WirePointer*>(ptr);
ptr += POINTER_SIZE_IN_WORDS;
if (segment != nullptr) {
VALIDATE_INPUT(segment->containsInterval(ptr - REFERENCE_SIZE_IN_WORDS,
VALIDATE_INPUT(segment->containsInterval(ptr - POINTER_SIZE_IN_WORDS,
ptr + wordCount),
"Message contains out-of-bounds list reference.") {
"Message contains out-of-bounds list pointer.") {
goto useDefault;
}
VALIDATE_INPUT(tag->kind() == WireReference::STRUCT,
VALIDATE_INPUT(tag->kind() == WirePointer::STRUCT,
"INLINE_COMPOSITE lists of non-STRUCT type are not supported.") {
goto useDefault;
}
......@@ -1138,23 +1138,23 @@ struct WireHelpers {
return ObjectReader(
ListReader(segment, ptr, elementCount, wordsPerElement * BITS_PER_WORD,
tag->structRef.dataSize.get() * BITS_PER_WORD,
tag->structRef.refCount.get(), nestingLimit - 1));
tag->structRef.ptrCount.get(), nestingLimit - 1));
} else {
BitCount dataSize = dataBitsPerElement(elementSize) * ELEMENTS;
WireReferenceCount referenceCount = pointersPerElement(elementSize) * ELEMENTS;
auto step = (dataSize + referenceCount * BITS_PER_REFERENCE) / ELEMENTS;
WirePointerCount pointerCount = pointersPerElement(elementSize) * ELEMENTS;
auto step = (dataSize + pointerCount * BITS_PER_POINTER) / ELEMENTS;
ElementCount elementCount = ref->listRef.elementCount();
WordCount wordCount = roundUpToWords(ElementCount64(elementCount) * step);
if (segment != nullptr) {
VALIDATE_INPUT(segment->containsInterval(ptr, ptr + wordCount),
"Message contains out-of-bounds list reference.") {
"Message contains out-of-bounds list pointer.") {
goto useDefault;
}
}
return ObjectReader(
ListReader(segment, ptr, elementCount, step, dataSize, referenceCount,
ListReader(segment, ptr, elementCount, step, dataSize, pointerCount,
nestingLimit - 1));
}
}
......@@ -1170,148 +1170,148 @@ struct WireHelpers {
StructBuilder StructBuilder::initRoot(
SegmentBuilder* segment, word* location, StructSize size) {
return WireHelpers::initStructReference(
reinterpret_cast<WireReference*>(location), segment, size);
return WireHelpers::initStructPointer(
reinterpret_cast<WirePointer*>(location), segment, size);
}
StructBuilder StructBuilder::getRoot(
SegmentBuilder* segment, word* location, StructSize size) {
return WireHelpers::getWritableStructReference(
reinterpret_cast<WireReference*>(location), segment, size, nullptr);
return WireHelpers::getWritableStructPointer(
reinterpret_cast<WirePointer*>(location), segment, size, nullptr);
}
StructBuilder StructBuilder::initStructField(
WireReferenceCount refIndex, StructSize size) const {
return WireHelpers::initStructReference(references + refIndex, segment, size);
WirePointerCount ptrIndex, StructSize size) const {
return WireHelpers::initStructPointer(pointers + ptrIndex, segment, size);
}
StructBuilder StructBuilder::getStructField(
WireReferenceCount refIndex, StructSize size, const word* defaultValue) const {
return WireHelpers::getWritableStructReference(
references + refIndex, segment, size, defaultValue);
WirePointerCount ptrIndex, StructSize size, const word* defaultValue) const {
return WireHelpers::getWritableStructPointer(
pointers + ptrIndex, segment, size, defaultValue);
}
ListBuilder StructBuilder::initListField(
WireReferenceCount refIndex, FieldSize elementSize, ElementCount elementCount) const {
return WireHelpers::initListReference(
references + refIndex, segment,
WirePointerCount ptrIndex, FieldSize elementSize, ElementCount elementCount) const {
return WireHelpers::initListPointer(
pointers + ptrIndex, segment,
elementCount, elementSize);
}
ListBuilder StructBuilder::initStructListField(
WireReferenceCount refIndex, ElementCount elementCount, StructSize elementSize) const {
return WireHelpers::initStructListReference(
references + refIndex, segment, elementCount, elementSize);
WirePointerCount ptrIndex, ElementCount elementCount, StructSize elementSize) const {
return WireHelpers::initStructListPointer(
pointers + ptrIndex, segment, elementCount, elementSize);
}
ListBuilder StructBuilder::getListField(
WireReferenceCount refIndex, const word* defaultValue) const {
return WireHelpers::getWritableListReference(
references + refIndex, segment, defaultValue);
WirePointerCount ptrIndex, const word* defaultValue) const {
return WireHelpers::getWritableListPointer(
pointers + ptrIndex, segment, defaultValue);
}
template <>
Text::Builder StructBuilder::initBlobField<Text>(WireReferenceCount refIndex, ByteCount size) const {
return WireHelpers::initTextReference(references + refIndex, segment, size);
Text::Builder StructBuilder::initBlobField<Text>(WirePointerCount ptrIndex, ByteCount size) const {
return WireHelpers::initTextPointer(pointers + ptrIndex, segment, size);
}
template <>
void StructBuilder::setBlobField<Text>(WireReferenceCount refIndex, Text::Reader value) const {
WireHelpers::setTextReference(references + refIndex, segment, value);
void StructBuilder::setBlobField<Text>(WirePointerCount ptrIndex, Text::Reader value) const {
WireHelpers::setTextPointer(pointers + ptrIndex, segment, value);
}
template <>
Text::Builder StructBuilder::getBlobField<Text>(
WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const {
return WireHelpers::getWritableTextReference(
references + refIndex, segment, defaultValue, defaultSize);
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const {
return WireHelpers::getWritableTextPointer(
pointers + ptrIndex, segment, defaultValue, defaultSize);
}
template <>
Data::Builder StructBuilder::initBlobField<Data>(WireReferenceCount refIndex, ByteCount size) const {
return WireHelpers::initDataReference(references + refIndex, segment, size);
Data::Builder StructBuilder::initBlobField<Data>(WirePointerCount ptrIndex, ByteCount size) const {
return WireHelpers::initDataPointer(pointers + ptrIndex, segment, size);
}
template <>
void StructBuilder::setBlobField<Data>(WireReferenceCount refIndex, Data::Reader value) const {
WireHelpers::setDataReference(references + refIndex, segment, value);
void StructBuilder::setBlobField<Data>(WirePointerCount ptrIndex, Data::Reader value) const {
WireHelpers::setDataPointer(pointers + ptrIndex, segment, value);
}
template <>
Data::Builder StructBuilder::getBlobField<Data>(
WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const {
return WireHelpers::getWritableDataReference(
references + refIndex, segment, defaultValue, defaultSize);
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const {
return WireHelpers::getWritableDataPointer(
pointers + ptrIndex, segment, defaultValue, defaultSize);
}
ObjectBuilder StructBuilder::getObjectField(
WireReferenceCount refIndex, const word* defaultValue) const {
return WireHelpers::getWritableObjectReference(segment, references + refIndex, defaultValue);
WirePointerCount ptrIndex, const word* defaultValue) const {
return WireHelpers::getWritableObjectPointer(segment, pointers + ptrIndex, defaultValue);
}
StructReader StructBuilder::asReader() const {
return StructReader(segment, data, references,
dataSize, referenceCount, bit0Offset, std::numeric_limits<int>::max());
return StructReader(segment, data, pointers,
dataSize, pointerCount, bit0Offset, std::numeric_limits<int>::max());
}
// =======================================================================================
// StructReader
StructReader StructReader::readRootTrusted(const word* location) {
return WireHelpers::readStructReference(nullptr, reinterpret_cast<const WireReference*>(location),
return WireHelpers::readStructPointer(nullptr, reinterpret_cast<const WirePointer*>(location),
nullptr, std::numeric_limits<int>::max());
}
StructReader StructReader::readRoot(
const word* location, SegmentReader* segment, int nestingLimit) {
VALIDATE_INPUT(segment->containsInterval(location, location + REFERENCE_SIZE_IN_WORDS),
VALIDATE_INPUT(segment->containsInterval(location, location + POINTER_SIZE_IN_WORDS),
"Root location out-of-bounds.") {
location = nullptr;
}
return WireHelpers::readStructReference(segment, reinterpret_cast<const WireReference*>(location),
return WireHelpers::readStructPointer(segment, reinterpret_cast<const WirePointer*>(location),
nullptr, nestingLimit);
}
StructReader StructReader::getStructField(
WireReferenceCount refIndex, const word* defaultValue) const {
const WireReference* ref = refIndex >= referenceCount ? nullptr : references + refIndex;
return WireHelpers::readStructReference(segment, ref, defaultValue, nestingLimit);
WirePointerCount ptrIndex, const word* defaultValue) const {
const WirePointer* ref = ptrIndex >= pointerCount ? nullptr : pointers + ptrIndex;
return WireHelpers::readStructPointer(segment, ref, defaultValue, nestingLimit);
}
ListReader StructReader::getListField(
WireReferenceCount refIndex, FieldSize expectedElementSize, const word* defaultValue) const {
const WireReference* ref = refIndex >= referenceCount ? nullptr : references + refIndex;
return WireHelpers::readListReference(
WirePointerCount ptrIndex, FieldSize expectedElementSize, const word* defaultValue) const {
const WirePointer* ref = ptrIndex >= pointerCount ? nullptr : pointers + ptrIndex;
return WireHelpers::readListPointer(
segment, ref, defaultValue, expectedElementSize, nestingLimit);
}
template <>
Text::Reader StructReader::getBlobField<Text>(
WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const {
const WireReference* ref = refIndex >= referenceCount ? nullptr : references + refIndex;
return WireHelpers::readTextReference(segment, ref, defaultValue, defaultSize);
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const {
const WirePointer* ref = ptrIndex >= pointerCount ? nullptr : pointers + ptrIndex;
return WireHelpers::readTextPointer(segment, ref, defaultValue, defaultSize);
}
template <>
Data::Reader StructReader::getBlobField<Data>(
WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const {
const WireReference* ref = refIndex >= referenceCount ? nullptr : references + refIndex;
return WireHelpers::readDataReference(segment, ref, defaultValue, defaultSize);
WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const {
const WirePointer* ref = ptrIndex >= pointerCount ? nullptr : pointers + ptrIndex;
return WireHelpers::readDataPointer(segment, ref, defaultValue, defaultSize);
}
ObjectReader StructReader::getObjectField(
WireReferenceCount refIndex, const word* defaultValue) const {
return WireHelpers::readObjectReference(
segment, references + refIndex, defaultValue, nestingLimit);
WirePointerCount ptrIndex, const word* defaultValue) const {
return WireHelpers::readObjectPointer(
segment, pointers + ptrIndex, defaultValue, nestingLimit);
}
const word* StructReader::getTrustedPointer(WireReferenceCount refIndex) const {
const word* StructReader::getTrustedPointer(WirePointerCount ptrIndex) const {
PRECOND(segment == nullptr, "getTrustedPointer() only allowed on trusted messages.");
return reinterpret_cast<const word*>(references + refIndex);
return reinterpret_cast<const word*>(pointers + ptrIndex);
}
// =======================================================================================
// ListBuilder
Text::Builder ListBuilder::asText() {
VALIDATE_INPUT(structDataSize == 8 * BITS && structReferenceCount == 0 * REFERENCES,
VALIDATE_INPUT(structDataSize == 8 * BITS && structPointerCount == 0 * POINTERS,
"Expected Text, got list of non-bytes.") {
return Text::Builder();
}
......@@ -1333,7 +1333,7 @@ Text::Builder ListBuilder::asText() {
}
Data::Builder ListBuilder::asData() {
VALIDATE_INPUT(structDataSize == 8 * BITS && structReferenceCount == 0 * REFERENCES,
VALIDATE_INPUT(structDataSize == 8 * BITS && structPointerCount == 0 * POINTERS,
"Expected Text, got list of non-bytes.") {
return Data::Builder();
}
......@@ -1345,69 +1345,69 @@ StructBuilder ListBuilder::getStructElement(ElementCount index) const {
BitCount64 indexBit = ElementCount64(index) * step;
byte* structData = ptr + indexBit / BITS_PER_BYTE;
return StructBuilder(segment, structData,
reinterpret_cast<WireReference*>(structData + structDataSize / BITS_PER_BYTE),
structDataSize, structReferenceCount, indexBit % BITS_PER_BYTE);
reinterpret_cast<WirePointer*>(structData + structDataSize / BITS_PER_BYTE),
structDataSize, structPointerCount, indexBit % BITS_PER_BYTE);
}
ListBuilder ListBuilder::initListElement(
ElementCount index, FieldSize elementSize, ElementCount elementCount) const {
return WireHelpers::initListReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE),
return WireHelpers::initListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE),
segment, elementCount, elementSize);
}
ListBuilder ListBuilder::initStructListElement(
ElementCount index, ElementCount elementCount, StructSize elementSize) const {
return WireHelpers::initStructListReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE),
return WireHelpers::initStructListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE),
segment, elementCount, elementSize);
}
ListBuilder ListBuilder::getListElement(ElementCount index) const {
return WireHelpers::getWritableListReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, nullptr);
return WireHelpers::getWritableListPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, nullptr);
}
template <>
Text::Builder ListBuilder::initBlobElement<Text>(ElementCount index, ByteCount size) const {
return WireHelpers::initTextReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, size);
return WireHelpers::initTextPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, size);
}
template <>
void ListBuilder::setBlobElement<Text>(ElementCount index, Text::Reader value) const {
WireHelpers::setTextReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, value);
WireHelpers::setTextPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, value);
}
template <>
Text::Builder ListBuilder::getBlobElement<Text>(ElementCount index) const {
return WireHelpers::getWritableTextReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, "", 0 * BYTES);
return WireHelpers::getWritableTextPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, "", 0 * BYTES);
}
template <>
Data::Builder ListBuilder::initBlobElement<Data>(ElementCount index, ByteCount size) const {
return WireHelpers::initDataReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, size);
return WireHelpers::initDataPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, size);
}
template <>
void ListBuilder::setBlobElement<Data>(ElementCount index, Data::Reader value) const {
WireHelpers::setDataReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, value);
WireHelpers::setDataPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, value);
}
template <>
Data::Builder ListBuilder::getBlobElement<Data>(ElementCount index) const {
return WireHelpers::getWritableDataReference(
reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), segment, nullptr,
return WireHelpers::getWritableDataPointer(
reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), segment, nullptr,
0 * BYTES);
}
ObjectBuilder ListBuilder::getObjectElement(ElementCount index) const {
return WireHelpers::getWritableObjectReference(
segment, reinterpret_cast<WireReference*>(ptr + index * step / BITS_PER_BYTE), nullptr);
return WireHelpers::getWritableObjectPointer(
segment, reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE), nullptr);
}
ListReader ListBuilder::asReader() const {
return ListReader(segment, ptr, elementCount, step, structDataSize, structReferenceCount,
return ListReader(segment, ptr, elementCount, step, structDataSize, structPointerCount,
std::numeric_limits<int>::max());
}
......@@ -1415,7 +1415,7 @@ ListReader ListBuilder::asReader() const {
// ListReader
Text::Reader ListReader::asText() {
VALIDATE_INPUT(structDataSize == 8 * BITS && structReferenceCount == 0 * REFERENCES,
VALIDATE_INPUT(structDataSize == 8 * BITS && structPointerCount == 0 * POINTERS,
"Expected Text, got list of non-bytes.") {
return Text::Reader();
}
......@@ -1437,7 +1437,7 @@ Text::Reader ListReader::asText() {
}
Data::Reader ListReader::asData() {
VALIDATE_INPUT(structDataSize == 8 * BITS && structReferenceCount == 0 * REFERENCES,
VALIDATE_INPUT(structDataSize == 8 * BITS && structPointerCount == 0 * POINTERS,
"Expected Text, got list of non-bytes.") {
return Data::Reader();
}
......@@ -1453,49 +1453,49 @@ StructReader ListReader::getStructElement(ElementCount index) const {
BitCount64 indexBit = ElementCount64(index) * step;
const byte* structData = ptr + indexBit / BITS_PER_BYTE;
const WireReference* structPointers =
reinterpret_cast<const WireReference*>(structData + structDataSize / BITS_PER_BYTE);
const WirePointer* structPointers =
reinterpret_cast<const WirePointer*>(structData + structDataSize / BITS_PER_BYTE);
// This check should pass if there are no bugs in the list pointer validation code.
DCHECK(structReferenceCount == 0 * REFERENCES ||
(uintptr_t)structPointers % sizeof(WireReference) == 0,
DCHECK(structPointerCount == 0 * POINTERS ||
(uintptr_t)structPointers % sizeof(WirePointer) == 0,
"Pointer segment of struct list element not aligned.");
return StructReader(
segment, structData, structPointers,
structDataSize, structReferenceCount,
structDataSize, structPointerCount,
indexBit % BITS_PER_BYTE, nestingLimit - 1);
}
static const WireReference* checkAlignment(const void* ptr) {
DCHECK((uintptr_t)ptr % sizeof(WireReference) == 0,
static const WirePointer* checkAlignment(const void* ptr) {
DCHECK((uintptr_t)ptr % sizeof(WirePointer) == 0,
"Pointer segment of struct list element not aligned.");
return reinterpret_cast<const WireReference*>(ptr);
return reinterpret_cast<const WirePointer*>(ptr);
}
ListReader ListReader::getListElement(
ElementCount index, FieldSize expectedElementSize) const {
return WireHelpers::readListReference(
return WireHelpers::readListPointer(
segment, checkAlignment(ptr + index * step / BITS_PER_BYTE),
nullptr, expectedElementSize, nestingLimit);
}
template <>
Text::Reader ListReader::getBlobElement<Text>(ElementCount index) const {
return WireHelpers::readTextReference(
return WireHelpers::readTextPointer(
segment, checkAlignment(ptr + index * step / BITS_PER_BYTE),
"", 0 * BYTES);
}
template <>
Data::Reader ListReader::getBlobElement<Data>(ElementCount index) const {
return WireHelpers::readDataReference(
return WireHelpers::readDataPointer(
segment, checkAlignment(ptr + index * step / BITS_PER_BYTE),
nullptr, 0 * BYTES);
}
ObjectReader ListReader::getObjectElement(ElementCount index) const {
return WireHelpers::readObjectReference(
return WireHelpers::readObjectPointer(
segment, checkAlignment(ptr + index * step / BITS_PER_BYTE), nullptr, nestingLimit);
}
......
......@@ -44,7 +44,7 @@ class ListBuilder;
class ListReader;
class ObjectBuilder;
class ObjectReader;
struct WireReference;
struct WirePointer;
struct WireHelpers;
class SegmentReader;
class SegmentBuilder;
......@@ -61,23 +61,23 @@ enum class FieldSize: uint8_t {
FOUR_BYTES = 4,
EIGHT_BYTES = 5,
REFERENCE = 6, // Indicates that the field lives in the reference segment, not the data segment.
POINTER = 6, // Indicates that the field lives in the pointer segment, not the data segment.
INLINE_COMPOSITE = 7
// A composite type of fixed width. This serves two purposes:
// 1) For lists of composite types where all the elements would have the exact same width,
// allocating a list of references which in turn point at the elements would waste space. We
// allocating a list of pointers which in turn point at the elements would waste space. We
// can avoid a layer of indirection by placing all the elements in a flat sequence, and only
// indicating the element properties (e.g. field count for structs) once.
//
// Specifically, a list reference indicating INLINE_COMPOSITE element size actually points to
// a "tag" describing one element. This tag is formatted like a wire reference, but the
// Specifically, a list pointer indicating INLINE_COMPOSITE element size actually points to
// a "tag" describing one element. This tag is formatted like a wire pointer, but the
// "offset" instead stores the element count of the list. The flat list of elements appears
// immediately after the tag. In the list reference itself, the element count is replaced with
// immediately after the tag. In the list pointer itself, the element count is replaced with
// a word count for the whole list (excluding tag). This allows the tag and elements to be
// precached in a single step rather than two sequential steps.
//
// It is NOT intended to be possible to substitute an INLINE_COMPOSITE list for a REFERENCE
// It is NOT intended to be possible to substitute an INLINE_COMPOSITE list for a POINTER
// list or vice-versa without breaking recipients. Recipients expect one or the other
// depending on the message definition.
//
......@@ -93,7 +93,7 @@ enum class FieldSize: uint8_t {
};
typedef decltype(BITS / ELEMENTS) BitsPerElement;
typedef decltype(REFERENCES / ELEMENTS) PointersPerElement;
typedef decltype(POINTERS / ELEMENTS) PointersPerElement;
namespace internal {
static constexpr BitsPerElement BITS_PER_ELEMENT_TABLE[8] = {
......@@ -113,7 +113,7 @@ inline constexpr BitsPerElement dataBitsPerElement(FieldSize size) {
}
inline constexpr PointersPerElement pointersPerElement(FieldSize size) {
return size == FieldSize::REFERENCE ? 1 * REFERENCES / ELEMENTS : 0 * REFERENCES / ELEMENTS;
return size == FieldSize::POINTER ? 1 * POINTERS / ELEMENTS : 0 * POINTERS / ELEMENTS;
}
} // namespace internal
......@@ -169,17 +169,17 @@ union AlignedData {
struct StructSize {
WordCount16 data;
WireReferenceCount16 pointers;
WirePointerCount16 pointers;
FieldSize preferredListEncoding;
// Preferred size to use when encoding a list of this struct. This is INLINE_COMPOSITE if and
// only if the struct is larger than one word; otherwise the struct list can be encoded more
// efficiently by encoding it as if it were some primitive type.
inline constexpr WordCount total() const { return data + pointers * WORDS_PER_REFERENCE; }
inline constexpr WordCount total() const { return data + pointers * WORDS_PER_POINTER; }
StructSize() = default;
inline constexpr StructSize(WordCount data, WireReferenceCount pointers,
inline constexpr StructSize(WordCount data, WirePointerCount pointers,
FieldSize preferredListEncoding)
: data(data), pointers(pointers), preferredListEncoding(preferredListEncoding) {}
};
......@@ -288,13 +288,13 @@ private:
class StructBuilder {
public:
inline StructBuilder(): segment(nullptr), data(nullptr), references(nullptr), bit0Offset(0) {}
inline StructBuilder(): segment(nullptr), data(nullptr), pointers(nullptr), bit0Offset(0) {}
static StructBuilder initRoot(SegmentBuilder* segment, word* location, StructSize size);
static StructBuilder getRoot(SegmentBuilder* segment, word* location, StructSize size);
inline BitCount getDataSectionSize() const { return dataSize; }
inline WireReferenceCount getPointerSectionSize() const { return referenceCount; }
inline WirePointerCount getPointerSectionSize() const { return pointerCount; }
inline Data::Builder getDataSectionAsBlob();
template <typename T>
......@@ -318,48 +318,48 @@ public:
// Like setDataField() but applies the given XOR mask before storing. Used for writing fields
// with non-zero default values.
StructBuilder initStructField(WireReferenceCount refIndex, StructSize size) const;
// Initializes the struct field at the given index in the reference segment. If it is already
StructBuilder initStructField(WirePointerCount ptrIndex, StructSize size) const;
// Initializes the struct field at the given index in the pointer segment. If it is already
// initialized, the previous value is discarded or overwritten. The struct is initialized to
// the type's default state (all-zero). Use getStructField() if you want the struct to be
// initialized as a copy of the field's default value (which may have non-null references).
// initialized as a copy of the field's default value (which may have non-null pointers).
StructBuilder getStructField(WireReferenceCount refIndex, StructSize size,
StructBuilder getStructField(WirePointerCount ptrIndex, StructSize size,
const word* defaultValue) const;
// Gets the struct field at the given index in the reference segment. If the field is not already
// Gets the struct field at the given index in the pointer segment. If the field is not already
// initialized, it is initialized as a deep copy of the given default value (a trusted message),
// or to the empty state if defaultValue is nullptr.
ListBuilder initListField(WireReferenceCount refIndex, FieldSize elementSize,
ListBuilder initListField(WirePointerCount ptrIndex, FieldSize elementSize,
ElementCount elementCount) const;
// Allocates a new list of the given size for the field at the given index in the reference
// Allocates a new list of the given size for the field at the given index in the pointer
// segment, and return a pointer to it. All elements are initialized to zero.
ListBuilder initStructListField(WireReferenceCount refIndex, ElementCount elementCount,
ListBuilder initStructListField(WirePointerCount ptrIndex, ElementCount elementCount,
StructSize size) const;
// Allocates a new list of the given size for the field at the given index in the reference
// Allocates a new list of the given size for the field at the given index in the pointer
// segment, and return a pointer to it. Each element is initialized to its empty state.
ListBuilder getListField(WireReferenceCount refIndex, const word* defaultValue) const;
// Gets the already-allocated list field for the given reference index. If the list is not
ListBuilder getListField(WirePointerCount ptrIndex, const word* defaultValue) const;
// Gets the already-allocated list field for the given pointer index. If the list is not
// already allocated, it is allocated as a deep copy of the given default value (a trusted
// message). If the default value is null, an empty list is used.
template <typename T>
typename T::Builder initBlobField(WireReferenceCount refIndex, ByteCount size) const;
typename T::Builder initBlobField(WirePointerCount ptrIndex, ByteCount size) const;
// Initialize a Text or Data field to the given size in bytes (not including NUL terminator for
// Text) and return a Text::Builder which can be used to fill in the content.
template <typename T>
void setBlobField(WireReferenceCount refIndex, typename T::Reader value) const;
void setBlobField(WirePointerCount ptrIndex, typename T::Reader value) const;
// Set the blob field to a copy of the given blob.
template <typename T>
typename T::Builder getBlobField(WireReferenceCount refIndex,
typename T::Builder getBlobField(WirePointerCount ptrIndex,
const void* defaultValue, ByteCount defaultSize) const;
// Get the blob field. If it is not initialized, initialize it to a copy of the given default.
ObjectBuilder getObjectField(WireReferenceCount refIndex, const word* defaultValue) const;
ObjectBuilder getObjectField(WirePointerCount ptrIndex, const word* defaultValue) const;
// Read a pointer of arbitrary type.
StructReader asReader() const;
......@@ -368,23 +368,23 @@ public:
private:
SegmentBuilder* segment; // Memory segment in which the struct resides.
void* data; // Pointer to the encoded data.
WireReference* references; // Pointer to the encoded references.
WirePointer* pointers; // Pointer to the encoded pointers.
BitCount32 dataSize;
// Size of data segment. We use a bit count rather than a word count to more easily handle the
// case of struct lists encoded with less than a word per element.
WireReferenceCount16 referenceCount; // Size of the reference segment.
WirePointerCount16 pointerCount; // Size of the pointer segment.
BitCount8 bit0Offset;
// A special hack: If dataSize == 1 bit, then bit0Offset is the offset of that bit within the
// byte pointed to by `data`. In all other cases, this is zero. This is needed to implement
// struct lists where each struct is one bit.
inline StructBuilder(SegmentBuilder* segment, void* data, WireReference* references,
BitCount dataSize, WireReferenceCount referenceCount, BitCount8 bit0Offset)
: segment(segment), data(data), references(references),
dataSize(dataSize), referenceCount(referenceCount), bit0Offset(bit0Offset) {}
inline StructBuilder(SegmentBuilder* segment, void* data, WirePointer* pointers,
BitCount dataSize, WirePointerCount pointerCount, BitCount8 bit0Offset)
: segment(segment), data(data), pointers(pointers),
dataSize(dataSize), pointerCount(pointerCount), bit0Offset(bit0Offset) {}
friend class ListBuilder;
friend struct WireHelpers;
......@@ -393,14 +393,14 @@ private:
class StructReader {
public:
inline StructReader()
: segment(nullptr), data(nullptr), references(nullptr), dataSize(0),
referenceCount(0), bit0Offset(0), nestingLimit(0) {}
: segment(nullptr), data(nullptr), pointers(nullptr), dataSize(0),
pointerCount(0), bit0Offset(0), nestingLimit(0) {}
static StructReader readRootTrusted(const word* location);
static StructReader readRoot(const word* location, SegmentReader* segment, int nestingLimit);
inline BitCount getDataSectionSize() const { return dataSize; }
inline WireReferenceCount getPointerSectionSize() const { return referenceCount; }
inline WirePointerCount getPointerSectionSize() const { return pointerCount; }
inline Data::Reader getDataSectionAsBlob();
template <typename T>
......@@ -415,26 +415,26 @@ public:
// Like getDataField(offset), but applies the given XOR mask to the result. Used for reading
// fields with non-zero default values.
StructReader getStructField(WireReferenceCount refIndex, const word* defaultValue) const;
// Get the struct field at the given index in the reference segment, or the default value if not
StructReader getStructField(WirePointerCount ptrIndex, const word* defaultValue) const;
// Get the struct field at the given index in the pointer segment, or the default value if not
// initialized. defaultValue will be interpreted as a trusted message -- it must point at a
// struct reference, which in turn points at the struct value. The default value is allowed to
// struct pointer, which in turn points at the struct value. The default value is allowed to
// be null, in which case an empty struct is used.
ListReader getListField(WireReferenceCount refIndex, FieldSize expectedElementSize,
ListReader getListField(WirePointerCount ptrIndex, FieldSize expectedElementSize,
const word* defaultValue) const;
// Get the list field at the given index in the reference segment, or the default value if not
// Get the list field at the given index in the pointer segment, or the default value if not
// initialized. The default value is allowed to be null, in which case an empty list is used.
template <typename T>
typename T::Reader getBlobField(WireReferenceCount refIndex,
typename T::Reader getBlobField(WirePointerCount ptrIndex,
const void* defaultValue, ByteCount defaultSize) const;
// Gets the text or data field, or the given default value if not initialized.
ObjectReader getObjectField(WireReferenceCount refIndex, const word* defaultValue) const;
ObjectReader getObjectField(WirePointerCount ptrIndex, const word* defaultValue) const;
// Read a pointer of arbitrary type.
const word* getTrustedPointer(WireReferenceCount refIndex) const;
const word* getTrustedPointer(WirePointerCount ptrIndex) const;
// If this is a trusted message, get a word* pointing at the location of the pointer. This
// word* can actually be passed to readTrusted() to read the designated sub-object later. If
// this isn't a trusted message, throws an exception.
......@@ -443,13 +443,13 @@ private:
SegmentReader* segment; // Memory segment in which the struct resides.
const void* data;
const WireReference* references;
const WirePointer* pointers;
BitCount32 dataSize;
// Size of data segment. We use a bit count rather than a word count to more easily handle the
// case of struct lists encoded with less than a word per element.
WireReferenceCount16 referenceCount; // Size of the reference segment.
WirePointerCount16 pointerCount; // Size of the pointer segment.
BitCount8 bit0Offset;
// A special hack: If dataSize == 1 bit, then bit0Offset is the offset of that bit within the
......@@ -466,11 +466,11 @@ private:
// Once this reaches zero, further pointers will be pruned.
// TODO: Limit to 8 bits for better alignment?
inline StructReader(SegmentReader* segment, const void* data, const WireReference* references,
BitCount dataSize, WireReferenceCount referenceCount, BitCount8 bit0Offset,
inline StructReader(SegmentReader* segment, const void* data, const WirePointer* pointers,
BitCount dataSize, WirePointerCount pointerCount, BitCount8 bit0Offset,
int nestingLimit)
: segment(segment), data(data), references(references),
dataSize(dataSize), referenceCount(referenceCount), bit0Offset(bit0Offset),
: segment(segment), data(data), pointers(pointers),
dataSize(dataSize), pointerCount(pointerCount), bit0Offset(bit0Offset),
nestingLimit(nestingLimit) {}
friend class ListReader;
......@@ -512,7 +512,7 @@ public:
ListBuilder initStructListElement(ElementCount index, ElementCount elementCount,
StructSize size) const;
// Allocates a new list of the given size for the field at the given index in the reference
// Allocates a new list of the given size for the field at the given index in the pointer
// segment, and return a pointer to it. Each element is initialized to its empty state.
ListBuilder getListElement(ElementCount index) const;
......@@ -549,16 +549,16 @@ private:
// The distance between elements.
BitCount32 structDataSize;
WireReferenceCount16 structReferenceCount;
WirePointerCount16 structPointerCount;
// The struct properties to use when interpreting the elements as structs. All lists can be
// interpreted as struct lists, so these are always filled in.
inline ListBuilder(SegmentBuilder* segment, void* ptr,
decltype(BITS / ELEMENTS) step, ElementCount size,
BitCount structDataSize, WireReferenceCount structReferenceCount)
BitCount structDataSize, WirePointerCount structPointerCount)
: segment(segment), ptr(reinterpret_cast<byte*>(ptr)),
elementCount(size), step(step), structDataSize(structDataSize),
structReferenceCount(structReferenceCount) {}
structPointerCount(structPointerCount) {}
friend class StructBuilder;
friend struct WireHelpers;
......@@ -568,7 +568,7 @@ class ListReader {
public:
inline ListReader()
: segment(nullptr), ptr(nullptr), elementCount(0), step(0 * BITS / ELEMENTS),
structDataSize(0), structReferenceCount(0), nestingLimit(0) {}
structDataSize(0), structPointerCount(0), nestingLimit(0) {}
inline ElementCount size() const;
// The number of elements in the list.
......@@ -605,7 +605,7 @@ private:
// The distance between elements.
BitCount32 structDataSize;
WireReferenceCount16 structReferenceCount;
WirePointerCount16 structPointerCount;
// The struct properties to use when interpreting the elements as structs. All lists can be
// interpreted as struct lists, so these are always filled in.
......@@ -615,11 +615,11 @@ private:
inline ListReader(SegmentReader* segment, const void* ptr,
ElementCount elementCount, decltype(BITS / ELEMENTS) step,
BitCount structDataSize, WireReferenceCount structReferenceCount,
BitCount structDataSize, WirePointerCount structPointerCount,
int nestingLimit)
: segment(segment), ptr(reinterpret_cast<const byte*>(ptr)), elementCount(elementCount),
step(step), structDataSize(structDataSize),
structReferenceCount(structReferenceCount), nestingLimit(nestingLimit) {}
structPointerCount(structPointerCount), nestingLimit(nestingLimit) {}
friend class StructReader;
friend class ListBuilder;
......@@ -835,19 +835,19 @@ inline Void ListReader::getDataElement<Void>(ElementCount index) const {
}
// These are defined in the source file.
template <> typename Text::Builder StructBuilder::initBlobField<Text>(WireReferenceCount refIndex, ByteCount size) const;
template <> void StructBuilder::setBlobField<Text>(WireReferenceCount refIndex, typename Text::Reader value) const;
template <> typename Text::Builder StructBuilder::getBlobField<Text>(WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Text::Reader StructReader::getBlobField<Text>(WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Text::Builder StructBuilder::initBlobField<Text>(WirePointerCount ptrIndex, ByteCount size) const;
template <> void StructBuilder::setBlobField<Text>(WirePointerCount ptrIndex, typename Text::Reader value) const;
template <> typename Text::Builder StructBuilder::getBlobField<Text>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Text::Reader StructReader::getBlobField<Text>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Text::Builder ListBuilder::initBlobElement<Text>(ElementCount index, ByteCount size) const;
template <> void ListBuilder::setBlobElement<Text>(ElementCount index, typename Text::Reader value) const;
template <> typename Text::Builder ListBuilder::getBlobElement<Text>(ElementCount index) const;
template <> typename Text::Reader ListReader::getBlobElement<Text>(ElementCount index) const;
template <> typename Data::Builder StructBuilder::initBlobField<Data>(WireReferenceCount refIndex, ByteCount size) const;
template <> void StructBuilder::setBlobField<Data>(WireReferenceCount refIndex, typename Data::Reader value) const;
template <> typename Data::Builder StructBuilder::getBlobField<Data>(WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Data::Reader StructReader::getBlobField<Data>(WireReferenceCount refIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Data::Builder StructBuilder::initBlobField<Data>(WirePointerCount ptrIndex, ByteCount size) const;
template <> void StructBuilder::setBlobField<Data>(WirePointerCount ptrIndex, typename Data::Reader value) const;
template <> typename Data::Builder StructBuilder::getBlobField<Data>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Data::Reader StructReader::getBlobField<Data>(WirePointerCount ptrIndex, const void* defaultValue, ByteCount defaultSize) const;
template <> typename Data::Builder ListBuilder::initBlobElement<Data>(ElementCount index, ByteCount size) const;
template <> void ListBuilder::setBlobElement<Data>(ElementCount index, typename Data::Reader value) const;
template <> typename Data::Builder ListBuilder::getBlobElement<Data>(ElementCount index) const;
......
......@@ -94,22 +94,22 @@ template <typename T> struct FieldSizeForType {
kind<T>() == Kind::STRUCT ? FieldSize::INLINE_COMPOSITE :
// Everything else is a pointer.
FieldSize::REFERENCE;
FieldSize::POINTER;
};
// Void and bool are special.
template <> struct FieldSizeForType<Void> { static constexpr FieldSize value = FieldSize::VOID; };
template <> struct FieldSizeForType<bool> { static constexpr FieldSize value = FieldSize::BIT; };
// Lists and blobs are references, not structs.
// Lists and blobs are pointers, not structs.
template <typename T, bool b> struct FieldSizeForType<List<T, b>> {
static constexpr FieldSize value = FieldSize::REFERENCE;
static constexpr FieldSize value = FieldSize::POINTER;
};
template <> struct FieldSizeForType<Text> {
static constexpr FieldSize value = FieldSize::REFERENCE;
static constexpr FieldSize value = FieldSize::POINTER;
};
template <> struct FieldSizeForType<Data> {
static constexpr FieldSize value = FieldSize::REFERENCE;
static constexpr FieldSize value = FieldSize::POINTER;
};
template <typename T>
......@@ -265,15 +265,15 @@ private:
}
inline static internal::ListBuilder initAsFieldOf(
internal::StructBuilder& builder, WireReferenceCount index, uint size) {
internal::StructBuilder& builder, WirePointerCount index, uint size) {
return builder.initListField(index, internal::FieldSizeForType<T>::value, size * ELEMENTS);
}
inline static internal::ListBuilder getAsFieldOf(
internal::StructBuilder& builder, WireReferenceCount index, const word* defaultValue) {
internal::StructBuilder& builder, WirePointerCount index, const word* defaultValue) {
return builder.getListField(index, defaultValue);
}
inline static internal::ListReader getAsFieldOf(
internal::StructReader& reader, WireReferenceCount index, const word* defaultValue) {
internal::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSizeForType<T>::value, defaultValue);
}
......@@ -351,15 +351,15 @@ private:
}
inline static internal::ListBuilder initAsFieldOf(
internal::StructBuilder& builder, WireReferenceCount index, uint size) {
internal::StructBuilder& builder, WirePointerCount index, uint size) {
return builder.initStructListField(index, size * ELEMENTS, internal::structSize<T>());
}
inline static internal::ListBuilder getAsFieldOf(
internal::StructBuilder& builder, WireReferenceCount index, const word* defaultValue) {
internal::StructBuilder& builder, WirePointerCount index, const word* defaultValue) {
return builder.getListField(index, defaultValue);
}
inline static internal::ListReader getAsFieldOf(
internal::StructReader& reader, WireReferenceCount index, const word* defaultValue) {
internal::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSize::INLINE_COMPOSITE, defaultValue);
}
......@@ -425,7 +425,7 @@ private:
inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) {
return builder.initListElement(
index * ELEMENTS, internal::FieldSize::REFERENCE, size * ELEMENTS);
index * ELEMENTS, internal::FieldSize::POINTER, size * ELEMENTS);
}
inline static internal::ListBuilder getAsElementOf(
const internal::ListBuilder& builder, uint index) {
......@@ -433,20 +433,20 @@ private:
}
inline static internal::ListReader getAsElementOf(
const internal::ListReader& reader, uint index) {
return reader.getListElement(index * ELEMENTS, internal::FieldSize::REFERENCE);
return reader.getListElement(index * ELEMENTS, internal::FieldSize::POINTER);
}
inline static internal::ListBuilder initAsFieldOf(
internal::StructBuilder& builder, WireReferenceCount index, uint size) {
return builder.initListField(index, internal::FieldSize::REFERENCE, size * ELEMENTS);
internal::StructBuilder& builder, WirePointerCount index, uint size) {
return builder.initListField(index, internal::FieldSize::POINTER, size * ELEMENTS);
}
inline static internal::ListBuilder getAsFieldOf(
internal::StructBuilder& builder, WireReferenceCount index, const word* defaultValue) {
internal::StructBuilder& builder, WirePointerCount index, const word* defaultValue) {
return builder.getListField(index, defaultValue);
}
inline static internal::ListReader getAsFieldOf(
internal::StructReader& reader, WireReferenceCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSize::REFERENCE, defaultValue);
internal::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSize::POINTER, defaultValue);
}
template <typename U, Kind k>
......@@ -526,7 +526,7 @@ private:
inline static internal::ListBuilder initAsElementOf(
const internal::ListBuilder& builder, uint index, uint size) {
return builder.initListElement(
index * ELEMENTS, internal::FieldSize::REFERENCE, size * ELEMENTS);
index * ELEMENTS, internal::FieldSize::POINTER, size * ELEMENTS);
}
inline static internal::ListBuilder getAsElementOf(
const internal::ListBuilder& builder, uint index) {
......@@ -534,20 +534,20 @@ private:
}
inline static internal::ListReader getAsElementOf(
const internal::ListReader& reader, uint index) {
return reader.getListElement(index * ELEMENTS, internal::FieldSize::REFERENCE);
return reader.getListElement(index * ELEMENTS, internal::FieldSize::POINTER);
}
inline static internal::ListBuilder initAsFieldOf(
internal::StructBuilder& builder, WireReferenceCount index, uint size) {
return builder.initListField(index, internal::FieldSize::REFERENCE, size * ELEMENTS);
internal::StructBuilder& builder, WirePointerCount index, uint size) {
return builder.initListField(index, internal::FieldSize::POINTER, size * ELEMENTS);
}
inline static internal::ListBuilder getAsFieldOf(
internal::StructBuilder& builder, WireReferenceCount index, const word* defaultValue) {
internal::StructBuilder& builder, WirePointerCount index, const word* defaultValue) {
return builder.getListField(index, defaultValue);
}
inline static internal::ListReader getAsFieldOf(
internal::StructReader& reader, WireReferenceCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSize::REFERENCE, defaultValue);
internal::StructReader& reader, WirePointerCount index, const word* defaultValue) {
return reader.getListField(index, internal::FieldSize::POINTER, defaultValue);
}
template <typename U, Kind k>
......
......@@ -78,11 +78,11 @@ internal::SegmentBuilder* MessageBuilder::getRootSegment() {
new(arena()) internal::BuilderArena(this);
allocatedArena = true;
WordCount refSize = 1 * REFERENCES * WORDS_PER_REFERENCE;
internal::SegmentBuilder* segment = arena()->getSegmentWithAvailable(refSize);
WordCount ptrSize = 1 * POINTERS * WORDS_PER_POINTER;
internal::SegmentBuilder* segment = arena()->getSegmentWithAvailable(ptrSize);
CHECK(segment->getSegmentId() == internal::SegmentId(0),
"First allocated word of new arena was not in segment ID 0.");
word* location = segment->allocate(refSize);
word* location = segment->allocate(ptrSize);
CHECK(location == segment->getPtrUnchecked(0 * WORDS),
"First allocated word of new arena was not the first word in its segment.");
return segment;
......
......@@ -391,7 +391,7 @@ TEST(Packed, RoundTripAllZero) {
checkTestMessageAllZero(reader.getRoot<TestAllTypes>());
// Segment table packs to 2 bytes.
// Root reference packs to 3 bytes.
// Root pointer packs to 3 bytes.
// Content packs to 2 bytes (zero span).
EXPECT_LE(pipe.getData().size(), 7u);
}
......
......@@ -787,7 +787,7 @@ class word { uint64_t content; CAPNPROTO_DISALLOW_COPY(word); public: word() = d
static_assert(sizeof(byte) == 1, "uint8_t is not one byte?");
static_assert(sizeof(word) == 8, "uint64_t is not 8 bytes?");
namespace internal { class BitLabel; class ElementLabel; class WireReference; }
namespace internal { class BitLabel; class ElementLabel; class WirePointer; }
#ifndef CAPNPROTO_DEBUG_TYPES
#define CAPNPROTO_DEBUG_TYPES 1
......@@ -828,11 +828,11 @@ typedef Quantity<uint16_t, internal::ElementLabel> ElementCount16;
typedef Quantity<uint32_t, internal::ElementLabel> ElementCount32;
typedef Quantity<uint64_t, internal::ElementLabel> ElementCount64;
typedef Quantity<uint, internal::WireReference> WireReferenceCount;
typedef Quantity<uint8_t, internal::WireReference> WireReferenceCount8;
typedef Quantity<uint16_t, internal::WireReference> WireReferenceCount16;
typedef Quantity<uint32_t, internal::WireReference> WireReferenceCount32;
typedef Quantity<uint64_t, internal::WireReference> WireReferenceCount64;
typedef Quantity<uint, internal::WirePointer> WirePointerCount;
typedef Quantity<uint8_t, internal::WirePointer> WirePointerCount8;
typedef Quantity<uint16_t, internal::WirePointer> WirePointerCount16;
typedef Quantity<uint32_t, internal::WirePointer> WirePointerCount32;
typedef Quantity<uint64_t, internal::WirePointer> WirePointerCount64;
#else
......@@ -860,11 +860,11 @@ typedef uint16_t ElementCount16;
typedef uint32_t ElementCount32;
typedef uint64_t ElementCount64;
typedef uint WireReferenceCount;
typedef uint8_t WireReferenceCount8;
typedef uint16_t WireReferenceCount16;
typedef uint32_t WireReferenceCount32;
typedef uint64_t WireReferenceCount64;
typedef uint WirePointerCount;
typedef uint8_t WirePointerCount8;
typedef uint16_t WirePointerCount16;
typedef uint32_t WirePointerCount32;
typedef uint64_t WirePointerCount64;
#endif
......@@ -872,17 +872,17 @@ constexpr BitCount BITS = unit<BitCount>();
constexpr ByteCount BYTES = unit<ByteCount>();
constexpr WordCount WORDS = unit<WordCount>();
constexpr ElementCount ELEMENTS = unit<ElementCount>();
constexpr WireReferenceCount REFERENCES = unit<WireReferenceCount>();
constexpr WirePointerCount POINTERS = unit<WirePointerCount>();
constexpr auto BITS_PER_BYTE = 8 * BITS / BYTES;
constexpr auto BITS_PER_WORD = 64 * BITS / WORDS;
constexpr auto BYTES_PER_WORD = 8 * BYTES / WORDS;
constexpr auto BITS_PER_REFERENCE = 64 * BITS / REFERENCES;
constexpr auto BYTES_PER_REFERENCE = 8 * BYTES / REFERENCES;
constexpr auto WORDS_PER_REFERENCE = 1 * WORDS / REFERENCES;
constexpr auto BITS_PER_POINTER = 64 * BITS / POINTERS;
constexpr auto BYTES_PER_POINTER = 8 * BYTES / POINTERS;
constexpr auto WORDS_PER_POINTER = 1 * WORDS / POINTERS;
constexpr WordCount REFERENCE_SIZE_IN_WORDS = 1 * REFERENCES * WORDS_PER_REFERENCE;
constexpr WordCount POINTER_SIZE_IN_WORDS = 1 * POINTERS * WORDS_PER_POINTER;
template <typename T>
inline constexpr decltype(BYTES / ELEMENTS) bytesPerElement() {
......
......@@ -481,26 +481,26 @@ extractFieldNumbers decls = concat
data PackingState = PackingState
{ packingHoles :: Map.Map DataSize Integer
, packingDataSize :: Integer
, packingReferenceCount :: Integer
, packingPointerCount :: Integer
}
initialPackingState = PackingState Map.empty 0 0
packValue :: FieldSize -> PackingState -> (FieldOffset, PackingState)
packValue SizeVoid s = (VoidOffset, s)
packValue SizeReference s@(PackingState { packingReferenceCount = rc }) =
(PointerOffset rc, s { packingReferenceCount = rc + 1 })
packValue SizePointer s@(PackingState { packingPointerCount = rc }) =
(PointerOffset rc, s { packingPointerCount = rc + 1 })
packValue (SizeInlineComposite (DataSectionWords inlineDs) inlineRc)
s@(PackingState { packingDataSize = ds, packingReferenceCount = rc }) =
s@(PackingState { packingDataSize = ds, packingPointerCount = rc }) =
(InlineCompositeOffset ds rc (DataSectionWords inlineDs) inlineRc,
s { packingDataSize = ds + inlineDs
, packingReferenceCount = rc + inlineRc })
, packingPointerCount = rc + inlineRc })
packValue (SizeInlineComposite inlineDs inlineRc)
s@(PackingState { packingReferenceCount = rc }) = let
s@(PackingState { packingPointerCount = rc }) = let
size = (dataSectionAlignment inlineDs)
(offset, s2) = packData size s
in (InlineCompositeOffset offset rc inlineDs inlineRc,
s2 { packingReferenceCount = rc + inlineRc })
s2 { packingPointerCount = rc + inlineRc })
packValue (SizeData size) s = let (o, s2) = packData size s in (DataOffset size o, s2)
packData :: DataSize -> PackingState -> (Integer, PackingState)
......@@ -569,14 +569,14 @@ packUnionizedValue (SizeData size) (UnionPackingState (UnionSlot slotSize slotOf
Nothing -> packUnionizedValue (SizeData size)
(UnionPackingState (UnionSlot (DataSectionWords 0) 0) p) s
-- Pack reference when we don't have a reference slot.
packUnionizedValue SizeReference u@(UnionPackingState _ (UnionSlot 0 _)) s = let
(PointerOffset offset, s2) = packValue SizeReference s
-- Pack pointer when we don't have a pointer slot.
packUnionizedValue SizePointer u@(UnionPackingState _ (UnionSlot 0 _)) s = let
(PointerOffset offset, s2) = packValue SizePointer s
u2 = u { unionPointerSlot = UnionSlot 1 offset }
in (PointerOffset offset, u2, s2)
-- Pack reference when we already have a reference slot allocated.
packUnionizedValue SizeReference u@(UnionPackingState _ (UnionSlot _ offset)) s =
-- Pack pointer when we already have a pointer slot allocated.
packUnionizedValue SizePointer u@(UnionPackingState _ (UnionSlot _ offset)) s =
(PointerOffset offset, u, s)
-- Pack inline composite.
......@@ -621,14 +621,14 @@ packUnionizedValue (SizeInlineComposite dataSize pointerCount)
-- Pack the pointer section.
(pointerOffset, u3, s3)
| pointerCount <= pointerSlotSize = (pointerSlotOffset, u2, s2)
| pointerSlotOffset + pointerSlotSize == packingReferenceCount s2 =
| pointerSlotOffset + pointerSlotSize == packingPointerCount s2 =
(pointerSlotOffset,
u2 { unionPointerSlot = UnionSlot pointerCount pointerSlotOffset },
s2 { packingReferenceCount = pointerSlotOffset + pointerCount })
s2 { packingPointerCount = pointerSlotOffset + pointerCount })
| otherwise =
(packingReferenceCount s2,
u2 { unionPointerSlot = UnionSlot pointerCount (packingReferenceCount s2) },
s2 { packingReferenceCount = packingReferenceCount s2 + pointerCount })
(packingPointerCount s2,
u2 { unionPointerSlot = UnionSlot pointerCount (packingPointerCount s2) },
s2 { packingPointerCount = packingPointerCount s2 + pointerCount })
combinedOffset = InlineCompositeOffset
{ inlineCompositeDataOffset = dataOffset
......@@ -738,7 +738,7 @@ packFields fields unions = let
then dataSizeToSectionSize $ stripHolesFromFirstWord Size64 $ packingHoles finalState
else DataSectionWords $ packingDataSize finalState
in (dataSectionSize, packingReferenceCount finalState, Map.fromList packedItems)
in (dataSectionSize, packingPointerCount finalState, Map.fromList packedItems)
enforceFixed Nothing sizes = return sizes
enforceFixed (Just (Located pos (requestedDataSize, requestedPointerCount)))
......
......@@ -195,7 +195,7 @@ cxxFieldSizeString (SizeData Size8) = "BYTE";
cxxFieldSizeString (SizeData Size16) = "TWO_BYTES";
cxxFieldSizeString (SizeData Size32) = "FOUR_BYTES";
cxxFieldSizeString (SizeData Size64) = "EIGHT_BYTES";
cxxFieldSizeString SizeReference = "REFERENCE";
cxxFieldSizeString SizePointer = "POINTER";
cxxFieldSizeString (SizeInlineComposite _ _) = "INLINE_COMPOSITE";
fieldOffsetInteger VoidOffset = "0"
......@@ -207,7 +207,7 @@ fieldOffsetInteger (InlineCompositeOffset d p ds ps) = let
DataSectionWords _ -> d * 8
_ -> d * byteSize
in printf "%d * ::capnproto::BYTES, %d * ::capnproto::BYTES, \
\%d * ::capnproto::REFERENCES, %d * ::capnproto::REFERENCES" byteOffset byteSize p ps
\%d * ::capnproto::POINTERS, %d * ::capnproto::POINTERS" byteOffset byteSize p ps
isDefaultZero VoidDesc = True
isDefaultZero (BoolDesc b) = not b
......@@ -473,7 +473,7 @@ outerFileContext schemaNodes = fileContext where
context "structFields" = MuList $ map (fieldContext context) $ structFields desc
context "structUnions" = MuList $ map (unionContext context) $ structUnions desc
context "structDataSize" = MuVariable $ dataSectionWordSize $ structDataSize desc
context "structReferenceCount" = MuVariable $ structPointerCount desc
context "structPointerCount" = MuVariable $ structPointerCount desc
context "structPreferredListEncoding" = case (structDataSize desc, structPointerCount desc) of
(DataSectionWords 0, 0) -> MuVariable "VOID"
(DataSection1, 0) -> MuVariable "BIT"
......@@ -481,7 +481,7 @@ outerFileContext schemaNodes = fileContext where
(DataSection16, 0) -> MuVariable "TWO_BYTES"
(DataSection32, 0) -> MuVariable "FOUR_BYTES"
(DataSectionWords 1, 0) -> MuVariable "EIGHT_BYTES"
(DataSectionWords 0, 1) -> MuVariable "REFERENCE"
(DataSectionWords 0, 1) -> MuVariable "POINTER"
_ -> MuVariable "INLINE_COMPOSITE"
context "structNestedEnums" =
MuList $ map (enumContext context) [m | DescEnum m <- structMembers desc]
......
......@@ -272,12 +272,12 @@ dataSizeInBits Size64 = 64
data FieldSize = SizeVoid
| SizeData DataSize
| SizeReference
| SizePointer
| SizeInlineComposite DataSectionSize Integer
fieldSizeInBits SizeVoid = 0
fieldSizeInBits (SizeData d) = dataSizeInBits d
fieldSizeInBits SizeReference = 64
fieldSizeInBits SizePointer = 64
fieldSizeInBits (SizeInlineComposite ds pc) = dataSectionBits ds + pc * 64
data FieldOffset = VoidOffset
......@@ -293,7 +293,7 @@ data FieldOffset = VoidOffset
offsetToSize :: FieldOffset -> FieldSize
offsetToSize VoidOffset = SizeVoid
offsetToSize (DataOffset s _) = SizeData s
offsetToSize (PointerOffset _) = SizeReference
offsetToSize (PointerOffset _) = SizePointer
offsetToSize (InlineCompositeOffset _ _ d p) = SizeInlineComposite d p
fieldSize (BuiltinType BuiltinVoid) = SizeVoid
......@@ -308,15 +308,15 @@ fieldSize (BuiltinType BuiltinUInt32) = SizeData Size32
fieldSize (BuiltinType BuiltinUInt64) = SizeData Size64
fieldSize (BuiltinType BuiltinFloat32) = SizeData Size32
fieldSize (BuiltinType BuiltinFloat64) = SizeData Size64
fieldSize (BuiltinType BuiltinText) = SizeReference
fieldSize (BuiltinType BuiltinData) = SizeReference
fieldSize (BuiltinType BuiltinObject) = SizeReference
fieldSize (BuiltinType BuiltinText) = SizePointer
fieldSize (BuiltinType BuiltinData) = SizePointer
fieldSize (BuiltinType BuiltinObject) = SizePointer
fieldSize (EnumType _) = SizeData Size16
fieldSize (StructType _) = SizeReference
fieldSize (StructType _) = SizePointer
fieldSize (InlineStructType StructDesc { structDataSize = ds, structPointerCount = ps }) =
SizeInlineComposite ds ps
fieldSize (InterfaceType _) = SizeReference
fieldSize (ListType _) = SizeReference
fieldSize (InterfaceType _) = SizePointer
fieldSize (ListType _) = SizePointer
fieldSize (InlineListType element size) = let
minDataSectionForBits bits
| bits <= 0 = DataSectionWords 0
......@@ -328,12 +328,12 @@ fieldSize (InlineListType element size) = let
dataSection = case fieldSize element of
SizeVoid -> DataSectionWords 0
SizeData s -> minDataSectionForBits $ dataSizeInBits s * size
SizeReference -> DataSectionWords 0
SizePointer -> DataSectionWords 0
SizeInlineComposite ds _ -> minDataSectionForBits $ dataSectionBits ds * size
pointerCount = case fieldSize element of
SizeVoid -> 0
SizeData _ -> 0
SizeReference -> size
SizePointer -> size
SizeInlineComposite _ pc -> pc * size
in SizeInlineComposite dataSection pointerCount
fieldSize (InlineDataType size)
......
......@@ -77,13 +77,13 @@ encodeMaskedDataValue t v (Just d) = xorData (encodeDataValue t v) (encodeDataVa
encodePointerValue :: TypeDesc -> ValueDesc -> (Integer -> [Word8], [Word8])
encodePointerValue _ (TextDesc text) = let
encoded = UTF8.encode text ++ [0]
in (encodeListReference (SizeData Size8) (genericLength encoded), padToWord encoded)
in (encodeListPointer (SizeData Size8) (genericLength encoded), padToWord encoded)
encodePointerValue _ (DataDesc d) =
(encodeListReference (SizeData Size8) (genericLength d), padToWord d)
(encodeListPointer (SizeData Size8) (genericLength d), padToWord d)
encodePointerValue (StructType desc) (StructValueDesc assignments) = let
(dataBytes, refBytes, childBytes) = encodeStruct desc assignments 0
in (encodeStructReference (structDataSize desc, structPointerCount desc),
concat [dataBytes, refBytes, childBytes])
(dataBytes, ptrBytes, childBytes) = encodeStruct desc assignments 0
in (encodeStructPointer (structDataSize desc, structPointerCount desc),
concat [dataBytes, ptrBytes, childBytes])
encodePointerValue (InlineStructType _) _ =
error "Tried to encode inline struct as a pointer."
encodePointerValue (ListType elementType) (ListDesc items) = encodeList elementType items
......@@ -132,15 +132,15 @@ packPointers size items o = loop 0 items (o + size - 1) where
in (genericReplicate (padCount * 8) 0 ++ restPtrs, restChildren)
loop idx [] _ = (genericReplicate ((size - idx) * 8) 0, [])
encodeStructReference (dataSize, pointerCount) offset =
encodeStructPointer (dataSize, pointerCount) offset =
intToBytes (offset * 4 + structTag) 4 ++
intToBytes (dataSectionWordSize dataSize) 2 ++
intToBytes pointerCount 2
encodeListReference elemSize@(SizeInlineComposite ds rc) elementCount offset =
encodeListPointer elemSize@(SizeInlineComposite ds rc) elementCount offset =
intToBytes (offset * 4 + listTag) 4 ++
intToBytes (fieldSizeEnum elemSize + shiftL (elementCount * (dataSectionWordSize ds + rc)) 3) 4
encodeListReference elemSize elementCount offset =
encodeListPointer elemSize elementCount offset =
intToBytes (offset * 4 + listTag) 4 ++
intToBytes (fieldSizeEnum elemSize + shiftL elementCount 3) 4
......@@ -150,13 +150,13 @@ fieldSizeEnum (SizeData Size8) = 2
fieldSizeEnum (SizeData Size16) = 3
fieldSizeEnum (SizeData Size32) = 4
fieldSizeEnum (SizeData Size64) = 5
fieldSizeEnum SizeReference = 6
fieldSizeEnum SizePointer = 6
fieldSizeEnum (SizeInlineComposite _ _) = 7
structTag = 0
listTag = 1
-- childOffset = number of words between the last reference and the location where children will
-- childOffset = number of words between the last pointer and the location where children will
-- be allocated.
encodeStruct desc assignments childOffset = let
dataSize = dataSectionBits $ structDataSize desc
......@@ -224,38 +224,38 @@ encodeList :: TypeDesc -- Type of each element.
-- Encode a list of empty structs as void.
encodeList (StructType StructDesc {
structDataSize = DataSectionWords 0, structPointerCount = 0 }) elements =
(encodeListReference SizeVoid (genericLength elements), [])
(encodeListPointer SizeVoid (genericLength elements), [])
-- Encode a list of sub-word data-only structs as a list of primitives.
encodeList (StructType desc@StructDesc { structDataSize = ds, structPointerCount = 0 }) elements
| dataSectionBits ds <= 64 = let
in (encodeListReference (SizeData $ dataSectionAlignment ds) (genericLength elements),
in (encodeListPointer (SizeData $ dataSectionAlignment ds) (genericLength elements),
inlineStructListDataSection desc elements)
-- Encode a list of single-pointer structs as a list of pointers.
encodeList (StructType desc@StructDesc {
structDataSize = DataSectionWords 0, structPointerCount = 1 }) elements = let
(refBytes, childBytes) = inlineStructListPointerSection desc elements
in (encodeListReference SizeReference (genericLength elements), refBytes ++ childBytes)
(ptrBytes, childBytes) = inlineStructListPointerSection desc elements
in (encodeListPointer SizePointer (genericLength elements), ptrBytes ++ childBytes)
-- Encode a list of any other sort of struct.
encodeList (StructType desc) elements = let
count = genericLength elements
tag = encodeStructReference (structDataSize desc, structPointerCount desc) count
tag = encodeStructPointer (structDataSize desc, structPointerCount desc) count
eSize = dataSectionWordSize (structDataSize desc) + structPointerCount desc
structElems = [v | StructValueDesc v <- elements]
(elemBytes, childBytes) = loop (eSize * genericLength structElems) structElems
loop _ [] = ([], [])
loop offset (element:rest) = let
offsetFromElementEnd = offset - eSize
(dataBytes, refBytes, childBytes2) = encodeStruct desc element offsetFromElementEnd
(dataBytes, ptrBytes, childBytes2) = encodeStruct desc element offsetFromElementEnd
childLen = genericLength childBytes2
childWordLen = if mod childLen 8 == 0
then div childLen 8
else error "Child not word-aligned."
(restBytes, restChildren) = loop (offsetFromElementEnd + childWordLen) rest
in (dataBytes ++ refBytes ++ restBytes, childBytes2 ++ restChildren)
in (encodeListReference (SizeInlineComposite (structDataSize desc) (structPointerCount desc))
in (dataBytes ++ ptrBytes ++ restBytes, childBytes2 ++ restChildren)
in (encodeListPointer (SizeInlineComposite (structDataSize desc) (structPointerCount desc))
(genericLength elements),
concat [tag, elemBytes, childBytes])
......@@ -280,14 +280,14 @@ encodeList elementType elements = let
dataBytes = case eSize of
SizeVoid -> []
SizeInlineComposite _ _ -> error "All inline composites should have been handled above."
SizeReference -> refBytes ++ childBytes where
SizePointer -> ptrBytes ++ childBytes where
encodedElements = zip [0..] $ map (encodePointerValue elementType) elements
(refBytes, childBytes) = packPointers (genericLength elements) encodedElements 0
(ptrBytes, childBytes) = packPointers (genericLength elements) encodedElements 0
SizeData size -> let
bits = dataSizeInBits size
encodedElements = zip [0,bits..] $ map (encodeDataValue elementType) elements
in packBytes (genericLength elements * bits) encodedElements
in (encodeListReference eSize (genericLength elements), dataBytes)
in (encodeListPointer eSize (genericLength elements), dataBytes)
---------------------------------------------
......@@ -298,7 +298,7 @@ inlineListDataSectionValues elementType elements = case fieldSize elementType of
InlineListType t _ -> inlineListDataSectionValues t (concat [l | ListDesc l <- elements])
InlineDataType _ -> [(0, EncodedBytes $ concat [l | DataDesc l <- elements])]
_ -> error "Unknown inline composite type."
SizeReference -> []
SizePointer -> []
SizeData size -> let
bits = dataSizeInBits size
in zip [0,bits..] $ map (encodeDataValue elementType) elements
......@@ -310,7 +310,7 @@ inlineListPointerSectionValues elementType elements = case fieldSize elementType
InlineListType t _ -> inlineListPointerSectionValues t (concat [l | ListDesc l <- elements])
InlineDataType _ -> []
_ -> error "Unknown inline composite type."
SizeReference -> zip [0..] $ map (encodePointerValue elementType) elements
SizePointer -> zip [0..] $ map (encodePointerValue elementType) elements
SizeData _ -> []
inlineStructListDataSection elementDesc elements =
......@@ -338,9 +338,9 @@ inlineStructListPointerSectionValues elementDesc elements = do
------------------------------------------------------------------------------------------
encodeMessage (StructType desc) (StructValueDesc assignments) = let
(dataBytes, refBytes, childBytes) = encodeStruct desc assignments 0
in concat [encodeStructReference (structDataSize desc, structPointerCount desc) (0::Integer),
dataBytes, refBytes, childBytes]
(dataBytes, ptrBytes, childBytes) = encodeStruct desc assignments 0
in concat [encodeStructPointer (structDataSize desc, structPointerCount desc) (0::Integer),
dataBytes, ptrBytes, childBytes]
encodeMessage (ListType elementType) (ListDesc elements) = let
(ptr, listBytes) = encodeList elementType elements
in ptr (0::Integer) ++ listBytes
......@@ -368,20 +368,20 @@ encodeSchema requestedFiles allFiles = (encRoot, nodesForEmbedding) where
elemBits = dataSizeInBits elementSize
bytes = packBytes (elemBits * genericLength elements)
$ zip [0,elemBits..] elements
in (encodeListReference (SizeData elementSize) (genericLength elements), bytes)
in (encodeListPointer (SizeData elementSize) (genericLength elements), bytes)
-- Not used, but maybe useful in the future.
--encPtrList :: [EncodedPtr] -> EncodedPtr
--encPtrList elements = let
-- (ptrBytes, childBytes) = packPointers (genericLength elements) (zip [0..] elements) 0
-- in (encodeListReference SizeReference (genericLength elements), ptrBytes ++ childBytes)
-- in (encodeListPointer SizePointer (genericLength elements), ptrBytes ++ childBytes)
encStructList :: (DataSectionSize, Integer)
-> [([(Integer, EncodedData)], [(Integer, EncodedPtr)])]
-> EncodedPtr
encStructList elementSize@(dataSize, pointerCount) elements = let
count = genericLength elements
tag = encodeStructReference elementSize count
tag = encodeStructPointer elementSize count
eSize = dataSectionWordSize dataSize + pointerCount
(elemBytes, childBytes) = loop (eSize * genericLength elements) elements
loop _ [] = ([], [])
......@@ -395,7 +395,7 @@ encodeSchema requestedFiles allFiles = (encRoot, nodesForEmbedding) where
else error "Child not word-aligned."
(restBytes, restChildren) = loop (offsetFromElementEnd + childWordLen) rest
in (concat [dataBytes, ptrBytes, restBytes], childBytes2 ++ restChildren)
in (encodeListReference (SizeInlineComposite dataSize pointerCount) (genericLength elements),
in (encodeListPointer (SizeInlineComposite dataSize pointerCount) (genericLength elements),
concat [tag, elemBytes, childBytes])
encStructBody :: (DataSectionSize, Integer)
......@@ -413,7 +413,7 @@ encodeSchema requestedFiles allFiles = (encRoot, nodesForEmbedding) where
-> EncodedPtr
encStruct size (dataValues, ptrValues) = let
(dataBytes, ptrBytes, childBytes) = encStructBody size dataValues ptrValues 0
in (encodeStructReference size, concat [dataBytes, ptrBytes, childBytes])
in (encodeStructPointer size, concat [dataBytes, ptrBytes, childBytes])
---------------------------------------------
......@@ -497,14 +497,14 @@ encodeSchema requestedFiles allFiles = (encRoot, nodesForEmbedding) where
(Nothing, _) -> []
(_, SizeVoid) -> []
(Just value, SizeData _) -> [ (64, encodeDataValue t value) ]
(_, SizeReference) -> []
(_, SizePointer) -> []
(_, SizeInlineComposite _ _) ->
error "Inline types not currently supported by codegen plugins.")
ptrValues = case (maybeValue, fieldSize t) of
(Nothing, _) -> []
(_, SizeVoid) -> []
(_, SizeData _) -> []
(Just value, SizeReference) -> [ (0, encodePointerValue t value) ]
(Just value, SizePointer) -> [ (0, encodePointerValue t value) ]
(_, SizeInlineComposite _ _) ->
error "Inline types not currently supported by codegen plugins."
......@@ -596,7 +596,7 @@ encodeSchema requestedFiles allFiles = (encRoot, nodesForEmbedding) where
preferredListEncoding = case (structDataSize desc, structPointerCount desc) of
(DataSectionWords 0, 0) -> SizeVoid
(DataSectionWords 0, 1) -> SizeReference
(DataSectionWords 0, 1) -> SizePointer
(DataSection1, 0) -> SizeData Size1
(DataSection8, 0) -> SizeData Size8
(DataSection16, 0) -> SizeData Size16
......
......@@ -113,7 +113,7 @@ namespace internal {
{{#typeStruct}}
CAPNPROTO_DECLARE_STRUCT(
::{{#fileNamespaces}}{{namespaceName}}::{{/fileNamespaces}}{{typeFullName}}, {{structId}},
{{structDataSize}}, {{structReferenceCount}}, {{structPreferredListEncoding}});
{{structDataSize}}, {{structPointerCount}}, {{structPreferredListEncoding}});
{{#structNestedEnums}}
CAPNPROTO_DECLARE_ENUM(
::{{#fileNamespaces}}{{namespaceName}}::{{/fileNamespaces}}{{typeFullName}}::{{enumName}}, {{enumId}});
......@@ -302,7 +302,7 @@ inline {{fieldType}}::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<{{fieldType}}>::get(
_reader, {{fieldOffset}} * ::capnproto::REFERENCES{{#fieldDefaultBytes}},
_reader, {{fieldOffset}} * ::capnproto::POINTERS{{#fieldDefaultBytes}},
DEFAULT_{{fieldUpperCase}}.words{{#fieldIsBlob}}, {{defaultBlobSize}}{{/fieldIsBlob}}{{/fieldDefaultBytes}});
}
......@@ -312,7 +312,7 @@ inline {{fieldType}}::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}()
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<{{fieldType}}>::get(
_builder, {{fieldOffset}} * ::capnproto::REFERENCES{{#fieldDefaultBytes}},
_builder, {{fieldOffset}} * ::capnproto::POINTERS{{#fieldDefaultBytes}},
DEFAULT_{{fieldUpperCase}}.words{{#fieldIsBlob}}, {{defaultBlobSize}}{{/fieldIsBlob}}{{/fieldDefaultBytes}});
}
......@@ -322,7 +322,7 @@ inline void {{typeFullName}}::Builder::set{{fieldTitleCase}}({{fieldType}}::Read
{{unionTagOffset}} * ::capnproto::ELEMENTS, {{unionTitleCase}}::{{fieldUpperCase}});
{{/fieldUnion}}
::capnproto::internal::PointerHelpers<{{fieldType}}>::set(
_builder, {{fieldOffset}} * ::capnproto::REFERENCES, value);
_builder, {{fieldOffset}} * ::capnproto::POINTERS, value);
}
{{#fieldIsList}}
......@@ -333,7 +333,7 @@ inline void {{typeFullName}}::Builder::set{{fieldTitleCase}}(
{{unionTagOffset}} * ::capnproto::ELEMENTS, {{unionTitleCase}}::{{fieldUpperCase}});
{{/fieldUnion}}
::capnproto::internal::PointerHelpers<{{fieldType}}>::set(
_builder, {{fieldOffset}} * ::capnproto::REFERENCES, value);
_builder, {{fieldOffset}} * ::capnproto::POINTERS, value);
}
{{/fieldIsList}}
......@@ -344,7 +344,7 @@ inline {{fieldType}}::Builder {{typeFullName}}::Builder::init{{fieldTitleCase}}(
{{unionTagOffset}} * ::capnproto::ELEMENTS, {{unionTitleCase}}::{{fieldUpperCase}});
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<{{fieldType}}>::init(
_builder, {{fieldOffset}} * ::capnproto::REFERENCES, size);
_builder, {{fieldOffset}} * ::capnproto::POINTERS, size);
}
{{/fieldIsListOrBlob}}
......@@ -355,7 +355,7 @@ inline {{fieldType}}::Builder {{typeFullName}}::Builder::init{{fieldTitleCase}}(
{{unionTagOffset}} * ::capnproto::ELEMENTS, {{unionTitleCase}}::{{fieldUpperCase}});
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<{{fieldType}}>::init(
_builder, {{fieldOffset}} * ::capnproto::REFERENCES);
_builder, {{fieldOffset}} * ::capnproto::POINTERS);
}
{{/fieldIsStruct}}
......@@ -370,7 +370,7 @@ inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<T>::get(
_reader, {{fieldOffset}} * ::capnproto::REFERENCES);
_reader, {{fieldOffset}} * ::capnproto::POINTERS);
}
template <typename T>
......@@ -380,7 +380,7 @@ inline typename T::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}() {
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<T>::get(
_builder, {{fieldOffset}} * ::capnproto::REFERENCES);
_builder, {{fieldOffset}} * ::capnproto::POINTERS);
}
template <typename T, typename Param>
......@@ -390,7 +390,7 @@ inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}(Param&
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<T>::getDynamic(
_reader, {{fieldOffset}} * ::capnproto::REFERENCES, ::capnproto::forward<Param>(param));
_reader, {{fieldOffset}} * ::capnproto::POINTERS, ::capnproto::forward<Param>(param));
}
template <typename T, typename Param>
......@@ -400,7 +400,7 @@ inline typename T::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}(Para
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<T>::getDynamic(
_builder, {{fieldOffset}} * ::capnproto::REFERENCES, ::capnproto::forward<Param>(param));
_builder, {{fieldOffset}} * ::capnproto::POINTERS, ::capnproto::forward<Param>(param));
}
template <typename T>
......@@ -410,7 +410,7 @@ inline void {{typeFullName}}::Builder::set{{fieldTitleCase}}(typename T::Reader
{{unionTagOffset}} * ::capnproto::ELEMENTS, {{unionTitleCase}}::{{fieldUpperCase}});
{{/fieldUnion}}
::capnproto::internal::PointerHelpers<T>::set(
_builder, {{fieldOffset}} * ::capnproto::REFERENCES, value);
_builder, {{fieldOffset}} * ::capnproto::POINTERS, value);
}
template <typename T, typename... Params>
......@@ -420,7 +420,7 @@ inline typename T::Builder {{typeFullName}}::Builder::init{{fieldTitleCase}}(Par
{{unionTagOffset}} * ::capnproto::ELEMENTS, {{unionTitleCase}}::{{fieldUpperCase}});
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<T>::init(
_builder, {{fieldOffset}} * ::capnproto::REFERENCES, ::capnproto::forward<Params>(params)...);
_builder, {{fieldOffset}} * ::capnproto::POINTERS, ::capnproto::forward<Params>(params)...);
}
{{/fieldIsGenericObject}}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment