From e63e17a761e2b0c7c9ff34921d0e4ae95a5bf977 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Thu, 11 Jan 2018 15:55:29 -0800 Subject: [PATCH] compact_*: support mempool allocated containers Signed-off-by: Patrick Donnelly --- src/include/compact_map.h | 62 ++++++++++++++++++++------------------- src/include/compact_set.h | 48 ++++++++++++++---------------- src/include/frag.h | 6 ++-- src/include/mempool.h | 9 ++++++ 4 files changed, 66 insertions(+), 59 deletions(-) diff --git a/src/include/compact_map.h b/src/include/compact_map.h index 1fa286e89843b..52a7792adc8e0 100644 --- a/src/include/compact_map.h +++ b/src/include/compact_map.h @@ -12,23 +12,24 @@ #ifndef CEPH_COMPACT_MAP_H #define CEPH_COMPACT_MAP_H +#include "buffer.h" +#include "encoding.h" + #include +#include #include "include/encoding.h" template class compact_map_base { protected: - Map *map; + std::unique_ptr map; void alloc_internal() { if (!map) - map = new Map; + map.reset(new Map); } void free_internal() { - if (map) { - delete map; - map = 0; - } + map.reset(); } template class const_iterator_base { @@ -156,14 +157,14 @@ public: const_reverse_iterator(const compact_map_base* m, const typename Map::const_reverse_iterator& i) : const_iterator_base(m, i) { } }; - compact_map_base() : map(0) {} - compact_map_base(const compact_map_base& o) : map(0) { + compact_map_base(const compact_map_base& o) { if (o.map) { alloc_internal(); *map = *o.map; } } - ~compact_map_base() { delete map; } + compact_map_base() {} + ~compact_map_base() {} bool empty() const { return !map || map->empty(); @@ -184,8 +185,9 @@ public: if (map) { assert(this == p.map); map->erase(p.it); - if (map->empty()) - free_internal(); + if (map->empty()) { + free_internal(); + } } } size_t erase (const Key& k) { @@ -200,9 +202,7 @@ public: free_internal(); } void swap(compact_map_base& o) { - Map *tmp = map; - map = o.map; - o.map = tmp; + map.swap(o.map); } compact_map_base& operator=(const compact_map_base& o) { if (o.map) { @@ -327,8 +327,8 @@ inline void decode(compact_map_base& m, bufferlist::iterator& p) { m.decode(p); } -template -class compact_map : public compact_map_base > { +template , class Alloc = std::allocator< std::pair > > +class compact_map : public compact_map_base > { public: T& operator[](const Key& k) { this->alloc_internal(); @@ -336,33 +336,35 @@ public: } }; -template -inline std::ostream& operator<<(std::ostream& out, const compact_map& m) +template , class Alloc = std::allocator< std::pair > > +inline std::ostream& operator<<(std::ostream& out, const compact_map& m) { out << "{"; - for (typename compact_map::const_iterator it = m.begin(); - it != m.end(); - ++it) { - if (it != m.begin()) + bool first = true; + for (const auto &p : m) { + if (!first) out << ","; - out << it->first << "=" << it->second; + out << p.first << "=" << p.second; + first = false; } out << "}"; return out; } -template -class compact_multimap : public compact_map_base > { +template , class Alloc = std::allocator< std::pair > > +class compact_multimap : public compact_map_base > { }; -template -inline std::ostream& operator<<(std::ostream& out, const compact_multimap& m) +template , class Alloc = std::allocator< std::pair > > +inline std::ostream& operator<<(std::ostream& out, const compact_multimap& m) { out << "{{"; - for (typename compact_map::const_iterator it = m.begin(); !it.end(); ++it) { - if (it != m.begin()) + bool first = true; + for (const auto &p : m) { + if (!first) out << ","; - out << it->first << "=" << it->second; + out << p.first << "=" << p.second; + first = false; } out << "}}"; return out; diff --git a/src/include/compact_set.h b/src/include/compact_set.h index dcbeec12a3e68..dd2ddd31165cd 100644 --- a/src/include/compact_set.h +++ b/src/include/compact_set.h @@ -12,25 +12,22 @@ #ifndef CEPH_COMPACT_SET_H #define CEPH_COMPACT_SET_H -#include - -#include "include/buffer.h" -#include "include/encoding.h" +#include "buffer.h" +#include "encoding.h" +#include +#include template class compact_set_base { protected: - Set *set; + std::unique_ptr set; void alloc_internal() { if (!set) - set = new Set; + set.reset(new Set); } void free_internal() { - if (set) { - delete set; - set = 0; - } + set.reset(); } template class iterator_base { @@ -118,14 +115,14 @@ public: } }; - compact_set_base() : set(0) {} - compact_set_base(const compact_set_base& o) : set(0) { + compact_set_base() {} + compact_set_base(const compact_set_base& o) { if (o.set) { alloc_internal(); *set = *o.set; } } - ~compact_set_base() { delete set; } + ~compact_set_base() {} bool empty() const { @@ -148,7 +145,7 @@ public: assert(this == p.set); set->erase(p.it); if (set->empty()) - free_internal(); + free_internal(); } } size_t erase (const T& t) { @@ -156,16 +153,14 @@ public: return 0; size_t r = set->erase(t); if (set->empty()) - free_internal(); + free_internal(); return r; } void clear() { free_internal(); } void swap(compact_set_base& o) { - Set *tmp = set; - set = o.set; - o.set = tmp; + set.swap(o.set); } compact_set_base& operator=(const compact_set_base& o) { if (o.set) { @@ -278,18 +273,19 @@ inline void decode(compact_set_base& m, bufferlist::iterator& p) { m.decode(p); } -template -class compact_set : public compact_set_base > { +template , class Alloc = std::allocator > +class compact_set : public compact_set_base > { }; -template -inline std::ostream& operator<<(std::ostream& out, const compact_set& s) +template , class Alloc = std::allocator > +inline std::ostream& operator<<(std::ostream& out, const compact_set& s) { - for (typename compact_set::const_iterator it = s.begin(); - it != s.end(); ++it) { - if (it != s.begin()) + bool first = true; + for (auto &v : s) { + if (!first) out << ","; - out << it->first << "=" << it->second; + out << v; + first = false; } return out; } diff --git a/src/include/frag.h b/src/include/frag.h index 0b7977acf1d09..4ca46cd03f599 100644 --- a/src/include/frag.h +++ b/src/include/frag.h @@ -163,10 +163,10 @@ inline std::ostream& operator<<(std::ostream& out, const frag_t& hb) return out << '*'; } -inline void encode(frag_t f, bufferlist& bl) { encode_raw(f._enc, bl); } -inline void decode(frag_t &f, bufferlist::iterator& p) { +inline void encode(const frag_t &f, bufferlist& bl) { encode_raw(f._enc, bl); } +inline void decode(frag_t &f, bufferlist::iterator& p) { __u32 v; - decode_raw(v, p); + decode_raw(v, p); f._enc = v; } diff --git a/src/include/mempool.h b/src/include/mempool.h index 99dbcc9738fcd..60f6326d357da 100644 --- a/src/include/mempool.h +++ b/src/include/mempool.h @@ -29,6 +29,8 @@ #include #include "include/assert.h" +#include "include/compact_map.h" +#include "include/compact_set.h" /* @@ -399,6 +401,13 @@ public: using map = std::map>>; \ \ + template > \ + using compact_map = compact_map>>; \ + \ + template > \ + using compact_set = compact_set>; \ + \ template > \ using multimap = std::multimap