summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/alignment.h56
1 files changed, 19 insertions, 37 deletions
diff --git a/src/common/alignment.h b/src/common/alignment.h
index b3fbdfe20..da2e61e10 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -3,10 +3,8 @@
3#pragma once 3#pragma once
4 4
5#include <cstddef> 5#include <cstddef>
6#include <cstdlib> 6#include <memory>
7#include <type_traits> 7#include <type_traits>
8#include <malloc.h>
9#include <stdlib.h>
10 8
11namespace Common { 9namespace Common {
12 10
@@ -43,59 +41,43 @@ constexpr bool IsWordAligned(T value) {
43template <typename T, std::size_t Align = 16> 41template <typename T, std::size_t Align = 16>
44class AlignmentAllocator { 42class AlignmentAllocator {
45public: 43public:
46 typedef T value_type; 44 using value_type = T;
47 typedef std::size_t size_type; 45 using size_type = std::size_t;
48 typedef std::ptrdiff_t difference_type; 46 using difference_type = std::ptrdiff_t;
49 47
50 typedef T* pointer; 48 using pointer = T*;
51 typedef const T* const_pointer; 49 using const_pointer = const T*;
52 50
53 typedef T& reference; 51 using reference = T&;
54 typedef const T& const_reference; 52 using const_reference = const T&;
55 53
56public: 54public:
57 inline AlignmentAllocator() throw() {}
58 55
59 template <typename T2> 56 pointer address(reference r) {
60 inline AlignmentAllocator(const AlignmentAllocator<T2, Align>&) throw() {} 57 return std::addressof(r);
61
62 inline ~AlignmentAllocator() throw() {}
63
64 inline pointer adress(reference r) {
65 return &r;
66 } 58 }
67 59
68 inline const_pointer adress(const_reference r) const { 60 const_pointer address(const_reference r) const {
69 return &r; 61 return std::addressof(r);
70 } 62 }
71 63
72#if (defined _MSC_VER) 64 pointer allocate(size_type n) {
73 inline pointer allocate(size_type n) { 65 return static_cast<pointer>(::operator new(n, std::align_val_t{Align}));
74 return (pointer)_aligned_malloc(n * sizeof(value_type), Align);
75 }
76
77 inline void deallocate(pointer p, size_type) {
78 _aligned_free(p);
79 }
80#else
81 inline pointer allocate(size_type n) {
82 return (pointer)std::aligned_alloc(Align, n * sizeof(value_type));
83 } 66 }
84 67
85 inline void deallocate(pointer p, size_type) { 68 void deallocate(pointer p, size_type) {
86 std::free(p); 69 ::operator delete(p, std::align_val_t{Align});
87 } 70 }
88#endif
89 71
90 inline void construct(pointer p, const value_type& wert) { 72 void construct(pointer p, const value_type& wert) {
91 new (p) value_type(wert); 73 new (p) value_type(wert);
92 } 74 }
93 75
94 inline void destroy(pointer p) { 76 void destroy(pointer p) {
95 p->~value_type(); 77 p->~value_type();
96 } 78 }
97 79
98 inline size_type max_size() const throw() { 80 size_type max_size() const noexcept {
99 return size_type(-1) / sizeof(value_type); 81 return size_type(-1) / sizeof(value_type);
100 } 82 }
101 83