cannam@148: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@148: // Licensed under the MIT License: cannam@148: // cannam@148: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@148: // of this software and associated documentation files (the "Software"), to deal cannam@148: // in the Software without restriction, including without limitation the rights cannam@148: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@148: // copies of the Software, and to permit persons to whom the Software is cannam@148: // furnished to do so, subject to the following conditions: cannam@148: // cannam@148: // The above copyright notice and this permission notice shall be included in cannam@148: // all copies or substantial portions of the Software. cannam@148: // cannam@148: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@148: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@148: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@148: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@148: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@148: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@148: // THE SOFTWARE. cannam@148: cannam@148: // This file contains types which are intended to help detect incorrect usage at compile cannam@148: // time, but should then be optimized down to basic primitives (usually, integers) by the cannam@148: // compiler. cannam@148: cannam@148: #ifndef CAPNP_COMMON_H_ cannam@148: #define CAPNP_COMMON_H_ cannam@148: cannam@148: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) cannam@148: #pragma GCC system_header cannam@148: #endif cannam@148: cannam@148: #include cannam@148: #include cannam@148: #include cannam@148: cannam@148: #if CAPNP_DEBUG_TYPES cannam@148: #include cannam@148: #endif cannam@148: cannam@148: namespace capnp { cannam@148: cannam@148: #define CAPNP_VERSION_MAJOR 0 cannam@148: #define CAPNP_VERSION_MINOR 6 cannam@148: #define CAPNP_VERSION_MICRO 0 cannam@148: cannam@148: #define CAPNP_VERSION \ cannam@148: (CAPNP_VERSION_MAJOR * 1000000 + CAPNP_VERSION_MINOR * 1000 + CAPNP_VERSION_MICRO) cannam@148: cannam@148: #ifndef CAPNP_LITE cannam@148: #define CAPNP_LITE 0 cannam@148: #endif cannam@148: cannam@148: typedef unsigned int uint; cannam@148: cannam@148: struct Void { cannam@148: // Type used for Void fields. Using C++'s "void" type creates a bunch of issues since it behaves cannam@148: // differently from other types. cannam@148: cannam@148: inline constexpr bool operator==(Void other) const { return true; } cannam@148: inline constexpr bool operator!=(Void other) const { return false; } cannam@148: }; cannam@148: cannam@148: static constexpr Void VOID = Void(); cannam@148: // Constant value for `Void`, which is an empty struct. cannam@148: cannam@148: inline kj::StringPtr KJ_STRINGIFY(Void) { return "void"; } cannam@148: cannam@148: struct Text; cannam@148: struct Data; cannam@148: cannam@148: enum class Kind: uint8_t { cannam@148: PRIMITIVE, cannam@148: BLOB, cannam@148: ENUM, cannam@148: STRUCT, cannam@148: UNION, cannam@148: INTERFACE, cannam@148: LIST, cannam@148: cannam@148: OTHER cannam@148: // Some other type which is often a type parameter to Cap'n Proto templates, but which needs cannam@148: // special handling. This includes types like AnyPointer, Dynamic*, etc. cannam@148: }; cannam@148: cannam@148: enum class Style: uint8_t { cannam@148: PRIMITIVE, cannam@148: POINTER, // other than struct cannam@148: STRUCT, cannam@148: CAPABILITY cannam@148: }; cannam@148: cannam@148: enum class ElementSize: uint8_t { cannam@148: // Size of a list element. cannam@148: cannam@148: VOID = 0, cannam@148: BIT = 1, cannam@148: BYTE = 2, cannam@148: TWO_BYTES = 3, cannam@148: FOUR_BYTES = 4, cannam@148: EIGHT_BYTES = 5, cannam@148: cannam@148: POINTER = 6, cannam@148: cannam@148: INLINE_COMPOSITE = 7 cannam@148: }; cannam@148: cannam@148: enum class PointerType { cannam@148: // Various wire types a pointer field can take cannam@148: cannam@148: NULL_, cannam@148: // Should be NULL, but that's #defined in stddef.h cannam@148: cannam@148: STRUCT, cannam@148: LIST, cannam@148: CAPABILITY cannam@148: }; cannam@148: cannam@148: namespace schemas { cannam@148: cannam@148: template cannam@148: struct EnumInfo; cannam@148: cannam@148: } // namespace schemas cannam@148: cannam@148: namespace _ { // private cannam@148: cannam@148: template struct Kind_; cannam@148: cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::BLOB; }; cannam@148: template <> struct Kind_ { static constexpr Kind kind = Kind::BLOB; }; cannam@148: cannam@148: template struct Kind_> { cannam@148: static constexpr Kind kind = Kind::STRUCT; cannam@148: }; cannam@148: template struct Kind_> { cannam@148: static constexpr Kind kind = Kind::INTERFACE; cannam@148: }; cannam@148: template struct Kind_::IsEnum>> { cannam@148: static constexpr Kind kind = Kind::ENUM; cannam@148: }; cannam@148: cannam@148: } // namespace _ (private) cannam@148: cannam@148: template ::kind> cannam@148: inline constexpr Kind kind() { cannam@148: // This overload of kind() matches types which have a Kind_ specialization. cannam@148: cannam@148: return k; cannam@148: } cannam@148: cannam@148: #if CAPNP_LITE cannam@148: cannam@148: #define CAPNP_KIND(T) ::capnp::_::Kind_::kind cannam@148: // Avoid constexpr methods in lite mode (MSVC is bad at constexpr). cannam@148: cannam@148: #else // CAPNP_LITE cannam@148: cannam@148: #define CAPNP_KIND(T) ::capnp::kind() cannam@148: // Use this macro rather than kind() in any code which must work in lite mode. cannam@148: cannam@148: template ()> cannam@148: inline constexpr Style style() { cannam@148: return k == Kind::PRIMITIVE || k == Kind::ENUM ? Style::PRIMITIVE cannam@148: : k == Kind::STRUCT ? Style::STRUCT cannam@148: : k == Kind::INTERFACE ? Style::CAPABILITY : Style::POINTER; cannam@148: } cannam@148: cannam@148: #endif // CAPNP_LITE, else cannam@148: cannam@148: template cannam@148: struct List; cannam@148: cannam@148: #if _MSC_VER cannam@148: cannam@148: template cannam@148: struct List {}; cannam@148: // For some reason, without this declaration, MSVC will error out on some uses of List cannam@148: // claiming that "T" -- as used in the default initializer for the second template param, "k" -- cannam@148: // is not defined. I do not understand this error, but adding this empty default declaration fixes cannam@148: // it. cannam@148: cannam@148: #endif cannam@148: cannam@148: template struct ListElementType_; cannam@148: template struct ListElementType_> { typedef T Type; }; cannam@148: template using ListElementType = typename ListElementType_::Type; cannam@148: cannam@148: namespace _ { // private cannam@148: template struct Kind_> { cannam@148: static constexpr Kind kind = Kind::LIST; cannam@148: }; cannam@148: } // namespace _ (private) cannam@148: cannam@148: template struct ReaderFor_ { typedef typename T::Reader Type; }; cannam@148: template struct ReaderFor_ { typedef T Type; }; cannam@148: template struct ReaderFor_ { typedef T Type; }; cannam@148: template struct ReaderFor_ { typedef typename T::Client Type; }; cannam@148: template using ReaderFor = typename ReaderFor_::Type; cannam@148: // The type returned by List::Reader::operator[]. cannam@148: cannam@148: template struct BuilderFor_ { typedef typename T::Builder Type; }; cannam@148: template struct BuilderFor_ { typedef T Type; }; cannam@148: template struct BuilderFor_ { typedef T Type; }; cannam@148: template struct BuilderFor_ { typedef typename T::Client Type; }; cannam@148: template using BuilderFor = typename BuilderFor_::Type; cannam@148: // The type returned by List::Builder::operator[]. cannam@148: cannam@148: template struct PipelineFor_ { typedef typename T::Pipeline Type;}; cannam@148: template struct PipelineFor_ { typedef typename T::Client Type; }; cannam@148: template using PipelineFor = typename PipelineFor_::Type; cannam@148: cannam@148: template struct TypeIfEnum_; cannam@148: template struct TypeIfEnum_ { typedef T Type; }; cannam@148: cannam@148: template cannam@148: using TypeIfEnum = typename TypeIfEnum_>::Type; cannam@148: cannam@148: template cannam@148: using FromReader = typename kj::Decay::Reads; cannam@148: // FromReader = MyType (for any Cap'n Proto type). cannam@148: cannam@148: template cannam@148: using FromBuilder = typename kj::Decay::Builds; cannam@148: // FromBuilder = MyType (for any Cap'n Proto type). cannam@148: cannam@148: template cannam@148: using FromPipeline = typename kj::Decay::Pipelines; cannam@148: // FromBuilder = MyType (for any Cap'n Proto type). cannam@148: cannam@148: template cannam@148: using FromClient = typename kj::Decay::Calls; cannam@148: // FromReader = MyType (for any Cap'n Proto interface type). cannam@148: cannam@148: template cannam@148: using FromServer = typename kj::Decay::Serves; cannam@148: // FromBuilder = MyType (for any Cap'n Proto interface type). cannam@148: cannam@148: template cannam@148: struct FromAny_; cannam@148: cannam@148: template cannam@148: struct FromAny_>> { cannam@148: using Type = FromReader; cannam@148: }; cannam@148: cannam@148: template cannam@148: struct FromAny_>> { cannam@148: using Type = FromBuilder; cannam@148: }; cannam@148: cannam@148: template cannam@148: struct FromAny_>> { cannam@148: using Type = FromPipeline; cannam@148: }; cannam@148: cannam@148: // Note that T::Client is covered by FromReader cannam@148: cannam@148: template cannam@148: struct FromAny_, kj::VoidSfinae>> { cannam@148: using Type = FromServer; cannam@148: }; cannam@148: cannam@148: template cannam@148: struct FromAny_::kind == Kind::PRIMITIVE || _::Kind_::kind == Kind::ENUM>> { cannam@148: // TODO(msvc): Ideally the EnableIf condition would be `style() == Style::PRIMITIVE`, but MSVC cannam@148: // cannot yet use style() in this constexpr context. cannam@148: cannam@148: using Type = kj::Decay; cannam@148: }; cannam@148: cannam@148: template cannam@148: using FromAny = typename FromAny_::Type; cannam@148: // Given any Cap'n Proto value type as an input, return the Cap'n Proto base type. That is: cannam@148: // cannam@148: // Foo::Reader -> Foo cannam@148: // Foo::Builder -> Foo cannam@148: // Foo::Pipeline -> Foo cannam@148: // Foo::Client -> Foo cannam@148: // Own -> Foo cannam@148: // uint32_t -> uint32_t cannam@148: cannam@148: namespace _ { // private cannam@148: cannam@148: template cannam@148: struct PointerHelpers; cannam@148: cannam@148: #if _MSC_VER cannam@148: cannam@148: template cannam@148: struct PointerHelpers {}; cannam@148: // For some reason, without this declaration, MSVC will error out on some uses of PointerHelpers cannam@148: // claiming that "T" -- as used in the default initializer for the second template param, "k" -- cannam@148: // is not defined. I do not understand this error, but adding this empty default declaration fixes cannam@148: // it. cannam@148: cannam@148: #endif cannam@148: cannam@148: } // namespace _ (private) cannam@148: cannam@148: struct MessageSize { cannam@148: // Size of a message. Every struct type has a method `.totalSize()` that returns this. cannam@148: uint64_t wordCount; cannam@148: uint capCount; cannam@148: }; cannam@148: cannam@148: // ======================================================================================= cannam@148: // Raw memory types and measures cannam@148: cannam@148: using kj::byte; cannam@148: cannam@148: class word { uint64_t content KJ_UNUSED_MEMBER; KJ_DISALLOW_COPY(word); public: word() = default; }; cannam@148: // word is an opaque type with size of 64 bits. This type is useful only to make pointer cannam@148: // arithmetic clearer. Since the contents are private, the only way to access them is to first cannam@148: // reinterpret_cast to some other pointer type. cannam@148: // cannam@148: // Copying is disallowed because you should always use memcpy(). Otherwise, you may run afoul of cannam@148: // aliasing rules. cannam@148: // cannam@148: // A pointer of type word* should always be word-aligned even if won't actually be dereferenced as cannam@148: // that type. cannam@148: cannam@148: static_assert(sizeof(byte) == 1, "uint8_t is not one byte?"); cannam@148: static_assert(sizeof(word) == 8, "uint64_t is not 8 bytes?"); cannam@148: cannam@148: #if CAPNP_DEBUG_TYPES cannam@148: // Set CAPNP_DEBUG_TYPES to 1 to use kj::Quantity for "count" types. Otherwise, plain integers are cannam@148: // used. All the code should still operate exactly the same, we just lose compile-time checking. cannam@148: // Note that this will also change symbol names, so it's important that the library and any clients cannam@148: // be compiled with the same setting here. cannam@148: // cannam@148: // We disable this by default to reduce symbol name size and avoid any possibility of the compiler cannam@148: // failing to fully-optimize the types, but anyone modifying Cap'n Proto itself should enable this cannam@148: // during development and testing. cannam@148: cannam@148: namespace _ { class BitLabel; class ElementLabel; struct WirePointer; } cannam@148: cannam@148: template cannam@148: using BitCountN = kj::Quantity(), T>, _::BitLabel>; cannam@148: template cannam@148: using ByteCountN = kj::Quantity(), T>, byte>; cannam@148: template cannam@148: using WordCountN = kj::Quantity(), T>, word>; cannam@148: template cannam@148: using ElementCountN = kj::Quantity(), T>, _::ElementLabel>; cannam@148: template cannam@148: using WirePointerCountN = kj::Quantity(), T>, _::WirePointer>; cannam@148: cannam@148: typedef BitCountN<8, uint8_t> BitCount8; cannam@148: typedef BitCountN<16, uint16_t> BitCount16; cannam@148: typedef BitCountN<32, uint32_t> BitCount32; cannam@148: typedef BitCountN<64, uint64_t> BitCount64; cannam@148: typedef BitCountN BitCount; cannam@148: cannam@148: typedef ByteCountN<8, uint8_t> ByteCount8; cannam@148: typedef ByteCountN<16, uint16_t> ByteCount16; cannam@148: typedef ByteCountN<32, uint32_t> ByteCount32; cannam@148: typedef ByteCountN<64, uint64_t> ByteCount64; cannam@148: typedef ByteCountN ByteCount; cannam@148: cannam@148: typedef WordCountN<8, uint8_t> WordCount8; cannam@148: typedef WordCountN<16, uint16_t> WordCount16; cannam@148: typedef WordCountN<32, uint32_t> WordCount32; cannam@148: typedef WordCountN<64, uint64_t> WordCount64; cannam@148: typedef WordCountN WordCount; cannam@148: cannam@148: typedef ElementCountN<8, uint8_t> ElementCount8; cannam@148: typedef ElementCountN<16, uint16_t> ElementCount16; cannam@148: typedef ElementCountN<32, uint32_t> ElementCount32; cannam@148: typedef ElementCountN<64, uint64_t> ElementCount64; cannam@148: typedef ElementCountN ElementCount; cannam@148: cannam@148: typedef WirePointerCountN<8, uint8_t> WirePointerCount8; cannam@148: typedef WirePointerCountN<16, uint16_t> WirePointerCount16; cannam@148: typedef WirePointerCountN<32, uint32_t> WirePointerCount32; cannam@148: typedef WirePointerCountN<64, uint64_t> WirePointerCount64; cannam@148: typedef WirePointerCountN WirePointerCount; cannam@148: cannam@148: template cannam@148: using BitsPerElementN = decltype(BitCountN() / ElementCountN()); cannam@148: template cannam@148: using BytesPerElementN = decltype(ByteCountN() / ElementCountN()); cannam@148: template cannam@148: using WordsPerElementN = decltype(WordCountN() / ElementCountN()); cannam@148: template cannam@148: using PointersPerElementN = decltype(WirePointerCountN() / ElementCountN()); cannam@148: cannam@148: using kj::bounded; cannam@148: using kj::unbound; cannam@148: using kj::unboundAs; cannam@148: using kj::unboundMax; cannam@148: using kj::unboundMaxBits; cannam@148: using kj::assertMax; cannam@148: using kj::assertMaxBits; cannam@148: using kj::upgradeBound; cannam@148: using kj::ThrowOverflow; cannam@148: using kj::assumeBits; cannam@148: using kj::assumeMax; cannam@148: using kj::subtractChecked; cannam@148: using kj::trySubtract; cannam@148: cannam@148: template cannam@148: inline constexpr U* operator+(U* ptr, kj::Quantity offset) { cannam@148: return ptr + unbound(offset / kj::unit>()); cannam@148: } cannam@148: template cannam@148: inline constexpr const U* operator+(const U* ptr, kj::Quantity offset) { cannam@148: return ptr + unbound(offset / kj::unit>()); cannam@148: } cannam@148: template cannam@148: inline constexpr U* operator+=(U*& ptr, kj::Quantity offset) { cannam@148: return ptr = ptr + unbound(offset / kj::unit>()); cannam@148: } cannam@148: template cannam@148: inline constexpr const U* operator+=(const U*& ptr, kj::Quantity offset) { cannam@148: return ptr = ptr + unbound(offset / kj::unit>()); cannam@148: } cannam@148: cannam@148: template cannam@148: inline constexpr U* operator-(U* ptr, kj::Quantity offset) { cannam@148: return ptr - unbound(offset / kj::unit>()); cannam@148: } cannam@148: template cannam@148: inline constexpr const U* operator-(const U* ptr, kj::Quantity offset) { cannam@148: return ptr - unbound(offset / kj::unit>()); cannam@148: } cannam@148: template cannam@148: inline constexpr U* operator-=(U*& ptr, kj::Quantity offset) { cannam@148: return ptr = ptr - unbound(offset / kj::unit>()); cannam@148: } cannam@148: template cannam@148: inline constexpr const U* operator-=(const U*& ptr, kj::Quantity offset) { cannam@148: return ptr = ptr - unbound(offset / kj::unit>()); cannam@148: } cannam@148: cannam@148: constexpr auto BITS = kj::unit>(); cannam@148: constexpr auto BYTES = kj::unit>(); cannam@148: constexpr auto WORDS = kj::unit>(); cannam@148: constexpr auto ELEMENTS = kj::unit>(); cannam@148: constexpr auto POINTERS = kj::unit>(); cannam@148: cannam@148: constexpr auto ZERO = kj::bounded<0>(); cannam@148: constexpr auto ONE = kj::bounded<1>(); cannam@148: cannam@148: // GCC 4.7 actually gives unused warnings on these constants in opt mode... cannam@148: constexpr auto BITS_PER_BYTE KJ_UNUSED = bounded<8>() * BITS / BYTES; cannam@148: constexpr auto BITS_PER_WORD KJ_UNUSED = bounded<64>() * BITS / WORDS; cannam@148: constexpr auto BYTES_PER_WORD KJ_UNUSED = bounded<8>() * BYTES / WORDS; cannam@148: cannam@148: constexpr auto BITS_PER_POINTER KJ_UNUSED = bounded<64>() * BITS / POINTERS; cannam@148: constexpr auto BYTES_PER_POINTER KJ_UNUSED = bounded<8>() * BYTES / POINTERS; cannam@148: constexpr auto WORDS_PER_POINTER KJ_UNUSED = ONE * WORDS / POINTERS; cannam@148: cannam@148: constexpr auto POINTER_SIZE_IN_WORDS = ONE * POINTERS * WORDS_PER_POINTER; cannam@148: cannam@148: constexpr uint SEGMENT_WORD_COUNT_BITS = 29; // Number of words in a segment. cannam@148: constexpr uint LIST_ELEMENT_COUNT_BITS = 29; // Number of elements in a list. cannam@148: constexpr uint STRUCT_DATA_WORD_COUNT_BITS = 16; // Number of words in a Struct data section. cannam@148: constexpr uint STRUCT_POINTER_COUNT_BITS = 16; // Number of pointers in a Struct pointer section. cannam@148: constexpr uint BLOB_SIZE_BITS = 29; // Number of bytes in a blob. cannam@148: cannam@148: typedef WordCountN SegmentWordCount; cannam@148: typedef ElementCountN ListElementCount; cannam@148: typedef WordCountN StructDataWordCount; cannam@148: typedef WirePointerCountN StructPointerCount; cannam@148: typedef ByteCountN BlobSize; cannam@148: cannam@148: constexpr auto MAX_SEGMENT_WORDS = cannam@148: bounded()>() * WORDS; cannam@148: constexpr auto MAX_LIST_ELEMENTS = cannam@148: bounded()>() * ELEMENTS; cannam@148: constexpr auto MAX_STUCT_DATA_WORDS = cannam@148: bounded()>() * WORDS; cannam@148: constexpr auto MAX_STRUCT_POINTER_COUNT = cannam@148: bounded()>() * POINTERS; cannam@148: cannam@148: using StructDataBitCount = decltype(WordCountN() * BITS_PER_WORD); cannam@148: // Number of bits in a Struct data segment (should come out to BitCountN<22>). cannam@148: cannam@148: using StructDataOffset = decltype(StructDataBitCount() * (ONE * ELEMENTS / BITS)); cannam@148: using StructPointerOffset = StructPointerCount; cannam@148: // Type of a field offset. cannam@148: cannam@148: inline StructDataOffset assumeDataOffset(uint32_t offset) { cannam@148: return assumeMax(MAX_STUCT_DATA_WORDS * BITS_PER_WORD * (ONE * ELEMENTS / BITS), cannam@148: bounded(offset) * ELEMENTS); cannam@148: } cannam@148: cannam@148: inline StructPointerOffset assumePointerOffset(uint32_t offset) { cannam@148: return assumeMax(MAX_STRUCT_POINTER_COUNT, bounded(offset) * POINTERS); cannam@148: } cannam@148: cannam@148: constexpr uint MAX_TEXT_SIZE = kj::maxValueForBits() - 1; cannam@148: typedef kj::Quantity, byte> TextSize; cannam@148: // Not including NUL terminator. cannam@148: cannam@148: template cannam@148: inline KJ_CONSTEXPR() decltype(bounded() * BYTES / ELEMENTS) bytesPerElement() { cannam@148: return bounded() * BYTES / ELEMENTS; cannam@148: } cannam@148: cannam@148: template cannam@148: inline KJ_CONSTEXPR() decltype(bounded() * BITS / ELEMENTS) bitsPerElement() { cannam@148: return bounded() * BITS / ELEMENTS; cannam@148: } cannam@148: cannam@148: template cannam@148: inline constexpr kj::Quantity, T> cannam@148: intervalLength(const T* a, const T* b, kj::Quantity, T>) { cannam@148: return kj::assumeMax(b - a) * kj::unit, T>>(); cannam@148: } cannam@148: cannam@148: template cannam@148: inline constexpr kj::ArrayPtr arrayPtr(const U* ptr, kj::Quantity size) { cannam@148: return kj::ArrayPtr(ptr, unbound(size / kj::unit>())); cannam@148: } cannam@148: template cannam@148: inline constexpr kj::ArrayPtr arrayPtr(U* ptr, kj::Quantity size) { cannam@148: return kj::ArrayPtr(ptr, unbound(size / kj::unit>())); cannam@148: } cannam@148: cannam@148: #else cannam@148: cannam@148: template cannam@148: using BitCountN = T; cannam@148: template cannam@148: using ByteCountN = T; cannam@148: template cannam@148: using WordCountN = T; cannam@148: template cannam@148: using ElementCountN = T; cannam@148: template cannam@148: using WirePointerCountN = T; cannam@148: cannam@148: cannam@148: // XXX cannam@148: typedef BitCountN<8, uint8_t> BitCount8; cannam@148: typedef BitCountN<16, uint16_t> BitCount16; cannam@148: typedef BitCountN<32, uint32_t> BitCount32; cannam@148: typedef BitCountN<64, uint64_t> BitCount64; cannam@148: typedef BitCountN BitCount; cannam@148: cannam@148: typedef ByteCountN<8, uint8_t> ByteCount8; cannam@148: typedef ByteCountN<16, uint16_t> ByteCount16; cannam@148: typedef ByteCountN<32, uint32_t> ByteCount32; cannam@148: typedef ByteCountN<64, uint64_t> ByteCount64; cannam@148: typedef ByteCountN ByteCount; cannam@148: cannam@148: typedef WordCountN<8, uint8_t> WordCount8; cannam@148: typedef WordCountN<16, uint16_t> WordCount16; cannam@148: typedef WordCountN<32, uint32_t> WordCount32; cannam@148: typedef WordCountN<64, uint64_t> WordCount64; cannam@148: typedef WordCountN WordCount; cannam@148: cannam@148: typedef ElementCountN<8, uint8_t> ElementCount8; cannam@148: typedef ElementCountN<16, uint16_t> ElementCount16; cannam@148: typedef ElementCountN<32, uint32_t> ElementCount32; cannam@148: typedef ElementCountN<64, uint64_t> ElementCount64; cannam@148: typedef ElementCountN ElementCount; cannam@148: cannam@148: typedef WirePointerCountN<8, uint8_t> WirePointerCount8; cannam@148: typedef WirePointerCountN<16, uint16_t> WirePointerCount16; cannam@148: typedef WirePointerCountN<32, uint32_t> WirePointerCount32; cannam@148: typedef WirePointerCountN<64, uint64_t> WirePointerCount64; cannam@148: typedef WirePointerCountN WirePointerCount; cannam@148: cannam@148: template cannam@148: using BitsPerElementN = decltype(BitCountN() / ElementCountN()); cannam@148: template cannam@148: using BytesPerElementN = decltype(ByteCountN() / ElementCountN()); cannam@148: template cannam@148: using WordsPerElementN = decltype(WordCountN() / ElementCountN()); cannam@148: template cannam@148: using PointersPerElementN = decltype(WirePointerCountN() / ElementCountN()); cannam@148: cannam@148: using kj::ThrowOverflow; cannam@148: // YYY cannam@148: cannam@148: template inline constexpr uint bounded() { return i; } cannam@148: template inline constexpr T bounded(T i) { return i; } cannam@148: template inline constexpr T unbound(T i) { return i; } cannam@148: cannam@148: template inline constexpr T unboundAs(U i) { return i; } cannam@148: cannam@148: template inline constexpr uint unboundMax(T i) { return i; } cannam@148: template inline constexpr uint unboundMaxBits(T i) { return i; } cannam@148: cannam@148: template cannam@148: inline T assertMax(T value, ErrorFunc&& func) { cannam@148: if (KJ_UNLIKELY(value > newMax)) func(); cannam@148: return value; cannam@148: } cannam@148: cannam@148: template cannam@148: inline T assertMax(uint newMax, T value, ErrorFunc&& func) { cannam@148: if (KJ_UNLIKELY(value > newMax)) func(); cannam@148: return value; cannam@148: } cannam@148: cannam@148: template cannam@148: inline T assertMaxBits(T value, ErrorFunc&& func = ErrorFunc()) { cannam@148: if (KJ_UNLIKELY(value > kj::maxValueForBits())) func(); cannam@148: return value; cannam@148: } cannam@148: cannam@148: template cannam@148: inline T assertMaxBits(uint bits, T value, ErrorFunc&& func = ErrorFunc()) { cannam@148: if (KJ_UNLIKELY(value > (1ull << bits) - 1)) func(); cannam@148: return value; cannam@148: } cannam@148: cannam@148: template inline constexpr T upgradeBound(U i) { return i; } cannam@148: cannam@148: template inline constexpr T assumeBits(T i) { return i; } cannam@148: template inline constexpr T assumeMax(T i) { return i; } cannam@148: cannam@148: template cannam@148: inline auto subtractChecked(T a, U b, ErrorFunc&& errorFunc = ErrorFunc()) cannam@148: -> decltype(a - b) { cannam@148: if (b > a) errorFunc(); cannam@148: return a - b; cannam@148: } cannam@148: cannam@148: template cannam@148: inline auto trySubtract(T a, U b) -> kj::Maybe { cannam@148: if (b > a) { cannam@148: return nullptr; cannam@148: } else { cannam@148: return a - b; cannam@148: } cannam@148: } cannam@148: cannam@148: constexpr uint BITS = 1; cannam@148: constexpr uint BYTES = 1; cannam@148: constexpr uint WORDS = 1; cannam@148: constexpr uint ELEMENTS = 1; cannam@148: constexpr uint POINTERS = 1; cannam@148: cannam@148: constexpr uint ZERO = 0; cannam@148: constexpr uint ONE = 1; cannam@148: cannam@148: // GCC 4.7 actually gives unused warnings on these constants in opt mode... cannam@148: constexpr uint BITS_PER_BYTE KJ_UNUSED = 8; cannam@148: constexpr uint BITS_PER_WORD KJ_UNUSED = 64; cannam@148: constexpr uint BYTES_PER_WORD KJ_UNUSED = 8; cannam@148: cannam@148: constexpr uint BITS_PER_POINTER KJ_UNUSED = 64; cannam@148: constexpr uint BYTES_PER_POINTER KJ_UNUSED = 8; cannam@148: constexpr uint WORDS_PER_POINTER KJ_UNUSED = 1; cannam@148: cannam@148: // XXX cannam@148: constexpr uint POINTER_SIZE_IN_WORDS = ONE * POINTERS * WORDS_PER_POINTER; cannam@148: cannam@148: constexpr uint SEGMENT_WORD_COUNT_BITS = 29; // Number of words in a segment. cannam@148: constexpr uint LIST_ELEMENT_COUNT_BITS = 29; // Number of elements in a list. cannam@148: constexpr uint STRUCT_DATA_WORD_COUNT_BITS = 16; // Number of words in a Struct data section. cannam@148: constexpr uint STRUCT_POINTER_COUNT_BITS = 16; // Number of pointers in a Struct pointer section. cannam@148: constexpr uint BLOB_SIZE_BITS = 29; // Number of bytes in a blob. cannam@148: cannam@148: typedef WordCountN SegmentWordCount; cannam@148: typedef ElementCountN ListElementCount; cannam@148: typedef WordCountN StructDataWordCount; cannam@148: typedef WirePointerCountN StructPointerCount; cannam@148: typedef ByteCountN BlobSize; cannam@148: // YYY cannam@148: cannam@148: constexpr auto MAX_SEGMENT_WORDS = kj::maxValueForBits(); cannam@148: constexpr auto MAX_LIST_ELEMENTS = kj::maxValueForBits(); cannam@148: constexpr auto MAX_STUCT_DATA_WORDS = kj::maxValueForBits(); cannam@148: constexpr auto MAX_STRUCT_POINTER_COUNT = kj::maxValueForBits(); cannam@148: cannam@148: typedef uint StructDataBitCount; cannam@148: typedef uint StructDataOffset; cannam@148: typedef uint StructPointerOffset; cannam@148: cannam@148: inline StructDataOffset assumeDataOffset(uint32_t offset) { return offset; } cannam@148: inline StructPointerOffset assumePointerOffset(uint32_t offset) { return offset; } cannam@148: cannam@148: constexpr uint MAX_TEXT_SIZE = kj::maxValueForBits() - 1; cannam@148: typedef uint TextSize; cannam@148: cannam@148: template cannam@148: inline KJ_CONSTEXPR() size_t bytesPerElement() { return sizeof(T); } cannam@148: cannam@148: template cannam@148: inline KJ_CONSTEXPR() size_t bitsPerElement() { return sizeof(T) * 8; } cannam@148: cannam@148: template cannam@148: inline constexpr ptrdiff_t intervalLength(const T* a, const T* b, uint) { cannam@148: return b - a; cannam@148: } cannam@148: cannam@148: template cannam@148: inline constexpr kj::ArrayPtr arrayPtr(const U* ptr, T size) { cannam@148: return kj::arrayPtr(ptr, size); cannam@148: } cannam@148: template cannam@148: inline constexpr kj::ArrayPtr arrayPtr(U* ptr, T size) { cannam@148: return kj::arrayPtr(ptr, size); cannam@148: } cannam@148: cannam@148: #endif cannam@148: cannam@148: } // namespace capnp cannam@148: cannam@148: #endif // CAPNP_COMMON_H_