File indexing completed on 2023-10-25 10:02:37
0001 #include <cstdint>
0002
0003
0004
0005
0006
0007
0008
0009 template <typename T, std::size_t Alignment>
0010 class aligned_allocator {
0011 public:
0012
0013 typedef T* pointer;
0014 typedef const T* const_pointer;
0015 typedef T& reference;
0016 typedef const T& const_reference;
0017 typedef T value_type;
0018 typedef std::size_t size_type;
0019 typedef ptrdiff_t difference_type;
0020
0021 T* address(T& r) const { return &r; }
0022
0023 const T* address(const T& s) const { return &s; }
0024
0025 std::size_t max_size() const {
0026
0027
0028 return (static_cast<std::size_t>(0) - static_cast<std::size_t>(1)) / sizeof(T);
0029 }
0030
0031
0032 template <typename U>
0033 struct rebind {
0034 typedef aligned_allocator<U, Alignment> other;
0035 };
0036
0037 bool operator!=(const aligned_allocator& other) const { return !(*this == other); }
0038
0039 void construct(T* const p, const T& t) const {
0040 void* const pv = static_cast<void*>(p);
0041
0042 new (pv) T(t);
0043 }
0044
0045 void construct(T* const p) { return construct(p, value_type()); }
0046
0047 void destroy(T* const p) const { p->~T(); }
0048
0049
0050
0051
0052 bool operator==(const aligned_allocator& other) const { return true; }
0053
0054
0055
0056 aligned_allocator() {}
0057
0058 aligned_allocator(const aligned_allocator&) {}
0059
0060 template <typename U>
0061 aligned_allocator(const aligned_allocator<U, Alignment>&) {}
0062
0063 ~aligned_allocator() {}
0064
0065
0066 T* allocate(const std::size_t n) const {
0067
0068
0069
0070
0071
0072
0073 if (n == 0) {
0074 return NULL;
0075 }
0076
0077
0078
0079
0080 if (n > max_size()) {
0081 throw std::length_error("aligned_allocator<T>::allocate() - Integer overflow.");
0082 }
0083
0084
0085 void* const pv = std::aligned_alloc(Alignment, n * sizeof(T));
0086
0087
0088 if (pv == NULL) {
0089 throw std::bad_alloc();
0090 }
0091
0092 return static_cast<T*>(pv);
0093 }
0094
0095 void deallocate(T* const p, const std::size_t n) const { std::free(p); }
0096
0097
0098 template <typename U>
0099 T* allocate(const std::size_t n, const U* ) const {
0100 return allocate(n);
0101 }
0102
0103
0104
0105
0106
0107
0108
0109
0110 private:
0111 aligned_allocator& operator=(const aligned_allocator&);
0112 };