annotate win32-mingw/include/capnp/serialize.h @ 141:1b5b6dfd0d0e

Add updated build of PortAudio for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 03 Jan 2017 15:10:52 +0000
parents 38d1c0e7850b
children eccd51b72864
rev   line source
cannam@135 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@135 2 // Licensed under the MIT License:
cannam@135 3 //
cannam@135 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@135 5 // of this software and associated documentation files (the "Software"), to deal
cannam@135 6 // in the Software without restriction, including without limitation the rights
cannam@135 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@135 8 // copies of the Software, and to permit persons to whom the Software is
cannam@135 9 // furnished to do so, subject to the following conditions:
cannam@135 10 //
cannam@135 11 // The above copyright notice and this permission notice shall be included in
cannam@135 12 // all copies or substantial portions of the Software.
cannam@135 13 //
cannam@135 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@135 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@135 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@135 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@135 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@135 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@135 20 // THE SOFTWARE.
cannam@135 21
cannam@135 22 // This file implements a simple serialization format for Cap'n Proto messages. The format
cannam@135 23 // is as follows:
cannam@135 24 //
cannam@135 25 // * 32-bit little-endian segment count (4 bytes).
cannam@135 26 // * 32-bit little-endian size of each segment (4*(segment count) bytes).
cannam@135 27 // * Padding so that subsequent data is 64-bit-aligned (0 or 4 bytes). (I.e., if there are an even
cannam@135 28 // number of segments, there are 4 bytes of zeros here, otherwise there is no padding.)
cannam@135 29 // * Data from each segment, in order (8*sum(segment sizes) bytes)
cannam@135 30 //
cannam@135 31 // This format has some important properties:
cannam@135 32 // - It is self-delimiting, so multiple messages may be written to a stream without any external
cannam@135 33 // delimiter.
cannam@135 34 // - The total size and position of each segment can be determined by reading only the first part
cannam@135 35 // of the message, allowing lazy and random-access reading of the segment data.
cannam@135 36 // - A message is always at least 8 bytes.
cannam@135 37 // - A single-segment message can be read entirely in two system calls with no buffering.
cannam@135 38 // - A multi-segment message can be read entirely in three system calls with no buffering.
cannam@135 39 // - The format is appropriate for mmap()ing since all data is aligned.
cannam@135 40
cannam@135 41 #ifndef CAPNP_SERIALIZE_H_
cannam@135 42 #define CAPNP_SERIALIZE_H_
cannam@135 43
cannam@135 44 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@135 45 #pragma GCC system_header
cannam@135 46 #endif
cannam@135 47
cannam@135 48 #include "message.h"
cannam@135 49 #include <kj/io.h>
cannam@135 50
cannam@135 51 namespace capnp {
cannam@135 52
cannam@135 53 class FlatArrayMessageReader: public MessageReader {
cannam@135 54 // Parses a message from a flat array. Note that it makes sense to use this together with mmap()
cannam@135 55 // for extremely fast parsing.
cannam@135 56
cannam@135 57 public:
cannam@135 58 FlatArrayMessageReader(kj::ArrayPtr<const word> array, ReaderOptions options = ReaderOptions());
cannam@135 59 // The array must remain valid until the MessageReader is destroyed.
cannam@135 60
cannam@135 61 kj::ArrayPtr<const word> getSegment(uint id) override;
cannam@135 62
cannam@135 63 const word* getEnd() const { return end; }
cannam@135 64 // Get a pointer just past the end of the message as determined by reading the message header.
cannam@135 65 // This could actually be before the end of the input array. This pointer is useful e.g. if
cannam@135 66 // you know that the input array has extra stuff appended after the message and you want to
cannam@135 67 // get at it.
cannam@135 68
cannam@135 69 private:
cannam@135 70 // Optimize for single-segment case.
cannam@135 71 kj::ArrayPtr<const word> segment0;
cannam@135 72 kj::Array<kj::ArrayPtr<const word>> moreSegments;
cannam@135 73 const word* end;
cannam@135 74 };
cannam@135 75
cannam@135 76 kj::ArrayPtr<const word> initMessageBuilderFromFlatArrayCopy(
cannam@135 77 kj::ArrayPtr<const word> array, MessageBuilder& target,
cannam@135 78 ReaderOptions options = ReaderOptions());
cannam@135 79 // Convenience function which reads a message using `FlatArrayMessageReader` then copies the
cannam@135 80 // content into the target `MessageBuilder`, verifying that the message structure is valid
cannam@135 81 // (although not necessarily that it matches the desired schema).
cannam@135 82 //
cannam@135 83 // Returns an ArrayPtr containing any words left over in the array after consuming the whole
cannam@135 84 // message. This is useful when reading multiple messages that have been concatenated. See also
cannam@135 85 // FlatArrayMessageReader::getEnd().
cannam@135 86 //
cannam@135 87 // (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one
cannam@135 88 // of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not
cannam@135 89 // safe to use on untrusted input. Therefore, we do not provide a convenience method for it.)
cannam@135 90
cannam@135 91 kj::Array<word> messageToFlatArray(MessageBuilder& builder);
cannam@135 92 // Constructs a flat array containing the entire content of the given message.
cannam@135 93 //
cannam@135 94 // To output the message as bytes, use `.asBytes()` on the returned word array. Keep in mind that
cannam@135 95 // `asBytes()` returns an ArrayPtr, so you have to save the Array as well to prevent it from being
cannam@135 96 // deleted. For example:
cannam@135 97 //
cannam@135 98 // kj::Array<capnp::word> words = messageToFlatArray(myMessage);
cannam@135 99 // kj::ArrayPtr<kj::byte> bytes = words.asBytes();
cannam@135 100 // write(fd, bytes.begin(), bytes.size());
cannam@135 101
cannam@135 102 kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
cannam@135 103 // Version of messageToFlatArray that takes a raw segment array.
cannam@135 104
cannam@135 105 size_t computeSerializedSizeInWords(MessageBuilder& builder);
cannam@135 106 // Returns the size, in words, that will be needed to serialize the message, including the header.
cannam@135 107
cannam@135 108 size_t computeSerializedSizeInWords(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
cannam@135 109 // Version of computeSerializedSizeInWords that takes a raw segment array.
cannam@135 110
cannam@135 111 size_t expectedSizeInWordsFromPrefix(kj::ArrayPtr<const word> messagePrefix);
cannam@135 112 // Given a prefix of a serialized message, try to determine the expected total size of the message,
cannam@135 113 // in words. The returned size is based on the information known so far; it may be an underestimate
cannam@135 114 // if the prefix doesn't contain the full segment table.
cannam@135 115 //
cannam@135 116 // If the returned value is greater than `messagePrefix.size()`, then the message is not yet
cannam@135 117 // complete and the app cannot parse it yet. If the returned value is less than or equal to
cannam@135 118 // `messagePrefix.size()`, then the returned value is the exact total size of the message; any
cannam@135 119 // remaining bytes are part of the next message.
cannam@135 120 //
cannam@135 121 // This function is useful when reading messages from a stream in an asynchronous way, but when
cannam@135 122 // using the full KJ async infrastructure would be too difficult. Each time bytes are received,
cannam@135 123 // use this function to determine if an entire message is ready to be parsed.
cannam@135 124
cannam@135 125 // =======================================================================================
cannam@135 126
cannam@135 127 class InputStreamMessageReader: public MessageReader {
cannam@135 128 // A MessageReader that reads from an abstract kj::InputStream. See also StreamFdMessageReader
cannam@135 129 // for a subclass specific to file descriptors.
cannam@135 130
cannam@135 131 public:
cannam@135 132 InputStreamMessageReader(kj::InputStream& inputStream,
cannam@135 133 ReaderOptions options = ReaderOptions(),
cannam@135 134 kj::ArrayPtr<word> scratchSpace = nullptr);
cannam@135 135 ~InputStreamMessageReader() noexcept(false);
cannam@135 136
cannam@135 137 // implements MessageReader ----------------------------------------
cannam@135 138 kj::ArrayPtr<const word> getSegment(uint id) override;
cannam@135 139
cannam@135 140 private:
cannam@135 141 kj::InputStream& inputStream;
cannam@135 142 byte* readPos;
cannam@135 143
cannam@135 144 // Optimize for single-segment case.
cannam@135 145 kj::ArrayPtr<const word> segment0;
cannam@135 146 kj::Array<kj::ArrayPtr<const word>> moreSegments;
cannam@135 147
cannam@135 148 kj::Array<word> ownedSpace;
cannam@135 149 // Only if scratchSpace wasn't big enough.
cannam@135 150
cannam@135 151 kj::UnwindDetector unwindDetector;
cannam@135 152 };
cannam@135 153
cannam@135 154 void readMessageCopy(kj::InputStream& input, MessageBuilder& target,
cannam@135 155 ReaderOptions options = ReaderOptions(),
cannam@135 156 kj::ArrayPtr<word> scratchSpace = nullptr);
cannam@135 157 // Convenience function which reads a message using `InputStreamMessageReader` then copies the
cannam@135 158 // content into the target `MessageBuilder`, verifying that the message structure is valid
cannam@135 159 // (although not necessarily that it matches the desired schema).
cannam@135 160 //
cannam@135 161 // (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one
cannam@135 162 // of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not
cannam@135 163 // safe to use on untrusted input. Therefore, we do not provide a convenience method for it.)
cannam@135 164
cannam@135 165 void writeMessage(kj::OutputStream& output, MessageBuilder& builder);
cannam@135 166 // Write the message to the given output stream.
cannam@135 167
cannam@135 168 void writeMessage(kj::OutputStream& output, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
cannam@135 169 // Write the segment array to the given output stream.
cannam@135 170
cannam@135 171 // =======================================================================================
cannam@135 172 // Specializations for reading from / writing to file descriptors.
cannam@135 173
cannam@135 174 class StreamFdMessageReader: private kj::FdInputStream, public InputStreamMessageReader {
cannam@135 175 // A MessageReader that reads from a steam-based file descriptor.
cannam@135 176
cannam@135 177 public:
cannam@135 178 StreamFdMessageReader(int fd, ReaderOptions options = ReaderOptions(),
cannam@135 179 kj::ArrayPtr<word> scratchSpace = nullptr)
cannam@135 180 : FdInputStream(fd), InputStreamMessageReader(*this, options, scratchSpace) {}
cannam@135 181 // Read message from a file descriptor, without taking ownership of the descriptor.
cannam@135 182
cannam@135 183 StreamFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(),
cannam@135 184 kj::ArrayPtr<word> scratchSpace = nullptr)
cannam@135 185 : FdInputStream(kj::mv(fd)), InputStreamMessageReader(*this, options, scratchSpace) {}
cannam@135 186 // Read a message from a file descriptor, taking ownership of the descriptor.
cannam@135 187
cannam@135 188 ~StreamFdMessageReader() noexcept(false);
cannam@135 189 };
cannam@135 190
cannam@135 191 void readMessageCopyFromFd(int fd, MessageBuilder& target,
cannam@135 192 ReaderOptions options = ReaderOptions(),
cannam@135 193 kj::ArrayPtr<word> scratchSpace = nullptr);
cannam@135 194 // Convenience function which reads a message using `StreamFdMessageReader` then copies the
cannam@135 195 // content into the target `MessageBuilder`, verifying that the message structure is valid
cannam@135 196 // (although not necessarily that it matches the desired schema).
cannam@135 197 //
cannam@135 198 // (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one
cannam@135 199 // of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not
cannam@135 200 // safe to use on untrusted input. Therefore, we do not provide a convenience method for it.)
cannam@135 201
cannam@135 202 void writeMessageToFd(int fd, MessageBuilder& builder);
cannam@135 203 // Write the message to the given file descriptor.
cannam@135 204 //
cannam@135 205 // This function throws an exception on any I/O error. If your code is not exception-safe, be sure
cannam@135 206 // you catch this exception at the call site. If throwing an exception is not acceptable, you
cannam@135 207 // can implement your own OutputStream with arbitrary error handling and then use writeMessage().
cannam@135 208
cannam@135 209 void writeMessageToFd(int fd, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
cannam@135 210 // Write the segment array to the given file descriptor.
cannam@135 211 //
cannam@135 212 // This function throws an exception on any I/O error. If your code is not exception-safe, be sure
cannam@135 213 // you catch this exception at the call site. If throwing an exception is not acceptable, you
cannam@135 214 // can implement your own OutputStream with arbitrary error handling and then use writeMessage().
cannam@135 215
cannam@135 216 // =======================================================================================
cannam@135 217 // inline stuff
cannam@135 218
cannam@135 219 inline kj::Array<word> messageToFlatArray(MessageBuilder& builder) {
cannam@135 220 return messageToFlatArray(builder.getSegmentsForOutput());
cannam@135 221 }
cannam@135 222
cannam@135 223 inline size_t computeSerializedSizeInWords(MessageBuilder& builder) {
cannam@135 224 return computeSerializedSizeInWords(builder.getSegmentsForOutput());
cannam@135 225 }
cannam@135 226
cannam@135 227 inline void writeMessage(kj::OutputStream& output, MessageBuilder& builder) {
cannam@135 228 writeMessage(output, builder.getSegmentsForOutput());
cannam@135 229 }
cannam@135 230
cannam@135 231 inline void writeMessageToFd(int fd, MessageBuilder& builder) {
cannam@135 232 writeMessageToFd(fd, builder.getSegmentsForOutput());
cannam@135 233 }
cannam@135 234
cannam@135 235 } // namespace capnp
cannam@135 236
cannam@135 237 #endif // SERIALIZE_H_