annotate win32-mingw/include/capnp/arena.h @ 50:37d53a7e8262

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