source: icGREP/icgrep-devel/icgrep/util/aligned_allocator.h @ 5893

Last change on this file since 5893 was 5236, checked in by nmedfort, 3 years ago

Reverted last change to support gcc 4.9 prior to changing UCD property table structure.

File size: 1.7 KB
Line 
1#ifndef ALIGNED_ALLOCATOR_H
2#define ALIGNED_ALLOCATOR_H
3
4#include <boost/version.hpp>
5#if (BOOST_VERSION >= 105600)
6#include <boost/align/aligned_allocator.hpp>
7template<typename T, unsigned alignment>
8using AlignedAllocator = boost::alignment::aligned_allocator<T, alignment>;
9#else
10#include <stdlib.h>
11template <typename T, unsigned alignment>
12class AlignedAllocator {
13    static_assert ((alignment & (alignment - 1)) == 0, "alignment must be power of two");
14public:
15    typedef T         value_type;
16    typedef T*        pointer;
17    typedef const T*  const_pointer;
18    typedef T&        reference;
19    typedef const T&  const_reference;
20    typedef size_t    size_type;
21    typedef ptrdiff_t difference_type;
22
23    template <class U>
24    struct rebind { typedef AlignedAllocator<U, alignment> other; };
25
26public:
27    AlignedAllocator() noexcept {}
28
29    pointer allocate(const size_type n, const_pointer = 0) {
30        const auto size = n * sizeof(T);
31        if (size == 0) {
32            return nullptr;
33        }
34        void * ptr = nullptr;
35        if (LLVM_UNLIKELY(::posix_memalign(&ptr, alignment, size) != 0)) {
36            throw std::bad_alloc();
37        }
38        assert (ptr && (reinterpret_cast<size_t>(ptr) & (alignment - 1)) == 0);
39        return reinterpret_cast<pointer>(ptr);
40    }
41
42    template<typename U, unsigned align>
43    inline bool operator==(const AlignedAllocator<U, align> &) const {
44        return false;
45    }
46
47    template<typename U, unsigned align>
48    inline bool operator!=(const AlignedAllocator<U, align> &) const {
49        return true;
50    }
51
52    void deallocate(pointer p, size_type) noexcept {
53        return ::free(p);
54    }
55};
56#endif
57#endif // ALIGNED_ALLOCATOR_H
Note: See TracBrowser for help on using the repository browser.