|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// UNSUPPORTED: c++03, c++11 |
| 10 | + |
| 11 | +#include <cstdint> |
| 12 | +#include <cstdlib> |
| 13 | +#include <cstring> |
| 14 | +#include <deque> |
| 15 | +#include <functional> |
| 16 | +#include <memory> |
| 17 | +#include <string> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include "benchmark/benchmark.h" |
| 21 | +#include "ContainerBenchmarks.h" |
| 22 | +#include "../GenerateInput.h" |
| 23 | + |
| 24 | +using namespace ContainerBenchmarks; |
| 25 | + |
| 26 | +template <typename T, typename SIZE_TYPE = std::size_t, typename DIFF_TYPE = std::ptrdiff_t> |
| 27 | +class CustomSizedAllocator { |
| 28 | + template <typename U, typename Sz, typename Diff> |
| 29 | + friend class CustomSizedAllocator; |
| 30 | + |
| 31 | +public: |
| 32 | + using value_type = T; |
| 33 | + using size_type = SIZE_TYPE; |
| 34 | + using difference_type = DIFF_TYPE; |
| 35 | + using propagate_on_container_swap = std::true_type; |
| 36 | + |
| 37 | + explicit CustomSizedAllocator(int i = 0) : data_(i) {} |
| 38 | + |
| 39 | + template <typename U, typename Sz, typename Diff> |
| 40 | + constexpr CustomSizedAllocator(const CustomSizedAllocator<U, Sz, Diff>& a) noexcept : data_(a.data_) {} |
| 41 | + |
| 42 | + constexpr T* allocate(size_type n) { |
| 43 | + if (n > max_size()) |
| 44 | + throw std::bad_array_new_length(); |
| 45 | + return std::allocator<T>().allocate(n); |
| 46 | + } |
| 47 | + |
| 48 | + constexpr void deallocate(T* p, size_type n) noexcept { std::allocator<T>().deallocate(p, n); } |
| 49 | + |
| 50 | + constexpr size_type max_size() const noexcept { return std::numeric_limits<size_type>::max() / sizeof(value_type); } |
| 51 | + |
| 52 | + int get() { return data_; } |
| 53 | + |
| 54 | +private: |
| 55 | + int data_; |
| 56 | + |
| 57 | + constexpr friend bool operator==(const CustomSizedAllocator& a, const CustomSizedAllocator& b) { |
| 58 | + return a.data_ == b.data_; |
| 59 | + } |
| 60 | + constexpr friend bool operator!=(const CustomSizedAllocator& a, const CustomSizedAllocator& b) { |
| 61 | + return a.data_ != b.data_; |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +BENCHMARK_CAPTURE(BM_Move_Assignment, |
| 66 | + vector_bool_uint32_t, |
| 67 | + std::vector<bool, CustomSizedAllocator<bool, std::uint32_t, std::int32_t>>{}, |
| 68 | + CustomSizedAllocator<bool, std::uint32_t, std::int32_t>{}) |
| 69 | + ->Arg(5140480); |
| 70 | + |
| 71 | +BENCHMARK_CAPTURE(BM_Move_Assignment, |
| 72 | + vector_bool_uint64_t, |
| 73 | + std::vector<bool, CustomSizedAllocator<bool, std::uint64_t, std::int64_t>>{}, |
| 74 | + CustomSizedAllocator<bool, std::uint64_t, std::int64_t>{}) |
| 75 | + ->Arg(5140480); |
| 76 | + |
| 77 | +BENCHMARK_MAIN(); |
0 commit comments