brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · cbd4685 Raw
63 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_SIZED_ALLOCATOR_H10#define TEST_SUPPORT_SIZED_ALLOCATOR_H11 12#include <cstddef>13#include <limits>14#include <memory>15#include <new>16 17#include "test_macros.h"18 19// Allocator with a provided size_type and difference_type, used to test corner cases20// like arithmetic on Allocator::size_type in generic code.21template <typename T, typename Size = std::size_t, typename Difference = std::ptrdiff_t>22class sized_allocator {23  template <typename U, typename Sz, typename Diff>24  friend class sized_allocator;25 26public:27  using value_type                  = T;28  using size_type                   = Size;29  using difference_type             = Difference;30  using propagate_on_container_swap = std::true_type;31 32  TEST_CONSTEXPR_CXX20 explicit sized_allocator(int d = 0) : data_(d) {}33 34  template <typename U, typename Sz, typename Diff>35  TEST_CONSTEXPR_CXX20 sized_allocator(const sized_allocator<U, Sz, Diff>& a) TEST_NOEXCEPT : data_(a.data_) {}36 37  TEST_CONSTEXPR_CXX20 T* allocate(size_type n) {38    if (n > max_size())39      TEST_THROW(std::bad_array_new_length());40    return std::allocator<T>().allocate(static_cast<std::size_t>(n));41  }42 43  TEST_CONSTEXPR_CXX20 void deallocate(T* p, size_type n) TEST_NOEXCEPT {44    std::allocator<T>().deallocate(p, static_cast<std::size_t>(n));45  }46 47  TEST_CONSTEXPR size_type max_size() const TEST_NOEXCEPT {48    return std::numeric_limits<size_type>::max() / sizeof(value_type);49  }50 51private:52  int data_;53 54  TEST_CONSTEXPR friend bool operator==(const sized_allocator& a, const sized_allocator& b) {55    return a.data_ == b.data_;56  }57  TEST_CONSTEXPR friend bool operator!=(const sized_allocator& a, const sized_allocator& b) {58    return a.data_ != b.data_;59  }60};61 62#endif // TEST_SUPPORT_SIZED_ALLOCATOR_H63