Add PR check to suggest alternatives to using undef (#118506)
[llvm-project.git] / libcxx / include / __memory_resource / synchronized_pool_resource.h
blobbcc1ac4a172e3ac0996cd94583b8e51042168b8a
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___MEMORY_RESOURCE_SYNCHRONIZED_POOL_RESOURCE_H
10 #define _LIBCPP___MEMORY_RESOURCE_SYNCHRONIZED_POOL_RESOURCE_H
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>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
22 #endif
24 #if _LIBCPP_STD_VER >= 17
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 namespace pmr {
30 // [mem.res.pool.overview]
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) {}
37 _LIBCPP_HIDE_FROM_ABI synchronized_pool_resource()
38 : synchronized_pool_resource(pool_options(), get_default_resource()) {}
40 _LIBCPP_HIDE_FROM_ABI explicit synchronized_pool_resource(memory_resource* __upstream)
41 : synchronized_pool_resource(pool_options(), __upstream) {}
43 _LIBCPP_HIDE_FROM_ABI explicit synchronized_pool_resource(const pool_options& __opts)
44 : synchronized_pool_resource(__opts, get_default_resource()) {}
46 synchronized_pool_resource(const synchronized_pool_resource&) = delete;
48 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~synchronized_pool_resource() override = default;
50 synchronized_pool_resource& operator=(const synchronized_pool_resource&) = delete;
52 _LIBCPP_HIDE_FROM_ABI void release() {
53 # if _LIBCPP_HAS_THREADS
54 unique_lock<mutex> __lk(__mut_);
55 # endif
56 __unsync_.release();
59 _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __unsync_.upstream_resource(); }
61 _LIBCPP_HIDE_FROM_ABI pool_options options() const { return __unsync_.options(); }
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);
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);
78 bool do_is_equal(const memory_resource& __other) const noexcept override; // key function
80 private:
81 # if _LIBCPP_HAS_THREADS
82 mutex __mut_;
83 # endif
84 unsynchronized_pool_resource __unsync_;
87 } // namespace pmr
89 _LIBCPP_END_NAMESPACE_STD
91 #endif // _LIBCPP_STD_VER >= 17
93 #endif // _LIBCPP___MEMORY_RESOURCE_SYNCHRONIZED_POOL_RESOURCE_H