xref: /llvm-project/libcxx/include/__memory_resource/unsynchronized_pool_resource.h (revision e99c4906e44ae3f921fa05356909d006cda8d954)
1243da90eSArthur O'Dwyer //===----------------------------------------------------------------------===//
2243da90eSArthur O'Dwyer //
3243da90eSArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4243da90eSArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5243da90eSArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6243da90eSArthur O'Dwyer //
7243da90eSArthur O'Dwyer //===----------------------------------------------------------------------===//
8243da90eSArthur O'Dwyer 
9243da90eSArthur O'Dwyer #ifndef _LIBCPP___MEMORY_RESOURCE_UNSYNCHRONIZED_POOL_RESOURCE_H
10243da90eSArthur O'Dwyer #define _LIBCPP___MEMORY_RESOURCE_UNSYNCHRONIZED_POOL_RESOURCE_H
11243da90eSArthur O'Dwyer 
12243da90eSArthur O'Dwyer #include <__config>
13*e99c4906SNikolas Klauser #include <__cstddef/size_t.h>
14243da90eSArthur O'Dwyer #include <__memory_resource/memory_resource.h>
15243da90eSArthur O'Dwyer #include <__memory_resource/pool_options.h>
16243da90eSArthur O'Dwyer #include <cstdint>
17243da90eSArthur O'Dwyer 
18243da90eSArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19243da90eSArthur O'Dwyer #  pragma GCC system_header
20243da90eSArthur O'Dwyer #endif
21243da90eSArthur O'Dwyer 
224f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 17
23243da90eSArthur O'Dwyer 
24243da90eSArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
25243da90eSArthur O'Dwyer 
26243da90eSArthur O'Dwyer namespace pmr {
27243da90eSArthur O'Dwyer 
28243da90eSArthur O'Dwyer // [mem.res.pool.overview]
29243da90eSArthur O'Dwyer 
302da049a1SLouis Dionne class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI unsynchronized_pool_resource : public memory_resource {
31243da90eSArthur O'Dwyer   class __fixed_pool;
32243da90eSArthur O'Dwyer 
33243da90eSArthur O'Dwyer   class __adhoc_pool {
34243da90eSArthur O'Dwyer     struct __chunk_footer;
35243da90eSArthur O'Dwyer     __chunk_footer* __first_;
36243da90eSArthur O'Dwyer 
37243da90eSArthur O'Dwyer   public:
38243da90eSArthur O'Dwyer     _LIBCPP_HIDE_FROM_ABI explicit __adhoc_pool() : __first_(nullptr) {}
39243da90eSArthur O'Dwyer 
40243da90eSArthur O'Dwyer     void __release_ptr(memory_resource* __upstream);
41243da90eSArthur O'Dwyer     void* __do_allocate(memory_resource* __upstream, size_t __bytes, size_t __align);
42243da90eSArthur O'Dwyer     void __do_deallocate(memory_resource* __upstream, void* __p, size_t __bytes, size_t __align);
43243da90eSArthur O'Dwyer   };
44243da90eSArthur O'Dwyer 
45243da90eSArthur O'Dwyer   static const size_t __min_blocks_per_chunk = 16;
46243da90eSArthur O'Dwyer   static const size_t __min_bytes_per_chunk  = 1024;
47243da90eSArthur O'Dwyer   static const size_t __max_blocks_per_chunk = (size_t(1) << 20);
48243da90eSArthur O'Dwyer   static const size_t __max_bytes_per_chunk  = (size_t(1) << 30);
49243da90eSArthur O'Dwyer 
50243da90eSArthur O'Dwyer   static const int __log2_smallest_block_size      = 3;
51243da90eSArthur O'Dwyer   static const size_t __smallest_block_size        = 8;
52243da90eSArthur O'Dwyer   static const size_t __default_largest_block_size = (size_t(1) << 20);
53243da90eSArthur O'Dwyer   static const size_t __max_largest_block_size     = (size_t(1) << 30);
54243da90eSArthur O'Dwyer 
55243da90eSArthur O'Dwyer   size_t __pool_block_size(int __i) const;
56243da90eSArthur O'Dwyer   int __log2_pool_block_size(int __i) const;
57243da90eSArthur O'Dwyer   int __pool_index(size_t __bytes, size_t __align) const;
58243da90eSArthur O'Dwyer 
59243da90eSArthur O'Dwyer public:
60243da90eSArthur O'Dwyer   unsynchronized_pool_resource(const pool_options& __opts, memory_resource* __upstream);
61243da90eSArthur O'Dwyer 
62243da90eSArthur O'Dwyer   _LIBCPP_HIDE_FROM_ABI unsynchronized_pool_resource()
63243da90eSArthur O'Dwyer       : unsynchronized_pool_resource(pool_options(), get_default_resource()) {}
64243da90eSArthur O'Dwyer 
65243da90eSArthur O'Dwyer   _LIBCPP_HIDE_FROM_ABI explicit unsynchronized_pool_resource(memory_resource* __upstream)
66243da90eSArthur O'Dwyer       : unsynchronized_pool_resource(pool_options(), __upstream) {}
67243da90eSArthur O'Dwyer 
68243da90eSArthur O'Dwyer   _LIBCPP_HIDE_FROM_ABI explicit unsynchronized_pool_resource(const pool_options& __opts)
69243da90eSArthur O'Dwyer       : unsynchronized_pool_resource(__opts, get_default_resource()) {}
70243da90eSArthur O'Dwyer 
71243da90eSArthur O'Dwyer   unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete;
72243da90eSArthur O'Dwyer 
73bfb12231SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~unsynchronized_pool_resource() override { release(); }
74243da90eSArthur O'Dwyer 
75243da90eSArthur O'Dwyer   unsynchronized_pool_resource& operator=(const unsynchronized_pool_resource&) = delete;
76243da90eSArthur O'Dwyer 
77243da90eSArthur O'Dwyer   void release();
78243da90eSArthur O'Dwyer 
79243da90eSArthur O'Dwyer   _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
80243da90eSArthur O'Dwyer 
81f4ca5da2SNikolas Klauser   [[__gnu__::__pure__]] pool_options options() const;
82243da90eSArthur O'Dwyer 
83243da90eSArthur O'Dwyer protected:
84243da90eSArthur O'Dwyer   void* do_allocate(size_t __bytes, size_t __align) override; // key function
85243da90eSArthur O'Dwyer 
86243da90eSArthur O'Dwyer   void do_deallocate(void* __p, size_t __bytes, size_t __align) override;
87243da90eSArthur O'Dwyer 
8806385491SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
89243da90eSArthur O'Dwyer     return &__other == this;
90243da90eSArthur O'Dwyer   }
91243da90eSArthur O'Dwyer 
92243da90eSArthur O'Dwyer private:
93243da90eSArthur O'Dwyer   memory_resource* __res_;
94243da90eSArthur O'Dwyer   __adhoc_pool __adhoc_pool_;
95243da90eSArthur O'Dwyer   __fixed_pool* __fixed_pools_;
96243da90eSArthur O'Dwyer   int __num_fixed_pools_;
97243da90eSArthur O'Dwyer   uint32_t __options_max_blocks_per_chunk_;
98243da90eSArthur O'Dwyer };
99243da90eSArthur O'Dwyer 
100243da90eSArthur O'Dwyer } // namespace pmr
101243da90eSArthur O'Dwyer 
102243da90eSArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
103243da90eSArthur O'Dwyer 
1044f15267dSNikolas Klauser #endif // _LIBCPP_STD_VER >= 17
105243da90eSArthur O'Dwyer 
106243da90eSArthur O'Dwyer #endif // _LIBCPP___MEMORY_RESOURCE_UNSYNCHRONIZED_POOL_RESOURCE_H
107