2 #ifndef _PVFMM_MEM_MGR_HPP_ 3 #define _PVFMM_MEM_MGR_HPP_ 14 #include <pvfmm/common.hpp> 22 template <
class ValueType>
class ConstIterator {
24 template <
typename T>
friend class ConstIterator;
26 template <
typename T>
friend class Iterator;
28 void IteratorAssertChecks(Long j = 0)
const;
31 typedef std::random_access_iterator_tag iterator_category;
32 typedef const ValueType& reference;
33 typedef Long difference_type;
34 typedef ValueType value_type;
35 typedef const ValueType* pointer;
39 difference_type len, offset;
42 static const Long ValueSize =
sizeof(ValueType);
45 ConstIterator(
void* base_ = NULL) {
57 ConstIterator(
const ValueType* base_, difference_type len_,
bool dynamic_alloc =
false);
59 template <
class AnotherType>
explicit ConstIterator(
const ConstIterator<AnotherType>& I) {
62 this->offset = I.offset;
63 PVFMM_ASSERT_MSG((uintptr_t)(this->base + this->offset) %
alignof(ValueType) == 0,
"invalid alignment during pointer type conversion.");
64 this->alloc_ctr = I.alloc_ctr;
65 this->mem_head = I.mem_head;
69 reference operator*()
const;
71 const value_type* operator->()
const;
73 reference operator[](difference_type off)
const;
76 ConstIterator& operator++() {
77 offset += (Long)
sizeof(ValueType);
81 ConstIterator operator++(
int) {
82 ConstIterator<ValueType> tmp(*
this);
87 ConstIterator& operator--() {
88 offset -= (Long)
sizeof(ValueType);
92 ConstIterator operator--(
int) {
93 ConstIterator<ValueType> tmp(*
this);
99 ConstIterator& operator+=(difference_type i) {
100 offset += i * (Long)
sizeof(ValueType);
104 ConstIterator operator+(difference_type i)
const {
105 ConstIterator<ValueType> tmp(*
this);
106 tmp.offset += i * (Long)
sizeof(ValueType);
110 friend ConstIterator operator+(difference_type i,
const ConstIterator& right) {
return (right + i); }
112 ConstIterator& operator-=(difference_type i) {
113 offset -= i * (Long)
sizeof(ValueType);
117 ConstIterator operator-(difference_type i)
const {
118 ConstIterator<ValueType> tmp(*
this);
119 tmp.offset -= i * (Long)
sizeof(ValueType);
123 difference_type operator-(
const ConstIterator& I)
const {
125 Long diff = ((ValueType*)(base + offset)) - ((ValueType*)(I.base + I.offset));
126 PVFMM_ASSERT_MSG(I.base + I.offset + diff * (Long)
sizeof(ValueType) == base + offset,
"invalid memory address alignment.");
131 bool operator==(
const ConstIterator& I)
const {
return (base + offset == I.base + I.offset); }
133 bool operator!=(
const ConstIterator& I)
const {
return !(*
this == I); }
135 bool operator<(
const ConstIterator& I)
const {
137 return (base + offset) < (I.base + I.offset);
140 bool operator<=(
const ConstIterator& I)
const {
142 return (base + offset) <= (I.base + I.offset);
145 bool operator>(
const ConstIterator& I)
const {
147 return (base + offset) > (I.base + I.offset);
150 bool operator>=(
const ConstIterator& I)
const {
152 return (base + offset) >= (I.base + I.offset);
155 friend std::ostream& operator<<(std::ostream& out,
const ConstIterator& I) {
156 out <<
"(" << (
long long)I.base <<
"+" << I.offset <<
":" << I.len <<
")";
161 template <
class ValueType>
class Iterator :
public ConstIterator<ValueType> {
164 typedef std::random_access_iterator_tag iterator_category;
165 typedef ValueType& reference;
166 typedef Long difference_type;
167 typedef ValueType value_type;
168 typedef ValueType* pointer;
171 Iterator(
void* base_ = NULL) : ConstIterator<ValueType>(base_) {}
173 template <
size_t LENGTH> Iterator(ValueType (&base_)[LENGTH]) : ConstIterator<ValueType>(base_) {}
175 Iterator(ValueType* base_, difference_type len_,
bool dynamic_alloc =
false) : ConstIterator<ValueType>(base_, len_, dynamic_alloc) {}
177 template <
class AnotherType>
explicit Iterator(
const ConstIterator<AnotherType>& I) : ConstIterator<ValueType>(I) {}
180 reference operator*()
const;
182 value_type* operator->()
const;
184 reference operator[](difference_type off)
const;
187 Iterator& operator++() {
188 this->offset += (Long)
sizeof(ValueType);
192 Iterator operator++(
int) {
193 Iterator<ValueType> tmp(*
this);
198 Iterator& operator--() {
199 this->offset -= (Long)
sizeof(ValueType);
203 Iterator operator--(
int) {
204 Iterator<ValueType> tmp(*
this);
210 Iterator& operator+=(difference_type i) {
211 this->offset += i * (Long)
sizeof(ValueType);
215 Iterator operator+(difference_type i)
const {
216 Iterator<ValueType> tmp(*
this);
217 tmp.offset += i * (Long)
sizeof(ValueType);
221 friend Iterator operator+(difference_type i,
const Iterator& right) {
return (right + i); }
223 Iterator& operator-=(difference_type i) {
224 this->offset -= i * (Long)
sizeof(ValueType);
228 Iterator operator-(difference_type i)
const {
229 Iterator<ValueType> tmp(*
this);
230 tmp.offset -= i * (Long)
sizeof(ValueType);
234 difference_type operator-(
const ConstIterator<ValueType>& I)
const {
return static_cast<const ConstIterator<ValueType>&
>(*this) - I; }
237 template <
class ValueType, Long DIM>
class StaticArray :
public Iterator<ValueType> {
244 StaticArray(std::initializer_list<ValueType> arr_) : StaticArray() {
246 PVFMM_ASSERT_MSG(arr_.size() <= DIM,
"too many initializer values");
247 for (Long i = 0; i < arr_.size(); i++) arr[i] = arr_.begin()[i];
251 StaticArray(
const StaticArray&);
252 StaticArray& operator=(
const StaticArray&);
254 Iterator<ValueType> arr;
258 #define PVFMM_PTR2ITR(type, ptr, len) pvfmm::Iterator<type>((type*)ptr, len) 259 #define PVFMM_PTR2CONSTITR(type, ptr, len) pvfmm::ConstIterator<type>((const type*)ptr, len) 263 #define PVFMM_PTR2ITR(type, ptr, len) (type*) ptr 264 #define PVFMM_PTR2CONSTITR(type, ptr, len) (const type*) ptr 274 static uintptr_t ID();
285 static const char init_mem_val = 42;
296 unsigned char check_sum;
309 static MemHead& GetMemHead(
char* p);
311 static void CheckMemHead(
const MemHead& p);
313 Iterator<char> malloc(
const Long n_elem = 1,
const Long type_size =
sizeof(
char),
const uintptr_t type_id =
TypeTraits<char>::ID())
const;
315 void free(Iterator<char> p)
const;
326 static MemoryManager m(PVFMM_GLOBAL_MEM_BUFF * 1024LL * 1024LL);
347 std::multimap<Long, Long>::iterator it;
354 Long new_node()
const;
359 void delete_node(Long indx)
const;
365 mutable std::vector<MemNode> node_buff;
366 mutable std::stack<Long> node_stack;
367 mutable std::multimap<Long, Long> free_map;
368 mutable omp_lock_t omp_lock;
369 mutable std::set<void*> system_malloc;
372 inline uintptr_t align_ptr(uintptr_t ptr) {
373 const uintptr_t ALIGN_MINUS_ONE = PVFMM_MEM_ALIGN - 1;
374 const uintptr_t NOT_ALIGN_MINUS_ONE = ~ALIGN_MINUS_ONE;
375 return ((ptr + ALIGN_MINUS_ONE) & NOT_ALIGN_MINUS_ONE);
382 template <
class ValueType> Iterator<ValueType> aligned_new(Long n_elem = 1,
const MemoryManager* mem_mgr = &MemoryManager::glbMemMgr());
389 template <
class ValueType>
void aligned_delete(Iterator<ValueType> A,
const MemoryManager* mem_mgr = &MemoryManager::glbMemMgr());
395 template <
class ValueType> Iterator<ValueType> memcopy(Iterator<ValueType> destination, ConstIterator<ValueType> source, Long num);
397 template <
class ValueType> Iterator<ValueType> memset(Iterator<ValueType> ptr,
int value, Long num);
401 #include <pvfmm/mem_mgr.txx> 403 #endif //_PVFMM_MEM_MGR_HPP_ Definition: cheb_utils.hpp:12
MemoryManager class declaration.
Definition: mem_mgr.hpp:282
Header data for each memory block.
Definition: mem_mgr.hpp:290
Identify each type uniquely.
Definition: mem_mgr.hpp:271