| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2017-2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
#ifndef _IGA_SAA_HPP_ |
| 10 |
#define _IGA_SAA_HPP_ |
| 11 |
|
| 12 |
#include "MemManager.hpp" |
| 13 |
|
| 14 |
#include <memory> |
| 15 |
|
| 16 |
namespace iga { |
| 17 |
template <class T> class std_arena_based_allocator { |
| 18 |
protected: |
| 19 |
std::shared_ptr<MemManager> MemManager_ptr; |
| 20 |
|
| 21 |
public: |
| 22 |
// for allocator_traits |
| 23 |
typedef size_t size_type; |
| 24 |
typedef ptrdiff_t difference_type; |
| 25 |
typedef T *pointer; |
| 26 |
typedef const T *const_pointer; |
| 27 |
typedef T &reference; |
| 28 |
typedef const T &const_reference; |
| 29 |
typedef T value_type; |
| 30 |
|
| 31 |
~std_arena_based_allocator() = default; |
| 32 |
|
| 33 |
explicit std_arena_based_allocator(std::shared_ptr<MemManager> _other_ptr) |
| 34 |
: MemManager_ptr(_other_ptr) {} |
| 35 |
|
| 36 |
explicit std_arena_based_allocator() |
| 37 |
: MemManager_ptr(std::make_shared<MemManager>(4096)) {} |
| 38 |
|
| 39 |
explicit std_arena_based_allocator(const std_arena_based_allocator &other) |
| 40 |
: MemManager_ptr(other.MemManager_ptr) {} |
| 41 |
|
| 42 |
template <class U> |
| 43 |
std_arena_based_allocator(const std_arena_based_allocator<U> &other) |
| 44 |
: MemManager_ptr(other.MemManager_ptr) {} |
| 45 |
|
| 46 |
template <class U> |
| 47 |
std_arena_based_allocator & |
| 48 |
operator=(const std_arena_based_allocator<U> &other) { |
| 49 |
MemManager_ptr = other.MemManager_ptr; |
| 50 |
return *this; |
| 51 |
} |
| 52 |
|
| 53 |
template <class U> struct rebind { |
| 54 |
typedef std_arena_based_allocator<U> other; |
| 55 |
}; |
| 56 |
|
| 57 |
template <class U> friend class std_arena_based_allocator; |
| 58 |
|
| 59 |
pointer allocate(size_type n, const void * = 0) { |
| 60 |
T *t = (T *)MemManager_ptr->alloc(n * sizeof(T)); |
| 61 |
return t; |
| 62 |
} |
| 63 |
|
| 64 |
void deallocate(void *, size_type) { |
| 65 |
// No deallocation for arena allocator. |
| 66 |
} |
| 67 |
|
| 68 |
pointer address(reference x) const { return &x; } |
| 69 |
const_pointer address(const_reference x) const { return &x; } |
| 70 |
|
| 71 |
std_arena_based_allocator<T> &operator=(const std_arena_based_allocator &) { |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
void construct(pointer p, const T &val) { new ((T *)p) T(val); } |
| 76 |
void destroy(pointer p) { p->~T(); } |
| 77 |
|
| 78 |
size_type max_size() const { return size_t(-1); } |
| 79 |
|
| 80 |
inline bool operator==(const std_arena_based_allocator &) const { |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
inline bool operator!=(const std_arena_based_allocator &a) const { |
| 85 |
return !operator==(a); |
| 86 |
} |
| 87 |
}; |
| 88 |
} // namespace iga |
| 89 |
#endif |