Commit 8a2bd42f authored by Kenton Varda's avatar Kenton Varda

Add computeSerializedSizeInWords() utility function.

parent c449295b
...@@ -91,15 +91,7 @@ kj::ArrayPtr<const word> FlatArrayMessageReader::getSegment(uint id) { ...@@ -91,15 +91,7 @@ kj::ArrayPtr<const word> FlatArrayMessageReader::getSegment(uint id) {
} }
kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) { kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
KJ_REQUIRE(segments.size() > 0, "Tried to serialize uninitialized message."); kj::Array<word> result = kj::heapArray<word>(computeSerializedSizeInWords(segments));
size_t totalSize = segments.size() / 2 + 1;
for (auto& segment: segments) {
totalSize += segment.size();
}
kj::Array<word> result = kj::heapArray<word>(totalSize);
_::WireValue<uint32_t>* table = _::WireValue<uint32_t>* table =
reinterpret_cast<_::WireValue<uint32_t>*>(result.begin()); reinterpret_cast<_::WireValue<uint32_t>*>(result.begin());
...@@ -130,6 +122,18 @@ kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> ...@@ -130,6 +122,18 @@ kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>>
return kj::mv(result); return kj::mv(result);
} }
size_t computeSerializedSizeInWords(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
KJ_REQUIRE(segments.size() > 0, "Tried to serialize uninitialized message.");
size_t totalSize = segments.size() / 2 + 1;
for (auto& segment: segments) {
totalSize += segment.size();
}
return totalSize;
}
// ======================================================================================= // =======================================================================================
InputStreamMessageReader::InputStreamMessageReader( InputStreamMessageReader::InputStreamMessageReader(
......
...@@ -77,6 +77,12 @@ kj::Array<word> messageToFlatArray(MessageBuilder& builder); ...@@ -77,6 +77,12 @@ kj::Array<word> messageToFlatArray(MessageBuilder& builder);
kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments); kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
// Version of messageToFlatArray that takes a raw segment array. // Version of messageToFlatArray that takes a raw segment array.
size_t computeSerializedSizeInWords(MessageBuilder& builder);
// Returns the size, in words, that will be needed to serialize the message, including the header.
size_t computeSerializedSizeInWords(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
// Version of computeSerializedSizeInWords that takes a raw segment array.
// ======================================================================================= // =======================================================================================
class InputStreamMessageReader: public MessageReader { class InputStreamMessageReader: public MessageReader {
...@@ -151,6 +157,10 @@ inline kj::Array<word> messageToFlatArray(MessageBuilder& builder) { ...@@ -151,6 +157,10 @@ inline kj::Array<word> messageToFlatArray(MessageBuilder& builder) {
return messageToFlatArray(builder.getSegmentsForOutput()); return messageToFlatArray(builder.getSegmentsForOutput());
} }
inline size_t computeSerializedSizeInWords(MessageBuilder& builder) {
return computeSerializedSizeInWords(builder.getSegmentsForOutput());
}
inline void writeMessage(kj::OutputStream& output, MessageBuilder& builder) { inline void writeMessage(kj::OutputStream& output, MessageBuilder& builder) {
writeMessage(output, builder.getSegmentsForOutput()); writeMessage(output, builder.getSegmentsForOutput());
} }
......
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