annotate win64-msvc/include/capnp/arena.h @ 47:d93140aac40b

Current Capnp libs and headers from git
author Chris Cannam
date Thu, 20 Oct 2016 18:15:38 +0100
parents
children 0f2d93caa50c
rev   line source
Chris@47 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@47 2 // Licensed under the MIT License:
Chris@47 3 //
Chris@47 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@47 5 // of this software and associated documentation files (the "Software"), to deal
Chris@47 6 // in the Software without restriction, including without limitation the rights
Chris@47 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@47 8 // copies of the Software, and to permit persons to whom the Software is
Chris@47 9 // furnished to do so, subject to the following conditions:
Chris@47 10 //
Chris@47 11 // The above copyright notice and this permission notice shall be included in
Chris@47 12 // all copies or substantial portions of the Software.
Chris@47 13 //
Chris@47 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@47 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@47 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@47 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@47 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@47 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@47 20 // THE SOFTWARE.
Chris@47 21
Chris@47 22 #ifndef CAPNP_ARENA_H_
Chris@47 23 #define CAPNP_ARENA_H_
Chris@47 24
Chris@47 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@47 26 #pragma GCC system_header
Chris@47 27 #endif
Chris@47 28
Chris@47 29 #ifndef CAPNP_PRIVATE
Chris@47 30 #error "This header is only meant to be included by Cap'n Proto's own source code."
Chris@47 31 #endif
Chris@47 32
Chris@47 33 #include <kj/common.h>
Chris@47 34 #include <kj/mutex.h>
Chris@47 35 #include <kj/exception.h>
Chris@47 36 #include <kj/vector.h>
Chris@47 37 #include "common.h"
Chris@47 38 #include "message.h"
Chris@47 39 #include "layout.h"
Chris@47 40 #include <unordered_map>
Chris@47 41
Chris@47 42 #if !CAPNP_LITE
Chris@47 43 #include "capability.h"
Chris@47 44 #endif // !CAPNP_LITE
Chris@47 45
Chris@47 46 namespace capnp {
Chris@47 47
Chris@47 48 #if !CAPNP_LITE
Chris@47 49 class ClientHook;
Chris@47 50 #endif // !CAPNP_LITE
Chris@47 51
Chris@47 52 namespace _ { // private
Chris@47 53
Chris@47 54 class SegmentReader;
Chris@47 55 class SegmentBuilder;
Chris@47 56 class Arena;
Chris@47 57 class BuilderArena;
Chris@47 58 class ReadLimiter;
Chris@47 59
Chris@47 60 class Segment;
Chris@47 61 typedef kj::Id<uint32_t, Segment> SegmentId;
Chris@47 62
Chris@47 63 class ReadLimiter {
Chris@47 64 // Used to keep track of how much data has been processed from a message, and cut off further
Chris@47 65 // processing if and when a particular limit is reached. This is primarily intended to guard
Chris@47 66 // against maliciously-crafted messages which contain cycles or overlapping structures. Cycles
Chris@47 67 // and overlapping are not permitted by the Cap'n Proto format because in many cases they could
Chris@47 68 // be used to craft a deceptively small message which could consume excessive server resources to
Chris@47 69 // process, perhaps even sending it into an infinite loop. Actually detecting overlaps would be
Chris@47 70 // time-consuming, so instead we just keep track of how many words worth of data structures the
Chris@47 71 // receiver has actually dereferenced and error out if this gets too high.
Chris@47 72 //
Chris@47 73 // This counting takes place as you call getters (for non-primitive values) on the message
Chris@47 74 // readers. If you call the same getter twice, the data it returns may be double-counted. This
Chris@47 75 // should not be a big deal in most cases -- just set the read limit high enough that it will
Chris@47 76 // only trigger in unreasonable cases.
Chris@47 77 //
Chris@47 78 // This class is "safe" to use from multiple threads for its intended use case. Threads may
Chris@47 79 // overwrite each others' changes to the counter, but this is OK because it only means that the
Chris@47 80 // limit is enforced a bit less strictly -- it will still kick in eventually.
Chris@47 81
Chris@47 82 public:
Chris@47 83 inline explicit ReadLimiter(); // No limit.
Chris@47 84 inline explicit ReadLimiter(WordCount64 limit); // Limit to the given number of words.
Chris@47 85
Chris@47 86 inline void reset(WordCount64 limit);
Chris@47 87
Chris@47 88 KJ_ALWAYS_INLINE(bool canRead(WordCount amount, Arena* arena));
Chris@47 89
Chris@47 90 void unread(WordCount64 amount);
Chris@47 91 // Adds back some words to the limit. Useful when the caller knows they are double-reading
Chris@47 92 // some data.
Chris@47 93
Chris@47 94 private:
Chris@47 95 volatile uint64_t limit;
Chris@47 96 // Current limit, decremented each time catRead() is called. Volatile because multiple threads
Chris@47 97 // could be trying to modify it at once. (This is not real thread-safety, but good enough for
Chris@47 98 // the purpose of this class. See class comment.)
Chris@47 99
Chris@47 100 KJ_DISALLOW_COPY(ReadLimiter);
Chris@47 101 };
Chris@47 102
Chris@47 103 #if !CAPNP_LITE
Chris@47 104 class BrokenCapFactory {
Chris@47 105 // Callback for constructing broken caps. We use this so that we can avoid arena.c++ having a
Chris@47 106 // link-time dependency on capability code that lives in libcapnp-rpc.
Chris@47 107
Chris@47 108 public:
Chris@47 109 virtual kj::Own<ClientHook> newBrokenCap(kj::StringPtr description) = 0;
Chris@47 110 virtual kj::Own<ClientHook> newNullCap() = 0;
Chris@47 111 };
Chris@47 112 #endif // !CAPNP_LITE
Chris@47 113
Chris@47 114 class SegmentReader {
Chris@47 115 public:
Chris@47 116 inline SegmentReader(Arena* arena, SegmentId id, kj::ArrayPtr<const word> ptr,
Chris@47 117 ReadLimiter* readLimiter);
Chris@47 118
Chris@47 119 KJ_ALWAYS_INLINE(bool containsInterval(const void* from, const void* to));
Chris@47 120
Chris@47 121 KJ_ALWAYS_INLINE(bool amplifiedRead(WordCount virtualAmount));
Chris@47 122 // Indicates that the reader should pretend that `virtualAmount` additional data was read even
Chris@47 123 // though no actual pointer was traversed. This is used e.g. when reading a struct list pointer
Chris@47 124 // where the element sizes are zero -- the sender could set the list size arbitrarily high and
Chris@47 125 // cause the receiver to iterate over this list even though the message itself is small, so we
Chris@47 126 // need to defend against DoS attacks based on this.
Chris@47 127
Chris@47 128 inline Arena* getArena();
Chris@47 129 inline SegmentId getSegmentId();
Chris@47 130
Chris@47 131 inline const word* getStartPtr();
Chris@47 132 inline WordCount getOffsetTo(const word* ptr);
Chris@47 133 inline WordCount getSize();
Chris@47 134
Chris@47 135 inline kj::ArrayPtr<const word> getArray();
Chris@47 136
Chris@47 137 inline void unread(WordCount64 amount);
Chris@47 138 // Add back some words to the ReadLimiter.
Chris@47 139
Chris@47 140 private:
Chris@47 141 Arena* arena;
Chris@47 142 SegmentId id;
Chris@47 143 kj::ArrayPtr<const word> ptr;
Chris@47 144 ReadLimiter* readLimiter;
Chris@47 145
Chris@47 146 KJ_DISALLOW_COPY(SegmentReader);
Chris@47 147
Chris@47 148 friend class SegmentBuilder;
Chris@47 149 };
Chris@47 150
Chris@47 151 class SegmentBuilder: public SegmentReader {
Chris@47 152 public:
Chris@47 153 inline SegmentBuilder(BuilderArena* arena, SegmentId id, kj::ArrayPtr<word> ptr,
Chris@47 154 ReadLimiter* readLimiter, size_t wordsUsed = 0);
Chris@47 155 inline SegmentBuilder(BuilderArena* arena, SegmentId id, kj::ArrayPtr<const word> ptr,
Chris@47 156 ReadLimiter* readLimiter);
Chris@47 157 inline SegmentBuilder(BuilderArena* arena, SegmentId id, decltype(nullptr),
Chris@47 158 ReadLimiter* readLimiter);
Chris@47 159
Chris@47 160 KJ_ALWAYS_INLINE(word* allocate(WordCount amount));
Chris@47 161
Chris@47 162 KJ_ALWAYS_INLINE(void checkWritable());
Chris@47 163 // Throw an exception if the segment is read-only (meaning it is a reference to external data).
Chris@47 164
Chris@47 165 KJ_ALWAYS_INLINE(word* getPtrUnchecked(WordCount offset));
Chris@47 166 // Get a writable pointer into the segment. Throws an exception if the segment is read-only (i.e.
Chris@47 167 // a reference to external immutable data).
Chris@47 168
Chris@47 169 inline BuilderArena* getArena();
Chris@47 170
Chris@47 171 inline kj::ArrayPtr<const word> currentlyAllocated();
Chris@47 172
Chris@47 173 inline void reset();
Chris@47 174
Chris@47 175 inline bool isWritable() { return !readOnly; }
Chris@47 176
Chris@47 177 inline void tryTruncate(word* from, word* to);
Chris@47 178 // If `from` points just past the current end of the segment, then move the end back to `to`.
Chris@47 179 // Otherwise, do nothing.
Chris@47 180
Chris@47 181 inline bool tryExtend(word* from, word* to);
Chris@47 182 // If `from` points just past the current end of the segment, and `to` is within the segment
Chris@47 183 // boundaries, then move the end up to `to` and return true. Otherwise, do nothing and return
Chris@47 184 // false.
Chris@47 185
Chris@47 186 private:
Chris@47 187 word* pos;
Chris@47 188 // Pointer to a pointer to the current end point of the segment, i.e. the location where the
Chris@47 189 // next object should be allocated.
Chris@47 190
Chris@47 191 bool readOnly;
Chris@47 192
Chris@47 193 void throwNotWritable();
Chris@47 194
Chris@47 195 KJ_DISALLOW_COPY(SegmentBuilder);
Chris@47 196 };
Chris@47 197
Chris@47 198 class Arena {
Chris@47 199 public:
Chris@47 200 virtual ~Arena() noexcept(false);
Chris@47 201
Chris@47 202 virtual SegmentReader* tryGetSegment(SegmentId id) = 0;
Chris@47 203 // Gets the segment with the given ID, or return nullptr if no such segment exists.
Chris@47 204
Chris@47 205 virtual void reportReadLimitReached() = 0;
Chris@47 206 // Called to report that the read limit has been reached. See ReadLimiter, below. This invokes
Chris@47 207 // the VALIDATE_INPUT() macro which may throw an exception; if it returns normally, the caller
Chris@47 208 // will need to continue with default values.
Chris@47 209 };
Chris@47 210
Chris@47 211 class ReaderArena final: public Arena {
Chris@47 212 public:
Chris@47 213 ReaderArena(MessageReader* message);
Chris@47 214 ~ReaderArena() noexcept(false);
Chris@47 215 KJ_DISALLOW_COPY(ReaderArena);
Chris@47 216
Chris@47 217 // implements Arena ------------------------------------------------
Chris@47 218 SegmentReader* tryGetSegment(SegmentId id) override;
Chris@47 219 void reportReadLimitReached() override;
Chris@47 220
Chris@47 221 private:
Chris@47 222 MessageReader* message;
Chris@47 223 ReadLimiter readLimiter;
Chris@47 224
Chris@47 225 // Optimize for single-segment messages so that small messages are handled quickly.
Chris@47 226 SegmentReader segment0;
Chris@47 227
Chris@47 228 typedef std::unordered_map<uint, kj::Own<SegmentReader>> SegmentMap;
Chris@47 229 kj::MutexGuarded<kj::Maybe<kj::Own<SegmentMap>>> moreSegments;
Chris@47 230 // We need to mutex-guard the segment map because we lazily initialize segments when they are
Chris@47 231 // first requested, but a Reader is allowed to be used concurrently in multiple threads. Luckily
Chris@47 232 // this only applies to large messages.
Chris@47 233 //
Chris@47 234 // TODO(perf): Thread-local thing instead? Some kind of lockless map? Or do sharing of data
Chris@47 235 // in a different way, where you have to construct a new MessageReader in each thread (but
Chris@47 236 // possibly backed by the same data)?
Chris@47 237 };
Chris@47 238
Chris@47 239 class BuilderArena final: public Arena {
Chris@47 240 // A BuilderArena that does not allow the injection of capabilities.
Chris@47 241
Chris@47 242 public:
Chris@47 243 explicit BuilderArena(MessageBuilder* message);
Chris@47 244 BuilderArena(MessageBuilder* message, kj::ArrayPtr<MessageBuilder::SegmentInit> segments);
Chris@47 245 ~BuilderArena() noexcept(false);
Chris@47 246 KJ_DISALLOW_COPY(BuilderArena);
Chris@47 247
Chris@47 248 inline SegmentBuilder* getRootSegment() { return &segment0; }
Chris@47 249
Chris@47 250 kj::ArrayPtr<const kj::ArrayPtr<const word>> getSegmentsForOutput();
Chris@47 251 // Get an array of all the segments, suitable for writing out. This only returns the allocated
Chris@47 252 // portion of each segment, whereas tryGetSegment() returns something that includes
Chris@47 253 // not-yet-allocated space.
Chris@47 254
Chris@47 255 inline CapTableBuilder* getLocalCapTable() {
Chris@47 256 // Return a CapTableBuilder that merely implements local loopback. That is, you can set
Chris@47 257 // capabilities, then read the same capabilities back, but there is no intent ever to transmit
Chris@47 258 // these capabilities. A MessageBuilder that isn't imbued with some other CapTable uses this
Chris@47 259 // by default.
Chris@47 260 //
Chris@47 261 // TODO(cleanup): It's sort of a hack that this exists. In theory, perhaps, unimbued
Chris@47 262 // MessageBuilders should throw exceptions on any attempt to access capability fields, like
Chris@47 263 // unimbued MessageReaders do. However, lots of code exists which uses MallocMessageBuilder
Chris@47 264 // as a temporary holder for data to be copied in and out (without being serialized), and it
Chris@47 265 // is expected that such data can include capabilities, which is admittedly reasonable.
Chris@47 266 // Therefore, all MessageBuilders must have a cap table by default. Arguably we should
Chris@47 267 // deprecate this usage and instead define a new helper type for this exact purpose.
Chris@47 268
Chris@47 269 return &localCapTable;
Chris@47 270 }
Chris@47 271
Chris@47 272 SegmentBuilder* getSegment(SegmentId id);
Chris@47 273 // Get the segment with the given id. Crashes or throws an exception if no such segment exists.
Chris@47 274
Chris@47 275 struct AllocateResult {
Chris@47 276 SegmentBuilder* segment;
Chris@47 277 word* words;
Chris@47 278 };
Chris@47 279
Chris@47 280 AllocateResult allocate(WordCount amount);
Chris@47 281 // Find a segment with at least the given amount of space available and allocate the space.
Chris@47 282 // Note that allocating directly from a particular segment is much faster, but allocating from
Chris@47 283 // the arena is guaranteed to succeed. Therefore callers should try to allocate from a specific
Chris@47 284 // segment first if there is one, then fall back to the arena.
Chris@47 285
Chris@47 286 SegmentBuilder* addExternalSegment(kj::ArrayPtr<const word> content);
Chris@47 287 // Add a new segment to the arena which points to some existing memory region. The segment is
Chris@47 288 // assumed to be completley full; the arena will never allocate from it. In fact, the segment
Chris@47 289 // is considered read-only. Any attempt to get a Builder pointing into this segment will throw
Chris@47 290 // an exception. Readers are allowed, however.
Chris@47 291 //
Chris@47 292 // This can be used to inject some external data into a message without a copy, e.g. embedding a
Chris@47 293 // large mmap'd file into a message as `Data` without forcing that data to actually be read in
Chris@47 294 // from disk (until the message itself is written out). `Orphanage` provides the public API for
Chris@47 295 // this feature.
Chris@47 296
Chris@47 297 // implements Arena ------------------------------------------------
Chris@47 298 SegmentReader* tryGetSegment(SegmentId id) override;
Chris@47 299 void reportReadLimitReached() override;
Chris@47 300
Chris@47 301 private:
Chris@47 302 MessageBuilder* message;
Chris@47 303 ReadLimiter dummyLimiter;
Chris@47 304
Chris@47 305 class LocalCapTable: public CapTableBuilder {
Chris@47 306 #if !CAPNP_LITE
Chris@47 307 public:
Chris@47 308 kj::Maybe<kj::Own<ClientHook>> extractCap(uint index) override;
Chris@47 309 uint injectCap(kj::Own<ClientHook>&& cap) override;
Chris@47 310 void dropCap(uint index) override;
Chris@47 311
Chris@47 312 private:
Chris@47 313 kj::Vector<kj::Maybe<kj::Own<ClientHook>>> capTable;
Chris@47 314 #endif // ! CAPNP_LITE
Chris@47 315 };
Chris@47 316
Chris@47 317 LocalCapTable localCapTable;
Chris@47 318
Chris@47 319 SegmentBuilder segment0;
Chris@47 320 kj::ArrayPtr<const word> segment0ForOutput;
Chris@47 321
Chris@47 322 struct MultiSegmentState {
Chris@47 323 kj::Vector<kj::Own<SegmentBuilder>> builders;
Chris@47 324 kj::Vector<kj::ArrayPtr<const word>> forOutput;
Chris@47 325 };
Chris@47 326 kj::Maybe<kj::Own<MultiSegmentState>> moreSegments;
Chris@47 327
Chris@47 328 SegmentBuilder* segmentWithSpace = nullptr;
Chris@47 329 // When allocating, look for space in this segment first before resorting to allocating a new
Chris@47 330 // segment. This is not necessarily the last segment because addExternalSegment() may add a
Chris@47 331 // segment that is already-full, in which case we don't update this pointer.
Chris@47 332
Chris@47 333 template <typename T> // Can be `word` or `const word`.
Chris@47 334 SegmentBuilder* addSegmentInternal(kj::ArrayPtr<T> content);
Chris@47 335 };
Chris@47 336
Chris@47 337 // =======================================================================================
Chris@47 338
Chris@47 339 inline ReadLimiter::ReadLimiter()
Chris@47 340 : limit(kj::maxValue) {}
Chris@47 341
Chris@47 342 inline ReadLimiter::ReadLimiter(WordCount64 limit): limit(limit / WORDS) {}
Chris@47 343
Chris@47 344 inline void ReadLimiter::reset(WordCount64 limit) { this->limit = limit / WORDS; }
Chris@47 345
Chris@47 346 inline bool ReadLimiter::canRead(WordCount amount, Arena* arena) {
Chris@47 347 // Be careful not to store an underflowed value into `limit`, even if multiple threads are
Chris@47 348 // decrementing it.
Chris@47 349 uint64_t current = limit;
Chris@47 350 if (KJ_UNLIKELY(amount / WORDS > current)) {
Chris@47 351 arena->reportReadLimitReached();
Chris@47 352 return false;
Chris@47 353 } else {
Chris@47 354 limit = current - amount / WORDS;
Chris@47 355 return true;
Chris@47 356 }
Chris@47 357 }
Chris@47 358
Chris@47 359 // -------------------------------------------------------------------
Chris@47 360
Chris@47 361 inline SegmentReader::SegmentReader(Arena* arena, SegmentId id, kj::ArrayPtr<const word> ptr,
Chris@47 362 ReadLimiter* readLimiter)
Chris@47 363 : arena(arena), id(id), ptr(ptr), readLimiter(readLimiter) {}
Chris@47 364
Chris@47 365 inline bool SegmentReader::containsInterval(const void* from, const void* to) {
Chris@47 366 return from >= this->ptr.begin() && to <= this->ptr.end() && from <= to &&
Chris@47 367 readLimiter->canRead(
Chris@47 368 intervalLength(reinterpret_cast<const byte*>(from),
Chris@47 369 reinterpret_cast<const byte*>(to)) / BYTES_PER_WORD,
Chris@47 370 arena);
Chris@47 371 }
Chris@47 372
Chris@47 373 inline bool SegmentReader::amplifiedRead(WordCount virtualAmount) {
Chris@47 374 return readLimiter->canRead(virtualAmount, arena);
Chris@47 375 }
Chris@47 376
Chris@47 377 inline Arena* SegmentReader::getArena() { return arena; }
Chris@47 378 inline SegmentId SegmentReader::getSegmentId() { return id; }
Chris@47 379 inline const word* SegmentReader::getStartPtr() { return ptr.begin(); }
Chris@47 380 inline WordCount SegmentReader::getOffsetTo(const word* ptr) {
Chris@47 381 return intervalLength(this->ptr.begin(), ptr);
Chris@47 382 }
Chris@47 383 inline WordCount SegmentReader::getSize() { return ptr.size() * WORDS; }
Chris@47 384 inline kj::ArrayPtr<const word> SegmentReader::getArray() { return ptr; }
Chris@47 385 inline void SegmentReader::unread(WordCount64 amount) { readLimiter->unread(amount); }
Chris@47 386
Chris@47 387 // -------------------------------------------------------------------
Chris@47 388
Chris@47 389 inline SegmentBuilder::SegmentBuilder(
Chris@47 390 BuilderArena* arena, SegmentId id, kj::ArrayPtr<word> ptr, ReadLimiter* readLimiter,
Chris@47 391 size_t wordsUsed)
Chris@47 392 : SegmentReader(arena, id, ptr, readLimiter), pos(ptr.begin() + wordsUsed), readOnly(false) {}
Chris@47 393 inline SegmentBuilder::SegmentBuilder(
Chris@47 394 BuilderArena* arena, SegmentId id, kj::ArrayPtr<const word> ptr, ReadLimiter* readLimiter)
Chris@47 395 : SegmentReader(arena, id, ptr, readLimiter),
Chris@47 396 // const_cast is safe here because the member won't ever be dereferenced because it appears
Chris@47 397 // to point to the end of the segment anyway.
Chris@47 398 pos(const_cast<word*>(ptr.end())),
Chris@47 399 readOnly(true) {}
Chris@47 400 inline SegmentBuilder::SegmentBuilder(BuilderArena* arena, SegmentId id, decltype(nullptr),
Chris@47 401 ReadLimiter* readLimiter)
Chris@47 402 : SegmentReader(arena, id, nullptr, readLimiter), pos(nullptr), readOnly(false) {}
Chris@47 403
Chris@47 404 inline word* SegmentBuilder::allocate(WordCount amount) {
Chris@47 405 if (intervalLength(pos, ptr.end()) < amount) {
Chris@47 406 // Not enough space in the segment for this allocation.
Chris@47 407 return nullptr;
Chris@47 408 } else {
Chris@47 409 // Success.
Chris@47 410 word* result = pos;
Chris@47 411 pos = pos + amount;
Chris@47 412 return result;
Chris@47 413 }
Chris@47 414 }
Chris@47 415
Chris@47 416 inline void SegmentBuilder::checkWritable() {
Chris@47 417 if (KJ_UNLIKELY(readOnly)) throwNotWritable();
Chris@47 418 }
Chris@47 419
Chris@47 420 inline word* SegmentBuilder::getPtrUnchecked(WordCount offset) {
Chris@47 421 return const_cast<word*>(ptr.begin() + offset);
Chris@47 422 }
Chris@47 423
Chris@47 424 inline BuilderArena* SegmentBuilder::getArena() {
Chris@47 425 // Down-cast safe because SegmentBuilder's constructor always initializes its SegmentReader base
Chris@47 426 // class with an Arena pointer that actually points to a BuilderArena.
Chris@47 427 return static_cast<BuilderArena*>(arena);
Chris@47 428 }
Chris@47 429
Chris@47 430 inline kj::ArrayPtr<const word> SegmentBuilder::currentlyAllocated() {
Chris@47 431 return kj::arrayPtr(ptr.begin(), pos - ptr.begin());
Chris@47 432 }
Chris@47 433
Chris@47 434 inline void SegmentBuilder::reset() {
Chris@47 435 word* start = getPtrUnchecked(0 * WORDS);
Chris@47 436 memset(start, 0, (pos - start) * sizeof(word));
Chris@47 437 pos = start;
Chris@47 438 }
Chris@47 439
Chris@47 440 inline void SegmentBuilder::tryTruncate(word* from, word* to) {
Chris@47 441 if (pos == from) pos = to;
Chris@47 442 }
Chris@47 443
Chris@47 444 inline bool SegmentBuilder::tryExtend(word* from, word* to) {
Chris@47 445 // Careful about overflow.
Chris@47 446 if (pos == from && to <= ptr.end() && to >= from) {
Chris@47 447 pos = to;
Chris@47 448 return true;
Chris@47 449 } else {
Chris@47 450 return false;
Chris@47 451 }
Chris@47 452 }
Chris@47 453
Chris@47 454 } // namespace _ (private)
Chris@47 455 } // namespace capnp
Chris@47 456
Chris@47 457 #endif // CAPNP_ARENA_H_