annotate win64-msvc/include/capnp/layout.h @ 145:13a516fa8999

Update PortAudio build for Win64
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 10 Jan 2017 11:28:20 +0000
parents 42a73082be24
children 0f2d93caa50c
rev   line source
cannam@132 1 // Copyright (c) 2013-2016 Sandstorm Development Group, Inc. and contributors
cannam@132 2 // Licensed under the MIT License:
cannam@132 3 //
cannam@132 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@132 5 // of this software and associated documentation files (the "Software"), to deal
cannam@132 6 // in the Software without restriction, including without limitation the rights
cannam@132 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@132 8 // copies of the Software, and to permit persons to whom the Software is
cannam@132 9 // furnished to do so, subject to the following conditions:
cannam@132 10 //
cannam@132 11 // The above copyright notice and this permission notice shall be included in
cannam@132 12 // all copies or substantial portions of the Software.
cannam@132 13 //
cannam@132 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@132 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@132 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@132 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@132 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@132 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@132 20 // THE SOFTWARE.
cannam@132 21
cannam@132 22 // This file is NOT intended for use by clients, except in generated code.
cannam@132 23 //
cannam@132 24 // This file defines low-level, non-type-safe classes for traversing the Cap'n Proto memory layout
cannam@132 25 // (which is also its wire format). Code generated by the Cap'n Proto compiler uses these classes,
cannam@132 26 // as does other parts of the Cap'n proto library which provide a higher-level interface for
cannam@132 27 // dynamic introspection.
cannam@132 28
cannam@132 29 #ifndef CAPNP_LAYOUT_H_
cannam@132 30 #define CAPNP_LAYOUT_H_
cannam@132 31
cannam@132 32 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
cannam@132 33 #pragma GCC system_header
cannam@132 34 #endif
cannam@132 35
cannam@132 36 #include <kj/common.h>
cannam@132 37 #include <kj/memory.h>
cannam@132 38 #include "common.h"
cannam@132 39 #include "blob.h"
cannam@132 40 #include "endian.h"
cannam@132 41
cannam@132 42 #if (defined(__mips__) || defined(__hppa__)) && !defined(CAPNP_CANONICALIZE_NAN)
cannam@132 43 #define CAPNP_CANONICALIZE_NAN 1
cannam@132 44 // Explicitly detect NaNs and canonicalize them to the quiet NaN value as would be returned by
cannam@132 45 // __builtin_nan("") on systems implementing the IEEE-754 recommended (but not required) NaN
cannam@132 46 // signalling/quiet differentiation (such as x86). Unfortunately, some architectures -- in
cannam@132 47 // particular, MIPS -- represent quiet vs. signalling nans differently than the rest of the world.
cannam@132 48 // Canonicalizing them makes output consistent (which is important!), but hurts performance
cannam@132 49 // slightly.
cannam@132 50 //
cannam@132 51 // Note that trying to convert MIPS NaNs to standard NaNs without losing data doesn't work.
cannam@132 52 // Signaling vs. quiet is indicated by a bit, with the meaning being the opposite on MIPS vs.
cannam@132 53 // everyone else. It would be great if we could just flip that bit, but we can't, because if the
cannam@132 54 // significand is all-zero, then the value is infinity rather than NaN. This means that on most
cannam@132 55 // machines, where the bit indicates quietness, there is one more quiet NaN value than signalling
cannam@132 56 // NaN value, whereas on MIPS there is one more sNaN than qNaN, and thus there is no isomorphic
cannam@132 57 // mapping that properly preserves quietness. Instead of doing something hacky, we just give up
cannam@132 58 // and blow away NaN payloads, because no one uses them anyway.
cannam@132 59 #endif
cannam@132 60
cannam@132 61 namespace capnp {
cannam@132 62
cannam@132 63 #if !CAPNP_LITE
cannam@132 64 class ClientHook;
cannam@132 65 #endif // !CAPNP_LITE
cannam@132 66
cannam@132 67 namespace _ { // private
cannam@132 68
cannam@132 69 class PointerBuilder;
cannam@132 70 class PointerReader;
cannam@132 71 class StructBuilder;
cannam@132 72 class StructReader;
cannam@132 73 class ListBuilder;
cannam@132 74 class ListReader;
cannam@132 75 class OrphanBuilder;
cannam@132 76 struct WirePointer;
cannam@132 77 struct WireHelpers;
cannam@132 78 class SegmentReader;
cannam@132 79 class SegmentBuilder;
cannam@132 80 class Arena;
cannam@132 81 class BuilderArena;
cannam@132 82
cannam@132 83 // =============================================================================
cannam@132 84
cannam@132 85 typedef decltype(BITS / ELEMENTS) BitsPerElement;
cannam@132 86 typedef decltype(POINTERS / ELEMENTS) PointersPerElement;
cannam@132 87
cannam@132 88 static constexpr BitsPerElement BITS_PER_ELEMENT_TABLE[8] = {
cannam@132 89 0 * BITS / ELEMENTS,
cannam@132 90 1 * BITS / ELEMENTS,
cannam@132 91 8 * BITS / ELEMENTS,
cannam@132 92 16 * BITS / ELEMENTS,
cannam@132 93 32 * BITS / ELEMENTS,
cannam@132 94 64 * BITS / ELEMENTS,
cannam@132 95 0 * BITS / ELEMENTS,
cannam@132 96 0 * BITS / ELEMENTS
cannam@132 97 };
cannam@132 98
cannam@132 99 inline KJ_CONSTEXPR() BitsPerElement dataBitsPerElement(ElementSize size) {
cannam@132 100 return _::BITS_PER_ELEMENT_TABLE[static_cast<int>(size)];
cannam@132 101 }
cannam@132 102
cannam@132 103 inline constexpr PointersPerElement pointersPerElement(ElementSize size) {
cannam@132 104 return size == ElementSize::POINTER ? 1 * POINTERS / ELEMENTS : 0 * POINTERS / ELEMENTS;
cannam@132 105 }
cannam@132 106
cannam@132 107 template <size_t size> struct ElementSizeForByteSize;
cannam@132 108 template <> struct ElementSizeForByteSize<1> { static constexpr ElementSize value = ElementSize::BYTE; };
cannam@132 109 template <> struct ElementSizeForByteSize<2> { static constexpr ElementSize value = ElementSize::TWO_BYTES; };
cannam@132 110 template <> struct ElementSizeForByteSize<4> { static constexpr ElementSize value = ElementSize::FOUR_BYTES; };
cannam@132 111 template <> struct ElementSizeForByteSize<8> { static constexpr ElementSize value = ElementSize::EIGHT_BYTES; };
cannam@132 112
cannam@132 113 template <typename T> struct ElementSizeForType {
cannam@132 114 static constexpr ElementSize value =
cannam@132 115 // Primitive types that aren't special-cased below can be determined from sizeof().
cannam@132 116 CAPNP_KIND(T) == Kind::PRIMITIVE ? ElementSizeForByteSize<sizeof(T)>::value :
cannam@132 117 CAPNP_KIND(T) == Kind::ENUM ? ElementSize::TWO_BYTES :
cannam@132 118 CAPNP_KIND(T) == Kind::STRUCT ? ElementSize::INLINE_COMPOSITE :
cannam@132 119
cannam@132 120 // Everything else is a pointer.
cannam@132 121 ElementSize::POINTER;
cannam@132 122 };
cannam@132 123
cannam@132 124 // Void and bool are special.
cannam@132 125 template <> struct ElementSizeForType<Void> { static constexpr ElementSize value = ElementSize::VOID; };
cannam@132 126 template <> struct ElementSizeForType<bool> { static constexpr ElementSize value = ElementSize::BIT; };
cannam@132 127
cannam@132 128 // Lists and blobs are pointers, not structs.
cannam@132 129 template <typename T, bool b> struct ElementSizeForType<List<T, b>> {
cannam@132 130 static constexpr ElementSize value = ElementSize::POINTER;
cannam@132 131 };
cannam@132 132 template <> struct ElementSizeForType<Text> {
cannam@132 133 static constexpr ElementSize value = ElementSize::POINTER;
cannam@132 134 };
cannam@132 135 template <> struct ElementSizeForType<Data> {
cannam@132 136 static constexpr ElementSize value = ElementSize::POINTER;
cannam@132 137 };
cannam@132 138
cannam@132 139 template <typename T>
cannam@132 140 inline constexpr ElementSize elementSizeForType() {
cannam@132 141 return ElementSizeForType<T>::value;
cannam@132 142 }
cannam@132 143
cannam@132 144 struct MessageSizeCounts {
cannam@132 145 WordCount64 wordCount;
cannam@132 146 uint capCount;
cannam@132 147
cannam@132 148 MessageSizeCounts& operator+=(const MessageSizeCounts& other) {
cannam@132 149 wordCount += other.wordCount;
cannam@132 150 capCount += other.capCount;
cannam@132 151 return *this;
cannam@132 152 }
cannam@132 153
cannam@132 154 MessageSize asPublic() {
cannam@132 155 return MessageSize { wordCount / WORDS, capCount };
cannam@132 156 }
cannam@132 157 };
cannam@132 158
cannam@132 159 // =============================================================================
cannam@132 160
cannam@132 161 template <int wordCount>
cannam@132 162 union AlignedData {
cannam@132 163 // Useful for declaring static constant data blobs as an array of bytes, but forcing those
cannam@132 164 // bytes to be word-aligned.
cannam@132 165
cannam@132 166 uint8_t bytes[wordCount * sizeof(word)];
cannam@132 167 word words[wordCount];
cannam@132 168 };
cannam@132 169
cannam@132 170 struct StructSize {
cannam@132 171 WordCount16 data;
cannam@132 172 WirePointerCount16 pointers;
cannam@132 173
cannam@132 174 inline constexpr WordCount total() const { return data + pointers * WORDS_PER_POINTER; }
cannam@132 175
cannam@132 176 StructSize() = default;
cannam@132 177 inline constexpr StructSize(WordCount data, WirePointerCount pointers)
cannam@132 178 : data(data), pointers(pointers) {}
cannam@132 179 };
cannam@132 180
cannam@132 181 template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
cannam@132 182 inline constexpr StructSize structSize() {
cannam@132 183 return StructSize(CapnpPrivate::dataWordSize * WORDS, CapnpPrivate::pointerCount * POINTERS);
cannam@132 184 }
cannam@132 185
cannam@132 186 template <typename T, typename CapnpPrivate = typename T::_capnpPrivate,
cannam@132 187 typename = kj::EnableIf<CAPNP_KIND(T) == Kind::STRUCT>>
cannam@132 188 inline constexpr StructSize minStructSizeForElement() {
cannam@132 189 // If T is a struct, return its struct size. Otherwise return the minimum struct size big enough
cannam@132 190 // to hold a T.
cannam@132 191
cannam@132 192 return StructSize(CapnpPrivate::dataWordSize * WORDS, CapnpPrivate::pointerCount * POINTERS);
cannam@132 193 }
cannam@132 194
cannam@132 195 template <typename T, typename = kj::EnableIf<CAPNP_KIND(T) != Kind::STRUCT>>
cannam@132 196 inline constexpr StructSize minStructSizeForElement() {
cannam@132 197 // If T is a struct, return its struct size. Otherwise return the minimum struct size big enough
cannam@132 198 // to hold a T.
cannam@132 199
cannam@132 200 return StructSize(
cannam@132 201 dataBitsPerElement(elementSizeForType<T>()) * ELEMENTS > 0 * BITS ? 1 * WORDS : 0 * WORDS,
cannam@132 202 pointersPerElement(elementSizeForType<T>()) * ELEMENTS);
cannam@132 203 }
cannam@132 204
cannam@132 205 // -------------------------------------------------------------------
cannam@132 206 // Masking of default values
cannam@132 207
cannam@132 208 template <typename T, Kind kind = CAPNP_KIND(T)> struct Mask_;
cannam@132 209 template <typename T> struct Mask_<T, Kind::PRIMITIVE> { typedef T Type; };
cannam@132 210 template <typename T> struct Mask_<T, Kind::ENUM> { typedef uint16_t Type; };
cannam@132 211 template <> struct Mask_<float, Kind::PRIMITIVE> { typedef uint32_t Type; };
cannam@132 212 template <> struct Mask_<double, Kind::PRIMITIVE> { typedef uint64_t Type; };
cannam@132 213
cannam@132 214 template <typename T> struct Mask_<T, Kind::OTHER> {
cannam@132 215 // Union discriminants end up here.
cannam@132 216 static_assert(sizeof(T) == 2, "Don't know how to mask this type.");
cannam@132 217 typedef uint16_t Type;
cannam@132 218 };
cannam@132 219
cannam@132 220 template <typename T>
cannam@132 221 using Mask = typename Mask_<T>::Type;
cannam@132 222
cannam@132 223 template <typename T>
cannam@132 224 KJ_ALWAYS_INLINE(Mask<T> mask(T value, Mask<T> mask));
cannam@132 225 template <typename T>
cannam@132 226 KJ_ALWAYS_INLINE(T unmask(Mask<T> value, Mask<T> mask));
cannam@132 227
cannam@132 228 template <typename T>
cannam@132 229 inline Mask<T> mask(T value, Mask<T> mask) {
cannam@132 230 return static_cast<Mask<T> >(value) ^ mask;
cannam@132 231 }
cannam@132 232
cannam@132 233 template <>
cannam@132 234 inline uint32_t mask<float>(float value, uint32_t mask) {
cannam@132 235 #if CAPNP_CANONICALIZE_NAN
cannam@132 236 if (value != value) {
cannam@132 237 return 0x7fc00000u ^ mask;
cannam@132 238 }
cannam@132 239 #endif
cannam@132 240
cannam@132 241 uint32_t i;
cannam@132 242 static_assert(sizeof(i) == sizeof(value), "float is not 32 bits?");
cannam@132 243 memcpy(&i, &value, sizeof(value));
cannam@132 244 return i ^ mask;
cannam@132 245 }
cannam@132 246
cannam@132 247 template <>
cannam@132 248 inline uint64_t mask<double>(double value, uint64_t mask) {
cannam@132 249 #if CAPNP_CANONICALIZE_NAN
cannam@132 250 if (value != value) {
cannam@132 251 return 0x7ff8000000000000ull ^ mask;
cannam@132 252 }
cannam@132 253 #endif
cannam@132 254
cannam@132 255 uint64_t i;
cannam@132 256 static_assert(sizeof(i) == sizeof(value), "double is not 64 bits?");
cannam@132 257 memcpy(&i, &value, sizeof(value));
cannam@132 258 return i ^ mask;
cannam@132 259 }
cannam@132 260
cannam@132 261 template <typename T>
cannam@132 262 inline T unmask(Mask<T> value, Mask<T> mask) {
cannam@132 263 return static_cast<T>(value ^ mask);
cannam@132 264 }
cannam@132 265
cannam@132 266 template <>
cannam@132 267 inline float unmask<float>(uint32_t value, uint32_t mask) {
cannam@132 268 value ^= mask;
cannam@132 269 float result;
cannam@132 270 static_assert(sizeof(result) == sizeof(value), "float is not 32 bits?");
cannam@132 271 memcpy(&result, &value, sizeof(value));
cannam@132 272 return result;
cannam@132 273 }
cannam@132 274
cannam@132 275 template <>
cannam@132 276 inline double unmask<double>(uint64_t value, uint64_t mask) {
cannam@132 277 value ^= mask;
cannam@132 278 double result;
cannam@132 279 static_assert(sizeof(result) == sizeof(value), "double is not 64 bits?");
cannam@132 280 memcpy(&result, &value, sizeof(value));
cannam@132 281 return result;
cannam@132 282 }
cannam@132 283
cannam@132 284 // -------------------------------------------------------------------
cannam@132 285
cannam@132 286 class CapTableReader {
cannam@132 287 public:
cannam@132 288 #if !CAPNP_LITE
cannam@132 289 virtual kj::Maybe<kj::Own<ClientHook>> extractCap(uint index) = 0;
cannam@132 290 // Extract the capability at the given index. If the index is invalid, returns null.
cannam@132 291 #endif // !CAPNP_LITE
cannam@132 292 };
cannam@132 293
cannam@132 294 class CapTableBuilder: public CapTableReader {
cannam@132 295 public:
cannam@132 296 #if !CAPNP_LITE
cannam@132 297 virtual uint injectCap(kj::Own<ClientHook>&& cap) = 0;
cannam@132 298 // Add the capability to the message and return its index. If the same ClientHook is injected
cannam@132 299 // twice, this may return the same index both times, but in this case dropCap() needs to be
cannam@132 300 // called an equal number of times to actually remove the cap.
cannam@132 301
cannam@132 302 virtual void dropCap(uint index) = 0;
cannam@132 303 // Remove a capability injected earlier. Called when the pointer is overwritten or zero'd out.
cannam@132 304 #endif // !CAPNP_LITE
cannam@132 305 };
cannam@132 306
cannam@132 307 // -------------------------------------------------------------------
cannam@132 308
cannam@132 309 class PointerBuilder: public kj::DisallowConstCopy {
cannam@132 310 // Represents a single pointer, usually embedded in a struct or a list.
cannam@132 311
cannam@132 312 public:
cannam@132 313 inline PointerBuilder(): segment(nullptr), capTable(nullptr), pointer(nullptr) {}
cannam@132 314
cannam@132 315 static inline PointerBuilder getRoot(
cannam@132 316 SegmentBuilder* segment, CapTableBuilder* capTable, word* location);
cannam@132 317 // Get a PointerBuilder representing a message root located in the given segment at the given
cannam@132 318 // location.
cannam@132 319
cannam@132 320 inline bool isNull() { return getPointerType() == PointerType::NULL_; }
cannam@132 321 PointerType getPointerType();
cannam@132 322
cannam@132 323 StructBuilder getStruct(StructSize size, const word* defaultValue);
cannam@132 324 ListBuilder getList(ElementSize elementSize, const word* defaultValue);
cannam@132 325 ListBuilder getStructList(StructSize elementSize, const word* defaultValue);
cannam@132 326 ListBuilder getListAnySize(const word* defaultValue);
cannam@132 327 template <typename T> typename T::Builder getBlob(const void* defaultValue,ByteCount defaultSize);
cannam@132 328 #if !CAPNP_LITE
cannam@132 329 kj::Own<ClientHook> getCapability();
cannam@132 330 #endif // !CAPNP_LITE
cannam@132 331 // Get methods: Get the value. If it is null, initialize it to a copy of the default value.
cannam@132 332 // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a
cannam@132 333 // simple byte array for blobs.
cannam@132 334
cannam@132 335 StructBuilder initStruct(StructSize size);
cannam@132 336 ListBuilder initList(ElementSize elementSize, ElementCount elementCount);
cannam@132 337 ListBuilder initStructList(ElementCount elementCount, StructSize size);
cannam@132 338 template <typename T> typename T::Builder initBlob(ByteCount size);
cannam@132 339 // Init methods: Initialize the pointer to a newly-allocated object, discarding the existing
cannam@132 340 // object.
cannam@132 341
cannam@132 342 void setStruct(const StructReader& value, bool canonical = false);
cannam@132 343 void setList(const ListReader& value, bool canonical = false);
cannam@132 344 template <typename T> void setBlob(typename T::Reader value);
cannam@132 345 #if !CAPNP_LITE
cannam@132 346 void setCapability(kj::Own<ClientHook>&& cap);
cannam@132 347 #endif // !CAPNP_LITE
cannam@132 348 // Set methods: Initialize the pointer to a newly-allocated copy of the given value, discarding
cannam@132 349 // the existing object.
cannam@132 350
cannam@132 351 void adopt(OrphanBuilder&& orphan);
cannam@132 352 // Set the pointer to point at the given orphaned value.
cannam@132 353
cannam@132 354 OrphanBuilder disown();
cannam@132 355 // Set the pointer to null and return its previous value as an orphan.
cannam@132 356
cannam@132 357 void clear();
cannam@132 358 // Clear the pointer to null, discarding its previous value.
cannam@132 359
cannam@132 360 void transferFrom(PointerBuilder other);
cannam@132 361 // Equivalent to `adopt(other.disown())`.
cannam@132 362
cannam@132 363 void copyFrom(PointerReader other, bool canonical = false);
cannam@132 364 // Equivalent to `set(other.get())`.
cannam@132 365 // If you set the canonical flag, it will attempt to lay the target out
cannam@132 366 // canonically, provided enough space is available.
cannam@132 367
cannam@132 368 PointerReader asReader() const;
cannam@132 369
cannam@132 370 BuilderArena* getArena() const;
cannam@132 371 // Get the arena containing this pointer.
cannam@132 372
cannam@132 373 CapTableBuilder* getCapTable();
cannam@132 374 // Gets the capability context in which this object is operating.
cannam@132 375
cannam@132 376 PointerBuilder imbue(CapTableBuilder* capTable);
cannam@132 377 // Return a copy of this builder except using the given capability context.
cannam@132 378
cannam@132 379 private:
cannam@132 380 SegmentBuilder* segment; // Memory segment in which the pointer resides.
cannam@132 381 CapTableBuilder* capTable; // Table of capability indexes.
cannam@132 382 WirePointer* pointer; // Pointer to the pointer.
cannam@132 383
cannam@132 384 inline PointerBuilder(SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* pointer)
cannam@132 385 : segment(segment), capTable(capTable), pointer(pointer) {}
cannam@132 386
cannam@132 387 friend class StructBuilder;
cannam@132 388 friend class ListBuilder;
cannam@132 389 friend class OrphanBuilder;
cannam@132 390 };
cannam@132 391
cannam@132 392 class PointerReader {
cannam@132 393 public:
cannam@132 394 inline PointerReader()
cannam@132 395 : segment(nullptr), capTable(nullptr), pointer(nullptr), nestingLimit(0x7fffffff) {}
cannam@132 396
cannam@132 397 static PointerReader getRoot(SegmentReader* segment, CapTableReader* capTable,
cannam@132 398 const word* location, int nestingLimit);
cannam@132 399 // Get a PointerReader representing a message root located in the given segment at the given
cannam@132 400 // location.
cannam@132 401
cannam@132 402 static inline PointerReader getRootUnchecked(const word* location);
cannam@132 403 // Get a PointerReader for an unchecked message.
cannam@132 404
cannam@132 405 MessageSizeCounts targetSize() const;
cannam@132 406 // Return the total size of the target object and everything to which it points. Does not count
cannam@132 407 // far pointer overhead. This is useful for deciding how much space is needed to copy the object
cannam@132 408 // into a flat array. However, the caller is advised NOT to treat this value as secure. Instead,
cannam@132 409 // use the result as a hint for allocating the first segment, do the copy, and then throw an
cannam@132 410 // exception if it overruns.
cannam@132 411
cannam@132 412 inline bool isNull() const { return getPointerType() == PointerType::NULL_; }
cannam@132 413 PointerType getPointerType() const;
cannam@132 414
cannam@132 415 StructReader getStruct(const word* defaultValue) const;
cannam@132 416 ListReader getList(ElementSize expectedElementSize, const word* defaultValue) const;
cannam@132 417 ListReader getListAnySize(const word* defaultValue) const;
cannam@132 418 template <typename T>
cannam@132 419 typename T::Reader getBlob(const void* defaultValue, ByteCount defaultSize) const;
cannam@132 420 #if !CAPNP_LITE
cannam@132 421 kj::Own<ClientHook> getCapability() const;
cannam@132 422 #endif // !CAPNP_LITE
cannam@132 423 // Get methods: Get the value. If it is null, return the default value instead.
cannam@132 424 // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a
cannam@132 425 // simple byte array for blobs.
cannam@132 426
cannam@132 427 const word* getUnchecked() const;
cannam@132 428 // If this is an unchecked message, get a word* pointing at the location of the pointer. This
cannam@132 429 // word* can actually be passed to readUnchecked() to read the designated sub-object later. If
cannam@132 430 // this isn't an unchecked message, throws an exception.
cannam@132 431
cannam@132 432 kj::Maybe<Arena&> getArena() const;
cannam@132 433 // Get the arena containing this pointer.
cannam@132 434
cannam@132 435 CapTableReader* getCapTable();
cannam@132 436 // Gets the capability context in which this object is operating.
cannam@132 437
cannam@132 438 PointerReader imbue(CapTableReader* capTable) const;
cannam@132 439 // Return a copy of this reader except using the given capability context.
cannam@132 440
cannam@132 441 bool isCanonical(const word **readHead);
cannam@132 442 // Validate this pointer's canonicity, subject to the conditions:
cannam@132 443 // * All data to the left of readHead has been read thus far (for pointer
cannam@132 444 // ordering)
cannam@132 445 // * All pointers in preorder have already been checked
cannam@132 446 // * This pointer is in the first and only segment of the message
cannam@132 447
cannam@132 448 private:
cannam@132 449 SegmentReader* segment; // Memory segment in which the pointer resides.
cannam@132 450 CapTableReader* capTable; // Table of capability indexes.
cannam@132 451 const WirePointer* pointer; // Pointer to the pointer. null = treat as null pointer.
cannam@132 452
cannam@132 453 int nestingLimit;
cannam@132 454 // Limits the depth of message structures to guard against stack-overflow-based DoS attacks.
cannam@132 455 // Once this reaches zero, further pointers will be pruned.
cannam@132 456
cannam@132 457 inline PointerReader(SegmentReader* segment, CapTableReader* capTable,
cannam@132 458 const WirePointer* pointer, int nestingLimit)
cannam@132 459 : segment(segment), capTable(capTable), pointer(pointer), nestingLimit(nestingLimit) {}
cannam@132 460
cannam@132 461 friend class StructReader;
cannam@132 462 friend class ListReader;
cannam@132 463 friend class PointerBuilder;
cannam@132 464 friend class OrphanBuilder;
cannam@132 465 };
cannam@132 466
cannam@132 467 // -------------------------------------------------------------------
cannam@132 468
cannam@132 469 class StructBuilder: public kj::DisallowConstCopy {
cannam@132 470 public:
cannam@132 471 inline StructBuilder(): segment(nullptr), capTable(nullptr), data(nullptr), pointers(nullptr) {}
cannam@132 472
cannam@132 473 inline word* getLocation() { return reinterpret_cast<word*>(data); }
cannam@132 474 // Get the object's location. Only valid for independently-allocated objects (i.e. not list
cannam@132 475 // elements).
cannam@132 476
cannam@132 477 inline BitCount getDataSectionSize() const { return dataSize; }
cannam@132 478 inline WirePointerCount getPointerSectionSize() const { return pointerCount; }
cannam@132 479 inline kj::ArrayPtr<byte> getDataSectionAsBlob();
cannam@132 480 inline _::ListBuilder getPointerSectionAsList();
cannam@132 481
cannam@132 482 template <typename T>
cannam@132 483 KJ_ALWAYS_INLINE(bool hasDataField(ElementCount offset));
cannam@132 484 // Return true if the field is set to something other than its default value.
cannam@132 485
cannam@132 486 template <typename T>
cannam@132 487 KJ_ALWAYS_INLINE(T getDataField(ElementCount offset));
cannam@132 488 // Gets the data field value of the given type at the given offset. The offset is measured in
cannam@132 489 // multiples of the field size, determined by the type.
cannam@132 490
cannam@132 491 template <typename T>
cannam@132 492 KJ_ALWAYS_INLINE(T getDataField(ElementCount offset, Mask<T> mask));
cannam@132 493 // Like getDataField() but applies the given XOR mask to the data on load. Used for reading
cannam@132 494 // fields with non-zero default values.
cannam@132 495
cannam@132 496 template <typename T>
cannam@132 497 KJ_ALWAYS_INLINE(void setDataField(
cannam@132 498 ElementCount offset, kj::NoInfer<T> value));
cannam@132 499 // Sets the data field value at the given offset.
cannam@132 500
cannam@132 501 template <typename T>
cannam@132 502 KJ_ALWAYS_INLINE(void setDataField(
cannam@132 503 ElementCount offset, kj::NoInfer<T> value, Mask<T> mask));
cannam@132 504 // Like setDataField() but applies the given XOR mask before storing. Used for writing fields
cannam@132 505 // with non-zero default values.
cannam@132 506
cannam@132 507 KJ_ALWAYS_INLINE(PointerBuilder getPointerField(WirePointerCount ptrIndex));
cannam@132 508 // Get a builder for a pointer field given the index within the pointer section.
cannam@132 509
cannam@132 510 void clearAll();
cannam@132 511 // Clear all pointers and data.
cannam@132 512
cannam@132 513 void transferContentFrom(StructBuilder other);
cannam@132 514 // Adopt all pointers from `other`, and also copy all data. If `other`'s sections are larger
cannam@132 515 // than this, the extra data is not transferred, meaning there is a risk of data loss when
cannam@132 516 // transferring from messages built with future versions of the protocol.
cannam@132 517
cannam@132 518 void copyContentFrom(StructReader other);
cannam@132 519 // Copy content from `other`. If `other`'s sections are larger than this, the extra data is not
cannam@132 520 // copied, meaning there is a risk of data loss when copying from messages built with future
cannam@132 521 // versions of the protocol.
cannam@132 522
cannam@132 523 StructReader asReader() const;
cannam@132 524 // Gets a StructReader pointing at the same memory.
cannam@132 525
cannam@132 526 BuilderArena* getArena();
cannam@132 527 // Gets the arena in which this object is allocated.
cannam@132 528
cannam@132 529 CapTableBuilder* getCapTable();
cannam@132 530 // Gets the capability context in which this object is operating.
cannam@132 531
cannam@132 532 StructBuilder imbue(CapTableBuilder* capTable);
cannam@132 533 // Return a copy of this builder except using the given capability context.
cannam@132 534
cannam@132 535 private:
cannam@132 536 SegmentBuilder* segment; // Memory segment in which the struct resides.
cannam@132 537 CapTableBuilder* capTable; // Table of capability indexes.
cannam@132 538 void* data; // Pointer to the encoded data.
cannam@132 539 WirePointer* pointers; // Pointer to the encoded pointers.
cannam@132 540
cannam@132 541 BitCount32 dataSize;
cannam@132 542 // Size of data section. We use a bit count rather than a word count to more easily handle the
cannam@132 543 // case of struct lists encoded with less than a word per element.
cannam@132 544
cannam@132 545 WirePointerCount16 pointerCount; // Size of the pointer section.
cannam@132 546
cannam@132 547 inline StructBuilder(SegmentBuilder* segment, CapTableBuilder* capTable,
cannam@132 548 void* data, WirePointer* pointers,
cannam@132 549 BitCount dataSize, WirePointerCount pointerCount)
cannam@132 550 : segment(segment), capTable(capTable), data(data), pointers(pointers),
cannam@132 551 dataSize(dataSize), pointerCount(pointerCount) {}
cannam@132 552
cannam@132 553 friend class ListBuilder;
cannam@132 554 friend struct WireHelpers;
cannam@132 555 friend class OrphanBuilder;
cannam@132 556 };
cannam@132 557
cannam@132 558 class StructReader {
cannam@132 559 public:
cannam@132 560 inline StructReader()
cannam@132 561 : segment(nullptr), capTable(nullptr), data(nullptr), pointers(nullptr), dataSize(0),
cannam@132 562 pointerCount(0), nestingLimit(0x7fffffff) {}
cannam@132 563 inline StructReader(kj::ArrayPtr<const word> data)
cannam@132 564 : segment(nullptr), capTable(nullptr), data(data.begin()), pointers(nullptr),
cannam@132 565 dataSize(data.size() * WORDS * BITS_PER_WORD), pointerCount(0), nestingLimit(0x7fffffff) {}
cannam@132 566
cannam@132 567 const void* getLocation() const { return data; }
cannam@132 568
cannam@132 569 inline BitCount getDataSectionSize() const { return dataSize; }
cannam@132 570 inline WirePointerCount getPointerSectionSize() const { return pointerCount; }
cannam@132 571 inline kj::ArrayPtr<const byte> getDataSectionAsBlob();
cannam@132 572 inline _::ListReader getPointerSectionAsList();
cannam@132 573
cannam@132 574 kj::Array<word> canonicalize();
cannam@132 575
cannam@132 576 template <typename T>
cannam@132 577 KJ_ALWAYS_INLINE(bool hasDataField(ElementCount offset) const);
cannam@132 578 // Return true if the field is set to something other than its default value.
cannam@132 579
cannam@132 580 template <typename T>
cannam@132 581 KJ_ALWAYS_INLINE(T getDataField(ElementCount offset) const);
cannam@132 582 // Get the data field value of the given type at the given offset. The offset is measured in
cannam@132 583 // multiples of the field size, determined by the type. Returns zero if the offset is past the
cannam@132 584 // end of the struct's data section.
cannam@132 585
cannam@132 586 template <typename T>
cannam@132 587 KJ_ALWAYS_INLINE(
cannam@132 588 T getDataField(ElementCount offset, Mask<T> mask) const);
cannam@132 589 // Like getDataField(offset), but applies the given XOR mask to the result. Used for reading
cannam@132 590 // fields with non-zero default values.
cannam@132 591
cannam@132 592 KJ_ALWAYS_INLINE(PointerReader getPointerField(WirePointerCount ptrIndex) const);
cannam@132 593 // Get a reader for a pointer field given the index within the pointer section. If the index
cannam@132 594 // is out-of-bounds, returns a null pointer.
cannam@132 595
cannam@132 596 MessageSizeCounts totalSize() const;
cannam@132 597 // Return the total size of the struct and everything to which it points. Does not count far
cannam@132 598 // pointer overhead. This is useful for deciding how much space is needed to copy the struct
cannam@132 599 // into a flat array. However, the caller is advised NOT to treat this value as secure. Instead,
cannam@132 600 // use the result as a hint for allocating the first segment, do the copy, and then throw an
cannam@132 601 // exception if it overruns.
cannam@132 602
cannam@132 603 CapTableReader* getCapTable();
cannam@132 604 // Gets the capability context in which this object is operating.
cannam@132 605
cannam@132 606 StructReader imbue(CapTableReader* capTable) const;
cannam@132 607 // Return a copy of this reader except using the given capability context.
cannam@132 608
cannam@132 609 bool isCanonical(const word **readHead, const word **ptrHead,
cannam@132 610 bool *dataTrunc, bool *ptrTrunc);
cannam@132 611 // Validate this pointer's canonicity, subject to the conditions:
cannam@132 612 // * All data to the left of readHead has been read thus far (for pointer
cannam@132 613 // ordering)
cannam@132 614 // * All pointers in preorder have already been checked
cannam@132 615 // * This pointer is in the first and only segment of the message
cannam@132 616 //
cannam@132 617 // If this function returns false, the struct is non-canonical. If it
cannam@132 618 // returns true, then:
cannam@132 619 // * If it is a composite in a list, it is canonical if at least one struct
cannam@132 620 // in the list outputs dataTrunc = 1, and at least one outputs ptrTrunc = 1
cannam@132 621 // * If it is derived from a struct pointer, it is canonical if
cannam@132 622 // dataTrunc = 1 AND ptrTrunc = 1
cannam@132 623
cannam@132 624 private:
cannam@132 625 SegmentReader* segment; // Memory segment in which the struct resides.
cannam@132 626 CapTableReader* capTable; // Table of capability indexes.
cannam@132 627
cannam@132 628 const void* data;
cannam@132 629 const WirePointer* pointers;
cannam@132 630
cannam@132 631 BitCount32 dataSize;
cannam@132 632 // Size of data section. We use a bit count rather than a word count to more easily handle the
cannam@132 633 // case of struct lists encoded with less than a word per element.
cannam@132 634
cannam@132 635 WirePointerCount16 pointerCount; // Size of the pointer section.
cannam@132 636
cannam@132 637 int nestingLimit;
cannam@132 638 // Limits the depth of message structures to guard against stack-overflow-based DoS attacks.
cannam@132 639 // Once this reaches zero, further pointers will be pruned.
cannam@132 640 // TODO(perf): Limit to 16 bits for better packing?
cannam@132 641
cannam@132 642 inline StructReader(SegmentReader* segment, CapTableReader* capTable,
cannam@132 643 const void* data, const WirePointer* pointers,
cannam@132 644 BitCount dataSize, WirePointerCount pointerCount, int nestingLimit)
cannam@132 645 : segment(segment), capTable(capTable), data(data), pointers(pointers),
cannam@132 646 dataSize(dataSize), pointerCount(pointerCount),
cannam@132 647 nestingLimit(nestingLimit) {}
cannam@132 648
cannam@132 649 friend class ListReader;
cannam@132 650 friend class StructBuilder;
cannam@132 651 friend struct WireHelpers;
cannam@132 652 };
cannam@132 653
cannam@132 654 // -------------------------------------------------------------------
cannam@132 655
cannam@132 656 class ListBuilder: public kj::DisallowConstCopy {
cannam@132 657 public:
cannam@132 658 inline explicit ListBuilder(ElementSize elementSize)
cannam@132 659 : segment(nullptr), capTable(nullptr), ptr(nullptr), elementCount(0 * ELEMENTS),
cannam@132 660 step(0 * BITS / ELEMENTS), structDataSize(0 * BITS), structPointerCount(0 * POINTERS),
cannam@132 661 elementSize(elementSize) {}
cannam@132 662
cannam@132 663 inline word* getLocation() {
cannam@132 664 // Get the object's location.
cannam@132 665
cannam@132 666 if (elementSize == ElementSize::INLINE_COMPOSITE && ptr != nullptr) {
cannam@132 667 return reinterpret_cast<word*>(ptr) - POINTER_SIZE_IN_WORDS;
cannam@132 668 } else {
cannam@132 669 return reinterpret_cast<word*>(ptr);
cannam@132 670 }
cannam@132 671 }
cannam@132 672
cannam@132 673 inline ElementSize getElementSize() const { return elementSize; }
cannam@132 674
cannam@132 675 inline ElementCount size() const;
cannam@132 676 // The number of elements in the list.
cannam@132 677
cannam@132 678 Text::Builder asText();
cannam@132 679 Data::Builder asData();
cannam@132 680 // Reinterpret the list as a blob. Throws an exception if the elements are not byte-sized.
cannam@132 681
cannam@132 682 template <typename T>
cannam@132 683 KJ_ALWAYS_INLINE(T getDataElement(ElementCount index));
cannam@132 684 // Get the element of the given type at the given index.
cannam@132 685
cannam@132 686 template <typename T>
cannam@132 687 KJ_ALWAYS_INLINE(void setDataElement(
cannam@132 688 ElementCount index, kj::NoInfer<T> value));
cannam@132 689 // Set the element at the given index.
cannam@132 690
cannam@132 691 KJ_ALWAYS_INLINE(PointerBuilder getPointerElement(ElementCount index));
cannam@132 692
cannam@132 693 StructBuilder getStructElement(ElementCount index);
cannam@132 694
cannam@132 695 ListReader asReader() const;
cannam@132 696 // Get a ListReader pointing at the same memory.
cannam@132 697
cannam@132 698 BuilderArena* getArena();
cannam@132 699 // Gets the arena in which this object is allocated.
cannam@132 700
cannam@132 701 CapTableBuilder* getCapTable();
cannam@132 702 // Gets the capability context in which this object is operating.
cannam@132 703
cannam@132 704 ListBuilder imbue(CapTableBuilder* capTable);
cannam@132 705 // Return a copy of this builder except using the given capability context.
cannam@132 706
cannam@132 707 private:
cannam@132 708 SegmentBuilder* segment; // Memory segment in which the list resides.
cannam@132 709 CapTableBuilder* capTable; // Table of capability indexes.
cannam@132 710
cannam@132 711 byte* ptr; // Pointer to list content.
cannam@132 712
cannam@132 713 ElementCount elementCount; // Number of elements in the list.
cannam@132 714
cannam@132 715 decltype(BITS / ELEMENTS) step;
cannam@132 716 // The distance between elements.
cannam@132 717
cannam@132 718 BitCount32 structDataSize;
cannam@132 719 WirePointerCount16 structPointerCount;
cannam@132 720 // The struct properties to use when interpreting the elements as structs. All lists can be
cannam@132 721 // interpreted as struct lists, so these are always filled in.
cannam@132 722
cannam@132 723 ElementSize elementSize;
cannam@132 724 // The element size as a ElementSize. This is only really needed to disambiguate INLINE_COMPOSITE
cannam@132 725 // from other types when the overall size is exactly zero or one words.
cannam@132 726
cannam@132 727 inline ListBuilder(SegmentBuilder* segment, CapTableBuilder* capTable, void* ptr,
cannam@132 728 decltype(BITS / ELEMENTS) step, ElementCount size,
cannam@132 729 BitCount structDataSize, WirePointerCount structPointerCount,
cannam@132 730 ElementSize elementSize)
cannam@132 731 : segment(segment), capTable(capTable), ptr(reinterpret_cast<byte*>(ptr)),
cannam@132 732 elementCount(size), step(step), structDataSize(structDataSize),
cannam@132 733 structPointerCount(structPointerCount), elementSize(elementSize) {}
cannam@132 734
cannam@132 735 friend class StructBuilder;
cannam@132 736 friend struct WireHelpers;
cannam@132 737 friend class OrphanBuilder;
cannam@132 738 };
cannam@132 739
cannam@132 740 class ListReader {
cannam@132 741 public:
cannam@132 742 inline explicit ListReader(ElementSize elementSize)
cannam@132 743 : segment(nullptr), capTable(nullptr), ptr(nullptr), elementCount(0),
cannam@132 744 step(0 * BITS / ELEMENTS), structDataSize(0), structPointerCount(0),
cannam@132 745 elementSize(elementSize), nestingLimit(0x7fffffff) {}
cannam@132 746
cannam@132 747 inline ElementCount size() const;
cannam@132 748 // The number of elements in the list.
cannam@132 749
cannam@132 750 inline ElementSize getElementSize() const { return elementSize; }
cannam@132 751
cannam@132 752 Text::Reader asText();
cannam@132 753 Data::Reader asData();
cannam@132 754 // Reinterpret the list as a blob. Throws an exception if the elements are not byte-sized.
cannam@132 755
cannam@132 756 kj::ArrayPtr<const byte> asRawBytes();
cannam@132 757
cannam@132 758 template <typename T>
cannam@132 759 KJ_ALWAYS_INLINE(T getDataElement(ElementCount index) const);
cannam@132 760 // Get the element of the given type at the given index.
cannam@132 761
cannam@132 762 KJ_ALWAYS_INLINE(PointerReader getPointerElement(ElementCount index) const);
cannam@132 763
cannam@132 764 StructReader getStructElement(ElementCount index) const;
cannam@132 765
cannam@132 766 CapTableReader* getCapTable();
cannam@132 767 // Gets the capability context in which this object is operating.
cannam@132 768
cannam@132 769 ListReader imbue(CapTableReader* capTable) const;
cannam@132 770 // Return a copy of this reader except using the given capability context.
cannam@132 771
cannam@132 772 bool isCanonical(const word **readHead);
cannam@132 773 // Validate this pointer's canonicity, subject to the conditions:
cannam@132 774 // * All data to the left of readHead has been read thus far (for pointer
cannam@132 775 // ordering)
cannam@132 776 // * All pointers in preorder have already been checked
cannam@132 777 // * This pointer is in the first and only segment of the message
cannam@132 778
cannam@132 779 private:
cannam@132 780 SegmentReader* segment; // Memory segment in which the list resides.
cannam@132 781 CapTableReader* capTable; // Table of capability indexes.
cannam@132 782
cannam@132 783 const byte* ptr; // Pointer to list content.
cannam@132 784
cannam@132 785 ElementCount elementCount; // Number of elements in the list.
cannam@132 786
cannam@132 787 decltype(BITS / ELEMENTS) step;
cannam@132 788 // The distance between elements.
cannam@132 789
cannam@132 790 BitCount32 structDataSize;
cannam@132 791 WirePointerCount16 structPointerCount;
cannam@132 792 // The struct properties to use when interpreting the elements as structs. All lists can be
cannam@132 793 // interpreted as struct lists, so these are always filled in.
cannam@132 794
cannam@132 795 ElementSize elementSize;
cannam@132 796 // The element size as a ElementSize. This is only really needed to disambiguate INLINE_COMPOSITE
cannam@132 797 // from other types when the overall size is exactly zero or one words.
cannam@132 798
cannam@132 799 int nestingLimit;
cannam@132 800 // Limits the depth of message structures to guard against stack-overflow-based DoS attacks.
cannam@132 801 // Once this reaches zero, further pointers will be pruned.
cannam@132 802
cannam@132 803 inline ListReader(SegmentReader* segment, CapTableReader* capTable, const void* ptr,
cannam@132 804 ElementCount elementCount, decltype(BITS / ELEMENTS) step,
cannam@132 805 BitCount structDataSize, WirePointerCount structPointerCount,
cannam@132 806 ElementSize elementSize, int nestingLimit)
cannam@132 807 : segment(segment), capTable(capTable), ptr(reinterpret_cast<const byte*>(ptr)),
cannam@132 808 elementCount(elementCount), step(step), structDataSize(structDataSize),
cannam@132 809 structPointerCount(structPointerCount), elementSize(elementSize),
cannam@132 810 nestingLimit(nestingLimit) {}
cannam@132 811
cannam@132 812 friend class StructReader;
cannam@132 813 friend class ListBuilder;
cannam@132 814 friend struct WireHelpers;
cannam@132 815 friend class OrphanBuilder;
cannam@132 816 };
cannam@132 817
cannam@132 818 // -------------------------------------------------------------------
cannam@132 819
cannam@132 820 class OrphanBuilder {
cannam@132 821 public:
cannam@132 822 inline OrphanBuilder(): segment(nullptr), capTable(nullptr), location(nullptr) {
cannam@132 823 memset(&tag, 0, sizeof(tag));
cannam@132 824 }
cannam@132 825 OrphanBuilder(const OrphanBuilder& other) = delete;
cannam@132 826 inline OrphanBuilder(OrphanBuilder&& other) noexcept;
cannam@132 827 inline ~OrphanBuilder() noexcept(false);
cannam@132 828
cannam@132 829 static OrphanBuilder initStruct(BuilderArena* arena, CapTableBuilder* capTable, StructSize size);
cannam@132 830 static OrphanBuilder initList(BuilderArena* arena, CapTableBuilder* capTable,
cannam@132 831 ElementCount elementCount, ElementSize elementSize);
cannam@132 832 static OrphanBuilder initStructList(BuilderArena* arena, CapTableBuilder* capTable,
cannam@132 833 ElementCount elementCount, StructSize elementSize);
cannam@132 834 static OrphanBuilder initText(BuilderArena* arena, CapTableBuilder* capTable, ByteCount size);
cannam@132 835 static OrphanBuilder initData(BuilderArena* arena, CapTableBuilder* capTable, ByteCount size);
cannam@132 836
cannam@132 837 static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, StructReader copyFrom);
cannam@132 838 static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, ListReader copyFrom);
cannam@132 839 static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, PointerReader copyFrom);
cannam@132 840 static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, Text::Reader copyFrom);
cannam@132 841 static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, Data::Reader copyFrom);
cannam@132 842 #if !CAPNP_LITE
cannam@132 843 static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable,
cannam@132 844 kj::Own<ClientHook> copyFrom);
cannam@132 845 #endif // !CAPNP_LITE
cannam@132 846
cannam@132 847 static OrphanBuilder concat(BuilderArena* arena, CapTableBuilder* capTable,
cannam@132 848 ElementSize expectedElementSize, StructSize expectedStructSize,
cannam@132 849 kj::ArrayPtr<const ListReader> lists);
cannam@132 850
cannam@132 851 static OrphanBuilder referenceExternalData(BuilderArena* arena, Data::Reader data);
cannam@132 852
cannam@132 853 OrphanBuilder& operator=(const OrphanBuilder& other) = delete;
cannam@132 854 inline OrphanBuilder& operator=(OrphanBuilder&& other);
cannam@132 855
cannam@132 856 inline bool operator==(decltype(nullptr)) const { return location == nullptr; }
cannam@132 857 inline bool operator!=(decltype(nullptr)) const { return location != nullptr; }
cannam@132 858
cannam@132 859 StructBuilder asStruct(StructSize size);
cannam@132 860 // Interpret as a struct, or throw an exception if not a struct.
cannam@132 861
cannam@132 862 ListBuilder asList(ElementSize elementSize);
cannam@132 863 // Interpret as a list, or throw an exception if not a list. elementSize cannot be
cannam@132 864 // INLINE_COMPOSITE -- use asStructList() instead.
cannam@132 865
cannam@132 866 ListBuilder asStructList(StructSize elementSize);
cannam@132 867 // Interpret as a struct list, or throw an exception if not a list.
cannam@132 868
cannam@132 869 Text::Builder asText();
cannam@132 870 Data::Builder asData();
cannam@132 871 // Interpret as a blob, or throw an exception if not a blob.
cannam@132 872
cannam@132 873 StructReader asStructReader(StructSize size) const;
cannam@132 874 ListReader asListReader(ElementSize elementSize) const;
cannam@132 875 #if !CAPNP_LITE
cannam@132 876 kj::Own<ClientHook> asCapability() const;
cannam@132 877 #endif // !CAPNP_LITE
cannam@132 878 Text::Reader asTextReader() const;
cannam@132 879 Data::Reader asDataReader() const;
cannam@132 880
cannam@132 881 bool truncate(ElementCount size, bool isText) KJ_WARN_UNUSED_RESULT;
cannam@132 882 // Resize the orphan list to the given size. Returns false if the list is currently empty but
cannam@132 883 // the requested size is non-zero, in which case the caller will need to allocate a new list.
cannam@132 884
cannam@132 885 void truncate(ElementCount size, ElementSize elementSize);
cannam@132 886 void truncate(ElementCount size, StructSize elementSize);
cannam@132 887 void truncateText(ElementCount size);
cannam@132 888 // Versions of truncate() that know how to allocate a new list if needed.
cannam@132 889
cannam@132 890 private:
cannam@132 891 static_assert(1 * POINTERS * WORDS_PER_POINTER == 1 * WORDS,
cannam@132 892 "This struct assumes a pointer is one word.");
cannam@132 893 word tag;
cannam@132 894 // Contains an encoded WirePointer representing this object. WirePointer is defined in
cannam@132 895 // layout.c++, but fits in a word.
cannam@132 896 //
cannam@132 897 // This may be a FAR pointer. Even in that case, `location` points to the eventual destination
cannam@132 898 // of that far pointer. The reason we keep the far pointer around rather than just making `tag`
cannam@132 899 // represent the final destination is because if the eventual adopter of the pointer is not in
cannam@132 900 // the target's segment then it may be useful to reuse the far pointer landing pad.
cannam@132 901 //
cannam@132 902 // If `tag` is not a far pointer, its offset is garbage; only `location` points to the actual
cannam@132 903 // target.
cannam@132 904
cannam@132 905 SegmentBuilder* segment;
cannam@132 906 // Segment in which the object resides.
cannam@132 907
cannam@132 908 CapTableBuilder* capTable;
cannam@132 909 // Table of capability indexes.
cannam@132 910
cannam@132 911 word* location;
cannam@132 912 // Pointer to the object, or nullptr if the pointer is null. For capabilities, we make this
cannam@132 913 // 0x1 just so that it is non-null for operator==, but it is never used.
cannam@132 914
cannam@132 915 inline OrphanBuilder(const void* tagPtr, SegmentBuilder* segment,
cannam@132 916 CapTableBuilder* capTable, word* location)
cannam@132 917 : segment(segment), capTable(capTable), location(location) {
cannam@132 918 memcpy(&tag, tagPtr, sizeof(tag));
cannam@132 919 }
cannam@132 920
cannam@132 921 inline WirePointer* tagAsPtr() { return reinterpret_cast<WirePointer*>(&tag); }
cannam@132 922 inline const WirePointer* tagAsPtr() const { return reinterpret_cast<const WirePointer*>(&tag); }
cannam@132 923
cannam@132 924 void euthanize();
cannam@132 925 // Erase the target object, zeroing it out and possibly reclaiming the memory. Called when
cannam@132 926 // the OrphanBuilder is being destroyed or overwritten and it is non-null.
cannam@132 927
cannam@132 928 friend struct WireHelpers;
cannam@132 929 };
cannam@132 930
cannam@132 931 // =======================================================================================
cannam@132 932 // Internal implementation details...
cannam@132 933
cannam@132 934 // These are defined in the source file.
cannam@132 935 template <> typename Text::Builder PointerBuilder::initBlob<Text>(ByteCount size);
cannam@132 936 template <> void PointerBuilder::setBlob<Text>(typename Text::Reader value);
cannam@132 937 template <> typename Text::Builder PointerBuilder::getBlob<Text>(const void* defaultValue, ByteCount defaultSize);
cannam@132 938 template <> typename Text::Reader PointerReader::getBlob<Text>(const void* defaultValue, ByteCount defaultSize) const;
cannam@132 939
cannam@132 940 template <> typename Data::Builder PointerBuilder::initBlob<Data>(ByteCount size);
cannam@132 941 template <> void PointerBuilder::setBlob<Data>(typename Data::Reader value);
cannam@132 942 template <> typename Data::Builder PointerBuilder::getBlob<Data>(const void* defaultValue, ByteCount defaultSize);
cannam@132 943 template <> typename Data::Reader PointerReader::getBlob<Data>(const void* defaultValue, ByteCount defaultSize) const;
cannam@132 944
cannam@132 945 inline PointerBuilder PointerBuilder::getRoot(
cannam@132 946 SegmentBuilder* segment, CapTableBuilder* capTable, word* location) {
cannam@132 947 return PointerBuilder(segment, capTable, reinterpret_cast<WirePointer*>(location));
cannam@132 948 }
cannam@132 949
cannam@132 950 inline PointerReader PointerReader::getRootUnchecked(const word* location) {
cannam@132 951 return PointerReader(nullptr, nullptr,
cannam@132 952 reinterpret_cast<const WirePointer*>(location), 0x7fffffff);
cannam@132 953 }
cannam@132 954
cannam@132 955 // -------------------------------------------------------------------
cannam@132 956
cannam@132 957 inline kj::ArrayPtr<byte> StructBuilder::getDataSectionAsBlob() {
cannam@132 958 return kj::ArrayPtr<byte>(reinterpret_cast<byte*>(data), dataSize / BITS_PER_BYTE / BYTES);
cannam@132 959 }
cannam@132 960
cannam@132 961 inline _::ListBuilder StructBuilder::getPointerSectionAsList() {
cannam@132 962 return _::ListBuilder(segment, capTable, pointers, 1 * POINTERS * BITS_PER_POINTER / ELEMENTS,
cannam@132 963 pointerCount * (1 * ELEMENTS / POINTERS),
cannam@132 964 0 * BITS, 1 * POINTERS, ElementSize::POINTER);
cannam@132 965 }
cannam@132 966
cannam@132 967 template <typename T>
cannam@132 968 inline bool StructBuilder::hasDataField(ElementCount offset) {
cannam@132 969 return getDataField<Mask<T>>(offset) != 0;
cannam@132 970 }
cannam@132 971
cannam@132 972 template <>
cannam@132 973 inline bool StructBuilder::hasDataField<Void>(ElementCount offset) {
cannam@132 974 return false;
cannam@132 975 }
cannam@132 976
cannam@132 977 template <typename T>
cannam@132 978 inline T StructBuilder::getDataField(ElementCount offset) {
cannam@132 979 return reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].get();
cannam@132 980 }
cannam@132 981
cannam@132 982 template <>
cannam@132 983 inline bool StructBuilder::getDataField<bool>(ElementCount offset) {
cannam@132 984 BitCount boffset = offset * (1 * BITS / ELEMENTS);
cannam@132 985 byte* b = reinterpret_cast<byte*>(data) + boffset / BITS_PER_BYTE;
cannam@132 986 return (*reinterpret_cast<uint8_t*>(b) & (1 << (boffset % BITS_PER_BYTE / BITS))) != 0;
cannam@132 987 }
cannam@132 988
cannam@132 989 template <>
cannam@132 990 inline Void StructBuilder::getDataField<Void>(ElementCount offset) {
cannam@132 991 return VOID;
cannam@132 992 }
cannam@132 993
cannam@132 994 template <typename T>
cannam@132 995 inline T StructBuilder::getDataField(ElementCount offset, Mask<T> mask) {
cannam@132 996 return unmask<T>(getDataField<Mask<T> >(offset), mask);
cannam@132 997 }
cannam@132 998
cannam@132 999 template <typename T>
cannam@132 1000 inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer<T> value) {
cannam@132 1001 reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].set(value);
cannam@132 1002 }
cannam@132 1003
cannam@132 1004 #if CAPNP_CANONICALIZE_NAN
cannam@132 1005 // Use mask() on floats and doubles to make sure we canonicalize NaNs.
cannam@132 1006 template <>
cannam@132 1007 inline void StructBuilder::setDataField<float>(ElementCount offset, float value) {
cannam@132 1008 setDataField<uint32_t>(offset, mask<float>(value, 0));
cannam@132 1009 }
cannam@132 1010 template <>
cannam@132 1011 inline void StructBuilder::setDataField<double>(ElementCount offset, double value) {
cannam@132 1012 setDataField<uint64_t>(offset, mask<double>(value, 0));
cannam@132 1013 }
cannam@132 1014 #endif
cannam@132 1015
cannam@132 1016 template <>
cannam@132 1017 inline void StructBuilder::setDataField<bool>(ElementCount offset, bool value) {
cannam@132 1018 BitCount boffset = offset * (1 * BITS / ELEMENTS);
cannam@132 1019 byte* b = reinterpret_cast<byte*>(data) + boffset / BITS_PER_BYTE;
cannam@132 1020 uint bitnum = boffset % BITS_PER_BYTE / BITS;
cannam@132 1021 *reinterpret_cast<uint8_t*>(b) = (*reinterpret_cast<uint8_t*>(b) & ~(1 << bitnum))
cannam@132 1022 | (static_cast<uint8_t>(value) << bitnum);
cannam@132 1023 }
cannam@132 1024
cannam@132 1025 template <>
cannam@132 1026 inline void StructBuilder::setDataField<Void>(ElementCount offset, Void value) {}
cannam@132 1027
cannam@132 1028 template <typename T>
cannam@132 1029 inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer<T> value, Mask<T> m) {
cannam@132 1030 setDataField<Mask<T> >(offset, mask<T>(value, m));
cannam@132 1031 }
cannam@132 1032
cannam@132 1033 inline PointerBuilder StructBuilder::getPointerField(WirePointerCount ptrIndex) {
cannam@132 1034 // Hacky because WirePointer is defined in the .c++ file (so is incomplete here).
cannam@132 1035 return PointerBuilder(segment, capTable, reinterpret_cast<WirePointer*>(
cannam@132 1036 reinterpret_cast<word*>(pointers) + ptrIndex * WORDS_PER_POINTER));
cannam@132 1037 }
cannam@132 1038
cannam@132 1039 // -------------------------------------------------------------------
cannam@132 1040
cannam@132 1041 inline kj::ArrayPtr<const byte> StructReader::getDataSectionAsBlob() {
cannam@132 1042 return kj::ArrayPtr<const byte>(reinterpret_cast<const byte*>(data), dataSize / BITS_PER_BYTE / BYTES);
cannam@132 1043 }
cannam@132 1044
cannam@132 1045 inline _::ListReader StructReader::getPointerSectionAsList() {
cannam@132 1046 return _::ListReader(segment, capTable, pointers, pointerCount * (1 * ELEMENTS / POINTERS),
cannam@132 1047 1 * POINTERS * BITS_PER_POINTER / ELEMENTS, 0 * BITS, 1 * POINTERS,
cannam@132 1048 ElementSize::POINTER, nestingLimit);
cannam@132 1049 }
cannam@132 1050
cannam@132 1051 template <typename T>
cannam@132 1052 inline bool StructReader::hasDataField(ElementCount offset) const {
cannam@132 1053 return getDataField<Mask<T>>(offset) != 0;
cannam@132 1054 }
cannam@132 1055
cannam@132 1056 template <>
cannam@132 1057 inline bool StructReader::hasDataField<Void>(ElementCount offset) const {
cannam@132 1058 return false;
cannam@132 1059 }
cannam@132 1060
cannam@132 1061 template <typename T>
cannam@132 1062 inline T StructReader::getDataField(ElementCount offset) const {
cannam@132 1063 if ((offset + 1 * ELEMENTS) * capnp::bitsPerElement<T>() <= dataSize) {
cannam@132 1064 return reinterpret_cast<const WireValue<T>*>(data)[offset / ELEMENTS].get();
cannam@132 1065 } else {
cannam@132 1066 return static_cast<T>(0);
cannam@132 1067 }
cannam@132 1068 }
cannam@132 1069
cannam@132 1070 template <>
cannam@132 1071 inline bool StructReader::getDataField<bool>(ElementCount offset) const {
cannam@132 1072 BitCount boffset = offset * (1 * BITS / ELEMENTS);
cannam@132 1073 if (boffset < dataSize) {
cannam@132 1074 const byte* b = reinterpret_cast<const byte*>(data) + boffset / BITS_PER_BYTE;
cannam@132 1075 return (*reinterpret_cast<const uint8_t*>(b) & (1 << (boffset % BITS_PER_BYTE / BITS))) != 0;
cannam@132 1076 } else {
cannam@132 1077 return false;
cannam@132 1078 }
cannam@132 1079 }
cannam@132 1080
cannam@132 1081 template <>
cannam@132 1082 inline Void StructReader::getDataField<Void>(ElementCount offset) const {
cannam@132 1083 return VOID;
cannam@132 1084 }
cannam@132 1085
cannam@132 1086 template <typename T>
cannam@132 1087 T StructReader::getDataField(ElementCount offset, Mask<T> mask) const {
cannam@132 1088 return unmask<T>(getDataField<Mask<T> >(offset), mask);
cannam@132 1089 }
cannam@132 1090
cannam@132 1091 inline PointerReader StructReader::getPointerField(WirePointerCount ptrIndex) const {
cannam@132 1092 if (ptrIndex < pointerCount) {
cannam@132 1093 // Hacky because WirePointer is defined in the .c++ file (so is incomplete here).
cannam@132 1094 return PointerReader(segment, capTable, reinterpret_cast<const WirePointer*>(
cannam@132 1095 reinterpret_cast<const word*>(pointers) + ptrIndex * WORDS_PER_POINTER), nestingLimit);
cannam@132 1096 } else{
cannam@132 1097 return PointerReader();
cannam@132 1098 }
cannam@132 1099 }
cannam@132 1100
cannam@132 1101 // -------------------------------------------------------------------
cannam@132 1102
cannam@132 1103 inline ElementCount ListBuilder::size() const { return elementCount; }
cannam@132 1104
cannam@132 1105 template <typename T>
cannam@132 1106 inline T ListBuilder::getDataElement(ElementCount index) {
cannam@132 1107 return reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->get();
cannam@132 1108
cannam@132 1109 // TODO(perf): Benchmark this alternate implementation, which I suspect may make better use of
cannam@132 1110 // the x86 SIB byte. Also use it for all the other getData/setData implementations below, and
cannam@132 1111 // the various non-inline methods that look up pointers.
cannam@132 1112 // Also if using this, consider changing ptr back to void* instead of byte*.
cannam@132 1113 // return reinterpret_cast<WireValue<T>*>(ptr)[
cannam@132 1114 // index / ELEMENTS * (step / capnp::bitsPerElement<T>())].get();
cannam@132 1115 }
cannam@132 1116
cannam@132 1117 template <>
cannam@132 1118 inline bool ListBuilder::getDataElement<bool>(ElementCount index) {
cannam@132 1119 // Ignore step for bit lists because bit lists cannot be upgraded to struct lists.
cannam@132 1120 BitCount bindex = index * (1 * BITS / ELEMENTS);
cannam@132 1121 byte* b = ptr + bindex / BITS_PER_BYTE;
cannam@132 1122 return (*reinterpret_cast<uint8_t*>(b) & (1 << (bindex % BITS_PER_BYTE / BITS))) != 0;
cannam@132 1123 }
cannam@132 1124
cannam@132 1125 template <>
cannam@132 1126 inline Void ListBuilder::getDataElement<Void>(ElementCount index) {
cannam@132 1127 return VOID;
cannam@132 1128 }
cannam@132 1129
cannam@132 1130 template <typename T>
cannam@132 1131 inline void ListBuilder::setDataElement(ElementCount index, kj::NoInfer<T> value) {
cannam@132 1132 reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->set(value);
cannam@132 1133 }
cannam@132 1134
cannam@132 1135 #if CAPNP_CANONICALIZE_NAN
cannam@132 1136 // Use mask() on floats and doubles to make sure we canonicalize NaNs.
cannam@132 1137 template <>
cannam@132 1138 inline void ListBuilder::setDataElement<float>(ElementCount index, float value) {
cannam@132 1139 setDataElement<uint32_t>(index, mask<float>(value, 0));
cannam@132 1140 }
cannam@132 1141 template <>
cannam@132 1142 inline void ListBuilder::setDataElement<double>(ElementCount index, double value) {
cannam@132 1143 setDataElement<uint64_t>(index, mask<double>(value, 0));
cannam@132 1144 }
cannam@132 1145 #endif
cannam@132 1146
cannam@132 1147 template <>
cannam@132 1148 inline void ListBuilder::setDataElement<bool>(ElementCount index, bool value) {
cannam@132 1149 // Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists.
cannam@132 1150 BitCount bindex = index * (1 * BITS / ELEMENTS);
cannam@132 1151 byte* b = ptr + bindex / BITS_PER_BYTE;
cannam@132 1152 uint bitnum = bindex % BITS_PER_BYTE / BITS;
cannam@132 1153 *reinterpret_cast<uint8_t*>(b) = (*reinterpret_cast<uint8_t*>(b) & ~(1 << bitnum))
cannam@132 1154 | (static_cast<uint8_t>(value) << bitnum);
cannam@132 1155 }
cannam@132 1156
cannam@132 1157 template <>
cannam@132 1158 inline void ListBuilder::setDataElement<Void>(ElementCount index, Void value) {}
cannam@132 1159
cannam@132 1160 inline PointerBuilder ListBuilder::getPointerElement(ElementCount index) {
cannam@132 1161 return PointerBuilder(segment, capTable,
cannam@132 1162 reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE));
cannam@132 1163 }
cannam@132 1164
cannam@132 1165 // -------------------------------------------------------------------
cannam@132 1166
cannam@132 1167 inline ElementCount ListReader::size() const { return elementCount; }
cannam@132 1168
cannam@132 1169 template <typename T>
cannam@132 1170 inline T ListReader::getDataElement(ElementCount index) const {
cannam@132 1171 return reinterpret_cast<const WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->get();
cannam@132 1172 }
cannam@132 1173
cannam@132 1174 template <>
cannam@132 1175 inline bool ListReader::getDataElement<bool>(ElementCount index) const {
cannam@132 1176 // Ignore step for bit lists because bit lists cannot be upgraded to struct lists.
cannam@132 1177 BitCount bindex = index * (1 * BITS / ELEMENTS);
cannam@132 1178 const byte* b = ptr + bindex / BITS_PER_BYTE;
cannam@132 1179 return (*reinterpret_cast<const uint8_t*>(b) & (1 << (bindex % BITS_PER_BYTE / BITS))) != 0;
cannam@132 1180 }
cannam@132 1181
cannam@132 1182 template <>
cannam@132 1183 inline Void ListReader::getDataElement<Void>(ElementCount index) const {
cannam@132 1184 return VOID;
cannam@132 1185 }
cannam@132 1186
cannam@132 1187 inline PointerReader ListReader::getPointerElement(ElementCount index) const {
cannam@132 1188 return PointerReader(segment, capTable,
cannam@132 1189 reinterpret_cast<const WirePointer*>(ptr + index * step / BITS_PER_BYTE), nestingLimit);
cannam@132 1190 }
cannam@132 1191
cannam@132 1192 // -------------------------------------------------------------------
cannam@132 1193
cannam@132 1194 inline OrphanBuilder::OrphanBuilder(OrphanBuilder&& other) noexcept
cannam@132 1195 : segment(other.segment), capTable(other.capTable), location(other.location) {
cannam@132 1196 memcpy(&tag, &other.tag, sizeof(tag)); // Needs memcpy to comply with aliasing rules.
cannam@132 1197 other.segment = nullptr;
cannam@132 1198 other.location = nullptr;
cannam@132 1199 }
cannam@132 1200
cannam@132 1201 inline OrphanBuilder::~OrphanBuilder() noexcept(false) {
cannam@132 1202 if (segment != nullptr) euthanize();
cannam@132 1203 }
cannam@132 1204
cannam@132 1205 inline OrphanBuilder& OrphanBuilder::operator=(OrphanBuilder&& other) {
cannam@132 1206 // With normal smart pointers, it's important to handle the case where the incoming pointer
cannam@132 1207 // is actually transitively owned by this one. In this case, euthanize() would destroy `other`
cannam@132 1208 // before we copied it. This isn't possible in the case of `OrphanBuilder` because it only
cannam@132 1209 // owns message objects, and `other` is not itself a message object, therefore cannot possibly
cannam@132 1210 // be transitively owned by `this`.
cannam@132 1211
cannam@132 1212 if (segment != nullptr) euthanize();
cannam@132 1213 segment = other.segment;
cannam@132 1214 capTable = other.capTable;
cannam@132 1215 location = other.location;
cannam@132 1216 memcpy(&tag, &other.tag, sizeof(tag)); // Needs memcpy to comply with aliasing rules.
cannam@132 1217 other.segment = nullptr;
cannam@132 1218 other.location = nullptr;
cannam@132 1219 return *this;
cannam@132 1220 }
cannam@132 1221
cannam@132 1222 } // namespace _ (private)
cannam@132 1223 } // namespace capnp
cannam@132 1224
cannam@132 1225 #endif // CAPNP_LAYOUT_H_