// Copyright 2021 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef GRPC_SRC_CORE_LIB_GPRPP_BITSET_H #define GRPC_SRC_CORE_LIB_GPRPP_BITSET_H #include <grpc/support/port_platform.h> #include <stddef.h> #include <stdint.h> #include <type_traits> #include "src/core/lib/gpr/useful.h" namespace grpc_core { // Given a bit count as an integer, vend as member type `Type` a type with // exactly that number of bits. Undefined if that bit count is not available. template <size_t kBits> struct UintSelector; template <> struct UintSelector<8> { … }; template <> struct UintSelector<16> { … }; template <> struct UintSelector<32> { … }; template <> struct UintSelector<64> { … }; // An unsigned integer of some number of bits. Uint; // Given the total number of bits that need to be stored, choose the size of // 'unit' for a BitSet... We'll use an array of units to store the total set. // For small bit counts we are selective in the type to try and balance byte // size and performance // - the details will likely be tweaked into the future. // Once we get over 96 bits, we just use uint64_t for everything. constexpr size_t ChooseUnitBitsForBitSet(size_t total_bits) { … } // A BitSet that's configurable. // Contains storage for kTotalBits, stored as an array of integers of size // kUnitBits. e.g. to store 72 bits in 8 bit chunks, we'd say BitSet<72, 8>. // Since most users shouldn't care about the size of unit used, we default // kUnitBits to whatever is selected by ChooseUnitBitsForBitSet template <size_t kTotalBits, size_t kUnitBits = ChooseUnitBitsForBitSet(kTotalBits)> class BitSet { … }; // Zero-size specialization of BitSet. // Useful for generic programming. // Make a compile time error out of get/set type accesses, and hard-codes // queries that do make sense. template <> class BitSet<0> { … }; } // namespace grpc_core #endif // GRPC_SRC_CORE_LIB_GPRPP_BITSET_H