125 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef TEST_SUPPORT_INCREASING_ALLOCATOR_H10#define TEST_SUPPORT_INCREASING_ALLOCATOR_H11 12#include <cstddef>13#include <limits>14#include <memory>15 16#include "test_macros.h"17 18// The increasing_allocator is a custom allocator that enforces an increasing minimum allocation size,19// ensuring that it allocates an increasing amount of memory, possibly exceeding the requested amount.20// This unique behavior is particularly useful for testing the shrink_to_fit functionality in std::vector,21// vector<bool>, and std::basic_string, ensuring that shrink_to_fit does not increase the capacity of22// the allocated memory.23 24template <typename T>25struct increasing_allocator {26 using value_type = T;27 std::size_t min_elements = 1000;28 increasing_allocator() = default;29 30 template <typename U>31 TEST_CONSTEXPR_CXX20 increasing_allocator(const increasing_allocator<U>& other) TEST_NOEXCEPT32 : min_elements(other.min_elements) {}33 34#if TEST_STD_VER >= 2335 TEST_CONSTEXPR_CXX23 std::allocation_result<T*> allocate_at_least(std::size_t n) {36 if (n < min_elements)37 n = min_elements;38 min_elements += 1000;39 return std::allocator<T>{}.allocate_at_least(n);40 }41#endif // TEST_STD_VER >= 2342 43 TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }44 45 TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }46};47 48template <typename T, typename U>49TEST_CONSTEXPR_CXX20 bool operator==(increasing_allocator<T>, increasing_allocator<U>) TEST_NOEXCEPT {50 return true;51}52 53template <std::size_t MinAllocSize, typename T>54class min_size_allocator {55public:56 using value_type = T;57 min_size_allocator() = default;58 59 template <typename U>60 TEST_CONSTEXPR_CXX20 min_size_allocator(const min_size_allocator<MinAllocSize, U>&) TEST_NOEXCEPT {}61 62 TEST_NODISCARD TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) {63 if (n < MinAllocSize)64 n = MinAllocSize;65 return static_cast<T*>(::operator new(n * sizeof(T)));66 }67 68 TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t) TEST_NOEXCEPT { ::operator delete(static_cast<void*>(p)); }69 70 template <typename U>71 struct rebind {72 using other = min_size_allocator<MinAllocSize, U>;73 };74};75 76template <std::size_t MinAllocSize, typename T, typename U>77TEST_CONSTEXPR_CXX20 bool78operator==(const min_size_allocator<MinAllocSize, T>&, const min_size_allocator<MinAllocSize, U>&) {79 return true;80}81 82template <std::size_t MinAllocSize, typename T, typename U>83TEST_CONSTEXPR_CXX20 bool84operator!=(const min_size_allocator<MinAllocSize, T>&, const min_size_allocator<MinAllocSize, U>&) {85 return false;86}87 88template <typename T>89class pow2_allocator {90public:91 using value_type = T;92 pow2_allocator() = default;93 94 template <typename U>95 TEST_CONSTEXPR_CXX20 pow2_allocator(const pow2_allocator<U>&) TEST_NOEXCEPT {}96 97 TEST_NODISCARD TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) {98 return static_cast<T*>(::operator new(next_power_of_two(n) * sizeof(T)));99 }100 101 TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t) TEST_NOEXCEPT { ::operator delete(static_cast<void*>(p)); }102 103private:104 TEST_CONSTEXPR_CXX20 std::size_t next_power_of_two(std::size_t n) const {105 if ((n & (n - 1)) == 0)106 return n;107 for (std::size_t shift = 1; shift < std::numeric_limits<std::size_t>::digits; shift <<= 1) {108 n |= n >> shift;109 }110 return n + 1;111 }112};113 114template <typename T, typename U>115TEST_CONSTEXPR_CXX20 bool operator==(const pow2_allocator<T>&, const pow2_allocator<U>&) {116 return true;117}118 119template <typename T, typename U>120TEST_CONSTEXPR_CXX20 bool operator!=(const pow2_allocator<T>&, const pow2_allocator<U>&) {121 return false;122}123 124#endif // TEST_SUPPORT_INCREASING_ALLOCATOR_H125