Commit 43abe32e authored by Kenton Varda's avatar Kenton Varda

Merge branch 'master' of github.com:kentonv/capnproto

parents 2ef919b2 e987b815
......@@ -86,10 +86,16 @@ private:
std::string::size_type readPos;
};
void expectPacksTo(kj::ArrayPtr<const byte> unpacked, kj::ArrayPtr<const byte> packed) {
void expectPacksTo(kj::ArrayPtr<const byte> unpackedUnaligned, kj::ArrayPtr<const byte> packed) {
TestPipe pipe;
EXPECT_EQ(unpacked.size(), computeUnpackedSizeInWords(packed) * sizeof(word));
auto unpackedSizeInWords = computeUnpackedSizeInWords(packed);
EXPECT_EQ(unpackedUnaligned.size(), unpackedSizeInWords * sizeof(word));
// Make a guaranteed-to-be-aligned copy of the unpacked buffer.
kj::Array<word> unpackedWords = kj::heapArray<word>(unpackedSizeInWords);
memcpy(unpackedWords.begin(), unpackedUnaligned.begin(), unpackedUnaligned.size());
kj::ArrayPtr<const byte> unpacked = unpackedWords.asBytes();
// -----------------------------------------------------------------
// write
......
......@@ -351,7 +351,8 @@ void PackedOutputStream::write(const void* src, size_t size) {
// An all-zero word is followed by a count of consecutive zero words (not including the
// first one).
// We can check a whole word at a time.
// We can check a whole word at a time. (Here is where we use the assumption that
// `src` is word-aligned.)
const uint64_t* inWord = reinterpret_cast<const uint64_t*>(in);
// The count must fit it 1 byte, so limit to 255 words.
......
......@@ -50,6 +50,7 @@ private:
};
class PackedOutputStream: public kj::OutputStream {
// An output stream that packs data. Buffers passed to `write()` must be word-aligned.
public:
explicit PackedOutputStream(kj::BufferedOutputStream& inner);
KJ_DISALLOW_COPY(PackedOutputStream);
......
......@@ -450,11 +450,17 @@ public:
// check might catch bugs. Probably people should use Vector if they want to build arrays
// without knowing the final size in advance.
KJ_IREQUIRE(pos == endPtr, "ArrayBuilder::finish() called prematurely.");
Array<T> result(reinterpret_cast<T*>(ptr), pos - ptr, *disposer);
const ptrdiff_t size = pos - ptr;
T* start = reinterpret_cast<T*>(ptr);
ptr = nullptr;
pos = nullptr;
endPtr = nullptr;
return result;
if (size == 0) {
// `disposer` possibly does not point to anything, so don't pass `*disposer` to `Array`.
return Array<T>();
} else {
return Array<T>(start, size, *disposer);
}
}
inline bool isFull() const {
......
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