libcopp  2.2.0
stack_allocator_split_segment.cpp
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
3 #include <libcopp/utils/config/libcopp_build_features.h>
4 
9 
10 // clang-format off
11 #include <libcopp/utils/config/stl_include_prefix.h> // NOLINT(build/include_order)
12 // clang-format on
13 extern "C" {
14 #include <signal.h>
15 }
16 
17 #include <assert.h>
18 #include <algorithm>
19 #include <cstring>
20 #include <limits>
21 // clang-format off
22 #include <libcopp/utils/config/stl_include_suffix.h> // NOLINT(build/include_order)
23 // clang-format on
24 
25 #ifdef LIBCOPP_MACRO_USE_SEGMENTED_STACKS
26 extern "C" {
27 void *__splitstack_makecontext(std::size_t, void *[COPP_MACRO_SEGMENTED_STACK_NUMBER], std::size_t *);
28 
29 void __splitstack_releasecontext(void *[COPP_MACRO_SEGMENTED_STACK_NUMBER]);
30 
31 void __splitstack_resetcontext(void *[COPP_MACRO_SEGMENTED_STACK_NUMBER]);
32 
33 void __splitstack_block_signals_context(void *[COPP_MACRO_SEGMENTED_STACK_NUMBER], int *new_value, int *old_value);
34 }
35 #endif
36 
37 #ifdef COPP_HAS_ABI_HEADERS
38 # include COPP_ABI_PREFIX
39 #endif
40 
41 LIBCOPP_COPP_NAMESPACE_BEGIN
42 namespace allocator {
43 
44 LIBCOPP_COPP_API stack_allocator_split_segment::stack_allocator_split_segment() LIBCOPP_MACRO_NOEXCEPT {}
46 LIBCOPP_COPP_API
48  LIBCOPP_MACRO_NOEXCEPT {}
50  const stack_allocator_split_segment &) LIBCOPP_MACRO_NOEXCEPT {
51  return *this;
52 }
53 
54 LIBCOPP_COPP_API
57  stack_allocator_split_segment &&) LIBCOPP_MACRO_NOEXCEPT {
58  return *this;
59 }
60 
62  std::size_t size) LIBCOPP_MACRO_NOEXCEPT {
63  void *start_ptr = __splitstack_makecontext(size, ctx.segments_ctx, &ctx.size);
64  assert(start_ptr);
65  if (!start_ptr) {
66  ctx.sp = nullptr;
67  return;
68  }
69 
70  ctx.sp = static_cast<char *>(start_ptr) + ctx.size; // stack down
71 
72  int off = 0;
73  __splitstack_block_signals_context(ctx.segments_ctx, &off, 0);
74 }
75 
76 LIBCOPP_COPP_API void stack_allocator_split_segment::deallocate(stack_context &ctx) LIBCOPP_MACRO_NOEXCEPT {
77  __splitstack_releasecontext(ctx.segments_ctx);
78 }
79 } // namespace allocator
80 LIBCOPP_COPP_NAMESPACE_END
81 
82 #ifdef COPP_HAS_ABI_HEADERS
83 # include COPP_ABI_SUFFIX
84 #endif
memory allocator this allocator will create buffer using gcc split segment api
void allocate(stack_context &, std::size_t) LIBCOPP_MACRO_NOEXCEPT
void deallocate(stack_context &) LIBCOPP_MACRO_NOEXCEPT
stack_allocator_split_segment & operator=(const stack_allocator_split_segment &other) LIBCOPP_MACRO_NOEXCEPT
constexpr auto size(TCONTAINER &&container) -> decltype(container.size())
Definition: span.h:44