annotate osx/include/capnp/layout.h @ 49:3ab5a40c4e3b

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