From eef17d68ad6ef2e35f95c134376cf72b9f327676 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 5 Jul 2020 15:41:10 +0800 Subject: [PATCH] include/interval_set: use template as the 2nd template parameter to avoid repeating the unit for the interval in the internal map's parameter. Signed-off-by: Kefu Chai --- src/include/interval_set.h | 28 ++++++++++++++++------------ src/os/bluestore/StupidAllocator.h | 9 +++++---- src/osd/osd_types.h | 2 +- src/test/common/test_interval_set.cc | 6 ++---- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/include/interval_set.h b/src/include/interval_set.h index 3090ef9570f..5ff065cd55b 100644 --- a/src/include/interval_set.h +++ b/src/include/interval_set.h @@ -30,9 +30,10 @@ * flat_map and btree_map). */ -template> +template class C = std::map> class interval_set { public: + using Map = C; 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 -struct denc_traits> { +template class C> +struct denc_traits> { +private: + using container_t = interval_set; +public: static constexpr bool supported = true; static constexpr bool bounded = false; static constexpr bool featured = false; - static constexpr bool need_contiguous = denc_traits::need_contiguous; - static void bound_encode(const interval_set& v, size_t& p) { + static constexpr bool need_contiguous = denc_traits>::need_contiguous; + static void bound_encode(const container_t& v, size_t& p) { v.bound_encode(p); } - static void encode(const interval_set& v, + static void encode(const container_t& v, ceph::buffer::list::contiguous_appender& p) { v.encode(p); } - static void decode(interval_set& v, ceph::buffer::ptr::const_iterator& p) { + static void decode(container_t& v, ceph::buffer::ptr::const_iterator& p) { v.decode(p); } template static typename std::enable_if::type - decode(interval_set& 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& 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& v, + static void decode_nohead(size_t n, container_t& v, ceph::buffer::ptr::const_iterator& p) { v.decode_nohead(n, p); } }; -template -inline std::ostream& operator<<(std::ostream& out, const interval_set &s) { +template class C> +inline std::ostream& operator<<(std::ostream& out, const interval_set &s) { out << "["; bool first = true; for (const auto& [start, len] : s) { diff --git a/src/os/bluestore/StupidAllocator.h b/src/os/bluestore/StupidAllocator.h index a272d88daa0..4139de81b60 100644 --- a/src/os/bluestore/StupidAllocator.h +++ b/src/os/bluestore/StupidAllocator.h @@ -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> allocator_t; - typedef btree::btree_map,allocator_t> interval_set_map_t; - typedef interval_set interval_set_t; + template using allocator_t = + mempool::bluestore_alloc::pool_allocator>; + template using btree_map_t = + btree::btree_map, allocator_t>; + using interval_set_t = interval_set; std::vector free; ///< leading-edge copy uint64_t last_alloc = 0; diff --git a/src/osd/osd_types.h b/src/osd/osd_types.h index fc8426802d3..e21cc8c9a70 100644 --- a/src/osd/osd_types.h +++ b/src/osd/osd_types.h @@ -142,7 +142,7 @@ void dump(ceph::Formatter* f, const osd_alerts_t& alerts); typedef interval_set< snapid_t, - mempool::osdmap::flat_map> snap_interval_set_t; + mempool::osdmap::flat_map> snap_interval_set_t; /** diff --git a/src/test/common/test_interval_set.cc b/src/test/common/test_interval_set.cc index 27b80fea7a0..7eb1dcbe24c 100644 --- a/src/test/common/test_interval_set.cc +++ b/src/test/common/test_interval_set.cc @@ -32,10 +32,8 @@ class IntervalSetTest : public ::testing::Test { typedef ::testing::Types< interval_set, - interval_set>, - interval_set> + interval_set, + interval_set > IntervalSetTypes; TYPED_TEST_SUITE(IntervalSetTest, IntervalSetTypes); -- 2.39.5