File indexing completed on 2025-01-18 10:00:09
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <cstdlib>
0012 #include <memory>
0013
0014 namespace gloo {
0015
0016
0017 const size_t kBufferAlignment = 32;
0018
0019 template <typename T, int ALIGNMENT = kBufferAlignment>
0020 class aligned_allocator {
0021 static_assert(
0022 !(ALIGNMENT & (ALIGNMENT - 1)),
0023 "alignment must be a power of 2");
0024
0025 public:
0026 using value_type = T;
0027 using pointer = value_type*;
0028 using const_pointer = const value_type*;
0029 using reference = value_type&;
0030 using const_reference = const value_type&;
0031 using size_type = std::size_t;
0032 using difference_type = std::ptrdiff_t;
0033
0034 template <typename U>
0035 struct rebind {
0036 using other = aligned_allocator<U, ALIGNMENT>;
0037 };
0038
0039 inline explicit aligned_allocator() = default;
0040 inline ~aligned_allocator() = default;
0041 inline explicit aligned_allocator(const aligned_allocator& a) = default;
0042
0043 inline pointer address(reference r) {
0044 return &r;
0045 }
0046
0047 inline const_pointer address(const_reference r) {
0048 return &r;
0049 }
0050
0051 inline pointer allocate(size_type sz) {
0052 pointer p;
0053 if (posix_memalign(
0054 reinterpret_cast<void**>(&p), ALIGNMENT, sizeof(T) * sz)) {
0055 abort();
0056 }
0057 return p;
0058 }
0059
0060 void deallocate(pointer p, size_type ) {
0061 free(p);
0062 }
0063 };
0064
0065 }