Commit 49be2e68 by Evan Brown Committed by Copybara-Service

Move CountingAllocator into test_allocator.h and add some other allocators that…

Move CountingAllocator into test_allocator.h and add some other allocators that can be shared between different container tests.

PiperOrigin-RevId: 565693736
Change-Id: I59af987e30da03a805ce59ff0fb7eeae3fc08293
parent e68f1412
...@@ -75,7 +75,6 @@ set(ABSL_INTERNAL_DLL_FILES ...@@ -75,7 +75,6 @@ set(ABSL_INTERNAL_DLL_FILES
"container/internal/common_policy_traits.h" "container/internal/common_policy_traits.h"
"container/internal/compressed_tuple.h" "container/internal/compressed_tuple.h"
"container/internal/container_memory.h" "container/internal/container_memory.h"
"container/internal/counting_allocator.h"
"container/internal/hash_function_defaults.h" "container/internal/hash_function_defaults.h"
"container/internal/hash_policy_traits.h" "container/internal/hash_policy_traits.h"
"container/internal/hashtable_debug.h" "container/internal/hashtable_debug.h"
......
...@@ -73,8 +73,8 @@ cc_test( ...@@ -73,8 +73,8 @@ cc_test(
copts = ABSL_TEST_COPTS, copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [ deps = [
":counting_allocator",
":fixed_array", ":fixed_array",
":test_allocator",
"//absl/base:config", "//absl/base:config",
"//absl/base:exception_testing", "//absl/base:exception_testing",
"//absl/hash:hash_testing", "//absl/hash:hash_testing",
...@@ -139,9 +139,9 @@ cc_library( ...@@ -139,9 +139,9 @@ cc_library(
) )
cc_library( cc_library(
name = "counting_allocator", name = "test_allocator",
testonly = 1, testonly = 1,
hdrs = ["internal/counting_allocator.h"], hdrs = ["internal/test_allocator.h"],
copts = ABSL_DEFAULT_COPTS, copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
...@@ -154,8 +154,8 @@ cc_test( ...@@ -154,8 +154,8 @@ cc_test(
copts = ABSL_TEST_COPTS, copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [ deps = [
":counting_allocator",
":inlined_vector", ":inlined_vector",
":test_allocator",
":test_instance_tracker", ":test_instance_tracker",
"//absl/base:config", "//absl/base:config",
"//absl/base:core_headers", "//absl/base:core_headers",
...@@ -656,6 +656,7 @@ cc_test( ...@@ -656,6 +656,7 @@ cc_test(
":hash_policy_testing", ":hash_policy_testing",
":hashtable_debug", ":hashtable_debug",
":raw_hash_set", ":raw_hash_set",
":test_allocator",
"//absl/base", "//absl/base",
"//absl/base:config", "//absl/base:config",
"//absl/base:core_headers", "//absl/base:core_headers",
...@@ -993,7 +994,7 @@ cc_test( ...@@ -993,7 +994,7 @@ cc_test(
deps = [ deps = [
":btree", ":btree",
":btree_test_common", ":btree_test_common",
":counting_allocator", ":test_allocator",
":test_instance_tracker", ":test_instance_tracker",
"//absl/algorithm:container", "//absl/algorithm:container",
"//absl/base:core_headers", "//absl/base:core_headers",
......
...@@ -77,13 +77,13 @@ absl_cc_test( ...@@ -77,13 +77,13 @@ absl_cc_test(
absl::btree_test_common absl::btree_test_common
absl::compare absl::compare
absl::core_headers absl::core_headers
absl::counting_allocator
absl::flags absl::flags
absl::hash_testing absl::hash_testing
absl::optional absl::optional
absl::random_random absl::random_random
absl::raw_logging_internal absl::raw_logging_internal
absl::strings absl::strings
absl::test_allocator
absl::test_instance_tracker absl::test_instance_tracker
GTest::gmock_main GTest::gmock_main
) )
...@@ -145,11 +145,11 @@ absl_cc_test( ...@@ -145,11 +145,11 @@ absl_cc_test(
${ABSL_TEST_COPTS} ${ABSL_TEST_COPTS}
DEPS DEPS
absl::fixed_array absl::fixed_array
absl::counting_allocator
absl::config absl::config
absl::exception_testing absl::exception_testing
absl::hash_testing absl::hash_testing
absl::memory absl::memory
absl::test_allocator
GTest::gmock_main GTest::gmock_main
) )
...@@ -204,9 +204,9 @@ absl_cc_library( ...@@ -204,9 +204,9 @@ absl_cc_library(
# Internal-only target, do not depend on directly. # Internal-only target, do not depend on directly.
absl_cc_library( absl_cc_library(
NAME NAME
counting_allocator test_allocator
HDRS HDRS
"internal/counting_allocator.h" "internal/test_allocator.h"
COPTS COPTS
${ABSL_DEFAULT_COPTS} ${ABSL_DEFAULT_COPTS}
DEPS DEPS
...@@ -224,12 +224,12 @@ absl_cc_test( ...@@ -224,12 +224,12 @@ absl_cc_test(
absl::check absl::check
absl::config absl::config
absl::core_headers absl::core_headers
absl::counting_allocator
absl::exception_testing absl::exception_testing
absl::hash_testing absl::hash_testing
absl::inlined_vector absl::inlined_vector
absl::memory absl::memory
absl::strings absl::strings
absl::test_allocator
absl::test_instance_tracker absl::test_instance_tracker
GTest::gmock_main GTest::gmock_main
) )
...@@ -747,6 +747,7 @@ absl_cc_test( ...@@ -747,6 +747,7 @@ absl_cc_test(
absl::prefetch absl::prefetch
absl::raw_hash_set absl::raw_hash_set
absl::strings absl::strings
absl::test_allocator
GTest::gmock_main GTest::gmock_main
) )
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
#include "absl/base/macros.h" #include "absl/base/macros.h"
#include "absl/container/btree_map.h" #include "absl/container/btree_map.h"
#include "absl/container/btree_set.h" #include "absl/container/btree_set.h"
#include "absl/container/internal/counting_allocator.h" #include "absl/container/internal/test_allocator.h"
#include "absl/container/internal/test_instance_tracker.h" #include "absl/container/internal/test_instance_tracker.h"
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#include "absl/hash/hash_testing.h" #include "absl/hash/hash_testing.h"
...@@ -667,25 +667,10 @@ void BtreeMultiTest() { ...@@ -667,25 +667,10 @@ void BtreeMultiTest() {
DoTest("identical: ", &container, identical_values); DoTest("identical: ", &container, identical_values);
} }
template <typename T> // TODO(ezb): get rid of BtreeAllocatorTest and replace with test cases using
struct PropagatingCountingAlloc : public CountingAllocator<T> { // specific propagating allocs (e.g. CopyAssignPropagatingCountingAlloc) and
using propagate_on_container_copy_assignment = std::true_type; // also a test for MinimumAlignmentAlloc. Motivation is better test coverage and
using propagate_on_container_move_assignment = std::true_type; // faster compilation time.
using propagate_on_container_swap = std::true_type;
using Base = CountingAllocator<T>;
using Base::Base;
template <typename U>
explicit PropagatingCountingAlloc(const PropagatingCountingAlloc<U> &other)
: Base(other.bytes_used_) {}
template <typename U>
struct rebind {
using other = PropagatingCountingAlloc<U>;
};
};
template <typename T> template <typename T>
void BtreeAllocatorTest() { void BtreeAllocatorTest() {
using value_type = typename T::value_type; using value_type = typename T::value_type;
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/internal/exception_testing.h" #include "absl/base/internal/exception_testing.h"
#include "absl/base/options.h" #include "absl/base/options.h"
#include "absl/container/internal/counting_allocator.h" #include "absl/container/internal/test_allocator.h"
#include "absl/hash/hash_testing.h" #include "absl/hash/hash_testing.h"
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#include "absl/base/internal/exception_testing.h" #include "absl/base/internal/exception_testing.h"
#include "absl/base/macros.h" #include "absl/base/macros.h"
#include "absl/base/options.h" #include "absl/base/options.h"
#include "absl/container/internal/counting_allocator.h" #include "absl/container/internal/test_allocator.h"
#include "absl/container/internal/test_instance_tracker.h" #include "absl/container/internal/test_instance_tracker.h"
#include "absl/hash/hash_testing.h" #include "absl/hash/hash_testing.h"
#include "absl/log/check.h" #include "absl/log/check.h"
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#include "absl/container/internal/hash_function_defaults.h" #include "absl/container/internal/hash_function_defaults.h"
#include "absl/container/internal/hash_policy_testing.h" #include "absl/container/internal/hash_policy_testing.h"
#include "absl/container/internal/hashtable_debug.h" #include "absl/container/internal/hashtable_debug.h"
#include "absl/container/internal/test_allocator.h"
#include "absl/log/log.h" #include "absl/log/log.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
...@@ -441,34 +442,6 @@ struct CustomAllocIntTable ...@@ -441,34 +442,6 @@ struct CustomAllocIntTable
using Base::Base; using Base::Base;
}; };
// Tries to allocate memory at the minimum alignment even when the default
// allocator uses a higher alignment.
template <typename T>
struct MinimumAlignmentAlloc : std::allocator<T> {
MinimumAlignmentAlloc() = default;
template <typename U>
explicit MinimumAlignmentAlloc(const MinimumAlignmentAlloc<U>& /*other*/) {}
template <class U>
struct rebind {
using other = MinimumAlignmentAlloc<U>;
};
T* allocate(size_t n) {
T* ptr = std::allocator<T>::allocate(n + 1);
char* cptr = reinterpret_cast<char*>(ptr);
cptr += alignof(T);
return reinterpret_cast<T*>(cptr);
}
void deallocate(T* ptr, size_t n) {
char* cptr = reinterpret_cast<char*>(ptr);
cptr -= alignof(T);
std::allocator<T>::deallocate(reinterpret_cast<T*>(cptr), n + 1);
}
};
struct MinimumAlignmentUint8Table struct MinimumAlignmentUint8Table
: raw_hash_set<Uint8Policy, container_internal::hash_default_hash<uint8_t>, : raw_hash_set<Uint8Policy, container_internal::hash_default_hash<uint8_t>,
std::equal_to<uint8_t>, MinimumAlignmentAlloc<uint8_t>> { std::equal_to<uint8_t>, MinimumAlignmentAlloc<uint8_t>> {
......
...@@ -12,11 +12,13 @@ ...@@ -12,11 +12,13 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#ifndef ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_ #ifndef ABSL_CONTAINER_INTERNAL_TEST_ALLOCATOR_H_
#define ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_ #define ABSL_CONTAINER_INTERNAL_TEST_ALLOCATOR_H_
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <type_traits>
#include "absl/base/config.h" #include "absl/base/config.h"
...@@ -115,8 +117,109 @@ class CountingAllocator { ...@@ -115,8 +117,109 @@ class CountingAllocator {
int64_t* instance_count_ = nullptr; int64_t* instance_count_ = nullptr;
}; };
template <typename T>
struct CopyAssignPropagatingCountingAlloc : public CountingAllocator<T> {
using propagate_on_container_copy_assignment = std::true_type;
using Base = CountingAllocator<T>;
using Base::Base;
template <typename U>
explicit CopyAssignPropagatingCountingAlloc(
const CopyAssignPropagatingCountingAlloc<U>& other)
: Base(other.bytes_used_, other.instance_count_) {}
template <typename U>
struct rebind {
using other = CopyAssignPropagatingCountingAlloc<U>;
};
};
template <typename T>
struct MoveAssignPropagatingCountingAlloc : public CountingAllocator<T> {
using propagate_on_container_move_assignment = std::true_type;
using Base = CountingAllocator<T>;
using Base::Base;
template <typename U>
explicit MoveAssignPropagatingCountingAlloc(
const MoveAssignPropagatingCountingAlloc<U>& other)
: Base(other.bytes_used_, other.instance_count_) {}
template <typename U>
struct rebind {
using other = MoveAssignPropagatingCountingAlloc<U>;
};
};
template <typename T>
struct SwapPropagatingCountingAlloc : public CountingAllocator<T> {
using propagate_on_container_swap = std::true_type;
using Base = CountingAllocator<T>;
using Base::Base;
template <typename U>
explicit SwapPropagatingCountingAlloc(
const SwapPropagatingCountingAlloc<U>& other)
: Base(other.bytes_used_, other.instance_count_) {}
template <typename U>
struct rebind {
using other = SwapPropagatingCountingAlloc<U>;
};
};
template <typename T>
struct PropagatingCountingAlloc : public CountingAllocator<T> {
using propagate_on_container_copy_assignment = std::true_type;
using propagate_on_container_move_assignment = std::true_type;
using propagate_on_container_swap = std::true_type;
using Base = CountingAllocator<T>;
using Base::Base;
template <typename U>
explicit PropagatingCountingAlloc(const PropagatingCountingAlloc<U> &other)
: Base(other.bytes_used_, other.instance_count_) {}
template <typename U>
struct rebind {
using other = PropagatingCountingAlloc<U>;
};
};
// Tries to allocate memory at the minimum alignment even when the default
// allocator uses a higher alignment.
template <typename T>
struct MinimumAlignmentAlloc : std::allocator<T> {
MinimumAlignmentAlloc() = default;
template <typename U>
explicit MinimumAlignmentAlloc(const MinimumAlignmentAlloc<U>& /*other*/) {}
template <class U>
struct rebind {
using other = MinimumAlignmentAlloc<U>;
};
T* allocate(size_t n) {
T* ptr = std::allocator<T>::allocate(n + 1);
char* cptr = reinterpret_cast<char*>(ptr);
cptr += alignof(T);
return reinterpret_cast<T*>(cptr);
}
void deallocate(T* ptr, size_t n) {
char* cptr = reinterpret_cast<char*>(ptr);
cptr -= alignof(T);
std::allocator<T>::deallocate(reinterpret_cast<T*>(cptr), n + 1);
}
};
} // namespace container_internal } // namespace container_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
} // namespace absl } // namespace absl
#endif // ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_ #endif // ABSL_CONTAINER_INTERNAL_TEST_ALLOCATOR_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment