libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 LIBCOPP_HAS_ABI_HEADERS
16# include LIBCOPP_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
31LIBCOPP_COPP_NAMESPACE_BEGIN
32namespace allocator {
33
34LIBCOPP_COPP_API stack_allocator_memory::stack_allocator_memory() LIBCOPP_MACRO_NOEXCEPT : start_ptr_(nullptr),
35 memory_size_(0),
36 is_used_(false) {}
37
38LIBCOPP_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
44LIBCOPP_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
53LIBCOPP_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;
84 swap(is_used_, other.is_used_);
85}
86
87LIBCOPP_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
93LIBCOPP_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
127LIBCOPP_COPP_NAMESPACE_END
128
129#ifdef LIBCOPP_HAS_ABI_HEADERS
130# include LIBCOPP_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
#define LIBCOPP_EXPLICIT_UNUSED_ATTR
maybe_unused attribute usage: LIBCOPP_EXPLICIT_UNUSED_ATTR int a; class LIBCOPP_EXPLICIT_UNUSED_ATTR ...
LIBCOPP_COPP_API_HEAD_ONLY void swap(LIBCOPP_COPP_NAMESPACE_ID::memory::strong_rc_ptr< T > &a, LIBCOPP_COPP_NAMESPACE_ID::memory::strong_rc_ptr< T > &b) noexcept
Support std::swap for strong_rc_ptr.
Definition rc_ptr.h:1377
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