Commit ed6b71aa authored by Kenton Varda's avatar Kenton Varda

Also add io.{h,c++} to KJ.

parent c98eb066
......@@ -38,7 +38,7 @@ namespace capnproto {
namespace benchmark {
namespace capnp {
class CountingOutputStream: public FdOutputStream {
class CountingOutputStream: public kj::FdOutputStream {
public:
CountingOutputStream(int fd): FdOutputStream(fd), throughput(0) {}
......@@ -60,7 +60,7 @@ public:
// =======================================================================================
struct Uncompressed {
typedef FdInputStream& BufferedInput;
typedef kj::FdInputStream& BufferedInput;
typedef InputStreamMessageReader MessageReader;
class ArrayMessageReader: public FlatArrayMessageReader {
......@@ -73,16 +73,16 @@ struct Uncompressed {
reinterpret_cast<const word*>(array.end())), options) {}
};
static inline void write(OutputStream& output, MessageBuilder& builder) {
static inline void write(kj::OutputStream& output, MessageBuilder& builder) {
writeMessage(output, builder);
}
};
struct Packed {
typedef BufferedInputStreamWrapper BufferedInput;
typedef kj::BufferedInputStreamWrapper BufferedInput;
typedef PackedMessageReader MessageReader;
class ArrayMessageReader: private ArrayInputStream, public PackedMessageReader {
class ArrayMessageReader: private kj::ArrayInputStream, public PackedMessageReader {
public:
ArrayMessageReader(kj::ArrayPtr<const byte> array,
ReaderOptions options = ReaderOptions(),
......@@ -91,11 +91,11 @@ struct Packed {
PackedMessageReader(*this, options, scratchSpace) {}
};
static inline void write(OutputStream& output, MessageBuilder& builder) {
static inline void write(kj::OutputStream& output, MessageBuilder& builder) {
writePackedMessage(output, builder);
}
static inline void write(BufferedOutputStream& output, MessageBuilder& builder) {
static inline void write(kj::BufferedOutputStream& output, MessageBuilder& builder) {
writePackedMessage(output, builder);
}
};
......@@ -240,7 +240,7 @@ struct UseScratch {
template <typename TestCase, typename ReuseStrategy, typename Compression>
struct BenchmarkMethods {
static uint64_t syncClient(int inputFd, int outputFd, uint64_t iters) {
FdInputStream inputStream(inputFd);
kj::FdInputStream inputStream(inputFd);
typename Compression::BufferedInput bufferedInput(inputStream);
CountingOutputStream output(outputFd);
......@@ -288,7 +288,7 @@ struct BenchmarkMethods {
static void asyncClientReceiver(
int inputFd, ProducerConsumerQueue<typename TestCase::Expectation>* expectations,
uint64_t iters) {
FdInputStream inputStream(inputFd);
kj::FdInputStream inputStream(inputFd);
typename Compression::BufferedInput bufferedInput(inputStream);
typename ReuseStrategy::ScratchSpace scratch;
......@@ -312,7 +312,7 @@ struct BenchmarkMethods {
}
static uint64_t server(int inputFd, int outputFd, uint64_t iters) {
FdInputStream inputStream(inputFd);
kj::FdInputStream inputStream(inputFd);
typename Compression::BufferedInput bufferedInput(inputStream);
CountingOutputStream output(outputFd);
......@@ -372,7 +372,7 @@ struct BenchmarkMethods {
typename TestCase::Expectation expected = TestCase::setupRequest(
requestBuilder.template initRoot<typename TestCase::Request>());
ArrayOutputStream requestOutput(kj::arrayPtr(
kj::ArrayOutputStream requestOutput(kj::arrayPtr(
reinterpret_cast<byte*>(requestBytesScratch.words), SCRATCH_SIZE * sizeof(word)));
Compression::write(requestOutput, requestBuilder);
throughput += requestOutput.getArray().size();
......@@ -383,7 +383,7 @@ struct BenchmarkMethods {
TestCase::handleRequest(requestReader.template getRoot<typename TestCase::Request>(),
responseBuilder.template initRoot<typename TestCase::Response>());
ArrayOutputStream responseOutput(
kj::ArrayOutputStream responseOutput(
kj::arrayPtr(reinterpret_cast<byte*>(responseBytesScratch.words),
SCRATCH_SIZE * sizeof(word)));
Compression::write(responseOutput, responseBuilder);
......
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This file contains types which are intended to help detect incorrect usage at compile
// time, but should then be optimized down to basic primitives (usually, integers) by the
// compiler.
#ifndef CAPNPROTO_COMMON_H_
#define CAPNPROTO_COMMON_H_
#include <kj/type-safety.h>
namespace capnproto {
typedef unsigned int uint;
enum class Void {
// Type used for Void fields. There is only one value. Using C++'s "void" type creates a bunch
// of issues since it behaves differently from other types.
VOID
};
template <typename T>
inline T& operator<<(T& os, Void) { return os << "void"; }
// =======================================================================================
// Raw memory types and measures
using kj::byte;
class word { uint64_t content KJ_UNUSED_FOR_CLANG; KJ_DISALLOW_COPY(word); public: word() = default; };
// word is an opaque type with size of 64 bits. This type is useful only to make pointer
// arithmetic clearer. Since the contents are private, the only way to access them is to first
// reinterpret_cast to some other pointer type.
//
// Coping is disallowed because you should always use memcpy(). Otherwise, you may run afoul of
// aliasing rules.
//
// A pointer of type word* should always be word-aligned even if won't actually be dereferenced as
// that type.
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; struct WirePointer; }
#ifndef KJ_DEBUG_TYPES
#define KJ_DEBUG_TYPES 1
// Set this to zero to degrade all the "count" types below to being plain integers. All the code
// should still operate exactly the same, we just lose compile-time checking. Note that this will
// also change symbol names, so it's important that the library and any clients be compiled with
// the same setting here.
//
// TODO(soon): Decide policy on this. It may make sense to only use KJ_DEBUG_TYPES when
// compiling the tests of libraries that explicitly want the safety (like Cap'n Proto), but
// disable it for all real builds, as some clients may find this safety tiring. Also, need to
// benchmark to verify there really is no perf hit.
#endif
#if KJ_DEBUG_TYPES
typedef kj::Quantity<uint, internal::BitLabel> BitCount;
typedef kj::Quantity<uint8_t, internal::BitLabel> BitCount8;
typedef kj::Quantity<uint16_t, internal::BitLabel> BitCount16;
typedef kj::Quantity<uint32_t, internal::BitLabel> BitCount32;
typedef kj::Quantity<uint64_t, internal::BitLabel> BitCount64;
typedef kj::Quantity<uint, byte> ByteCount;
typedef kj::Quantity<uint8_t, byte> ByteCount8;
typedef kj::Quantity<uint16_t, byte> ByteCount16;
typedef kj::Quantity<uint32_t, byte> ByteCount32;
typedef kj::Quantity<uint64_t, byte> ByteCount64;
typedef kj::Quantity<uint, word> WordCount;
typedef kj::Quantity<uint8_t, word> WordCount8;
typedef kj::Quantity<uint16_t, word> WordCount16;
typedef kj::Quantity<uint32_t, word> WordCount32;
typedef kj::Quantity<uint64_t, word> WordCount64;
typedef kj::Quantity<uint, internal::ElementLabel> ElementCount;
typedef kj::Quantity<uint8_t, internal::ElementLabel> ElementCount8;
typedef kj::Quantity<uint16_t, internal::ElementLabel> ElementCount16;
typedef kj::Quantity<uint32_t, internal::ElementLabel> ElementCount32;
typedef kj::Quantity<uint64_t, internal::ElementLabel> ElementCount64;
typedef kj::Quantity<uint, internal::WirePointer> WirePointerCount;
typedef kj::Quantity<uint8_t, internal::WirePointer> WirePointerCount8;
typedef kj::Quantity<uint16_t, internal::WirePointer> WirePointerCount16;
typedef kj::Quantity<uint32_t, internal::WirePointer> WirePointerCount32;
typedef kj::Quantity<uint64_t, internal::WirePointer> WirePointerCount64;
#else
typedef uint BitCount;
typedef uint8_t BitCount8;
typedef uint16_t BitCount16;
typedef uint32_t BitCount32;
typedef uint64_t BitCount64;
typedef uint ByteCount;
typedef uint8_t ByteCount8;
typedef uint16_t ByteCount16;
typedef uint32_t ByteCount32;
typedef uint64_t ByteCount64;
typedef uint WordCount;
typedef uint8_t WordCount8;
typedef uint16_t WordCount16;
typedef uint32_t WordCount32;
typedef uint64_t WordCount64;
typedef uint ElementCount;
typedef uint8_t ElementCount8;
typedef uint16_t ElementCount16;
typedef uint32_t ElementCount32;
typedef uint64_t ElementCount64;
typedef uint WirePointerCount;
typedef uint8_t WirePointerCount8;
typedef uint16_t WirePointerCount16;
typedef uint32_t WirePointerCount32;
typedef uint64_t WirePointerCount64;
#endif
constexpr BitCount BITS = kj::unit<BitCount>();
constexpr ByteCount BYTES = kj::unit<ByteCount>();
constexpr WordCount WORDS = kj::unit<WordCount>();
constexpr ElementCount ELEMENTS = kj::unit<ElementCount>();
constexpr WirePointerCount POINTERS = kj::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_POINTER = 64 * BITS / POINTERS;
constexpr auto BYTES_PER_POINTER = 8 * BYTES / POINTERS;
constexpr auto WORDS_PER_POINTER = 1 * WORDS / POINTERS;
constexpr WordCount POINTER_SIZE_IN_WORDS = 1 * POINTERS * WORDS_PER_POINTER;
template <typename T>
inline constexpr decltype(BYTES / ELEMENTS) bytesPerElement() {
return sizeof(T) * BYTES / ELEMENTS;
}
template <typename T>
inline constexpr decltype(BITS / ELEMENTS) bitsPerElement() {
return sizeof(T) * 8 * BITS / ELEMENTS;
}
#ifndef __CDT_PARSER__
template <typename T, typename U>
inline constexpr U* operator+(U* ptr, kj::Quantity<T, U> offset) {
return ptr + offset / kj::unit<kj::Quantity<T, U>>();
}
template <typename T, typename U>
inline constexpr const U* operator+(const U* ptr, kj::Quantity<T, U> offset) {
return ptr + offset / kj::unit<kj::Quantity<T, U>>();
}
template <typename T, typename U>
inline constexpr U* operator+=(U*& ptr, kj::Quantity<T, U> offset) {
return ptr = ptr + offset / kj::unit<kj::Quantity<T, U>>();
}
template <typename T, typename U>
inline constexpr const U* operator+=(const U*& ptr, kj::Quantity<T, U> offset) {
return ptr = ptr + offset / kj::unit<kj::Quantity<T, U>>();
}
template <typename T, typename U>
inline constexpr U* operator-(U* ptr, kj::Quantity<T, U> offset) {
return ptr - offset / kj::unit<kj::Quantity<T, U>>();
}
template <typename T, typename U>
inline constexpr const U* operator-(const U* ptr, kj::Quantity<T, U> offset) {
return ptr - offset / kj::unit<kj::Quantity<T, U>>();
}
template <typename T, typename U>
inline constexpr U* operator-=(U*& ptr, kj::Quantity<T, U> offset) {
return ptr = ptr - offset / kj::unit<kj::Quantity<T, U>>();
}
template <typename T, typename U>
inline constexpr const U* operator-=(const U*& ptr, kj::Quantity<T, U> offset) {
return ptr = ptr - offset / kj::unit<kj::Quantity<T, U>>();
}
#endif
inline constexpr ByteCount intervalLength(const byte* a, const byte* b) {
return uint(b - a) * BYTES;
}
inline constexpr WordCount intervalLength(const word* a, const word* b) {
return uint(b - a) * WORDS;
}
} // namespace capnproto
#endif // CAPNPROTO_COMMON_H_
......@@ -29,7 +29,7 @@
#include "../schema.capnp.h"
#include "../serialize.h"
#include <kj/logging.h>
#include "../io.h"
#include <kj/io.h>
#include "../schema-loader.h"
#include "../dynamic.h"
#include "../stringify.h"
......@@ -47,7 +47,7 @@ public:
TextBlob(Params&&... params);
TextBlob(kj::Array<TextBlob>&& params);
void writeTo(OutputStream& out) const;
void writeTo(kj::OutputStream& out) const;
private:
kj::Array<char> text;
......@@ -102,7 +102,7 @@ TextBlob::TextBlob(kj::Array<TextBlob>&& params) {
}
}
void TextBlob::writeTo(OutputStream& out) const {
void TextBlob::writeTo(kj::OutputStream& out) const {
const char* pos = text.begin();
for (auto& branch: branches) {
out.write(pos, branch.pos - pos);
......@@ -601,8 +601,8 @@ int main(int argc, char* argv[]) {
schemaLoader.load(node);
}
FdOutputStream rawOut(STDOUT_FILENO);
BufferedOutputStreamWrapper out(rawOut);
kj::FdOutputStream rawOut(STDOUT_FILENO);
kj::BufferedOutputStreamWrapper out(rawOut);
for (auto fileId: request.getRequestedFiles()) {
genFile(schemaLoader.get(fileId)).writeTo(out);
......
......@@ -34,7 +34,7 @@ namespace capnproto {
namespace internal {
namespace {
class TestPipe: public BufferedInputStream, public OutputStream {
class TestPipe: public kj::BufferedInputStream, public kj::OutputStream {
public:
TestPipe()
: preferredReadSize(std::numeric_limits<size_t>::max()), readPos(0) {}
......@@ -116,7 +116,7 @@ void expectPacksTo(std::initializer_list<uint8_t> unpacked,
// write
{
BufferedOutputStreamWrapper bufferedOut(pipe);
kj::BufferedOutputStreamWrapper bufferedOut(pipe);
PackedOutputStream packedOut(bufferedOut);
packedOut.write(unpacked.begin(), unpacked.size());
}
......@@ -195,7 +195,7 @@ void expectPacksTo(std::initializer_list<uint8_t> unpacked,
// write / read multiple
{
BufferedOutputStreamWrapper bufferedOut(pipe);
kj::BufferedOutputStreamWrapper bufferedOut(pipe);
PackedOutputStream packedOut(bufferedOut);
for (uint i = 0; i < 5; i++) {
packedOut.write(unpacked.begin(), unpacked.size());
......
......@@ -31,7 +31,7 @@ namespace capnproto {
namespace internal {
PackedInputStream::PackedInputStream(BufferedInputStream& inner): inner(inner) {}
PackedInputStream::PackedInputStream(kj::BufferedInputStream& inner): inner(inner) {}
PackedInputStream::~PackedInputStream() {}
size_t PackedInputStream::read(void* dst, size_t minBytes, size_t maxBytes) {
......@@ -303,7 +303,7 @@ void PackedInputStream::skip(size_t bytes) {
// -------------------------------------------------------------------
PackedOutputStream::PackedOutputStream(BufferedOutputStream& inner)
PackedOutputStream::PackedOutputStream(kj::BufferedOutputStream& inner)
: inner(inner) {}
PackedOutputStream::~PackedOutputStream() {}
......@@ -437,7 +437,7 @@ void PackedOutputStream::write(const void* src, size_t size) {
// =======================================================================================
PackedMessageReader::PackedMessageReader(
BufferedInputStream& inputStream, ReaderOptions options, kj::ArrayPtr<word> scratchSpace)
kj::BufferedInputStream& inputStream, ReaderOptions options, kj::ArrayPtr<word> scratchSpace)
: PackedInputStream(inputStream),
InputStreamMessageReader(static_cast<PackedInputStream&>(*this), options, scratchSpace) {}
......@@ -451,7 +451,7 @@ PackedFdMessageReader::PackedFdMessageReader(
options, scratchSpace) {}
PackedFdMessageReader::PackedFdMessageReader(
AutoCloseFd fd, ReaderOptions options, kj::ArrayPtr<word> scratchSpace)
kj::AutoCloseFd fd, ReaderOptions options, kj::ArrayPtr<word> scratchSpace)
: FdInputStream(kj::move(fd)),
BufferedInputStreamWrapper(static_cast<FdInputStream&>(*this)),
PackedMessageReader(static_cast<BufferedInputStreamWrapper&>(*this),
......@@ -459,25 +459,26 @@ PackedFdMessageReader::PackedFdMessageReader(
PackedFdMessageReader::~PackedFdMessageReader() {}
void writePackedMessage(BufferedOutputStream& output,
void writePackedMessage(kj::BufferedOutputStream& output,
kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
internal::PackedOutputStream packedOutput(output);
writeMessage(packedOutput, segments);
}
void writePackedMessage(OutputStream& output,
void writePackedMessage(kj::OutputStream& output,
kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
if (BufferedOutputStream* bufferedOutputPtr = dynamic_cast<BufferedOutputStream*>(&output)) {
if (kj::BufferedOutputStream* bufferedOutputPtr =
dynamic_cast<kj::BufferedOutputStream*>(&output)) {
writePackedMessage(*bufferedOutputPtr, segments);
} else {
byte buffer[8192];
BufferedOutputStreamWrapper bufferedOutput(output, kj::arrayPtr(buffer, sizeof(buffer)));
kj::BufferedOutputStreamWrapper bufferedOutput(output, kj::arrayPtr(buffer, sizeof(buffer)));
writePackedMessage(bufferedOutput, segments);
}
}
void writePackedMessageToFd(int fd, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
FdOutputStream output(fd);
kj::FdOutputStream output(fd);
writePackedMessage(output, segments);
}
......
......@@ -30,12 +30,12 @@ namespace capnproto {
namespace internal {
class PackedInputStream: public InputStream {
class PackedInputStream: public kj::InputStream {
// An input stream that unpacks packed data with a picky constraint: The caller must read data
// in the exact same size and sequence as the data was written to PackedOutputStream.
public:
explicit PackedInputStream(BufferedInputStream& inner);
explicit PackedInputStream(kj::BufferedInputStream& inner);
KJ_DISALLOW_COPY(PackedInputStream);
~PackedInputStream();
......@@ -44,12 +44,12 @@ public:
void skip(size_t bytes) override;
private:
BufferedInputStream& inner;
kj::BufferedInputStream& inner;
};
class PackedOutputStream: public OutputStream {
class PackedOutputStream: public kj::OutputStream {
public:
explicit PackedOutputStream(BufferedOutputStream& inner);
explicit PackedOutputStream(kj::BufferedOutputStream& inner);
KJ_DISALLOW_COPY(PackedOutputStream);
~PackedOutputStream();
......@@ -57,20 +57,20 @@ public:
void write(const void* buffer, size_t bytes) override;
private:
BufferedOutputStream& inner;
kj::BufferedOutputStream& inner;
};
} // namespace internal
class PackedMessageReader: private internal::PackedInputStream, public InputStreamMessageReader {
public:
PackedMessageReader(BufferedInputStream& inputStream, ReaderOptions options = ReaderOptions(),
PackedMessageReader(kj::BufferedInputStream& inputStream, ReaderOptions options = ReaderOptions(),
kj::ArrayPtr<word> scratchSpace = nullptr);
KJ_DISALLOW_COPY(PackedMessageReader);
~PackedMessageReader();
};
class PackedFdMessageReader: private FdInputStream, private BufferedInputStreamWrapper,
class PackedFdMessageReader: private kj::FdInputStream, private kj::BufferedInputStreamWrapper,
public PackedMessageReader {
public:
PackedFdMessageReader(int fd, ReaderOptions options = ReaderOptions(),
......@@ -79,7 +79,7 @@ public:
// Note that if you want to reuse the descriptor after the reader is destroyed, you'll need to
// seek it, since otherwise the position is unspecified.
PackedFdMessageReader(AutoCloseFd fd, ReaderOptions options = ReaderOptions(),
PackedFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(),
kj::ArrayPtr<word> scratchSpace = nullptr);
// Read a message from a file descriptor, taking ownership of the descriptor.
......@@ -88,13 +88,13 @@ public:
~PackedFdMessageReader();
};
void writePackedMessage(BufferedOutputStream& output, MessageBuilder& builder);
void writePackedMessage(BufferedOutputStream& output,
void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder);
void writePackedMessage(kj::BufferedOutputStream& output,
kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
// Write a packed message to a buffered output stream.
void writePackedMessage(OutputStream& output, MessageBuilder& builder);
void writePackedMessage(OutputStream& output,
void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder);
void writePackedMessage(kj::OutputStream& output,
kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
// Write a packed message to an unbuffered output stream. If you intend to write multiple messages
// in succession, consider wrapping your output in a buffered stream in order to reduce system
......@@ -107,11 +107,11 @@ void writePackedMessageToFd(int fd, kj::ArrayPtr<const kj::ArrayPtr<const word>>
// =======================================================================================
// inline stuff
inline void writePackedMessage(BufferedOutputStream& output, MessageBuilder& builder) {
inline void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder) {
writePackedMessage(output, builder.getSegmentsForOutput());
}
inline void writePackedMessage(OutputStream& output, MessageBuilder& builder) {
inline void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder) {
writePackedMessage(output, builder.getSegmentsForOutput());
}
......
......@@ -65,7 +65,7 @@ private:
uint desiredSegmentCount;
};
class TestPipe: public BufferedInputStream, public OutputStream {
class TestPipe: public kj::BufferedInputStream, public kj::OutputStream {
public:
TestPipe()
: preferredReadSize(std::numeric_limits<size_t>::max()), readPos(0) {}
......
......@@ -201,7 +201,7 @@ SnappyPackedMessageReader::SnappyPackedMessageReader(
SnappyPackedMessageReader::~SnappyPackedMessageReader() {}
void writeSnappyPackedMessage(OutputStream& output,
void writeSnappyPackedMessage(kj::OutputStream& output,
kj::ArrayPtr<const kj::ArrayPtr<const word>> segments,
kj::ArrayPtr<byte> buffer, kj::ArrayPtr<byte> compressedBuffer) {
SnappyOutputStream snappyOut(output, buffer, compressedBuffer);
......
......@@ -32,7 +32,7 @@ namespace capnproto {
constexpr size_t SNAPPY_BUFFER_SIZE = 65536;
constexpr size_t SNAPPY_COMPRESSED_BUFFER_SIZE = 76490;
class SnappyInputStream: public BufferedInputStream {
class SnappyInputStream: public kj::BufferedInputStream {
public:
explicit SnappyInputStream(BufferedInputStream& inner, kj::ArrayPtr<byte> buffer = nullptr);
KJ_DISALLOW_COPY(SnappyInputStream);
......@@ -54,7 +54,7 @@ private:
void refill();
};
class SnappyOutputStream: public BufferedOutputStream {
class SnappyOutputStream: public kj::BufferedOutputStream {
public:
explicit SnappyOutputStream(OutputStream& inner,
kj::ArrayPtr<byte> buffer = nullptr,
......@@ -89,10 +89,10 @@ public:
~SnappyPackedMessageReader();
};
void writeSnappyPackedMessage(OutputStream& output, MessageBuilder& builder,
void writeSnappyPackedMessage(kj::OutputStream& output, MessageBuilder& builder,
kj::ArrayPtr<byte> buffer = nullptr,
kj::ArrayPtr<byte> compressedBuffer = nullptr);
void writeSnappyPackedMessage(OutputStream& output,
void writeSnappyPackedMessage(kj::OutputStream& output,
kj::ArrayPtr<const kj::ArrayPtr<const word>> segments,
kj::ArrayPtr<byte> buffer = nullptr,
kj::ArrayPtr<byte> compressedBuffer = nullptr);
......@@ -100,7 +100,7 @@ void writeSnappyPackedMessage(OutputStream& output,
// =======================================================================================
// inline stuff
inline void writeSnappyPackedMessage(OutputStream& output, MessageBuilder& builder,
inline void writeSnappyPackedMessage(kj::OutputStream& output, MessageBuilder& builder,
kj::ArrayPtr<byte> buffer,
kj::ArrayPtr<byte> compressedBuffer) {
writeSnappyPackedMessage(output, builder.getSegmentsForOutput(), buffer, compressedBuffer);
......
......@@ -95,7 +95,7 @@ TEST(Serialize, FlatArrayEvenSegmentCount) {
checkTestMessage(reader.getRoot<TestAllTypes>());
}
class TestInputStream: public InputStream {
class TestInputStream: public kj::InputStream {
public:
TestInputStream(kj::ArrayPtr<const word> data, bool lazy)
: pos(reinterpret_cast<const char*>(data.begin())),
......@@ -202,7 +202,7 @@ TEST(Serialize, InputStreamEvenSegmentCountLazy) {
checkTestMessage(reader.getRoot<TestAllTypes>());
}
class TestOutputStream: public OutputStream {
class TestOutputStream: public kj::OutputStream {
public:
TestOutputStream() {}
~TestOutputStream() {}
......@@ -258,7 +258,7 @@ TEST(Serialize, WriteMessageEvenSegmentCount) {
TEST(Serialize, FileDescriptors) {
char filename[] = "/tmp/capnproto-serialize-test-XXXXXX";
AutoCloseFd tmpfile(mkstemp(filename));
kj::AutoCloseFd tmpfile(mkstemp(filename));
ASSERT_GE(tmpfile.get(), 0);
// Unlink the file so that it will be deleted on close.
......
......@@ -130,7 +130,7 @@ kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>>
// =======================================================================================
InputStreamMessageReader::InputStreamMessageReader(
InputStream& inputStream, ReaderOptions options, kj::ArrayPtr<word> scratchSpace)
kj::InputStream& inputStream, ReaderOptions options, kj::ArrayPtr<word> scratchSpace)
: MessageReader(options), inputStream(inputStream), readPos(nullptr) {
internal::WireValue<uint32_t> firstWord[2];
......@@ -236,7 +236,7 @@ kj::ArrayPtr<const word> InputStreamMessageReader::getSegment(uint id) {
// -------------------------------------------------------------------
void writeMessage(OutputStream& output, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
void writeMessage(kj::OutputStream& output, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
PRECOND(segments.size() > 0, "Tried to serialize uninitialized message.");
internal::WireValue<uint32_t> table[(segments.size() + 2) & ~size_t(1)];
......@@ -268,7 +268,7 @@ void writeMessage(OutputStream& output, kj::ArrayPtr<const kj::ArrayPtr<const wo
StreamFdMessageReader::~StreamFdMessageReader() {}
void writeMessageToFd(int fd, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
FdOutputStream stream(fd);
kj::FdOutputStream stream(fd);
writeMessage(stream, segments);
}
......
......@@ -44,7 +44,7 @@
#define CAPNPROTO_SERIALIZE_H_
#include "message.h"
#include "io.h"
#include <kj/io.h>
namespace capnproto {
......@@ -74,7 +74,7 @@ kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>>
class InputStreamMessageReader: public MessageReader {
public:
InputStreamMessageReader(InputStream& inputStream,
InputStreamMessageReader(kj::InputStream& inputStream,
ReaderOptions options = ReaderOptions(),
kj::ArrayPtr<word> scratchSpace = nullptr);
~InputStreamMessageReader();
......@@ -83,7 +83,7 @@ public:
kj::ArrayPtr<const word> getSegment(uint id) override;
private:
InputStream& inputStream;
kj::InputStream& inputStream;
byte* readPos;
// Optimize for single-segment case.
......@@ -94,16 +94,16 @@ private:
// Only if scratchSpace wasn't big enough.
};
void writeMessage(OutputStream& output, MessageBuilder& builder);
void writeMessage(kj::OutputStream& output, MessageBuilder& builder);
// Write the message to the given output stream.
void writeMessage(OutputStream& output, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
void writeMessage(kj::OutputStream& output, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
// Write the segment array to the given output stream.
// =======================================================================================
// Specializations for reading from / writing to file descriptors.
class StreamFdMessageReader: private FdInputStream, public InputStreamMessageReader {
class StreamFdMessageReader: private kj::FdInputStream, public InputStreamMessageReader {
// A MessageReader that reads from a steam-based file descriptor. For seekable file descriptors
// (e.g. actual disk files), FdFileMessageReader is better, but this will still work.
......@@ -113,7 +113,7 @@ public:
: FdInputStream(fd), InputStreamMessageReader(*this, options, scratchSpace) {}
// Read message from a file descriptor, without taking ownership of the descriptor.
StreamFdMessageReader(AutoCloseFd fd, ReaderOptions options = ReaderOptions(),
StreamFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(),
kj::ArrayPtr<word> scratchSpace = nullptr)
: FdInputStream(kj::move(fd)), InputStreamMessageReader(*this, options, scratchSpace) {}
// Read a message from a file descriptor, taking ownership of the descriptor.
......@@ -142,7 +142,7 @@ inline kj::Array<word> messageToFlatArray(MessageBuilder& builder) {
return messageToFlatArray(builder.getSegmentsForOutput());
}
inline void writeMessage(OutputStream& output, MessageBuilder& builder) {
inline void writeMessage(kj::OutputStream& output, MessageBuilder& builder) {
writeMessage(output, builder.getSegmentsForOutput());
}
......
......@@ -21,14 +21,13 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define CAPNPROTO_PRIVATE
#include "io.h"
#include <kj/logging.h>
#include "logging.h"
#include <unistd.h>
#include <sys/uio.h>
#include <string>
namespace capnproto {
namespace kj {
InputStream::~InputStream() {}
OutputStream::~OutputStream() {}
......@@ -44,7 +43,7 @@ void InputStream::skip(size_t bytes) {
}
}
void OutputStream::write(kj::ArrayPtr<const kj::ArrayPtr<const byte>> pieces) {
void OutputStream::write(ArrayPtr<const ArrayPtr<const byte>> pieces) {
for (auto piece: pieces) {
write(piece.begin(), piece.size());
}
......@@ -52,13 +51,13 @@ void OutputStream::write(kj::ArrayPtr<const kj::ArrayPtr<const byte>> pieces) {
// =======================================================================================
BufferedInputStreamWrapper::BufferedInputStreamWrapper(InputStream& inner, kj::ArrayPtr<byte> buffer)
: inner(inner), ownedBuffer(buffer == nullptr ? kj::newArray<byte>(8192) : nullptr),
BufferedInputStreamWrapper::BufferedInputStreamWrapper(InputStream& inner, ArrayPtr<byte> buffer)
: inner(inner), ownedBuffer(buffer == nullptr ? newArray<byte>(8192) : nullptr),
buffer(buffer == nullptr ? ownedBuffer : buffer) {}
BufferedInputStreamWrapper::~BufferedInputStreamWrapper() {}
kj::ArrayPtr<const byte> BufferedInputStreamWrapper::getReadBuffer() {
ArrayPtr<const byte> BufferedInputStreamWrapper::getReadBuffer() {
if (bufferAvailable.size() == 0) {
size_t n = inner.read(buffer.begin(), 1, buffer.size());
bufferAvailable = buffer.slice(0, n);
......@@ -117,9 +116,9 @@ void BufferedInputStreamWrapper::skip(size_t bytes) {
// -------------------------------------------------------------------
BufferedOutputStreamWrapper::BufferedOutputStreamWrapper(OutputStream& inner, kj::ArrayPtr<byte> buffer)
BufferedOutputStreamWrapper::BufferedOutputStreamWrapper(OutputStream& inner, ArrayPtr<byte> buffer)
: inner(inner),
ownedBuffer(buffer == nullptr ? kj::newArray<byte>(8192) : nullptr),
ownedBuffer(buffer == nullptr ? newArray<byte>(8192) : nullptr),
buffer(buffer == nullptr ? ownedBuffer : buffer),
bufferPos(this->buffer.begin()) {}
......@@ -144,8 +143,8 @@ void BufferedOutputStreamWrapper::flush() {
}
}
kj::ArrayPtr<byte> BufferedOutputStreamWrapper::getWriteBuffer() {
return kj::arrayPtr(bufferPos, buffer.end());
ArrayPtr<byte> BufferedOutputStreamWrapper::getWriteBuffer() {
return arrayPtr(bufferPos, buffer.end());
}
void BufferedOutputStreamWrapper::write(const void* src, size_t size) {
......@@ -179,10 +178,10 @@ void BufferedOutputStreamWrapper::write(const void* src, size_t size) {
// =======================================================================================
ArrayInputStream::ArrayInputStream(kj::ArrayPtr<const byte> array): array(array) {}
ArrayInputStream::ArrayInputStream(ArrayPtr<const byte> array): array(array) {}
ArrayInputStream::~ArrayInputStream() {}
kj::ArrayPtr<const byte> ArrayInputStream::getReadBuffer() {
ArrayPtr<const byte> ArrayInputStream::getReadBuffer() {
return array;
}
......@@ -206,11 +205,11 @@ void ArrayInputStream::skip(size_t bytes) {
// -------------------------------------------------------------------
ArrayOutputStream::ArrayOutputStream(kj::ArrayPtr<byte> array): array(array), fillPos(array.begin()) {}
ArrayOutputStream::ArrayOutputStream(ArrayPtr<byte> array): array(array), fillPos(array.begin()) {}
ArrayOutputStream::~ArrayOutputStream() {}
kj::ArrayPtr<byte> ArrayOutputStream::getWriteBuffer() {
return kj::arrayPtr(fillPos, array.end());
ArrayPtr<byte> ArrayOutputStream::getWriteBuffer() {
return arrayPtr(fillPos, array.end());
}
void ArrayOutputStream::write(const void* src, size_t size) {
......@@ -264,7 +263,7 @@ void FdOutputStream::write(const void* buffer, size_t size) {
}
}
void FdOutputStream::write(kj::ArrayPtr<const kj::ArrayPtr<const byte>> pieces) {
void FdOutputStream::write(ArrayPtr<const ArrayPtr<const byte>> pieces) {
KJ_STACK_ARRAY(struct iovec, iov, pieces.size(), 16, 128);
for (uint i = 0; i < pieces.size(); i++) {
......@@ -296,4 +295,4 @@ void FdOutputStream::write(kj::ArrayPtr<const kj::ArrayPtr<const byte>> pieces)
}
}
} // namespace capnproto
} // namespace kj
......@@ -21,14 +21,14 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef CAPNPROTO_IO_H_
#define CAPNPROTO_IO_H_
#ifndef KJ_IO_H_
#define KJ_IO_H_
#include <cstddef>
#include <kj/macros.h>
#include "common.h"
#include "macros.h"
#include "type-safety.h"
namespace capnproto {
namespace kj {
// =======================================================================================
// Abstract interfaces
......@@ -68,7 +68,7 @@ public:
virtual void write(const void* buffer, size_t size) = 0;
// Always writes the full size. Throws exception on error.
virtual void write(kj::ArrayPtr<const kj::ArrayPtr<const byte>> pieces);
virtual void write(ArrayPtr<const ArrayPtr<const byte>> pieces);
// Equivalent to write()ing each byte array in sequence, which is what the default implementation
// does. Override if you can do something better, e.g. use writev() to do the write in a single
// syscall.
......@@ -83,7 +83,7 @@ class BufferedInputStream: public InputStream {
public:
virtual ~BufferedInputStream();
virtual kj::ArrayPtr<const byte> getReadBuffer() = 0;
virtual ArrayPtr<const byte> getReadBuffer() = 0;
// Get a direct pointer into the read buffer, which contains the next bytes in the input. If the
// caller consumes any bytes, it should then call skip() to indicate this. This always returns a
// non-empty buffer unless at EOF.
......@@ -98,7 +98,7 @@ class BufferedOutputStream: public OutputStream {
public:
virtual ~BufferedOutputStream();
virtual kj::ArrayPtr<byte> getWriteBuffer() = 0;
virtual ArrayPtr<byte> getWriteBuffer() = 0;
// Get a direct pointer into the write buffer. The caller may choose to fill in some prefix of
// this buffer and then pass it to write(), in which case write() may avoid a copy. It is
// incorrect to pass to write any slice of this buffer which is not a prefix.
......@@ -117,7 +117,7 @@ class BufferedInputStreamWrapper: public BufferedInputStream {
// but is not provided by the library at this time.
public:
explicit BufferedInputStreamWrapper(InputStream& inner, kj::ArrayPtr<byte> buffer = nullptr);
explicit BufferedInputStreamWrapper(InputStream& inner, ArrayPtr<byte> buffer = nullptr);
// Creates a buffered stream wrapping the given non-buffered stream. No guarantee is made about
// the position of the inner stream after a buffered wrapper has been created unless the entire
// input is read.
......@@ -129,15 +129,15 @@ public:
~BufferedInputStreamWrapper();
// implements BufferedInputStream ----------------------------------
kj::ArrayPtr<const byte> getReadBuffer() override;
ArrayPtr<const byte> getReadBuffer() override;
size_t read(void* buffer, size_t minBytes, size_t maxBytes) override;
void skip(size_t bytes) override;
private:
InputStream& inner;
kj::Array<byte> ownedBuffer;
kj::ArrayPtr<byte> buffer;
kj::ArrayPtr<byte> bufferAvailable;
Array<byte> ownedBuffer;
ArrayPtr<byte> buffer;
ArrayPtr<byte> bufferAvailable;
};
class BufferedOutputStreamWrapper: public BufferedOutputStream {
......@@ -145,7 +145,7 @@ class BufferedOutputStreamWrapper: public BufferedOutputStream {
// underlying stream may be delayed until flush() is called or the wrapper is destroyed.
public:
explicit BufferedOutputStreamWrapper(OutputStream& inner, kj::ArrayPtr<byte> buffer = nullptr);
explicit BufferedOutputStreamWrapper(OutputStream& inner, ArrayPtr<byte> buffer = nullptr);
// Creates a buffered stream wrapping the given non-buffered stream.
//
// If the second parameter is non-null, the stream uses the given buffer instead of allocating
......@@ -160,13 +160,13 @@ public:
// that may be present in the underlying stream.
// implements BufferedOutputStream ---------------------------------
kj::ArrayPtr<byte> getWriteBuffer() override;
ArrayPtr<byte> getWriteBuffer() override;
void write(const void* buffer, size_t size) override;
private:
OutputStream& inner;
kj::Array<byte> ownedBuffer;
kj::ArrayPtr<byte> buffer;
Array<byte> ownedBuffer;
ArrayPtr<byte> buffer;
byte* bufferPos;
};
......@@ -175,36 +175,36 @@ private:
class ArrayInputStream: public BufferedInputStream {
public:
explicit ArrayInputStream(kj::ArrayPtr<const byte> array);
explicit ArrayInputStream(ArrayPtr<const byte> array);
KJ_DISALLOW_COPY(ArrayInputStream);
~ArrayInputStream();
// implements BufferedInputStream ----------------------------------
kj::ArrayPtr<const byte> getReadBuffer() override;
ArrayPtr<const byte> getReadBuffer() override;
size_t read(void* buffer, size_t minBytes, size_t maxBytes) override;
void skip(size_t bytes) override;
private:
kj::ArrayPtr<const byte> array;
ArrayPtr<const byte> array;
};
class ArrayOutputStream: public BufferedOutputStream {
public:
explicit ArrayOutputStream(kj::ArrayPtr<byte> array);
explicit ArrayOutputStream(ArrayPtr<byte> array);
KJ_DISALLOW_COPY(ArrayOutputStream);
~ArrayOutputStream();
kj::ArrayPtr<byte> getArray() {
ArrayPtr<byte> getArray() {
// Get the portion of the array which has been filled in.
return kj::arrayPtr(array.begin(), fillPos);
return arrayPtr(array.begin(), fillPos);
}
// implements BufferedInputStream ----------------------------------
kj::ArrayPtr<byte> getWriteBuffer() override;
ArrayPtr<byte> getWriteBuffer() override;
void write(const void* buffer, size_t size) override;
private:
kj::ArrayPtr<byte> array;
ArrayPtr<byte> array;
byte* fillPos;
};
......@@ -243,7 +243,7 @@ class FdInputStream: public InputStream {
public:
explicit FdInputStream(int fd): fd(fd) {};
explicit FdInputStream(AutoCloseFd fd): fd(fd), autoclose(kj::move(fd)) {}
explicit FdInputStream(AutoCloseFd fd): fd(fd), autoclose(move(fd)) {}
KJ_DISALLOW_COPY(FdInputStream);
~FdInputStream();
......@@ -259,18 +259,18 @@ class FdOutputStream: public OutputStream {
public:
explicit FdOutputStream(int fd): fd(fd) {};
explicit FdOutputStream(AutoCloseFd fd): fd(fd), autoclose(kj::move(fd)) {}
explicit FdOutputStream(AutoCloseFd fd): fd(fd), autoclose(move(fd)) {}
KJ_DISALLOW_COPY(FdOutputStream);
~FdOutputStream();
void write(const void* buffer, size_t size) override;
void write(kj::ArrayPtr<const kj::ArrayPtr<const byte>> pieces) override;
void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override;
private:
int fd;
AutoCloseFd autoclose;
};
} // namespace capnproto
} // namespace kj
#endif // CAPNPROTO_IO_H_
#endif // KJ_IO_H_
......@@ -35,6 +35,7 @@
namespace kj {
typedef unsigned int uint;
typedef unsigned char byte;
template <typename T>
struct NoInfer_ {
......
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