b123a6424520db4aacbbbe844e909b00f4fae4ca
[libcds.git] / cds / os / posix / alloc_aligned.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_OS_POSIX_ALLOC_ALIGNED_H
4 #define __CDS_OS_POSIX_ALLOC_ALIGNED_H
5
6 #ifndef _GNU_SOURCE
7 #   define _GNU_SOURCE
8 #endif
9 #ifndef _XOPEN_SOURCE
10 #   define _XOPEN_SOURCE 600
11 #endif
12
13 #include <stdlib.h>
14
15 //@cond none
16 namespace cds { namespace OS {
17     CDS_CXX11_INLINE_NAMESPACE namespace posix {
18         /// Allocates memory on a specified alignment boundary
19         static inline void * aligned_malloc(
20             size_t nSize,       ///< Size of the requested memory allocation
21             size_t nAlignment   ///< The alignment value, which must be an integer power of 2
22             )
23         {
24             void * pMem;
25             return ::posix_memalign( &pMem, nAlignment, nSize ) == 0 ? pMem : nullptr;
26         }
27
28         /// Frees a block of memory that was allocated with aligned_malloc.
29         static inline void aligned_free(
30             void * pBlock   ///< A pointer to the memory block that was returned to the aligned_malloc function
31             )
32         {
33             ::free( pBlock );
34         }
35     }   // namespace posix
36
37 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
38     using posix::aligned_malloc;
39     using posix::aligned_free;
40 #endif
41
42 }} // namespace cds::OS
43 //@endcond
44
45
46 #endif // #ifndef __CDS_OS_POSIX_ALLOC_ALIGNED_H
47