libcopp  1.2.0
stack_allocator_memory.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <assert.h>
3 #include <cstring>
4 #include <limits>
5 #include <numeric>
6 
9 
13 
14 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
15 #include <valgrind/valgrind.h>
16 #endif
17 
18 
19 #ifdef COPP_HAS_ABI_HEADERS
20 #include COPP_ABI_PREFIX
21 #endif
22 
23 namespace copp {
24  namespace allocator {
25 
26  stack_allocator_memory::stack_allocator_memory() UTIL_CONFIG_NOEXCEPT : start_ptr_(UTIL_CONFIG_NULLPTR),
27  memory_size_(0),
28  is_used_(false) {}
29 
30  stack_allocator_memory::stack_allocator_memory(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT : start_ptr_(start_ptr),
31  memory_size_(max_size),
32  is_used_(false) {}
33 
35  : start_ptr_(UTIL_CONFIG_NULLPTR),
36  memory_size_(0),
37  is_used_(false) {
38  if (!other.is_used_) {
39  swap(other);
40  }
41  }
42 
43 #if defined(UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES) && UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES
45  : start_ptr_(UTIL_CONFIG_NULLPTR),
46  memory_size_(0),
47  is_used_(false) {
48  if (!other.is_used_) {
49  swap(other);
50  }
51  }
52 #endif
53 
55 
57  if (!other.is_used_) {
58  swap(other);
59  }
60  return *this;
61  }
62 #if defined(UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES) && UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES
64  if (!other.is_used_) {
65  swap(other);
66  }
67  return *this;
68  }
69 #endif
70 
72  using std::swap;
73  swap(start_ptr_, other.start_ptr_);
75  swap(is_used_, other.is_used_);
76  }
77 
78  void stack_allocator_memory::attach(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT {
79  start_ptr_ = start_ptr;
80  memory_size_ = max_size;
81  is_used_ = false;
82  }
83 
84  void stack_allocator_memory::allocate(stack_context &ctx, std::size_t size) UTIL_CONFIG_NOEXCEPT {
85  if (UTIL_CONFIG_NULLPTR == start_ptr_ || is_used_) {
86  ctx.sp = UTIL_CONFIG_NULLPTR;
87  return;
88  }
89 
90  size = (std::max)(size, stack_traits::minimum_size());
91  size = (std::min)(size, stack_traits::maximum_size());
92  size = (std::min)(size, memory_size_);
93 
94  std::size_t size_ = stack_traits::round_to_page_size(size);
95  assert(size > 0 && size_ > 0 && size_ <= memory_size_);
96 
97  ctx.size = size_;
98  ctx.sp = static_cast<char *>(start_ptr_) + ctx.size; // stack down
99 
100 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
101  ctx.valgrind_stack_id = VALGRIND_STACK_REGISTER(ctx.sp, start_ptr_);
102 #endif
103  is_used_ = true;
104  }
105 
107  assert(ctx.sp);
108  assert(stack_traits::minimum_size() <= ctx.size);
109  assert(stack_traits::is_unbounded() || (stack_traits::maximum_size() >= ctx.size));
110 
111 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
112  VALGRIND_STACK_DEREGISTER(ctx.valgrind_stack_id);
113 #endif
114  is_used_ = false;
115  }
116  } // namespace allocator
117 } // namespace copp
118 
119 #ifdef COPP_HAS_ABI_HEADERS
120 #include COPP_ABI_SUFFIX
121 #endif
导入继承关系约束 Licensed under the MIT licenses.
memory allocator this allocator will return address of specified memory section
void allocate(stack_context &ctx, std::size_t size) UTIL_CONFIG_NOEXCEPT
void deallocate(stack_context &ctx) UTIL_CONFIG_NOEXCEPT
#define EXPLICIT_UNUSED_ATTR
maybe_unused, 标记忽略unused警告 usage: EXPLICIT_UNUSED_ATTR int a; class EXPLICIT_UNUSED_ATTR a; EXP...
static bool is_unbounded() COPP_MACRO_NOEXCEPT
static std::size_t maximum_size() COPP_MACRO_NOEXCEPT
static std::size_t minimum_size() COPP_MACRO_NOEXCEPT
static std::size_t round_to_page_size(std::size_t stacksize) COPP_MACRO_NOEXCEPT
void swap(stack_allocator_memory &other)
void attach(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT
stack_allocator_memory & operator=(stack_allocator_memory &other) UTIL_CONFIG_NOEXCEPT