libcopp  1.3.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  LIBCOPP_COPP_API stack_allocator_memory::stack_allocator_memory() UTIL_CONFIG_NOEXCEPT : start_ptr_(UTIL_CONFIG_NULLPTR),
27  memory_size_(0),
28  is_used_(false) {}
29 
30  LIBCOPP_COPP_API stack_allocator_memory::stack_allocator_memory(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT
31  : start_ptr_(start_ptr),
32  memory_size_(max_size),
33  is_used_(false) {}
34 
36  : start_ptr_(UTIL_CONFIG_NULLPTR),
37  memory_size_(0),
38  is_used_(false) {
39  if (!other.is_used_) {
40  swap(other);
41  }
42  }
43 
44 #if defined(UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES) && UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES
46  : start_ptr_(UTIL_CONFIG_NULLPTR),
47  memory_size_(0),
48  is_used_(false) {
49  if (!other.is_used_) {
50  swap(other);
51  }
52  }
53 #endif
54 
56 
58  if (!other.is_used_) {
59  swap(other);
60  }
61  return *this;
62  }
63 #if defined(UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES) && UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES
65  if (!other.is_used_) {
66  swap(other);
67  }
68  return *this;
69  }
70 #endif
71 
73  using std::swap;
74  swap(start_ptr_, other.start_ptr_);
76  swap(is_used_, other.is_used_);
77  }
78 
79  LIBCOPP_COPP_API void stack_allocator_memory::attach(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT {
80  start_ptr_ = start_ptr;
81  memory_size_ = max_size;
82  is_used_ = false;
83  }
84 
85  LIBCOPP_COPP_API void stack_allocator_memory::allocate(stack_context &ctx, std::size_t size) UTIL_CONFIG_NOEXCEPT {
86  if (UTIL_CONFIG_NULLPTR == start_ptr_ || is_used_) {
87  ctx.sp = UTIL_CONFIG_NULLPTR;
88  return;
89  }
90 
91  size = (std::max)(size, stack_traits::minimum_size());
92  size = (std::min)(size, stack_traits::maximum_size());
93  size = (std::min)(size, memory_size_);
94 
95  std::size_t size_ = stack_traits::round_to_page_size(size);
96  assert(size > 0 && size_ > 0 && size_ <= memory_size_);
97 
98  ctx.size = size_;
99  ctx.sp = static_cast<char *>(start_ptr_) + ctx.size; // stack down
100 
101 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
102  ctx.valgrind_stack_id = VALGRIND_STACK_REGISTER(ctx.sp, start_ptr_);
103 #endif
104  is_used_ = true;
105  }
106 
108  assert(ctx.sp);
109  assert(stack_traits::minimum_size() <= ctx.size);
110  assert(stack_traits::is_unbounded() || (stack_traits::maximum_size() >= ctx.size));
111 
112 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
113  VALGRIND_STACK_DEREGISTER(ctx.valgrind_stack_id);
114 #endif
115  is_used_ = false;
116  }
117  } // namespace allocator
118 } // namespace copp
119 
120 #ifdef COPP_HAS_ABI_HEADERS
121 #include COPP_ABI_SUFFIX
122 #endif
void swap(stack_allocator_memory &other)
导入继承关系约束 Licensed under the MIT licenses.
#define LIBCOPP_COPP_API
void allocate(stack_context &ctx, std::size_t size) UTIL_CONFIG_NOEXCEPT
memory allocator this allocator will return address of specified memory section
void attach(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT
static LIBCOPP_COPP_API std::size_t maximum_size() COPP_MACRO_NOEXCEPT
#define EXPLICIT_UNUSED_ATTR
maybe_unused, 标记忽略unused警告 usage: EXPLICIT_UNUSED_ATTR int a; class EXPLICIT_UNUSED_ATTR a; EXP...
static LIBCOPP_COPP_API std::size_t minimum_size() COPP_MACRO_NOEXCEPT
void deallocate(stack_context &ctx) UTIL_CONFIG_NOEXCEPT
void swap(intrusive_ptr< T > &lhs, intrusive_ptr< T > &rhs)
static LIBCOPP_COPP_API bool is_unbounded() COPP_MACRO_NOEXCEPT
static LIBCOPP_COPP_API std::size_t round_to_page_size(std::size_t stacksize) COPP_MACRO_NOEXCEPT
stack_allocator_memory & operator=(stack_allocator_memory &other) UTIL_CONFIG_NOEXCEPT