3 #ifndef CDSLIB_OS_ALLOC_ALIGNED_H
4 #define CDSLIB_OS_ALLOC_ALIGNED_H
6 #include <cds/details/defs.h>
8 #if CDS_OS_TYPE == CDS_OS_WIN32 || CDS_OS_TYPE == CDS_OS_WIN64 || CDS_OS_TYPE == CDS_OS_MINGW
9 # include <cds/os/win/alloc_aligned.h>
10 #elif CDS_OS_TYPE == CDS_OS_LINUX
11 # include <cds/os/linux/alloc_aligned.h>
12 #elif CDS_OS_TYPE == CDS_OS_SUN_SOLARIS
13 # include <cds/os/sunos/alloc_aligned.h>
14 #elif CDS_OS_TYPE == CDS_OS_HPUX
15 # include <cds/os/hpux/alloc_aligned.h>
16 #elif CDS_OS_TYPE == CDS_OS_AIX
17 # include <cds/os/aix/alloc_aligned.h>
18 #elif CDS_OS_TYPE == CDS_OS_FREE_BSD || CDS_OS_TYPE == CDS_OS_OPEN_BSD || CDS_OS_TYPE == CDS_OS_NET_BSD
19 # include <cds/os/free_bsd/alloc_aligned.h>
20 #elif CDS_OS_TYPE == CDS_OS_OSX || CDS_OS_TYPE == CDS_OS_PTHREAD
21 # include <cds/os/posix/alloc_aligned.h>
23 # error Unknown OS. Compilation aborted
27 #include <cds/details/is_aligned.h>
28 #include <cds/algo/int_algo.h>
31 /// OS specific wrappers
33 This namespace contains OS-specific implementations.
34 Usually, the sub-namespaces contain OS-specific wrappers for a feature.
36 Typical usage pattern:
46 // import Linux::Feature class into cds::OS namespace
48 // now, cds::OS::Feature refers to cds::OS::Linux::Feature
56 This allocator is intended for allocating of an aligned memory block. It uses wrappers around platform-specific
57 function for allocating and deallocating the block of memory:
58 \li \p aligned_malloc for allocating
59 \li \p aligned_free for deallocating
61 The \p aligned_malloc function wraps:
62 \li \p for Win: \p _aligned_malloc function
63 \li \p for other OSes: \p posix_memalign / \p memalign function
65 The \p aligned_free function wraps:
66 \li \p for Win: \p _aligned_free function
67 \li \p for other OSes: \p free function
69 This class should not be used directly. Use cds::details::AlignedAllocator instead.
72 class aligned_allocator
74 typedef std::allocator<T> prototype ; ///< prototype (the source of typedefs)
76 typedef typename prototype::value_type value_type ; ///< value type
77 typedef typename prototype::pointer pointer ; ///< pointer to value type
78 typedef typename prototype::reference reference ; ///< value reference type
79 typedef typename prototype::const_pointer const_pointer ; ///< const pointer to value type
80 typedef typename prototype::const_reference const_reference ; ///< const value reference type
82 typedef typename prototype::size_type size_type ; ///< size type
83 typedef typename prototype::difference_type difference_type ; ///< difference type
85 /// convert an aligned_allocator<T> to an aligned_allocator<OTHER>
89 typedef aligned_allocator<OTHER> other; ///< Rebinding result
93 /// return address of mutable \p v
94 pointer address(reference v) const
97 return a.address( v );
100 /// return address of nonmutable \p v
101 const_pointer address(const_reference v) const
104 return a.address( v );
107 // construct default allocator (do nothing)
108 aligned_allocator() throw()
111 /// construct by copying (do nothing)
112 aligned_allocator(const aligned_allocator<T>&) throw()
115 /// construct from a related allocator (do nothing)
116 template <class OTHER>
117 aligned_allocator(const aligned_allocator<OTHER>&) throw()
120 /// assign from a related allocator (do nothing)
121 template <class OTHER>
122 aligned_allocator<T>& operator=(const aligned_allocator<OTHER>&)
127 /// deallocate object at \p ptr, ignore size
128 void deallocate(pointer ptr, size_type)
130 cds::OS::aligned_free( ptr );
133 /// allocate array of \p nCount elements
135 The address returned is aligned by \p nAlign boundary.
136 \p nAlign parameter should be power of 2.
138 The function guarantees the alignment for first element of array only.
139 To guarantee the alignment for each element of the array the size of an object of type \p T must be multiple of \p nAlign:
141 sizeof(T) % nAlign == 0
144 The function, like operator \p new does not return \p nullptr.
145 In no memory situation the function throws \p std::bad_alloc exception.
147 pointer allocate( size_type nAlign, size_type nCount )
149 assert( cds::beans::is_power2( nAlign ) );
150 pointer p = reinterpret_cast<T *>( cds::OS::aligned_malloc( sizeof(T) * nCount, nAlign ) );
152 throw std::bad_alloc();
153 assert( cds::details::is_aligned( p, nAlign ));
157 /// allocate array of \p nCount elements, ignore hint
159 The address returned is aligned by \p nAlign boundary.
160 \p nAlign parameter should be power of 2.
162 The function guarantees alignment for first element of array only.
164 pointer allocate(size_type nAlign, size_type nCount, const void *)
166 return ( allocate( nAlign, nCount ) );
169 /// construct object at \p ptr with value \p val
170 void construct(pointer ptr, const T& val)
173 a.construct( ptr, val );
176 /// destroy object at \p ptr
177 void destroy(pointer ptr)
183 /// estimate maximum array size
184 size_type max_size() const throw()
193 #endif // #ifndef CDSLIB_OS_ALLOC_ALIGNED_H