• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 Copyright 2014 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4 
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
7 */
8 #ifndef BOOST_ALIGN_DETAIL_ALIGNED_ALLOC_MACOS_HPP
9 #define BOOST_ALIGN_DETAIL_ALIGNED_ALLOC_MACOS_HPP
10 
11 #include <boost/align/detail/is_alignment.hpp>
12 #include <boost/assert.hpp>
13 #include <stdlib.h>
14 
15 namespace boost {
16 namespace alignment {
17 
18 inline void*
aligned_alloc(std::size_t alignment,std::size_t size)19 aligned_alloc(std::size_t alignment, std::size_t size) BOOST_NOEXCEPT
20 {
21     BOOST_ASSERT(detail::is_alignment(alignment));
22     if (size == 0) {
23         return 0;
24     }
25     if (alignment < sizeof(void*)) {
26         alignment = sizeof(void*);
27     }
28     void* p;
29     if (::posix_memalign(&p, alignment, size) != 0) {
30         p = 0;
31     }
32     return p;
33 }
34 
35 inline void
aligned_free(void * ptr)36 aligned_free(void* ptr) BOOST_NOEXCEPT
37 {
38     ::free(ptr);
39 }
40 
41 } /* alignment */
42 } /* boost */
43 
44 #endif
45