From 70e4591214f3b5b7da4a8c0a17b71d7c69aa4aa1 Mon Sep 17 00:00:00 2001 From: Dominik Laskowski Date: Mon, 18 Apr 2022 08:12:19 -0700 Subject: FTL: Fix SmallMap::try_emplace SmallVector::emplace_back returns a reference, whereas SmallVector::emplace_back returns an iterator (because its API must be compatible with StaticVector). Bug: 185536303 Test: ftl_test Change-Id: I6bdcd983c0bb2d2a326f79906997f85e89d7df44 --- include/ftl/small_map.h | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'include/ftl') diff --git a/include/ftl/small_map.h b/include/ftl/small_map.h index 49cde7fedc..83d5967464 100644 --- a/include/ftl/small_map.h +++ b/include/ftl/small_map.h @@ -107,12 +107,20 @@ class SmallMap final { template SmallMap(SmallMap other) : map_(std::move(other.map_)) {} + static constexpr size_type static_capacity() { return N; } + size_type max_size() const { return map_.max_size(); } size_type size() const { return map_.size(); } bool empty() const { return map_.empty(); } // Returns whether the map is backed by static or dynamic storage. - bool dynamic() const { return map_.dynamic(); } + bool dynamic() const { + if constexpr (static_capacity() > 0) { + return map_.dynamic(); + } else { + return true; + } + } iterator begin() { return map_.begin(); } const_iterator begin() const { return cbegin(); } @@ -171,9 +179,15 @@ class SmallMap final { return {it, false}; } - auto& ref = map_.emplace_back(std::piecewise_construct, std::forward_as_tuple(key), - std::forward_as_tuple(std::forward(args)...)); - return {&ref, true}; + decltype(auto) ref_or_it = + map_.emplace_back(std::piecewise_construct, std::forward_as_tuple(key), + std::forward_as_tuple(std::forward(args)...)); + + if constexpr (static_capacity() > 0) { + return {&ref_or_it, true}; + } else { + return {ref_or_it, true}; + } } // Replaces a mapping if it exists, and returns an iterator to it. Returns the end() iterator -- cgit v1.2.3-59-g8ed1b From d7629e7fc637efc2b5c9ad6f61ba5d96b40f8d16 Mon Sep 17 00:00:00 2001 From: Dominik Laskowski Date: Mon, 18 Apr 2022 08:25:04 -0700 Subject: FTL: Silence __VA_ARGS__ warning with empty arg Bug: 185536303 Test: ftl_test Change-Id: I3fc52c3dc3904ace7822200ca6c0f9ac39df8fe2 --- include/ftl/fake_guard.h | 3 +-- include/ftl/small_vector.h | 19 +++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) (limited to 'include/ftl') diff --git a/include/ftl/fake_guard.h b/include/ftl/fake_guard.h index bacd1b29ef..e6012516fc 100644 --- a/include/ftl/fake_guard.h +++ b/include/ftl/fake_guard.h @@ -85,6 +85,5 @@ struct [[clang::scoped_lockable]] FakeGuard final { #define FTL_MAKE_FAKE_GUARD(arg1, arg2, guard, ...) guard -// The void argument suppresses a warning about zero variadic macro arguments. #define FTL_FAKE_GUARD(...) \ - FTL_MAKE_FAKE_GUARD(__VA_ARGS__, FTL_FAKE_GUARD2, FTL_FAKE_GUARD1, void)(__VA_ARGS__) + FTL_MAKE_FAKE_GUARD(__VA_ARGS__, FTL_FAKE_GUARD2, FTL_FAKE_GUARD1, )(__VA_ARGS__) diff --git a/include/ftl/small_vector.h b/include/ftl/small_vector.h index 11294c3ac8..43e9fac5e2 100644 --- a/include/ftl/small_vector.h +++ b/include/ftl/small_vector.h @@ -124,30 +124,29 @@ class SmallVector final : details::ArrayTraits, details::ArrayComparators, details::ArrayComparators