density
C++11 library for paged memory management, function queues, heterogeneous queues and lifo memory management
default_allocator.h
1 
2 // Copyright Giuseppe Campana (giu.campana@gmail.com) 2016-2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #pragma once
9 #include <density/detail/page_allocator.h>
10 #include <density/detail/system_page_manager.h>
11 
12 namespace density
13 {
149  template <size_t PAGE_CAPACITY_AND_ALIGNMENT> class basic_default_allocator;
150 
153 
158  template <size_t PAGE_CAPACITY_AND_ALIGNMENT = default_page_capacity>
160  {
161  private:
162  using PageAllocator =
163  detail::PageAllocator<typename detail::SystemPageManager<PAGE_CAPACITY_AND_ALIGNMENT>>;
164 
165  public:
167  static constexpr size_t page_size = PageAllocator::page_size;
168 
170  static constexpr size_t page_alignment = PageAllocator::page_alignment;
171 
188  void * allocate(size_t i_size, size_t i_alignment, size_t i_alignment_offset = 0)
189  {
190  return density::aligned_allocate(i_size, i_alignment, i_alignment_offset);
191  }
192 
209  void *
210  try_allocate(size_t i_size, size_t i_alignment, size_t i_alignment_offset = 0) noexcept
211  {
212  return density::try_aligned_allocate(i_size, i_alignment, i_alignment_offset);
213  }
214 
231  void * i_block, size_t i_size, size_t i_alignment, size_t i_alignment_offset = 0) noexcept
232  {
233  density::aligned_deallocate(i_block, i_size, i_alignment, i_alignment_offset);
234  }
235 
243  void * allocate_page()
244  {
245  auto const new_page =
246  PageAllocator::thread_local_instance()
247  .template try_allocate_page<detail::page_allocation_type::uninitialized>(
249  if (new_page == nullptr)
250  throw std::bad_alloc();
251  return new_page;
252  }
253 
261  void * try_allocate_page(progress_guarantee i_progress_guarantee) noexcept
262  {
263  return PageAllocator::thread_local_instance()
264  .template try_allocate_page<detail::page_allocation_type::uninitialized>(
265  i_progress_guarantee);
266  }
267 
276  {
277  auto const new_page =
278  PageAllocator::thread_local_instance()
279  .template try_allocate_page<detail::page_allocation_type::zeroed>(
281  if (new_page == nullptr)
282  throw std::bad_alloc();
283  return new_page;
284  }
285 
293  void * try_allocate_page_zeroed(progress_guarantee i_progress_guarantee) noexcept
294  {
295  return PageAllocator::thread_local_instance()
296  .template try_allocate_page<detail::page_allocation_type::zeroed>(
297  i_progress_guarantee);
298  }
299 
309  void deallocate_page(void * i_page) noexcept
310  {
311  DENSITY_ASSUME(i_page != nullptr);
312  PageAllocator::thread_local_instance()
313  .template deallocate_page<detail::page_allocation_type::uninitialized>(i_page);
314  }
315 
327  void deallocate_page_zeroed(void * i_page) noexcept
328  {
329  PageAllocator::thread_local_instance()
330  .template deallocate_page<detail::page_allocation_type::zeroed>(i_page);
331  }
332 
348  static void reserve_lockfree_page_memory(size_t i_size, size_t * o_reserved_size = nullptr)
349  {
350  if (!try_reserve_lockfree_page_memory(progress_blocking, i_size, o_reserved_size))
351  {
352  throw std::bad_alloc();
353  }
354  }
355 
373  progress_guarantee i_progress_guarantee,
374  size_t i_size,
375  size_t * o_reserved_size = nullptr) noexcept
376  {
377  auto const reserved_size =
378  PageAllocator::thread_local_instance().try_reserve_lockfree_memory(
379  i_progress_guarantee, i_size);
380  if (o_reserved_size != nullptr)
381  *o_reserved_size = reserved_size;
382  return reserved_size >= i_size;
383  }
384 
407  void pin_page(void * i_page) noexcept { PageAllocator::pin_page(i_page); }
408 
417  void unpin_page(void * i_address) noexcept { PageAllocator::unpin_page(i_address); }
418 
425  bool try_pin_page(progress_guarantee i_progress_guarantee, void * i_address) noexcept
426  {
427  return PageAllocator::try_pin_page(i_progress_guarantee, i_address);
428  }
429 
438  void unpin_page(progress_guarantee i_progress_guarantee, void * i_address) noexcept
439  {
440  return PageAllocator::unpin_page(i_progress_guarantee, i_address);
441  }
442 
450  uintptr_t get_pin_count(const void * i_address) noexcept
451  {
452  return PageAllocator::get_pin_count(i_address);
453  }
454 
457  bool operator==(const basic_default_allocator &) const noexcept { return true; }
458 
461  bool operator!=(const basic_default_allocator &) const noexcept { return false; }
462  };
463 
464 } // namespace density
static void reserve_lockfree_page_memory(size_t i_size, size_t *o_reserved_size=nullptr)
Definition: default_allocator.h:348
Definition: conc_function_queue.h:11
void deallocate_page_zeroed(void *i_page) noexcept
Definition: default_allocator.h:327
bool operator==(const basic_default_allocator &) const noexcept
Definition: default_allocator.h:457
Definition: density_common.h:86
bool operator!=(const basic_default_allocator &) const noexcept
Definition: default_allocator.h:461
void * try_allocate_page_zeroed(progress_guarantee i_progress_guarantee) noexcept
Definition: default_allocator.h:293
uintptr_t get_pin_count(const void *i_address) noexcept
Definition: default_allocator.h:450
bool try_pin_page(progress_guarantee i_progress_guarantee, void *i_address) noexcept
Definition: default_allocator.h:425
static bool try_reserve_lockfree_page_memory(progress_guarantee i_progress_guarantee, size_t i_size, size_t *o_reserved_size=nullptr) noexcept
Definition: default_allocator.h:372
void * allocate_page()
Definition: default_allocator.h:243
Definition: default_allocator.h:149
progress_guarantee
Definition: density_common.h:84
void * allocate_page_zeroed()
Definition: default_allocator.h:275
void * try_allocate(size_t i_size, size_t i_alignment, size_t i_alignment_offset=0) noexcept
Definition: default_allocator.h:210
void deallocate(void *i_block, size_t i_size, size_t i_alignment, size_t i_alignment_offset=0) noexcept
Definition: default_allocator.h:230
void * try_allocate_page(progress_guarantee i_progress_guarantee) noexcept
Definition: default_allocator.h:261
void * aligned_allocate(size_t i_size, size_t i_alignment, size_t i_alignment_offset=0)
Definition: density_common.h:460
void unpin_page(void *i_address) noexcept
Definition: default_allocator.h:417
void unpin_page(progress_guarantee i_progress_guarantee, void *i_address) noexcept
Definition: default_allocator.h:438
void pin_page(void *i_page) noexcept
Definition: default_allocator.h:407
void * try_aligned_allocate(size_t i_size, size_t i_alignment, size_t i_alignment_offset=0) noexcept
Definition: density_common.h:510
static constexpr size_t page_size
Definition: default_allocator.h:167
void aligned_deallocate(void *i_block, size_t i_size, size_t i_alignment, size_t i_alignment_offset=0) noexcept
Definition: density_common.h:564
static constexpr size_t page_alignment
Definition: default_allocator.h:170
void * allocate(size_t i_size, size_t i_alignment, size_t i_alignment_offset=0)
Definition: default_allocator.h:188
#define DENSITY_ASSUME(bool_expr,...)
Definition: density_config.h:46
void deallocate_page(void *i_page) noexcept
Definition: default_allocator.h:309