annotate osx/include/capnp/layout.h @ 139:413e081fcc6f

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