libcopp  2.2.0
stack_allocator_malloc.cpp
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
3 #include <libcopp/utils/config/libcopp_build_features.h>
4 
8 
9 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
10 # include <valgrind/valgrind.h>
11 #endif
12 
13 // clang-format off
14 #include <libcopp/utils/config/stl_include_prefix.h> // NOLINT(build/include_order)
15 // clang-format on
16 #include <assert.h>
17 #include <algorithm>
18 #include <cstdlib>
19 #include <cstring>
20 #include <limits>
21 #include <memory>
22 #include <numeric>
23 // clang-format off
24 #include <libcopp/utils/config/stl_include_suffix.h> // NOLINT(build/include_order)
25 // clang-format on
26 
27 #ifdef COPP_HAS_ABI_HEADERS
28 # include COPP_ABI_PREFIX
29 #endif
30 
31 LIBCOPP_COPP_NAMESPACE_BEGIN
32 namespace allocator {
33 LIBCOPP_COPP_API stack_allocator_malloc::stack_allocator_malloc() LIBCOPP_MACRO_NOEXCEPT {}
35 LIBCOPP_COPP_API stack_allocator_malloc::stack_allocator_malloc(const stack_allocator_malloc &) LIBCOPP_MACRO_NOEXCEPT {
36 }
38  LIBCOPP_MACRO_NOEXCEPT {
39  return *this;
40 }
41 
42 LIBCOPP_COPP_API stack_allocator_malloc::stack_allocator_malloc(stack_allocator_malloc &&) LIBCOPP_MACRO_NOEXCEPT {}
44  LIBCOPP_MACRO_NOEXCEPT {
45  return *this;
46 }
47 
48 LIBCOPP_COPP_API void stack_allocator_malloc::allocate(stack_context &ctx, std::size_t size) LIBCOPP_MACRO_NOEXCEPT {
49  size = (std::max)(size, stack_traits::minimum_size());
50  size = (std::min)(size, stack_traits::maximum_size());
51 
52  std::size_t size_ = stack_traits::round_to_page_size(size);
53 
54  void *start_ptr = malloc(size_);
55 
56  if (!start_ptr) {
57  ctx.sp = nullptr;
58  return;
59  }
60 
61  ctx.size = size_;
62  ctx.sp = static_cast<char *>(start_ptr) + ctx.size; // stack down
63 
64 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
65  ctx.valgrind_stack_id = VALGRIND_STACK_REGISTER(ctx.sp, start_ptr);
66 #endif
67 }
68 
69 LIBCOPP_COPP_API void stack_allocator_malloc::deallocate(stack_context &ctx) LIBCOPP_MACRO_NOEXCEPT {
70  assert(ctx.sp);
71  assert(stack_traits::minimum_size() <= ctx.size);
72  assert(stack_traits::is_unbounded() || (stack_traits::maximum_size() >= ctx.size));
73 
74 #if defined(LIBCOPP_MACRO_USE_VALGRIND)
75  VALGRIND_STACK_DEREGISTER(ctx.valgrind_stack_id);
76 #endif
77  void *start_ptr = static_cast<char *>(ctx.sp) - ctx.size;
78  free(start_ptr);
79 }
80 } // namespace allocator
81 LIBCOPP_COPP_NAMESPACE_END
82 
83 #ifdef COPP_HAS_ABI_HEADERS
84 # include COPP_ABI_SUFFIX
85 #endif
memory allocator this allocator will maintain buffer using malloc/free function
void allocate(stack_context &ctx, std::size_t size) LIBCOPP_MACRO_NOEXCEPT
stack_allocator_malloc() LIBCOPP_MACRO_NOEXCEPT
stack_allocator_malloc & operator=(const stack_allocator_malloc &other) LIBCOPP_MACRO_NOEXCEPT
void deallocate(stack_context &ctx) LIBCOPP_MACRO_NOEXCEPT
constexpr auto size(TCONTAINER &&container) -> decltype(container.size())
Definition: span.h:44
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