MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
aligned_allocator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Benjamin Richter, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef MVE_ALIGNED_ALLOCATOR_HEADER
11#define MVE_ALIGNED_ALLOCATOR_HEADER
12
13#include <memory>
14#include <vector>
15#include <limits>
16#include <cstdlib>
17
18#ifdef _WIN32
19# include <malloc.h>
20#endif
21
22#include "util/defines.h"
23
25
29template <typename T, size_t alignment>
31{
32public:
33 typedef T value_type;
34 typedef T *pointer;
35 typedef T const *const_pointer;
36 typedef T &reference;
37 typedef T const &const_reference;
38 typedef std::size_t size_type;
39 typedef std::ptrdiff_t difference_type;
40
41 template<class U>
46
47public:
48 AlignedAllocator (void);
49
50 template <class T_other, size_t alignment_other>
52
53 pointer allocate (size_type n);
54 void deallocate (pointer p, size_type /*n*/);
55 size_type max_size (void) const;
56 void construct (pointer p, const_reference other);
57 void destroy (pointer p);
58
59 template <class T_other, size_t alignment_other>
60 bool operator== (AlignedAllocator<T_other, alignment_other> const&) const;
61
62 template <class T_other, size_t alignment_other>
63 bool operator!= (AlignedAllocator<T_other, alignment_other> const&) const;
64};
65
66/* ------------------------ Implementation ------------------------ */
67
68template <typename T, size_t alignment>
69inline
73
74template <typename T, size_t alignment>
75template <class T_other, size_t alignment_other>
76inline
81
82template <typename T, size_t alignment>
85{
86 if (n > this->max_size())
87 throw std::bad_alloc();
88 size_t size = n * sizeof(T);
89 pointer p = nullptr;
90#ifdef _WIN32
91 p = reinterpret_cast<pointer>(::_aligned_malloc(size, alignment));
92 if (p == nullptr)
93 throw std::bad_alloc();
94#else
95 if (::posix_memalign(&reinterpret_cast<void*&>(p), alignment, size))
96 throw std::bad_alloc();
97#endif
98 return p;
99}
100
101template <typename T, size_t alignment>
102inline void
104{
105#ifdef _WIN32
106 ::_aligned_free(p);
107#else
108 ::free(p);
109#endif
110}
111
112template <typename T, size_t alignment>
115{
116 return std::numeric_limits<size_t>::max() / sizeof(T);
117}
118
119template <typename T, size_t alignment>
120inline void
122{
123 ::new((void*)p) T(other);
124}
125
126template <typename T, size_t alignment>
127inline void
132
133template <typename T, size_t alignment>
134template <class T_other, size_t alignment_other>
135inline bool
141
142template <typename T, size_t alignment>
143template <class T_other, size_t alignment_other>
144inline bool
150
152
153#endif // MVE_ALIGNED_ALLOCATOR_HEADER
AlignedAllocator< U, alignment > other
Implements the STL allocator interface for aligned memory allocation.
std::ptrdiff_t difference_type
#define UTIL_NAMESPACE_BEGIN
Definition defines.h:13
#define UTIL_NAMESPACE_END
Definition defines.h:14