20 #ifndef RAJA_BASIC_MEMPOOL_HPP
21 #define RAJA_BASIC_MEMPOOL_HPP
36 namespace basic_mempool
65 : m_allocation {ptr, static_cast<char*>(ptr) + size},
69 m_free_space[ptr] =
static_cast<char*
>(ptr) + size;
70 if (m_allocation.begin ==
nullptr)
72 fprintf(stderr,
"Attempt to create MemoryArena with no memory");
85 return static_cast<char*
>(m_allocation.end) -
86 static_cast<char*
>(m_allocation.begin);
89 bool unused() {
return m_used_space.empty(); }
93 void*
get(
size_t nbytes,
size_t alignment)
95 void* ptr_out =
nullptr;
98 free_type::iterator end = m_free_space.end();
99 for (free_type::iterator
iter = m_free_space.begin();
iter != end; ++
iter)
102 void* adj_ptr =
iter->first;
104 static_cast<char*
>(
iter->second) -
static_cast<char*
>(adj_ptr);
106 if (::
RAJA::align(alignment, nbytes, adj_ptr, cap))
111 remove_free_chunk(
iter, adj_ptr,
112 static_cast<char*
>(adj_ptr) + nbytes);
114 add_used_chunk(adj_ptr,
static_cast<char*
>(adj_ptr) + nbytes);
125 if (m_allocation.begin <= ptr && ptr < m_allocation.end)
128 used_type::iterator found = m_used_space.find(ptr);
130 if (found != m_used_space.end())
133 add_free_chunk(found->first, found->second);
135 m_used_space.erase(found);
139 fprintf(stderr,
"Invalid free %p", ptr);
158 void add_free_chunk(
void* begin,
void* end)
161 free_type::iterator invl = m_free_space.end();
162 free_type::iterator
next = m_free_space.lower_bound(begin);
165 if (
next != m_free_space.begin())
170 if (
prev->second == begin)
178 assert(
next->first != begin);
180 if (
next->first == end)
186 m_free_space.erase(
next);
195 assert(
next->first != begin);
197 if (
next->first == end)
201 m_free_space.erase(
next);
212 void remove_free_chunk(free_type::iterator
iter,
void* begin,
void* end)
215 void* ptr =
iter->first;
216 void* ptr_end =
iter->second;
223 iter->second = begin;
234 else if (end != ptr_end)
241 m_free_space.erase(
iter);
247 m_free_space.erase(
iter);
251 void add_used_chunk(
void* begin,
void* end)
257 memory_chunk m_allocation;
304 template<
typename allocator_t>
334 std::lock_guard<std::mutex> lock(m_mutex);
336 while (!m_arenas.empty())
338 void* allocation_ptr = m_arenas.front().get_allocation();
339 m_alloc.free(allocation_ptr);
340 m_arenas.pop_front();
346 std::lock_guard<std::mutex> lock(m_mutex);
348 return m_default_arena_size;
353 std::lock_guard<std::mutex> lock(m_mutex);
355 size_t prev_size = m_default_arena_size;
356 m_default_arena_size = new_size;
361 T*
malloc(
size_t nTs,
size_t alignment =
alignof(T))
363 std::lock_guard<std::mutex> lock(m_mutex);
365 const size_t size = nTs *
sizeof(T);
367 arena_container_type::iterator end = m_arenas.end();
368 for (arena_container_type::iterator
iter = m_arenas.begin();
iter != end;
371 ptr =
iter->get(size, alignment);
380 const size_t alloc_size =
381 std::max(size + alignment, m_default_arena_size);
382 void* arena_ptr = m_alloc.malloc(alloc_size);
383 if (arena_ptr !=
nullptr)
385 m_arenas.emplace_front(arena_ptr, alloc_size);
386 ptr = m_arenas.front().get(size, alignment);
390 return static_cast<T*
>(ptr);
395 std::lock_guard<std::mutex> lock(m_mutex);
397 void* ptr =
const_cast<void*
>(cptr);
398 arena_container_type::iterator end = m_arenas.end();
399 for (arena_container_type::iterator
iter = m_arenas.begin();
iter != end;
410 fprintf(stderr,
"Unknown pointer %p", ptr);
415 using arena_container_type = std::list<detail::MemoryArena>;
419 arena_container_type m_arenas;
420 size_t m_default_arena_size;
429 void*
malloc(
size_t nbytes) {
return std::malloc(nbytes); }
RAJA header file containing an implementation of std align.
MemPool pre-allocates a large chunk of memory and provides generic malloc/free for the user to alloca...
Definition: basic_mempool.hpp:306
void free(const void *cptr)
Definition: basic_mempool.hpp:393
T * malloc(size_t nTs, size_t alignment=alignof(T))
Definition: basic_mempool.hpp:361
static MemPool< allocator_t > & getInstance()
Definition: basic_mempool.hpp:310
static const size_t default_default_arena_size
Definition: basic_mempool.hpp:316
size_t arena_size(size_t new_size)
Definition: basic_mempool.hpp:351
void free_chunks()
Free all backing allocations, even if they are currently in use.
Definition: basic_mempool.hpp:332
size_t arena_size()
Definition: basic_mempool.hpp:344
~MemPool()
Definition: basic_mempool.hpp:324
allocator_t allocator_type
Definition: basic_mempool.hpp:308
MemPool()
Definition: basic_mempool.hpp:318
MemoryArena is a map based subclass for class MemPool provides book-keeping to divy a large chunk of ...
Definition: basic_mempool.hpp:57
MemoryArena & operator=(MemoryArena const &)=delete
std::map< void *, void * > free_type
Definition: basic_mempool.hpp:59
MemoryArena(MemoryArena const &)=delete
typename used_type::value_type used_value_type
Definition: basic_mempool.hpp:62
typename free_type::value_type free_value_type
Definition: basic_mempool.hpp:60
void * get(size_t nbytes, size_t alignment)
Definition: basic_mempool.hpp:93
bool give(void *ptr)
Definition: basic_mempool.hpp:123
MemoryArena & operator=(MemoryArena &&)=default
MemoryArena(void *ptr, size_t size)
Definition: basic_mempool.hpp:64
std::map< void *, void * > used_type
Definition: basic_mempool.hpp:61
bool unused()
Definition: basic_mempool.hpp:89
void * get_allocation()
Definition: basic_mempool.hpp:91
MemoryArena(MemoryArena &&)=default
size_t capacity()
Definition: basic_mempool.hpp:83
value_type::device_call &[i_loop] iter
Definition: WorkRunner.hpp:216
Definition: AlignedRangeIndexSetBuilders.cpp:35
RAJA_HOST_DEVICE RAJA_INLINE Iter next(Iter it)
returns iterator to next item
Definition: algorithm.hpp:90
RAJA_INLINE void * align(size_t alignment, size_t size, void *&ptr, size_t &space)
Definition: align.hpp:33
RAJA_HOST_DEVICE RAJA_INLINE Iter prev(Iter it)
returns iterator to next item
Definition: algorithm.hpp:100
RAJA_HOST_DEVICE constexpr RAJA_INLINE Result max(Args... args)
Definition: foldl.hpp:155
example allocator for basic_mempool using malloc/free
Definition: basic_mempool.hpp:426
void * malloc(size_t nbytes)
Definition: basic_mempool.hpp:429
bool free(void *ptr)
Definition: basic_mempool.hpp:432