libcopp  1.1.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 
8 
12 
13 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
14 #include <valgrind/valgrind.h>
15 #endif
16 
17 
18 #ifdef COPP_HAS_ABI_HEADERS
19 #include COPP_ABI_PREFIX
20 #endif
21 
22 namespace copp {
23  namespace allocator {
24 
25  stack_allocator_memory::stack_allocator_memory() UTIL_CONFIG_NOEXCEPT : start_ptr_(NULL), memory_size_(0), is_used_(false) {}
26 
27  stack_allocator_memory::stack_allocator_memory(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT : start_ptr_(start_ptr),
28  memory_size_(max_size),
29  is_used_(false) {}
30 
32  memory_size_(0),
33  is_used_(false) {
34  if (!other.is_used_) {
35  swap(other);
36  }
37  }
38 
39 #if defined(UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES) && UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES
41  memory_size_(0),
42  is_used_(false) {
43  if (!other.is_used_) {
44  swap(other);
45  }
46  }
47 #endif
48 
50 
52  if (!other.is_used_) {
53  swap(other);
54  }
55  return *this;
56  }
57 #if defined(UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES) && UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES
59  if (!other.is_used_) {
60  swap(other);
61  }
62  return *this;
63  }
64 #endif
65 
67  using std::swap;
68  swap(start_ptr_, other.start_ptr_);
70  swap(is_used_, other.is_used_);
71  }
72 
73  void stack_allocator_memory::attach(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT {
74  start_ptr_ = start_ptr;
75  memory_size_ = max_size;
76  is_used_ = false;
77  }
78 
79  void stack_allocator_memory::allocate(stack_context &ctx, std::size_t size) UTIL_CONFIG_NOEXCEPT {
80  if (NULL == start_ptr_ || is_used_) {
81  ctx.sp = NULL;
82  return;
83  }
84 
85  size = (std::max)(size, stack_traits::minimum_size());
86  size = (std::min)(size, stack_traits::maximum_size());
87  size = (std::min)(size, memory_size_);
88 
89  std::size_t size_ = stack_traits::round_to_page_size(size);
90  assert(size > 0 && size_ > 0 && size_ <= memory_size_);
91 
92  ctx.size = size_;
93  ctx.sp = static_cast<char *>(start_ptr_) + ctx.size; // stack down
94 
95 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
96  ctx.valgrind_stack_id = VALGRIND_STACK_REGISTER(ctx.sp, start_ptr_);
97 #endif
98  is_used_ = true;
99  }
100 
102  assert(ctx.sp);
103  assert(stack_traits::minimum_size() <= ctx.size);
104  assert(stack_traits::is_unbounded() || (stack_traits::maximum_size() >= ctx.size));
105 
106 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
107  VALGRIND_STACK_DEREGISTER(ctx.valgrind_stack_id);
108 #endif
109  is_used_ = false;
110  }
111  } // namespace allocator
112 } // namespace copp
113 
114 #ifdef COPP_HAS_ABI_HEADERS
115 #include COPP_ABI_SUFFIX
116 #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