libcopp  2.2.0
stack_allocator_memory.cpp
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
3 #include <libcopp/utils/config/libcopp_build_features.h>
4 
6 
10 
11 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
12 # include <valgrind/valgrind.h>
13 #endif
14 
15 #ifdef COPP_HAS_ABI_HEADERS
16 # include COPP_ABI_PREFIX
17 #endif
18 
19 // clang-format off
20 #include <libcopp/utils/config/stl_include_prefix.h> // NOLINT(build/include_order)
21 // clang-format on
22 #include <assert.h>
23 #include <algorithm>
24 #include <cstring>
25 #include <limits>
26 #include <numeric>
27 // clang-format off
28 #include <libcopp/utils/config/stl_include_suffix.h> // NOLINT(build/include_order)
29 // clang-format on
30 
31 LIBCOPP_COPP_NAMESPACE_BEGIN
32 namespace allocator {
33 
34 LIBCOPP_COPP_API stack_allocator_memory::stack_allocator_memory() LIBCOPP_MACRO_NOEXCEPT : start_ptr_(nullptr),
35  memory_size_(0),
36  is_used_(false) {}
37 
38 LIBCOPP_COPP_API stack_allocator_memory::stack_allocator_memory(void *start_ptr,
39  std::size_t max_size) LIBCOPP_MACRO_NOEXCEPT
40  : start_ptr_(start_ptr),
41  memory_size_(max_size),
42  is_used_(false) {}
43 
44 LIBCOPP_COPP_API stack_allocator_memory::stack_allocator_memory(stack_allocator_memory &other) LIBCOPP_MACRO_NOEXCEPT
45  : start_ptr_(nullptr),
46  memory_size_(0),
47  is_used_(false) {
48  if (!other.is_used_) {
49  swap(other);
50  }
51 }
52 
53 LIBCOPP_COPP_API stack_allocator_memory::stack_allocator_memory(stack_allocator_memory &&other) LIBCOPP_MACRO_NOEXCEPT
54  : start_ptr_(nullptr),
55  memory_size_(0),
56  is_used_(false) {
57  if (!other.is_used_) {
58  swap(other);
59  }
60 }
61 
63 
65  LIBCOPP_MACRO_NOEXCEPT {
66  if (!other.is_used_) {
67  swap(other);
68  }
69  return *this;
70 }
71 
73  LIBCOPP_MACRO_NOEXCEPT {
74  if (!other.is_used_) {
75  swap(other);
76  }
77  return *this;
78 }
79 
81  using std::swap;
82  swap(start_ptr_, other.start_ptr_);
84  swap(is_used_, other.is_used_);
85 }
86 
87 LIBCOPP_COPP_API void stack_allocator_memory::attach(void *start_ptr, std::size_t max_size) LIBCOPP_MACRO_NOEXCEPT {
88  start_ptr_ = start_ptr;
89  memory_size_ = max_size;
90  is_used_ = false;
91 }
92 
93 LIBCOPP_COPP_API void stack_allocator_memory::allocate(stack_context &ctx, std::size_t size) LIBCOPP_MACRO_NOEXCEPT {
94  if (nullptr == start_ptr_ || is_used_) {
95  ctx.sp = nullptr;
96  return;
97  }
98 
99  size = (std::max)(size, stack_traits::minimum_size());
100  size = (std::min)(size, stack_traits::maximum_size());
101  size = (std::min)(size, memory_size_);
102 
103  std::size_t size_ = stack_traits::round_to_page_size(size);
104  assert(size > 0 && size_ > 0 && size_ <= memory_size_);
105 
106  ctx.size = size_;
107  ctx.sp = static_cast<char *>(start_ptr_) + ctx.size; // stack down
108 
109 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
110  ctx.valgrind_stack_id = VALGRIND_STACK_REGISTER(ctx.sp, start_ptr_);
111 #endif
112  is_used_ = true;
113 }
114 
116  LIBCOPP_MACRO_NOEXCEPT {
117  assert(ctx.sp);
118  assert(stack_traits::minimum_size() <= ctx.size);
119  assert(stack_traits::is_unbounded() || (stack_traits::maximum_size() >= ctx.size));
120 
121 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
122  VALGRIND_STACK_DEREGISTER(ctx.valgrind_stack_id);
123 #endif
124  is_used_ = false;
125 }
126 } // namespace allocator
127 LIBCOPP_COPP_NAMESPACE_END
128 
129 #ifdef COPP_HAS_ABI_HEADERS
130 # include COPP_ABI_SUFFIX
131 #endif
memory allocator this allocator will return address of specified memory section
void attach(void *start_ptr, std::size_t max_size) LIBCOPP_MACRO_NOEXCEPT
void deallocate(stack_context &ctx) LIBCOPP_MACRO_NOEXCEPT
void swap(stack_allocator_memory &other)
stack_allocator_memory & operator=(stack_allocator_memory &other) LIBCOPP_MACRO_NOEXCEPT
stack_allocator_memory() LIBCOPP_MACRO_NOEXCEPT
void allocate(stack_context &ctx, std::size_t size) LIBCOPP_MACRO_NOEXCEPT
导入继承关系约束 Licensed under the MIT licenses.
#define EXPLICIT_UNUSED_ATTR
maybe_unused, 标记忽略unused警告 usage: EXPLICIT_UNUSED_ATTR int a; class EXPLICIT_UNUSED_ATTR a; EXPLICIT_...
constexpr auto size(TCONTAINER &&container) -> decltype(container.size())
Definition: span.h:44
void swap(intrusive_ptr< T > &lhs, intrusive_ptr< T > &rhs)
static LIBCOPP_COPP_API std::size_t maximum_size() LIBCOPP_MACRO_NOEXCEPT
static LIBCOPP_COPP_API std::size_t round_to_page_size(std::size_t stacksize) LIBCOPP_MACRO_NOEXCEPT
static LIBCOPP_COPP_API std::size_t minimum_size() LIBCOPP_MACRO_NOEXCEPT
static LIBCOPP_COPP_API bool is_unbounded() LIBCOPP_MACRO_NOEXCEPT