1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___MEMORY_RESOURCE_UNSYNCHRONIZED_POOL_RESOURCE_H
10 #define _LIBCPP___MEMORY_RESOURCE_UNSYNCHRONIZED_POOL_RESOURCE_H
11 
12 #include <__availability>
13 #include <__config>
14 #include <__memory_resource/memory_resource.h>
15 #include <__memory_resource/pool_options.h>
16 #include <cstddef>
17 #include <cstdint>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 #if _LIBCPP_STD_VER >= 17
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 namespace pmr {
28 
29 // [mem.res.pool.overview]
30 
31 class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI unsynchronized_pool_resource : public memory_resource {
32   class __fixed_pool;
33 
34   class __adhoc_pool {
35     struct __chunk_footer;
36     __chunk_footer* __first_;
37 
38   public:
__adhoc_pool()39     _LIBCPP_HIDE_FROM_ABI explicit __adhoc_pool() : __first_(nullptr) {}
40 
41     void __release_ptr(memory_resource* __upstream);
42     void* __do_allocate(memory_resource* __upstream, size_t __bytes, size_t __align);
43     void __do_deallocate(memory_resource* __upstream, void* __p, size_t __bytes, size_t __align);
44   };
45 
46   static const size_t __min_blocks_per_chunk = 16;
47   static const size_t __min_bytes_per_chunk  = 1024;
48   static const size_t __max_blocks_per_chunk = (size_t(1) << 20);
49   static const size_t __max_bytes_per_chunk  = (size_t(1) << 30);
50 
51   static const int __log2_smallest_block_size      = 3;
52   static const size_t __smallest_block_size        = 8;
53   static const size_t __default_largest_block_size = (size_t(1) << 20);
54   static const size_t __max_largest_block_size     = (size_t(1) << 30);
55 
56   size_t __pool_block_size(int __i) const;
57   int __log2_pool_block_size(int __i) const;
58   int __pool_index(size_t __bytes, size_t __align) const;
59 
60 public:
61   unsynchronized_pool_resource(const pool_options& __opts, memory_resource* __upstream);
62 
unsynchronized_pool_resource()63   _LIBCPP_HIDE_FROM_ABI unsynchronized_pool_resource()
64       : unsynchronized_pool_resource(pool_options(), get_default_resource()) {}
65 
unsynchronized_pool_resource(memory_resource * __upstream)66   _LIBCPP_HIDE_FROM_ABI explicit unsynchronized_pool_resource(memory_resource* __upstream)
67       : unsynchronized_pool_resource(pool_options(), __upstream) {}
68 
unsynchronized_pool_resource(const pool_options & __opts)69   _LIBCPP_HIDE_FROM_ABI explicit unsynchronized_pool_resource(const pool_options& __opts)
70       : unsynchronized_pool_resource(__opts, get_default_resource()) {}
71 
72   unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete;
73 
~unsynchronized_pool_resource()74   _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~unsynchronized_pool_resource() override { release(); }
75 
76   unsynchronized_pool_resource& operator=(const unsynchronized_pool_resource&) = delete;
77 
78   void release();
79 
upstream_resource()80   _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
81 
82   [[__gnu__::__pure__]] pool_options options() const;
83 
84 protected:
85   void* do_allocate(size_t __bytes, size_t __align) override; // key function
86 
87   void do_deallocate(void* __p, size_t __bytes, size_t __align) override;
88 
do_is_equal(const memory_resource & __other)89   _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
90     return &__other == this;
91   }
92 
93 private:
94   memory_resource* __res_;
95   __adhoc_pool __adhoc_pool_;
96   __fixed_pool* __fixed_pools_;
97   int __num_fixed_pools_;
98   uint32_t __options_max_blocks_per_chunk_;
99 };
100 
101 } // namespace pmr
102 
103 _LIBCPP_END_NAMESPACE_STD
104 
105 #endif // _LIBCPP_STD_VER >= 17
106 
107 #endif // _LIBCPP___MEMORY_RESOURCE_UNSYNCHRONIZED_POOL_RESOURCE_H
108