Warning: This file is not a C or C++ file. It does not have highlighting.
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_SYNCHRONIZED_POOL_RESOURCE_H |
10 | #define _LIBCPP___MEMORY_RESOURCE_SYNCHRONIZED_POOL_RESOURCE_H |
11 | |
12 | #include <__config> |
13 | #include <__cstddef/size_t.h> |
14 | #include <__memory_resource/memory_resource.h> |
15 | #include <__memory_resource/pool_options.h> |
16 | #include <__memory_resource/unsynchronized_pool_resource.h> |
17 | #include <__mutex/mutex.h> |
18 | #include <__mutex/unique_lock.h> |
19 | |
20 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
21 | # pragma GCC system_header |
22 | #endif |
23 | |
24 | #if _LIBCPP_STD_VER >= 17 |
25 | |
26 | _LIBCPP_BEGIN_NAMESPACE_STD |
27 | |
28 | namespace pmr { |
29 | |
30 | // [mem.res.pool.overview] |
31 | |
32 | class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI synchronized_pool_resource : public memory_resource { |
33 | public: |
34 | _LIBCPP_HIDE_FROM_ABI synchronized_pool_resource(const pool_options& __opts, memory_resource* __upstream) |
35 | : __unsync_(__opts, __upstream) {} |
36 | |
37 | _LIBCPP_HIDE_FROM_ABI synchronized_pool_resource() |
38 | : synchronized_pool_resource(pool_options(), get_default_resource()) {} |
39 | |
40 | _LIBCPP_HIDE_FROM_ABI explicit synchronized_pool_resource(memory_resource* __upstream) |
41 | : synchronized_pool_resource(pool_options(), __upstream) {} |
42 | |
43 | _LIBCPP_HIDE_FROM_ABI explicit synchronized_pool_resource(const pool_options& __opts) |
44 | : synchronized_pool_resource(__opts, get_default_resource()) {} |
45 | |
46 | synchronized_pool_resource(const synchronized_pool_resource&) = delete; |
47 | |
48 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~synchronized_pool_resource() override = default; |
49 | |
50 | synchronized_pool_resource& operator=(const synchronized_pool_resource&) = delete; |
51 | |
52 | _LIBCPP_HIDE_FROM_ABI void release() { |
53 | # if _LIBCPP_HAS_THREADS |
54 | unique_lock<mutex> __lk(__mut_); |
55 | # endif |
56 | __unsync_.release(); |
57 | } |
58 | |
59 | _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __unsync_.upstream_resource(); } |
60 | |
61 | _LIBCPP_HIDE_FROM_ABI pool_options options() const { return __unsync_.options(); } |
62 | |
63 | protected: |
64 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void* do_allocate(size_t __bytes, size_t __align) override { |
65 | # if _LIBCPP_HAS_THREADS |
66 | unique_lock<mutex> __lk(__mut_); |
67 | # endif |
68 | return __unsync_.allocate(__bytes, __align); |
69 | } |
70 | |
71 | _LIBCPP_HIDE_FROM_ABI_VIRTUAL void do_deallocate(void* __p, size_t __bytes, size_t __align) override { |
72 | # if _LIBCPP_HAS_THREADS |
73 | unique_lock<mutex> __lk(__mut_); |
74 | # endif |
75 | return __unsync_.deallocate(__p, __bytes, __align); |
76 | } |
77 | |
78 | bool do_is_equal(const memory_resource& __other) const noexcept override; // key function |
79 | |
80 | private: |
81 | # if _LIBCPP_HAS_THREADS |
82 | mutex __mut_; |
83 | # endif |
84 | unsynchronized_pool_resource __unsync_; |
85 | }; |
86 | |
87 | } // namespace pmr |
88 | |
89 | _LIBCPP_END_NAMESPACE_STD |
90 | |
91 | #endif // _LIBCPP_STD_VER >= 17 |
92 | |
93 | #endif // _LIBCPP___MEMORY_RESOURCE_SYNCHRONIZED_POOL_RESOURCE_H |
94 |
Warning: This file is not a C or C++ file. It does not have highlighting.