]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mempool: add operator== for std::vector/mempool::vector comparison
authorKefu Chai <kchai@redhat.com>
Sun, 1 Oct 2017 06:36:30 +0000 (14:36 +0800)
committerKefu Chai <kchai@redhat.com>
Sun, 1 Oct 2017 07:09:03 +0000 (15:09 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/include/mempool.h

index 2cd61ad2afc3453d40fd875c78e89c93ea986d93..e4c8c32b68b06508af88f4e09368c360d68dd1e5 100644 (file)
@@ -431,7 +431,40 @@ DEFINE_MEMORY_POOLS_HELPER(P)
 
 };
 
-
+// the elements allocated by mempool is in the same memory space as the ones
+// allocated by the default allocator. so compare them in an efficient way:
+// libstdc++'s std::equal is specialized to use memcmp if T is integer or
+// pointer. this is good enough for our usecase. use
+// std::is_trivially_copyable<T> to expand the support to more types if
+// nececssary.
+template<typename T, mempool::pool_index_t pool_index>
+bool operator==(const std::vector<T, std::allocator<T>>& lhs,
+               const std::vector<T, mempool::pool_allocator<pool_index, T>>& rhs)
+{
+  return (lhs.size() == rhs.size() &&
+         std::equal(lhs.begin(), lhs.end(), rhs.begin()));
+}
+
+template<typename T, mempool::pool_index_t pool_index>
+bool operator!=(const std::vector<T, std::allocator<T>>& lhs,
+               const std::vector<T, mempool::pool_allocator<pool_index, T>>& rhs)
+{
+  return !(lhs == rhs);
+}
+
+template<typename T, mempool::pool_index_t pool_index>
+bool operator==(const std::vector<T, mempool::pool_allocator<pool_index, T>>& lhs,
+               const std::vector<T, std::allocator<T>>& rhs)
+{
+  return rhs == lhs;
+}
+
+template<typename T, mempool::pool_index_t pool_index>
+bool operator!=(const std::vector<T, mempool::pool_allocator<pool_index, T>>& lhs,
+               const std::vector<T, std::allocator<T>>& rhs)
+{
+  return !(lhs == rhs);
+}
 
 // Use this for any type that is contained by a container (unless it
 // is a class you defined; see below).