annotate win32-mingw/include/capnp/serialize.h @ 79:91c729825bca pa_catalina

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