Ignore:
Timestamp:
Dec 28, 2016, 7:21:28 PM (2 years ago)
Author:
nmedfort
Message:

Cleaned up memory leaks + some warning messages.

File:
1 edited

Legend:

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

    r5230 r5240  
    44#include <llvm/Support/Allocator.h>
    55
    6 template <typename T>
     6template <typename T = uint8_t>
    77class SlabAllocator {
    8     using LLVMAllocator = llvm::BumpPtrAllocator;
     8    template <typename U> friend class ProxyAllocator;
     9    using LLVMAllocator = llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, 16 * 1024>;
    910public:
    1011
     
    3233
    3334    template<typename Type = T>
    34     inline void deallocate(Type * p, size_type = 0) noexcept {
    35         mAllocator.Deallocate(p);
     35    inline void deallocate(Type * p, size_type size = 0) noexcept {
     36        mAllocator.Deallocate(p, size);
    3637    }
    3738
     
    4041    }
    4142
    42     inline LLVMAllocator & get_allocator() {
    43         return mAllocator;
    44     }
    45 
    46     void reset() {
    47         mAllocator.Reset();
    48     }
    49 
    5043    template<typename Type = T>
    5144    inline bool operator==(SlabAllocator<Type> const & other) {
    52         return &mAllocator == &other.mAllocator;
     45        return this == &other;
    5346    }
    5447
    5548    template<typename Type = T>
    5649    inline bool operator!=(SlabAllocator<Type> const & other) {
    57         return &mAllocator != &other.mAllocator;
     50        return this != &other;
    5851    }
    5952
    6053    inline SlabAllocator() noexcept {}
    61     inline SlabAllocator(const SlabAllocator &) noexcept {}
    62     template <class U> inline SlabAllocator (const std::allocator<U>&) noexcept {}
    63     inline ~SlabAllocator() { reset(); }
     54    inline SlabAllocator(const SlabAllocator &) noexcept { assert (false); }
     55    template <class U> inline SlabAllocator (const SlabAllocator<U> &) noexcept { assert (false); }
    6456private:
    6557    LLVMAllocator mAllocator;
    6658};
    6759
     60template <typename T = uint8_t>
     61class ProxyAllocator {
     62    using LLVMAllocator = typename SlabAllocator<T>::LLVMAllocator;
     63public:
     64    using value_type = T;
     65    using pointer = value_type*;
     66    using const_pointer = const value_type*;
     67    using reference = value_type&;
     68    using const_reference = const value_type&;
     69    using size_type = std::size_t;
     70    using difference_type = std::ptrdiff_t;
     71
     72    template<class U>
     73    struct rebind {
     74        typedef ProxyAllocator<U> other;
     75    };
     76
     77    template<typename Type = T>
     78    inline Type * allocate(size_type n, const_pointer = nullptr) noexcept {
     79        static_assert(sizeof(Type) > 0, "Cannot allocate a zero-length type.");
     80        assert ("Cannot allocate 0 items." && n > 0);
     81        auto ptr = static_cast<Type *>(mAllocator->Allocate(n * sizeof(Type), sizeof(void*)));
     82        assert ("Allocating returned a null pointer. Function was likely called before Allocator creation!" && ptr);
     83        return ptr;
     84    }
     85
     86    template<typename Type = T>
     87    inline void deallocate(Type * p, size_type size = 0) noexcept {
     88        mAllocator->Deallocate(p, size);
     89    }
     90
     91    inline size_type max_size() const {
     92        return std::numeric_limits<size_type>::max();
     93    }
     94
     95    template<typename Type = T>
     96    inline bool operator==(ProxyAllocator<Type> const & other) {
     97        return mAllocator == other.mAllocator;
     98    }
     99
     100    template<typename Type = T>
     101    inline bool operator!=(ProxyAllocator<Type> const & other) {
     102        return mAllocator != other.mAllocator;
     103    }
     104
     105    inline ProxyAllocator() noexcept { assert (false); }
     106    inline ProxyAllocator(ProxyAllocator const & a) noexcept : mAllocator(const_cast<LLVMAllocator *>(a.mAllocator)) {}
     107    template <class U> inline ProxyAllocator (const SlabAllocator<U> & a) noexcept : mAllocator(const_cast<LLVMAllocator *>(&a.mAllocator)) {}
     108private:
     109    LLVMAllocator * const mAllocator;
     110};
     111
    68112#endif // SLAB_ALLOCATOR_H
Note: See TracChangeset for help on using the changeset viewer.