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