layout.h 42.1 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
21

Kenton Varda's avatar
Kenton Varda committed
22 23
// This file is NOT intended for use by clients, except in generated code.
//
Kenton Varda's avatar
Kenton Varda committed
24 25 26 27
// This file defines low-level, non-type-safe classes for traversing the Cap'n Proto memory layout
// (which is also its wire format).  Code generated by the Cap'n Proto compiler uses these classes,
// as does other parts of the Cap'n proto library which provide a higher-level interface for
// dynamic introspection.
Kenton Varda's avatar
Kenton Varda committed
28

Kenton Varda's avatar
Kenton Varda committed
29 30
#ifndef CAPNP_LAYOUT_H_
#define CAPNP_LAYOUT_H_
31

32 33 34 35
#if defined(__GNUC__) && !CAPNP_HEADER_WARNINGS
#pragma GCC system_header
#endif

Kenton Varda's avatar
Kenton Varda committed
36
#include <kj/common.h>
37
#include <kj/memory.h>
38
#include "common.h"
Kenton Varda's avatar
Kenton Varda committed
39
#include "blob.h"
40
#include "endian.h"
41

42
#if (defined(__mips__) || defined(__hppa__)) && !defined(CAPNP_CANONICALIZE_NAN)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#define CAPNP_CANONICALIZE_NAN 1
// Explicitly detect NaNs and canonicalize them to the quiet NaN value as would be returned by
// __builtin_nan("") on systems implementing the IEEE-754 recommended (but not required) NaN
// signalling/quiet differentiation (such as x86).  Unfortunately, some architectures -- in
// particular, MIPS -- represent quiet vs. signalling nans differently than the rest of the world.
// Canonicalizing them makes output consistent (which is important!), but hurts performance
// slightly.
//
// Note that trying to convert MIPS NaNs to standard NaNs without losing data doesn't work.
// Signaling vs. quiet is indicated by a bit, with the meaning being the opposite on MIPS vs.
// everyone else.  It would be great if we could just flip that bit, but we can't, because if the
// significand is all-zero, then the value is infinity rather than NaN.  This means that on most
// machines, where the bit indicates quietness, there is one more quiet NaN value than signalling
// NaN value, whereas on MIPS there is one more sNaN than qNaN, and thus there is no isomorphic
// mapping that properly preserves quietness.  Instead of doing something hacky, we just give up
// and blow away NaN payloads, because no one uses them anyway.
#endif

61
namespace capnp {
62

63
#if !CAPNP_LITE
64
class ClientHook;
65
#endif  // !CAPNP_LITE
66

67
namespace _ {  // private
Kenton Varda's avatar
Kenton Varda committed
68

69 70
class PointerBuilder;
class PointerReader;
71 72 73 74
class StructBuilder;
class StructReader;
class ListBuilder;
class ListReader;
75
class OrphanBuilder;
76
struct WirePointer;
77
struct WireHelpers;
78 79
class SegmentReader;
class SegmentBuilder;
80
class Arena;
81
class BuilderArena;
82

83
// =============================================================================
84 85

typedef decltype(BITS / ELEMENTS) BitsPerElement;
86
typedef decltype(POINTERS / ELEMENTS) PointersPerElement;
87

Kenton Varda's avatar
Kenton Varda committed
88 89 90 91 92 93 94 95 96 97
static constexpr BitsPerElement BITS_PER_ELEMENT_TABLE[8] = {
    0 * BITS / ELEMENTS,
    1 * BITS / ELEMENTS,
    8 * BITS / ELEMENTS,
    16 * BITS / ELEMENTS,
    32 * BITS / ELEMENTS,
    64 * BITS / ELEMENTS,
    0 * BITS / ELEMENTS,
    0 * BITS / ELEMENTS
};
98

99
inline KJ_CONSTEXPR() BitsPerElement dataBitsPerElement(ElementSize size) {
100
  return _::BITS_PER_ELEMENT_TABLE[static_cast<int>(size)];
101 102
}

103 104
inline constexpr PointersPerElement pointersPerElement(ElementSize size) {
  return size == ElementSize::POINTER ? 1 * POINTERS / ELEMENTS : 0 * POINTERS / ELEMENTS;
105 106
}

107
template <size_t size> struct ElementSizeForByteSize;
108 109 110 111
template <> struct ElementSizeForByteSize<1> { static constexpr ElementSize value = ElementSize::BYTE; };
template <> struct ElementSizeForByteSize<2> { static constexpr ElementSize value = ElementSize::TWO_BYTES; };
template <> struct ElementSizeForByteSize<4> { static constexpr ElementSize value = ElementSize::FOUR_BYTES; };
template <> struct ElementSizeForByteSize<8> { static constexpr ElementSize value = ElementSize::EIGHT_BYTES; };
112 113

template <typename T> struct ElementSizeForType {
114
  static constexpr ElementSize value =
115
      // Primitive types that aren't special-cased below can be determined from sizeof().
116
      CAPNP_KIND(T) == Kind::PRIMITIVE ? ElementSizeForByteSize<sizeof(T)>::value :
117 118
      CAPNP_KIND(T) == Kind::ENUM ? ElementSize::TWO_BYTES :
      CAPNP_KIND(T) == Kind::STRUCT ? ElementSize::INLINE_COMPOSITE :
119 120

      // Everything else is a pointer.
121
      ElementSize::POINTER;
122 123
};

124
// Void and bool are special.
125 126
template <> struct ElementSizeForType<Void> { static constexpr ElementSize value = ElementSize::VOID; };
template <> struct ElementSizeForType<bool> { static constexpr ElementSize value = ElementSize::BIT; };
127

128 129
// Lists and blobs are pointers, not structs.
template <typename T, bool b> struct ElementSizeForType<List<T, b>> {
130
  static constexpr ElementSize value = ElementSize::POINTER;
131 132
};
template <> struct ElementSizeForType<Text> {
133
  static constexpr ElementSize value = ElementSize::POINTER;
134 135
};
template <> struct ElementSizeForType<Data> {
136
  static constexpr ElementSize value = ElementSize::POINTER;
137
};
138

139
template <typename T>
140
inline constexpr ElementSize elementSizeForType() {
141 142 143
  return ElementSizeForType<T>::value;
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157
struct MessageSizeCounts {
  WordCount64 wordCount;
  uint capCount;

  MessageSizeCounts& operator+=(const MessageSizeCounts& other) {
    wordCount += other.wordCount;
    capCount += other.capCount;
    return *this;
  }

  MessageSize asPublic() {
    return MessageSize { wordCount / WORDS, capCount };
  }
};
158 159 160

// =============================================================================

161 162 163 164 165 166 167 168 169
template <int wordCount>
union AlignedData {
  // Useful for declaring static constant data blobs as an array of bytes, but forcing those
  // bytes to be word-aligned.

  uint8_t bytes[wordCount * sizeof(word)];
  word words[wordCount];
};

170 171
struct StructSize {
  WordCount16 data;
172
  WirePointerCount16 pointers;
173

174
  inline constexpr WordCount total() const { return data + pointers * WORDS_PER_POINTER; }
175 176

  StructSize() = default;
177 178
  inline constexpr StructSize(WordCount data, WirePointerCount pointers)
      : data(data), pointers(pointers) {}
179 180
};

181
template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
182
inline constexpr StructSize structSize() {
183
  return StructSize(CapnpPrivate::dataWordSize * WORDS, CapnpPrivate::pointerCount * POINTERS);
184
}
185

186 187
// -------------------------------------------------------------------
// Masking of default values
188

189
template <typename T, Kind kind = CAPNP_KIND(T)> struct Mask_;
190 191 192 193
template <typename T> struct Mask_<T, Kind::PRIMITIVE> { typedef T Type; };
template <typename T> struct Mask_<T, Kind::ENUM> { typedef uint16_t Type; };
template <> struct Mask_<float, Kind::PRIMITIVE> { typedef uint32_t Type; };
template <> struct Mask_<double, Kind::PRIMITIVE> { typedef uint64_t Type; };
194

195
template <typename T> struct Mask_<T, Kind::OTHER> {
196 197 198
  // Union discriminants end up here.
  static_assert(sizeof(T) == 2, "Don't know how to mask this type.");
  typedef uint16_t Type;
199 200
};

201
template <typename T>
202
using Mask = typename Mask_<T>::Type;
203 204

template <typename T>
205
KJ_ALWAYS_INLINE(Mask<T> mask(T value, Mask<T> mask));
206
template <typename T>
207
KJ_ALWAYS_INLINE(T unmask(Mask<T> value, Mask<T> mask));
208 209

template <typename T>
210 211
inline Mask<T> mask(T value, Mask<T> mask) {
  return static_cast<Mask<T> >(value) ^ mask;
212 213 214 215
}

template <>
inline uint32_t mask<float>(float value, uint32_t mask) {
216 217
#if CAPNP_CANONICALIZE_NAN
  if (value != value) {
218
    return 0x7fc00000u ^ mask;
219 220 221
  }
#endif

222 223 224 225 226 227 228 229
  uint32_t i;
  static_assert(sizeof(i) == sizeof(value), "float is not 32 bits?");
  memcpy(&i, &value, sizeof(value));
  return i ^ mask;
}

template <>
inline uint64_t mask<double>(double value, uint64_t mask) {
230 231
#if CAPNP_CANONICALIZE_NAN
  if (value != value) {
232
    return 0x7ff8000000000000ull ^ mask;
233 234 235
  }
#endif

236 237 238 239 240 241 242
  uint64_t i;
  static_assert(sizeof(i) == sizeof(value), "double is not 64 bits?");
  memcpy(&i, &value, sizeof(value));
  return i ^ mask;
}

template <typename T>
243
inline T unmask(Mask<T> value, Mask<T> mask) {
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  return static_cast<T>(value ^ mask);
}

template <>
inline float unmask<float>(uint32_t value, uint32_t mask) {
  value ^= mask;
  float result;
  static_assert(sizeof(result) == sizeof(value), "float is not 32 bits?");
  memcpy(&result, &value, sizeof(value));
  return result;
}

template <>
inline double unmask<double>(uint64_t value, uint64_t mask) {
  value ^= mask;
  double result;
  static_assert(sizeof(result) == sizeof(value), "double is not 64 bits?");
  memcpy(&result, &value, sizeof(value));
  return result;
}

265 266
// -------------------------------------------------------------------

267 268 269 270 271 272
class PointerBuilder: public kj::DisallowConstCopy {
  // Represents a single pointer, usually embedded in a struct or a list.

public:
  inline PointerBuilder(): segment(nullptr), pointer(nullptr) {}

Kenton Varda's avatar
Kenton Varda committed
273 274 275 276
  static inline PointerBuilder getRoot(SegmentBuilder* segment, word* location);
  // Get a PointerBuilder representing a message root located in the given segment at the given
  // location.

277 278
  inline bool isNull() { return getPointerType() == PointerType::NULL_; }
  PointerType getPointerType();
279 280

  StructBuilder getStruct(StructSize size, const word* defaultValue);
281
  ListBuilder getList(ElementSize elementSize, const word* defaultValue);
282
  ListBuilder getStructList(StructSize elementSize, const word* defaultValue);
283
  ListBuilder getListAnySize(const word* defaultValue);
284
  template <typename T> typename T::Builder getBlob(const void* defaultValue,ByteCount defaultSize);
285
#if !CAPNP_LITE
286
  kj::Own<ClientHook> getCapability();
287
#endif  // !CAPNP_LITE
288 289 290 291 292
  // Get methods:  Get the value.  If it is null, initialize it to a copy of the default value.
  // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a
  // simple byte array for blobs.

  StructBuilder initStruct(StructSize size);
293
  ListBuilder initList(ElementSize elementSize, ElementCount elementCount);
294 295 296 297 298 299 300 301
  ListBuilder initStructList(ElementCount elementCount, StructSize size);
  template <typename T> typename T::Builder initBlob(ByteCount size);
  // Init methods:  Initialize the pointer to a newly-allocated object, discarding the existing
  // object.

  void setStruct(const StructReader& value);
  void setList(const ListReader& value);
  template <typename T> void setBlob(typename T::Reader value);
302
#if !CAPNP_LITE
303
  void setCapability(kj::Own<ClientHook>&& cap);
304
#endif  // !CAPNP_LITE
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  // Set methods:  Initialize the pointer to a newly-allocated copy of the given value, discarding
  // the existing object.

  void adopt(OrphanBuilder&& orphan);
  // Set the pointer to point at the given orphaned value.

  OrphanBuilder disown();
  // Set the pointer to null and return its previous value as an orphan.

  void clear();
  // Clear the pointer to null, discarding its previous value.

  void transferFrom(PointerBuilder other);
  // Equivalent to `adopt(other.disown())`.

  void copyFrom(PointerReader other);
  // Equivalent to `set(other.get())`.

323 324
  PointerReader asReader() const;

325
  BuilderArena* getArena() const;
326 327
  // Get the arena containing this pointer.

328 329 330 331 332 333 334 335 336
private:
  SegmentBuilder* segment;     // Memory segment in which the pointer resides.
  WirePointer* pointer;        // Pointer to the pointer.

  inline PointerBuilder(SegmentBuilder* segment, WirePointer* pointer)
      : segment(segment), pointer(pointer) {}

  friend class StructBuilder;
  friend class ListBuilder;
337
  friend class OrphanBuilder;
338 339 340 341 342 343
};

class PointerReader {
public:
  inline PointerReader(): segment(nullptr), pointer(nullptr), nestingLimit(0x7fffffff) {}

Kenton Varda's avatar
Kenton Varda committed
344 345 346 347 348 349 350
  static PointerReader getRoot(SegmentReader* segment, const word* location, int nestingLimit);
  // Get a PointerReader representing a message root located in the given segment at the given
  // location.

  static inline PointerReader getRootUnchecked(const word* location);
  // Get a PointerReader for an unchecked message.

351
  MessageSizeCounts targetSize() const;
Kenton Varda's avatar
Kenton Varda committed
352 353 354 355 356 357
  // Return the total size of the target object and everything to which it points.  Does not count
  // far pointer overhead.  This is useful for deciding how much space is needed to copy the object
  // into a flat array.  However, the caller is advised NOT to treat this value as secure.  Instead,
  // use the result as a hint for allocating the first segment, do the copy, and then throw an
  // exception if it overruns.

358 359
  inline bool isNull() const { return getPointerType() == PointerType::NULL_; }
  PointerType getPointerType() const;
360 361

  StructReader getStruct(const word* defaultValue) const;
362
  ListReader getList(ElementSize expectedElementSize, const word* defaultValue) const;
363
  ListReader getListAnySize(const word* defaultValue) const;
364 365
  template <typename T>
  typename T::Reader getBlob(const void* defaultValue, ByteCount defaultSize) const;
366
#if !CAPNP_LITE
367
  kj::Own<ClientHook> getCapability() const;
368
#endif  // !CAPNP_LITE
369 370 371 372 373 374 375 376 377
  // Get methods:  Get the value.  If it is null, return the default value instead.
  // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a
  // simple byte array for blobs.

  const word* getUnchecked() const;
  // If this is an unchecked message, get a word* pointing at the location of the pointer.  This
  // word* can actually be passed to readUnchecked() to read the designated sub-object later.  If
  // this isn't an unchecked message, throws an exception.

378 379 380
  kj::Maybe<Arena&> getArena() const;
  // Get the arena containing this pointer.

381 382 383 384 385 386 387 388 389 390 391 392 393
private:
  SegmentReader* segment;      // Memory segment in which the pointer resides.
  const WirePointer* pointer;  // Pointer to the pointer.  null = treat as null pointer.

  int nestingLimit;
  // Limits the depth of message structures to guard against stack-overflow-based DoS attacks.
  // Once this reaches zero, further pointers will be pruned.

  inline PointerReader(SegmentReader* segment, const WirePointer* pointer, int nestingLimit)
      : segment(segment), pointer(pointer), nestingLimit(nestingLimit) {}

  friend class StructReader;
  friend class ListReader;
394
  friend class PointerBuilder;
395
  friend class OrphanBuilder;
396 397
};

398
// -------------------------------------------------------------------
Kenton Varda's avatar
Kenton Varda committed
399

400
class StructBuilder: public kj::DisallowConstCopy {
401
public:
402
  inline StructBuilder(): segment(nullptr), data(nullptr), pointers(nullptr) {}
403

404 405 406 407
  inline word* getLocation() { return reinterpret_cast<word*>(data); }
  // Get the object's location.  Only valid for independently-allocated objects (i.e. not list
  // elements).

408
  inline BitCount getDataSectionSize() const { return dataSize; }
409
  inline WirePointerCount getPointerSectionSize() const { return pointerCount; }
410
  inline kj::ArrayPtr<byte> getDataSectionAsBlob();
411
  inline _::ListBuilder getPointerSectionAsList();
412

413 414 415 416
  template <typename T>
  KJ_ALWAYS_INLINE(bool hasDataField(ElementCount offset));
  // Return true if the field is set to something other than its default value.

417
  template <typename T>
418
  KJ_ALWAYS_INLINE(T getDataField(ElementCount offset));
419
  // Gets the data field value of the given type at the given offset.  The offset is measured in
420 421
  // multiples of the field size, determined by the type.

422
  template <typename T>
423
  KJ_ALWAYS_INLINE(T getDataField(ElementCount offset, Mask<T> mask));
424 425 426
  // Like getDataField() but applies the given XOR mask to the data on load.  Used for reading
  // fields with non-zero default values.

427
  template <typename T>
428
  KJ_ALWAYS_INLINE(void setDataField(
429
      ElementCount offset, kj::NoInfer<T> value));
430 431
  // Sets the data field value at the given offset.

432
  template <typename T>
433
  KJ_ALWAYS_INLINE(void setDataField(
434
      ElementCount offset, kj::NoInfer<T> value, Mask<T> mask));
435 436 437
  // Like setDataField() but applies the given XOR mask before storing.  Used for writing fields
  // with non-zero default values.

438 439
  KJ_ALWAYS_INLINE(PointerBuilder getPointerField(WirePointerCount ptrIndex));
  // Get a builder for a pointer field given the index within the pointer section.
440

441 442 443
  void clearAll();
  // Clear all pointers and data.

444 445 446 447 448
  void transferContentFrom(StructBuilder other);
  // Adopt all pointers from `other`, and also copy all data.  If `other`'s sections are larger
  // than this, the extra data is not transferred, meaning there is a risk of data loss when
  // transferring from messages built with future versions of the protocol.

449 450 451 452 453
  void copyContentFrom(StructReader other);
  // Copy content from `other`.  If `other`'s sections are larger than this, the extra data is not
  // copied, meaning there is a risk of data loss when copying from messages built with future
  // versions of the protocol.

454
  StructReader asReader() const;
455
  // Gets a StructReader pointing at the same memory.
456

457 458 459
  BuilderArena* getArena();
  // Gets the arena in which this object is allocated.

460
private:
461
  SegmentBuilder* segment;     // Memory segment in which the struct resides.
Kenton Varda's avatar
Kenton Varda committed
462
  void* data;                  // Pointer to the encoded data.
463
  WirePointer* pointers;   // Pointer to the encoded pointers.
464

465
  BitCount32 dataSize;
466
  // Size of data section.  We use a bit count rather than a word count to more easily handle the
467 468
  // case of struct lists encoded with less than a word per element.

469
  WirePointerCount16 pointerCount;  // Size of the pointer section.
470

471
  inline StructBuilder(SegmentBuilder* segment, void* data, WirePointer* pointers,
472
                       BitCount dataSize, WirePointerCount pointerCount)
473
      : segment(segment), data(data), pointers(pointers),
474
        dataSize(dataSize), pointerCount(pointerCount) {}
475

476
  friend class ListBuilder;
477
  friend struct WireHelpers;
478
  friend class OrphanBuilder;
479 480
};

481
class StructReader {
482
public:
483
  inline StructReader()
484
      : segment(nullptr), data(nullptr), pointers(nullptr), dataSize(0),
485
        pointerCount(0), nestingLimit(0x7fffffff) {}
486

Kenton Varda's avatar
Kenton Varda committed
487 488
  const void* getLocation() const { return data; }

489
  inline BitCount getDataSectionSize() const { return dataSize; }
490
  inline WirePointerCount getPointerSectionSize() const { return pointerCount; }
491
  inline kj::ArrayPtr<const byte> getDataSectionAsBlob();
492
  inline _::ListReader getPointerSectionAsList();
493

494 495 496 497
  template <typename T>
  KJ_ALWAYS_INLINE(bool hasDataField(ElementCount offset) const);
  // Return true if the field is set to something other than its default value.

498
  template <typename T>
499
  KJ_ALWAYS_INLINE(T getDataField(ElementCount offset) const);
500
  // Get the data field value of the given type at the given offset.  The offset is measured in
501
  // multiples of the field size, determined by the type.  Returns zero if the offset is past the
502
  // end of the struct's data section.
503 504

  template <typename T>
505
  KJ_ALWAYS_INLINE(
506
      T getDataField(ElementCount offset, Mask<T> mask) const);
507 508
  // Like getDataField(offset), but applies the given XOR mask to the result.  Used for reading
  // fields with non-zero default values.
509

510 511 512
  KJ_ALWAYS_INLINE(PointerReader getPointerField(WirePointerCount ptrIndex) const);
  // Get a reader for a pointer field given the index within the pointer section.  If the index
  // is out-of-bounds, returns a null pointer.
513

514
  MessageSizeCounts totalSize() const;
515 516 517 518 519 520
  // Return the total size of the struct and everything to which it points.  Does not count far
  // pointer overhead.  This is useful for deciding how much space is needed to copy the struct
  // into a flat array.  However, the caller is advised NOT to treat this value as secure.  Instead,
  // use the result as a hint for allocating the first segment, do the copy, and then throw an
  // exception if it overruns.

521
private:
Kenton Varda's avatar
Kenton Varda committed
522
  SegmentReader* segment;  // Memory segment in which the struct resides.
523

524
  const void* data;
525
  const WirePointer* pointers;
526

527
  BitCount32 dataSize;
528
  // Size of data section.  We use a bit count rather than a word count to more easily handle the
529 530
  // case of struct lists encoded with less than a word per element.

531
  WirePointerCount16 pointerCount;  // Size of the pointer section.
532

533
  int nestingLimit;
534 535
  // Limits the depth of message structures to guard against stack-overflow-based DoS attacks.
  // Once this reaches zero, further pointers will be pruned.
536
  // TODO(perf):  Limit to 16 bits for better packing?
537

538
  inline StructReader(SegmentReader* segment, const void* data, const WirePointer* pointers,
539
                      BitCount dataSize, WirePointerCount pointerCount, int nestingLimit)
540
      : segment(segment), data(data), pointers(pointers),
541
        dataSize(dataSize), pointerCount(pointerCount),
542
        nestingLimit(nestingLimit) {}
543

544 545
  friend class ListReader;
  friend class StructBuilder;
546 547 548 549 550
  friend struct WireHelpers;
};

// -------------------------------------------------------------------

551 552 553 554 555 556 557 558 559 560 561 562
#if _MSC_VER
  // TODO(msvc): MSVC insists List{Reader,Builder}::operator= are deleted unless we
  //   define them explicitly. Don't know why, especially for the readers.
#define MSVC_DEFAULT_ASSIGNMENT_WORKAROUND(const_, type) \
  inline type& operator=(const_ type& other) { \
    memcpy(this, &other, sizeof(*this)); \
    return *this; \
  }
#else
#define MSVC_DEFAULT_ASSIGNMENT_WORKAROUND(const_, type)
#endif

563
class ListBuilder: public kj::DisallowConstCopy {
564
public:
565
  inline ListBuilder()
566
      : segment(nullptr), ptr(nullptr), elementCount(0 * ELEMENTS),
567
        step(0 * BITS / ELEMENTS), elementSize(ElementSize::VOID) {}
568

569
  MSVC_DEFAULT_ASSIGNMENT_WORKAROUND(, ListBuilder)
570

571
  inline word* getLocation() {
572
    // Get the object's location.
573

574
    if (elementSize == ElementSize::INLINE_COMPOSITE) {
575
      return reinterpret_cast<word*>(ptr) - POINTER_SIZE_IN_WORDS;
576 577
    } else {
      return reinterpret_cast<word*>(ptr);
578 579 580
    }
  }

581 582
  inline ElementSize getElementSize() const { return elementSize; }

583
  inline ElementCount size() const;
584 585
  // The number of elements in the list.

586 587 588 589
  Text::Builder asText();
  Data::Builder asData();
  // Reinterpret the list as a blob.  Throws an exception if the elements are not byte-sized.

590
  template <typename T>
591
  KJ_ALWAYS_INLINE(T getDataElement(ElementCount index));
592 593 594
  // Get the element of the given type at the given index.

  template <typename T>
595
  KJ_ALWAYS_INLINE(void setDataElement(
596
      ElementCount index, kj::NoInfer<T> value));
597
  // Set the element at the given index.
598

599
  KJ_ALWAYS_INLINE(PointerBuilder getPointerElement(ElementCount index));
Kenton Varda's avatar
Kenton Varda committed
600

601
  StructBuilder getStructElement(ElementCount index);
602

603 604
  ListReader asReader() const;
  // Get a ListReader pointing at the same memory.
605

606 607 608
  BuilderArena* getArena();
  // Gets the arena in which this object is allocated.

609
private:
Kenton Varda's avatar
Kenton Varda committed
610
  SegmentBuilder* segment;  // Memory segment in which the list resides.
611

612
  byte* ptr;  // Pointer to list content.
613

614
  ElementCount elementCount;  // Number of elements in the list.
615

616
  decltype(BITS / ELEMENTS) step;
617
  // The distance between elements.
618 619

  BitCount32 structDataSize;
620
  WirePointerCount16 structPointerCount;
621 622
  // The struct properties to use when interpreting the elements as structs.  All lists can be
  // interpreted as struct lists, so these are always filled in.
623

624 625
  ElementSize elementSize;
  // The element size as a ElementSize. This is only really needed to disambiguate INLINE_COMPOSITE
626 627
  // from other types when the overall size is exactly zero or one words.

628
  inline ListBuilder(SegmentBuilder* segment, void* ptr,
629
                     decltype(BITS / ELEMENTS) step, ElementCount size,
630
                     BitCount structDataSize, WirePointerCount structPointerCount,
631
                     ElementSize elementSize)
632
      : segment(segment), ptr(reinterpret_cast<byte*>(ptr)),
633
        elementCount(size), step(step), structDataSize(structDataSize),
634
        structPointerCount(structPointerCount), elementSize(elementSize) {}
635

636
  friend class StructBuilder;
637
  friend struct WireHelpers;
638
  friend class OrphanBuilder;
639 640
};

641
class ListReader {
642
public:
643
  inline ListReader()
644
      : segment(nullptr), ptr(nullptr), elementCount(0), step(0 * BITS / ELEMENTS),
645
        structDataSize(0), structPointerCount(0), nestingLimit(0x7fffffff) {}
646

647
  MSVC_DEFAULT_ASSIGNMENT_WORKAROUND(const, ListReader)
648

649
  inline ElementCount size() const;
650 651
  // The number of elements in the list.

652 653
  inline ElementSize getElementSize() const { return elementSize; }

654 655 656 657
  Text::Reader asText();
  Data::Reader asData();
  // Reinterpret the list as a blob.  Throws an exception if the elements are not byte-sized.

658
  kj::ArrayPtr<const byte> asRawBytes();
659

660
  template <typename T>
661
  KJ_ALWAYS_INLINE(T getDataElement(ElementCount index) const);
662 663
  // Get the element of the given type at the given index.

664
  KJ_ALWAYS_INLINE(PointerReader getPointerElement(ElementCount index) const);
665

666
  StructReader getStructElement(ElementCount index) const;
667

668
private:
Kenton Varda's avatar
Kenton Varda committed
669
  SegmentReader* segment;  // Memory segment in which the list resides.
670

671
  const byte* ptr;  // Pointer to list content.
672

673
  ElementCount elementCount;  // Number of elements in the list.
674

675
  decltype(BITS / ELEMENTS) step;
676
  // The distance between elements.
677

678
  BitCount32 structDataSize;
679
  WirePointerCount16 structPointerCount;
680 681
  // The struct properties to use when interpreting the elements as structs.  All lists can be
  // interpreted as struct lists, so these are always filled in.
682

683 684
  ElementSize elementSize;
  // The element size as a ElementSize. This is only really needed to disambiguate INLINE_COMPOSITE
685 686
  // from other types when the overall size is exactly zero or one words.

687
  int nestingLimit;
688 689 690
  // Limits the depth of message structures to guard against stack-overflow-based DoS attacks.
  // Once this reaches zero, further pointers will be pruned.

691
  inline ListReader(SegmentReader* segment, const void* ptr,
692
                    ElementCount elementCount, decltype(BITS / ELEMENTS) step,
693
                    BitCount structDataSize, WirePointerCount structPointerCount,
694
                    ElementSize elementSize, int nestingLimit)
695
      : segment(segment), ptr(reinterpret_cast<const byte*>(ptr)), elementCount(elementCount),
696
        step(step), structDataSize(structDataSize),
697 698
        structPointerCount(structPointerCount), elementSize(elementSize),
        nestingLimit(nestingLimit) {}
699

700 701
  friend class StructReader;
  friend class ListBuilder;
702
  friend struct WireHelpers;
703
  friend class OrphanBuilder;
704 705
};

706 707
// -------------------------------------------------------------------

708 709 710 711
class OrphanBuilder {
public:
  inline OrphanBuilder(): segment(nullptr), location(nullptr) { memset(&tag, 0, sizeof(tag)); }
  OrphanBuilder(const OrphanBuilder& other) = delete;
712
  inline OrphanBuilder(OrphanBuilder&& other) noexcept;
713
  inline ~OrphanBuilder() noexcept(false);
714 715 716

  static OrphanBuilder initStruct(BuilderArena* arena, StructSize size);
  static OrphanBuilder initList(BuilderArena* arena, ElementCount elementCount,
717
                                ElementSize elementSize);
718 719 720 721 722 723 724
  static OrphanBuilder initStructList(BuilderArena* arena, ElementCount elementCount,
                                      StructSize elementSize);
  static OrphanBuilder initText(BuilderArena* arena, ByteCount size);
  static OrphanBuilder initData(BuilderArena* arena, ByteCount size);

  static OrphanBuilder copy(BuilderArena* arena, StructReader copyFrom);
  static OrphanBuilder copy(BuilderArena* arena, ListReader copyFrom);
725
  static OrphanBuilder copy(BuilderArena* arena, PointerReader copyFrom);
726 727
  static OrphanBuilder copy(BuilderArena* arena, Text::Reader copyFrom);
  static OrphanBuilder copy(BuilderArena* arena, Data::Reader copyFrom);
728
#if !CAPNP_LITE
729
  static OrphanBuilder copy(BuilderArena* arena, kj::Own<ClientHook> copyFrom);
730
#endif  // !CAPNP_LITE
731

732 733
  static OrphanBuilder referenceExternalData(BuilderArena* arena, Data::Reader data);

734 735 736
  OrphanBuilder& operator=(const OrphanBuilder& other) = delete;
  inline OrphanBuilder& operator=(OrphanBuilder&& other);

737 738
  inline bool operator==(decltype(nullptr)) const { return location == nullptr; }
  inline bool operator!=(decltype(nullptr)) const { return location != nullptr; }
739 740 741 742

  StructBuilder asStruct(StructSize size);
  // Interpret as a struct, or throw an exception if not a struct.

743
  ListBuilder asList(ElementSize elementSize);
744 745 746 747 748 749 750 751 752 753
  // Interpret as a list, or throw an exception if not a list.  elementSize cannot be
  // INLINE_COMPOSITE -- use asStructList() instead.

  ListBuilder asStructList(StructSize elementSize);
  // Interpret as a struct list, or throw an exception if not a list.

  Text::Builder asText();
  Data::Builder asData();
  // Interpret as a blob, or throw an exception if not a blob.

754
  StructReader asStructReader(StructSize size) const;
755
  ListReader asListReader(ElementSize elementSize) const;
756
#if !CAPNP_LITE
757
  kj::Own<ClientHook> asCapability() const;
758
#endif  // !CAPNP_LITE
759 760 761
  Text::Reader asTextReader() const;
  Data::Reader asDataReader() const;

762 763 764 765 766 767 768 769
  bool truncate(ElementCount size, bool isText) KJ_WARN_UNUSED_RESULT;
  // Resize the orphan list to the given size. Returns false if the list is currently empty but
  // the requested size is non-zero, in which case the caller will need to allocate a new list.

  void truncate(ElementCount size, ElementSize elementSize);
  void truncate(ElementCount size, StructSize elementSize);
  void truncateText(ElementCount size);
  // Versions of truncate() that know how to allocate a new list if needed.
770

771 772 773 774 775 776 777
private:
  static_assert(1 * POINTERS * WORDS_PER_POINTER == 1 * WORDS,
                "This struct assumes a pointer is one word.");
  word tag;
  // Contains an encoded WirePointer representing this object.  WirePointer is defined in
  // layout.c++, but fits in a word.
  //
778 779 780 781 782 783 784
  // This may be a FAR pointer.  Even in that case, `location` points to the eventual destination
  // of that far pointer.  The reason we keep the far pointer around rather than just making `tag`
  // represent the final destination is because if the eventual adopter of the pointer is not in
  // the target's segment then it may be useful to reuse the far pointer landing pad.
  //
  // If `tag` is not a far pointer, its offset is garbage; only `location` points to the actual
  // target.
785 786

  SegmentBuilder* segment;
787
  // Segment in which the object resides.
788 789

  word* location;
790 791
  // Pointer to the object, or nullptr if the pointer is null.  For capabilities, we make this
  // point at `tag` just so that it is non-null for operator==, but it is never used.
792 793 794 795 796 797 798

  inline OrphanBuilder(const void* tagPtr, SegmentBuilder* segment, word* location)
      : segment(segment), location(location) {
    memcpy(&tag, tagPtr, sizeof(tag));
  }

  inline WirePointer* tagAsPtr() { return reinterpret_cast<WirePointer*>(&tag); }
799
  inline const WirePointer* tagAsPtr() const { return reinterpret_cast<const WirePointer*>(&tag); }
800 801 802 803 804 805 806 807

  void euthanize();
  // Erase the target object, zeroing it out and possibly reclaiming the memory.  Called when
  // the OrphanBuilder is being destroyed or overwritten and it is non-null.

  friend struct WireHelpers;
};

808 809 810
// =======================================================================================
// Internal implementation details...

811 812 813 814 815 816 817 818 819 820 821
// These are defined in the source file.
template <> typename Text::Builder PointerBuilder::initBlob<Text>(ByteCount size);
template <> void PointerBuilder::setBlob<Text>(typename Text::Reader value);
template <> typename Text::Builder PointerBuilder::getBlob<Text>(const void* defaultValue, ByteCount defaultSize);
template <> typename Text::Reader PointerReader::getBlob<Text>(const void* defaultValue, ByteCount defaultSize) const;

template <> typename Data::Builder PointerBuilder::initBlob<Data>(ByteCount size);
template <> void PointerBuilder::setBlob<Data>(typename Data::Reader value);
template <> typename Data::Builder PointerBuilder::getBlob<Data>(const void* defaultValue, ByteCount defaultSize);
template <> typename Data::Reader PointerReader::getBlob<Data>(const void* defaultValue, ByteCount defaultSize) const;

Kenton Varda's avatar
Kenton Varda committed
822 823 824 825 826 827 828 829
inline PointerBuilder PointerBuilder::getRoot(SegmentBuilder* segment, word* location) {
  return PointerBuilder(segment, reinterpret_cast<WirePointer*>(location));
}

inline PointerReader PointerReader::getRootUnchecked(const word* location) {
  return PointerReader(nullptr, reinterpret_cast<const WirePointer*>(location), 0x7fffffff);
}

830 831
// -------------------------------------------------------------------

832 833
inline kj::ArrayPtr<byte> StructBuilder::getDataSectionAsBlob() {
  return kj::ArrayPtr<byte>(reinterpret_cast<byte*>(data), dataSize / BITS_PER_BYTE / BYTES);
834 835
}

836
inline _::ListBuilder StructBuilder::getPointerSectionAsList() {
837
  return _::ListBuilder(segment, pointers, 1 * POINTERS * BITS_PER_POINTER / ELEMENTS,
838
                        pointerCount * (1 * ELEMENTS / POINTERS),
839
                        0 * BITS, 1 * POINTERS, ElementSize::POINTER);
840 841
}

842 843 844 845 846 847 848 849 850 851
template <typename T>
inline bool StructBuilder::hasDataField(ElementCount offset) {
  return getDataField<Mask<T>>(offset) != 0;
}

template <>
inline bool StructBuilder::hasDataField<Void>(ElementCount offset) {
  return false;
}

852
template <typename T>
853
inline T StructBuilder::getDataField(ElementCount offset) {
854
  return reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].get();
855 856 857
}

template <>
858
inline bool StructBuilder::getDataField<bool>(ElementCount offset) {
859
  BitCount boffset = offset * (1 * BITS / ELEMENTS);
860
  byte* b = reinterpret_cast<byte*>(data) + boffset / BITS_PER_BYTE;
861
  return (*reinterpret_cast<uint8_t*>(b) & (1 << (boffset % BITS_PER_BYTE / BITS))) != 0;
862 863
}

864
template <>
865
inline Void StructBuilder::getDataField<Void>(ElementCount offset) {
866
  return VOID;
867 868
}

869
template <typename T>
870
inline T StructBuilder::getDataField(ElementCount offset, Mask<T> mask) {
871
  return unmask<T>(getDataField<Mask<T> >(offset), mask);
872 873
}

874
template <typename T>
875
inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer<T> value) {
876
  reinterpret_cast<WireValue<T>*>(data)[offset / ELEMENTS].set(value);
877 878
}

879 880 881 882 883 884 885 886 887 888 889 890
#if CAPNP_CANONICALIZE_NAN
// Use mask() on floats and doubles to make sure we canonicalize NaNs.
template <>
inline void StructBuilder::setDataField<float>(ElementCount offset, float value) {
  setDataField<uint32_t>(offset, mask<float>(value, 0));
}
template <>
inline void StructBuilder::setDataField<double>(ElementCount offset, double value) {
  setDataField<uint64_t>(offset, mask<double>(value, 0));
}
#endif

891
template <>
892
inline void StructBuilder::setDataField<bool>(ElementCount offset, bool value) {
893
  BitCount boffset = offset * (1 * BITS / ELEMENTS);
894
  byte* b = reinterpret_cast<byte*>(data) + boffset / BITS_PER_BYTE;
895 896 897
  uint bitnum = boffset % BITS_PER_BYTE / BITS;
  *reinterpret_cast<uint8_t*>(b) = (*reinterpret_cast<uint8_t*>(b) & ~(1 << bitnum))
                                 | (static_cast<uint8_t>(value) << bitnum);
898 899
}

900
template <>
901
inline void StructBuilder::setDataField<Void>(ElementCount offset, Void value) {}
902

903
template <typename T>
904
inline void StructBuilder::setDataField(ElementCount offset, kj::NoInfer<T> value, Mask<T> m) {
905
  setDataField<Mask<T> >(offset, mask<T>(value, m));
906 907
}

908 909 910 911 912 913
inline PointerBuilder StructBuilder::getPointerField(WirePointerCount ptrIndex) {
  // Hacky because WirePointer is defined in the .c++ file (so is incomplete here).
  return PointerBuilder(segment, reinterpret_cast<WirePointer*>(
      reinterpret_cast<word*>(pointers) + ptrIndex * WORDS_PER_POINTER));
}

914 915
// -------------------------------------------------------------------

916 917
inline kj::ArrayPtr<const byte> StructReader::getDataSectionAsBlob() {
  return kj::ArrayPtr<const byte>(reinterpret_cast<const byte*>(data), dataSize / BITS_PER_BYTE / BYTES);
918 919
}

920
inline _::ListReader StructReader::getPointerSectionAsList() {
921
  return _::ListReader(segment, pointers, pointerCount * (1 * ELEMENTS / POINTERS),
922 923
                       1 * POINTERS * BITS_PER_POINTER / ELEMENTS, 0 * BITS, 1 * POINTERS,
                       ElementSize::POINTER, nestingLimit);
924 925
}

926
template <typename T>
927 928 929 930 931 932 933 934 935 936 937
inline bool StructReader::hasDataField(ElementCount offset) const {
  return getDataField<Mask<T>>(offset) != 0;
}

template <>
inline bool StructReader::hasDataField<Void>(ElementCount offset) const {
  return false;
}

template <typename T>
inline T StructReader::getDataField(ElementCount offset) const {
938
  if ((offset + 1 * ELEMENTS) * capnp::bitsPerElement<T>() <= dataSize) {
939
    return reinterpret_cast<const WireValue<T>*>(data)[offset / ELEMENTS].get();
940
  } else {
941
    return static_cast<T>(0);
942
  }
943 944 945
}

template <>
946
inline bool StructReader::getDataField<bool>(ElementCount offset) const {
947
  BitCount boffset = offset * (1 * BITS / ELEMENTS);
948
  if (boffset < dataSize) {
949
    const byte* b = reinterpret_cast<const byte*>(data) + boffset / BITS_PER_BYTE;
950 951
    return (*reinterpret_cast<const uint8_t*>(b) & (1 << (boffset % BITS_PER_BYTE / BITS))) != 0;
  } else {
952
    return false;
953
  }
954 955
}

956
template <>
957
inline Void StructReader::getDataField<Void>(ElementCount offset) const {
958
  return VOID;
959 960
}

961
template <typename T>
962 963
T StructReader::getDataField(ElementCount offset, Mask<T> mask) const {
  return unmask<T>(getDataField<Mask<T> >(offset), mask);
964 965
}

966 967 968 969 970 971 972 973 974 975
inline PointerReader StructReader::getPointerField(WirePointerCount ptrIndex) const {
  if (ptrIndex < pointerCount) {
    // Hacky because WirePointer is defined in the .c++ file (so is incomplete here).
    return PointerReader(segment, reinterpret_cast<const WirePointer*>(
        reinterpret_cast<const word*>(pointers) + ptrIndex * WORDS_PER_POINTER), nestingLimit);
  } else{
    return PointerReader();
  }
}

976 977
// -------------------------------------------------------------------

978
inline ElementCount ListBuilder::size() const { return elementCount; }
979 980

template <typename T>
981
inline T ListBuilder::getDataElement(ElementCount index) {
982 983
  return reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->get();

Kenton Varda's avatar
Kenton Varda committed
984
  // TODO(perf):  Benchmark this alternate implementation, which I suspect may make better use of
985 986 987 988
  //   the x86 SIB byte.  Also use it for all the other getData/setData implementations below, and
  //   the various non-inline methods that look up pointers.
  //   Also if using this, consider changing ptr back to void* instead of byte*.
//  return reinterpret_cast<WireValue<T>*>(ptr)[
989
//      index / ELEMENTS * (step / capnp::bitsPerElement<T>())].get();
990 991 992
}

template <>
993
inline bool ListBuilder::getDataElement<bool>(ElementCount index) {
994 995
  // Ignore step for bit lists because bit lists cannot be upgraded to struct lists.
  BitCount bindex = index * (1 * BITS / ELEMENTS);
996
  byte* b = ptr + bindex / BITS_PER_BYTE;
997
  return (*reinterpret_cast<uint8_t*>(b) & (1 << (bindex % BITS_PER_BYTE / BITS))) != 0;
998 999
}

1000
template <>
1001
inline Void ListBuilder::getDataElement<Void>(ElementCount index) {
1002
  return VOID;
1003 1004
}

1005
template <typename T>
1006
inline void ListBuilder::setDataElement(ElementCount index, kj::NoInfer<T> value) {
1007
  reinterpret_cast<WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->set(value);
1008 1009
}

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
#if CAPNP_CANONICALIZE_NAN
// Use mask() on floats and doubles to make sure we canonicalize NaNs.
template <>
inline void ListBuilder::setDataElement<float>(ElementCount index, float value) {
  setDataElement<uint32_t>(index, mask<float>(value, 0));
}
template <>
inline void ListBuilder::setDataElement<double>(ElementCount index, double value) {
  setDataElement<uint64_t>(index, mask<double>(value, 0));
}
#endif

1022
template <>
1023
inline void ListBuilder::setDataElement<bool>(ElementCount index, bool value) {
1024 1025
  // Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists.
  BitCount bindex = index * (1 * BITS / ELEMENTS);
1026
  byte* b = ptr + bindex / BITS_PER_BYTE;
1027 1028 1029
  uint bitnum = bindex % BITS_PER_BYTE / BITS;
  *reinterpret_cast<uint8_t*>(b) = (*reinterpret_cast<uint8_t*>(b) & ~(1 << bitnum))
                                 | (static_cast<uint8_t>(value) << bitnum);
1030 1031
}

1032
template <>
1033
inline void ListBuilder::setDataElement<Void>(ElementCount index, Void value) {}
1034

1035 1036 1037 1038 1039
inline PointerBuilder ListBuilder::getPointerElement(ElementCount index) {
  return PointerBuilder(segment,
      reinterpret_cast<WirePointer*>(ptr + index * step / BITS_PER_BYTE));
}

1040 1041
// -------------------------------------------------------------------

1042
inline ElementCount ListReader::size() const { return elementCount; }
1043 1044

template <typename T>
1045
inline T ListReader::getDataElement(ElementCount index) const {
1046
  return reinterpret_cast<const WireValue<T>*>(ptr + index * step / BITS_PER_BYTE)->get();
1047 1048 1049
}

template <>
1050
inline bool ListReader::getDataElement<bool>(ElementCount index) const {
1051 1052
  // Ignore step for bit lists because bit lists cannot be upgraded to struct lists.
  BitCount bindex = index * (1 * BITS / ELEMENTS);
1053
  const byte* b = ptr + bindex / BITS_PER_BYTE;
1054
  return (*reinterpret_cast<const uint8_t*>(b) & (1 << (bindex % BITS_PER_BYTE / BITS))) != 0;
1055 1056
}

1057 1058
template <>
inline Void ListReader::getDataElement<Void>(ElementCount index) const {
1059
  return VOID;
1060 1061
}

1062 1063 1064 1065
inline PointerReader ListReader::getPointerElement(ElementCount index) const {
  return PointerReader(segment,
      reinterpret_cast<const WirePointer*>(ptr + index * step / BITS_PER_BYTE), nestingLimit);
}
1066

1067 1068
// -------------------------------------------------------------------

1069
inline OrphanBuilder::OrphanBuilder(OrphanBuilder&& other) noexcept
1070 1071 1072 1073 1074 1075
    : segment(other.segment), location(other.location) {
  memcpy(&tag, &other.tag, sizeof(tag));  // Needs memcpy to comply with aliasing rules.
  other.segment = nullptr;
  other.location = nullptr;
}

1076
inline OrphanBuilder::~OrphanBuilder() noexcept(false) {
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
  if (segment != nullptr) euthanize();
}

inline OrphanBuilder& OrphanBuilder::operator=(OrphanBuilder&& other) {
  // With normal smart pointers, it's important to handle the case where the incoming pointer
  // is actually transitively owned by this one.  In this case, euthanize() would destroy `other`
  // before we copied it.  This isn't possible in the case of `OrphanBuilder` because it only
  // owns message objects, and `other` is not itself a message object, therefore cannot possibly
  // be transitively owned by `this`.

  if (segment != nullptr) euthanize();
  segment = other.segment;
  location = other.location;
  memcpy(&tag, &other.tag, sizeof(tag));  // Needs memcpy to comply with aliasing rules.
  other.segment = nullptr;
  other.location = nullptr;
  return *this;
}

1096
}  // namespace _ (private)
1097
}  // namespace capnp
1098

Kenton Varda's avatar
Kenton Varda committed
1099
#endif  // CAPNP_LAYOUT_H_