Ignore:
Timestamp:
Dec 19, 2016, 2:39:35 PM (3 years ago)
Author:
nmedfort
Message:

Multi-threading support for PabloAST / PabloCompiler?. Requires unique LLVM Context / Module for each thread.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/util/slab_allocator.h

    r4983 r5230  
    44#include <llvm/Support/Allocator.h>
    55
    6 using LLVMAllocator = llvm::BumpPtrAllocator;
    7 
    86template <typename T>
    97class SlabAllocator {
     8    using LLVMAllocator = llvm::BumpPtrAllocator;
    109public:
    1110
     
    2322    };
    2423
    25     inline pointer allocate(size_type n, const_pointer = nullptr) noexcept {
    26         return static_cast<T*>(mAllocator.Allocate(n * sizeof(T), sizeof(void*)));
     24    template<typename Type = T>
     25    inline Type * allocate(size_type n, const_pointer = nullptr) noexcept {
     26        static_assert(sizeof(Type) > 0, "Cannot allocate a zero-length type.");
     27        assert ("Cannot allocate 0 items." && n > 0);
     28        auto ptr = static_cast<Type *>(mAllocator.Allocate(n * sizeof(Type), sizeof(void*)));
     29        assert ("Allocating returned a null pointer. Function was likely called before Allocator creation!" && ptr);
     30        return ptr;
    2731    }
    2832
    29     inline void deallocate(pointer p, size_type = 0) noexcept {
     33    template<typename Type = T>
     34    inline void deallocate(Type * p, size_type = 0) noexcept {
    3035        mAllocator.Deallocate(p);
    3136    }
     
    3944    }
    4045
    41     void Reset() {
     46    void reset() {
    4247        mAllocator.Reset();
    4348    }
    4449
    45     inline bool operator==(SlabAllocator<T> const&) { return true; }
    46     inline bool operator!=(SlabAllocator<T> const&) { return false; }
     50    template<typename Type = T>
     51    inline bool operator==(SlabAllocator<Type> const & other) {
     52        return &mAllocator == &other.mAllocator;
     53    }
     54
     55    template<typename Type = T>
     56    inline bool operator!=(SlabAllocator<Type> const & other) {
     57        return &mAllocator != &other.mAllocator;
     58    }
    4759
    4860    inline SlabAllocator() noexcept {}
    4961    inline SlabAllocator(const SlabAllocator &) noexcept {}
    5062    template <class U> inline SlabAllocator (const std::allocator<U>&) noexcept {}
    51     inline ~SlabAllocator() { }
     63    inline ~SlabAllocator() { reset(); }
    5264private:
    53     static LLVMAllocator mAllocator;
    54 };
    55 
    56 template <typename T> LLVMAllocator SlabAllocator<T>::mAllocator;
    57 
    58 inline void releaseSlabAllocatorMemory() {
    59     SlabAllocator<void *> T;
    60     T.Reset();
    61 }
    62 
    63 template <typename T>
    64 class LLVMAllocatorProxy {
    65 public:
    66 
    67     using value_type = T;
    68     using pointer = value_type*;
    69     using const_pointer = const value_type*;
    70     using reference = value_type&;
    71     using const_reference = const value_type&;
    72     using size_type = std::size_t;
    73     using difference_type = std::ptrdiff_t;
    74 
    75     template<class U>
    76     struct rebind {
    77         typedef SlabAllocator<U> other;
    78     };
    79 
    80     inline pointer allocate(size_type n, const_pointer = nullptr) noexcept {
    81         return mAllocator.Allocate<T>(n);
    82     }
    83 
    84     inline void deallocate(pointer p, size_type = 0) noexcept {
    85         mAllocator.Deallocate(p);
    86     }
    87 
    88     inline size_type max_size() const {
    89         return std::numeric_limits<size_type>::max();
    90     }
    91 
    92     inline bool operator==(SlabAllocator<T> const&) { return true; }
    93     inline bool operator!=(SlabAllocator<T> const&) { return false; }
    94 
    95     inline LLVMAllocatorProxy(LLVMAllocator & allocator) noexcept : mAllocator(allocator) {}
    96     inline LLVMAllocatorProxy(const LLVMAllocatorProxy & proxy) noexcept : mAllocator(proxy.mAllocator) {}
    97     inline ~LLVMAllocatorProxy() { }
    98 private:
    99     LLVMAllocator & mAllocator;
     65    LLVMAllocator mAllocator;
    10066};
    10167
Note: See TracChangeset for help on using the changeset viewer.