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