libcopp  2.2.0
stack_allocator_pool.h
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
3 #pragma once
4 
5 #include <libcopp/utils/config/libcopp_build_features.h>
6 
8 
9 #include <assert.h>
10 #include <cstddef>
11 #include <memory>
12 
13 #ifdef COPP_HAS_ABI_HEADERS
14 # include COPP_ABI_PREFIX
15 #endif
16 
17 LIBCOPP_COPP_NAMESPACE_BEGIN
18 struct stack_context;
19 
20 namespace allocator {
21 
26 template <typename TPool>
27 class LIBCOPP_COPP_API_HEAD_ONLY stack_allocator_pool {
28  public:
29  using pool_type = TPool;
30 
31  // Compability with libcopp-1.x
32  using pool_t = pool_type;
33 
34  public:
35  stack_allocator_pool() LIBCOPP_MACRO_NOEXCEPT {}
36  stack_allocator_pool(const std::shared_ptr<pool_type> &p) LIBCOPP_MACRO_NOEXCEPT : pool_(p) {}
38 
45  void attach(const std::shared_ptr<pool_type> &p) LIBCOPP_MACRO_NOEXCEPT { pool_ = p; }
46 
53  void allocate(stack_context &ctx, std::size_t) LIBCOPP_MACRO_NOEXCEPT {
54  assert(pool_);
55  if (pool_) {
56  pool_->allocate(ctx);
57  }
58  }
59 
64  void deallocate(stack_context &ctx) LIBCOPP_MACRO_NOEXCEPT {
65  assert(pool_);
66  if (pool_) {
67  pool_->deallocate(ctx);
68  }
69  }
70 
71  private:
72  std::shared_ptr<pool_type> pool_;
73 };
74 } // namespace allocator
75 LIBCOPP_COPP_NAMESPACE_END
76 
77 #ifdef COPP_HAS_ABI_HEADERS
78 # include COPP_ABI_SUFFIX
79 #endif
memory allocator this allocator will maintain buffer using malloc/free function
std::shared_ptr< pool_type > pool_
stack_allocator_pool() LIBCOPP_MACRO_NOEXCEPT
stack_allocator_pool(const std::shared_ptr< pool_type > &p) LIBCOPP_MACRO_NOEXCEPT
void deallocate(stack_context &ctx) LIBCOPP_MACRO_NOEXCEPT
void allocate(stack_context &ctx, std::size_t) LIBCOPP_MACRO_NOEXCEPT
void attach(const std::shared_ptr< pool_type > &p) LIBCOPP_MACRO_NOEXCEPT