Commit c13d6ad9 authored by Matthew Maurer's avatar Matthew Maurer

Change canonicalize interface

parent 6de429bb
...@@ -35,9 +35,8 @@ KJ_TEST("canonicalize yields cannonical message") { ...@@ -35,9 +35,8 @@ KJ_TEST("canonicalize yields cannonical message") {
initTestMessage(root); initTestMessage(root);
MallocMessageBuilder canonicalMessage; canonicalize(builder);
canonicalMessage.canonicalRoot(builder.getRoot<AnyPointer>().asReader()); //Will assert if canonicalize failed to do so
KJ_ASSERT(canonicalMessage.isCanonical());
} }
KJ_TEST("isCanonical requires pointer preorder") { KJ_TEST("isCanonical requires pointer preorder") {
...@@ -119,8 +118,7 @@ KJ_TEST("isCanonical requires truncation of 0-valued struct fields") { ...@@ -119,8 +118,7 @@ KJ_TEST("isCanonical requires truncation of 0-valued struct fields") {
KJ_ASSERT(!nonTruncated.isCanonical()); KJ_ASSERT(!nonTruncated.isCanonical());
} }
KJ_TEST("isCanonical requires truncation of 0-valued struct fields\ KJ_TEST("isCanonical requires truncation of 0-valued struct fields in all list members") {
in all list members") {
AlignedData<6> nonTruncatedList = {{ AlignedData<6> nonTruncatedList = {{
//List pointer, composite, //List pointer, composite,
0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
......
...@@ -199,12 +199,6 @@ public: ...@@ -199,12 +199,6 @@ public:
void setRoot(Reader&& value); void setRoot(Reader&& value);
// Set the root struct to a deep copy of the given struct. // Set the root struct to a deep copy of the given struct.
template <typename Reader>
void canonicalRoot(Reader&& value);
// Set the root to a canonical deep copy of the struct.
// will likely only work if the builder has not yet been used, and has
// been set up with an arena with a segment as big as value.targetSize();
template <typename RootType> template <typename RootType>
typename RootType::Builder getRoot(); typename RootType::Builder getRoot();
// Get the root struct of the message, interpreting it as the given struct type. // Get the root struct of the message, interpreting it as the given struct type.
...@@ -427,6 +421,21 @@ private: ...@@ -427,6 +421,21 @@ private:
bool allocated; bool allocated;
}; };
template <typename MR>
kj::Array<word> canonicalize(MR&& reader) {
AnyPointer::Reader root = reader.template getRoot<AnyPointer>();
WordCount size = root.targetSize().wordCount * WORDS + POINTER_SIZE_IN_WORDS;
kj::Array<word> backing = kj::heapArray<word>(size / WORDS);
bzero(backing.begin(), backing.asBytes().size());
FlatMessageBuilder builder(backing);
builder.initRoot<AnyPointer>().setCanonical(root);
KJ_ASSERT(builder.isCanonical());
auto output = builder.getSegmentsForOutput()[0];
kj::Array<word> trunc = kj::heapArray<word>(output.size());
memcpy(trunc.begin(), output.begin(), output.asBytes().size());
return trunc;
}
// ======================================================================================= // =======================================================================================
// implementation details // implementation details
...@@ -474,13 +483,6 @@ typename RootType::Builder MessageBuilder::initRoot(SchemaType schema) { ...@@ -474,13 +483,6 @@ typename RootType::Builder MessageBuilder::initRoot(SchemaType schema) {
return getRootInternal().initAs<RootType>(schema); return getRootInternal().initAs<RootType>(schema);
} }
template <typename Reader>
void MessageBuilder::canonicalRoot(Reader&& value) {
auto target = initRoot<AnyPointer>();
target.setCanonical(value);
KJ_ASSERT(isCanonical());
}
template <typename RootType> template <typename RootType>
typename RootType::Reader readMessageUnchecked(const word* data) { typename RootType::Reader readMessageUnchecked(const word* data) {
return AnyPointer::Reader(_::PointerReader::getRootUnchecked(data)).getAs<RootType>(); return AnyPointer::Reader(_::PointerReader::getRootUnchecked(data)).getAs<RootType>();
......
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