]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
include/interval_set: use template as the 2nd template parameter
authorKefu Chai <kchai@redhat.com>
Sun, 5 Jul 2020 07:41:10 +0000 (15:41 +0800)
committerKefu Chai <kchai@redhat.com>
Sun, 5 Jul 2020 13:23:01 +0000 (21:23 +0800)
to avoid repeating the unit for the interval in the internal map's
parameter.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/include/interval_set.h
src/os/bluestore/StupidAllocator.h
src/osd/osd_types.h
src/test/common/test_interval_set.cc

index 3090ef9570f55f68a775df47800236a981b18c41..5ff065cd55b21e3254433e51e067dd08150bde7c 100644 (file)
  * flat_map and btree_map).
  */
 
-template<typename T, typename Map = std::map<T,T>>
+template<typename T, template<typename, typename, typename ...> class C = std::map>
 class interval_set {
  public:
+  using Map = C<T, T>;
   using value_type = typename Map::value_type;
   using offset_type = T;
   using length_type = T;
@@ -730,40 +731,43 @@ private:
 
 // declare traits explicitly because (1) it's templatized, and (2) we
 // want to include _nohead variants.
-template<typename T, typename Map>
-struct denc_traits<interval_set<T,Map>> {
+template<typename T, template<typename, typename, typename ...> class C>
+struct denc_traits<interval_set<T, C>> {
+private:
+  using container_t = interval_set<T, C>;
+public:
   static constexpr bool supported = true;
   static constexpr bool bounded = false;
   static constexpr bool featured = false;
-  static constexpr bool need_contiguous = denc_traits<T,Map>::need_contiguous;
-  static void bound_encode(const interval_set<T,Map>& v, size_t& p) {
+  static constexpr bool need_contiguous = denc_traits<T, C<T,T>>::need_contiguous;
+  static void bound_encode(const container_t& v, size_t& p) {
     v.bound_encode(p);
   }
-  static void encode(const interval_set<T,Map>& v,
+  static void encode(const container_t& v,
                     ceph::buffer::list::contiguous_appender& p) {
     v.encode(p);
   }
-  static void decode(interval_set<T,Map>& v, ceph::buffer::ptr::const_iterator& p) {
+  static void decode(container_t& v, ceph::buffer::ptr::const_iterator& p) {
     v.decode(p);
   }
   template<typename U=T>
     static typename std::enable_if<sizeof(U) && !need_contiguous>::type
-  decode(interval_set<T,Map>& v, ceph::buffer::list::iterator& p) {
+  decode(container_t& v, ceph::buffer::list::iterator& p) {
     v.decode(p);
   }
-  static void encode_nohead(const interval_set<T,Map>& v,
+  static void encode_nohead(const container_t& v,
                            ceph::buffer::list::contiguous_appender& p) {
     v.encode_nohead(p);
   }
-  static void decode_nohead(size_t n, interval_set<T,Map>& v,
+  static void decode_nohead(size_t n, container_t& v,
                            ceph::buffer::ptr::const_iterator& p) {
     v.decode_nohead(n, p);
   }
 };
 
 
-template<class T, typename Map>
-inline std::ostream& operator<<(std::ostream& out, const interval_set<T,Map> &s) {
+template<typename T, template<typename, typename, typename ...> class C>
+inline std::ostream& operator<<(std::ostream& out, const interval_set<T,C> &s) {
   out << "[";
   bool first = true;
   for (const auto& [start, len] : s) {
index a272d88daa01041636cb2d9df3a162447f56a45d..4139de81b60344ba17ea10a5495158e68c04f304 100644 (file)
@@ -20,10 +20,11 @@ class StupidAllocator : public Allocator {
   int64_t num_free;     ///< total bytes in freelist
   int64_t block_size;
 
-  typedef mempool::bluestore_alloc::pool_allocator<
-    std::pair<const uint64_t,uint64_t>> allocator_t;
-  typedef btree::btree_map<uint64_t,uint64_t,std::less<uint64_t>,allocator_t> interval_set_map_t;
-  typedef interval_set<uint64_t,interval_set_map_t> interval_set_t;
+  template <typename K, typename V> using allocator_t =
+    mempool::bluestore_alloc::pool_allocator<std::pair<const K, V>>;
+  template <typename K, typename V> using btree_map_t =
+    btree::btree_map<K, V, std::less<K>, allocator_t<K, V>>;
+  using interval_set_t = interval_set<uint64_t, btree_map_t>;
   std::vector<interval_set_t> free;  ///< leading-edge copy
 
   uint64_t last_alloc = 0;
index fc8426802d3802a5751878c58b15ea68ea4bb67c..e21cc8c9a7097e23622ffbef7ce58840b38c680b 100644 (file)
@@ -142,7 +142,7 @@ void dump(ceph::Formatter* f, const osd_alerts_t& alerts);
 
 typedef interval_set<
   snapid_t,
-  mempool::osdmap::flat_map<snapid_t,snapid_t>> snap_interval_set_t;
+  mempool::osdmap::flat_map> snap_interval_set_t;
 
 
 /**
index 27b80fea7a00f7efa05770977ff39d3ae0a9c1fb..7eb1dcbe24c09c3f8b3065805386bd4003a498ef 100644 (file)
@@ -32,10 +32,8 @@ class IntervalSetTest : public ::testing::Test {
 
 typedef ::testing::Types<
   interval_set<IntervalValueType>,
-  interval_set<IntervalValueType,
-              btree::btree_map<IntervalValueType,IntervalValueType>>,
-  interval_set<IntervalValueType,
-              boost::container::flat_map<IntervalValueType,IntervalValueType>>
+  interval_set<IntervalValueType, btree::btree_map>,
+  interval_set<IntervalValueType, boost::container::flat_map>
   > IntervalSetTypes;
 
 TYPED_TEST_SUITE(IntervalSetTest, IntervalSetTypes);