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_SYNCHRONIZED_POOL_RESOURCE_H 10243da90eSArthur O'Dwyer #define _LIBCPP___MEMORY_RESOURCE_SYNCHRONIZED_POOL_RESOURCE_H 11243da90eSArthur O'Dwyer 12243da90eSArthur O'Dwyer #include <__config> 13*4a8329cdSNikolas 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 <__memory_resource/unsynchronized_pool_resource.h> 17*4a8329cdSNikolas Klauser #include <__mutex/mutex.h> 18*4a8329cdSNikolas Klauser #include <__mutex/unique_lock.h> 19243da90eSArthur O'Dwyer 20243da90eSArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 21243da90eSArthur O'Dwyer # pragma GCC system_header 22243da90eSArthur O'Dwyer #endif 23243da90eSArthur O'Dwyer 244f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 17 25243da90eSArthur O'Dwyer 26243da90eSArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD 27243da90eSArthur O'Dwyer 28243da90eSArthur O'Dwyer namespace pmr { 29243da90eSArthur O'Dwyer 30243da90eSArthur O'Dwyer // [mem.res.pool.overview] 31243da90eSArthur O'Dwyer 322da049a1SLouis Dionne class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI synchronized_pool_resource : public memory_resource { 33243da90eSArthur O'Dwyer public: 34243da90eSArthur O'Dwyer _LIBCPP_HIDE_FROM_ABI synchronized_pool_resource(const pool_options& __opts, memory_resource* __upstream) 35243da90eSArthur O'Dwyer : __unsync_(__opts, __upstream) {} 36243da90eSArthur O'Dwyer 37243da90eSArthur O'Dwyer _LIBCPP_HIDE_FROM_ABI synchronized_pool_resource() 38243da90eSArthur O'Dwyer : synchronized_pool_resource(pool_options(), get_default_resource()) {} 39243da90eSArthur O'Dwyer 40243da90eSArthur O'Dwyer _LIBCPP_HIDE_FROM_ABI explicit synchronized_pool_resource(memory_resource* __upstream) 41243da90eSArthur O'Dwyer : synchronized_pool_resource(pool_options(), __upstream) {} 42243da90eSArthur O'Dwyer 43243da90eSArthur O'Dwyer _LIBCPP_HIDE_FROM_ABI explicit synchronized_pool_resource(const pool_options& __opts) 44243da90eSArthur O'Dwyer : synchronized_pool_resource(__opts, get_default_resource()) {} 45243da90eSArthur O'Dwyer 46243da90eSArthur O'Dwyer synchronized_pool_resource(const synchronized_pool_resource&) = delete; 47243da90eSArthur O'Dwyer 4883ce1397SNikolas Klauser _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~synchronized_pool_resource() override = default; 49243da90eSArthur O'Dwyer 50243da90eSArthur O'Dwyer synchronized_pool_resource& operator=(const synchronized_pool_resource&) = delete; 51243da90eSArthur O'Dwyer 52243da90eSArthur O'Dwyer _LIBCPP_HIDE_FROM_ABI void release() { 53c6f3b7bcSNikolas Klauser # if _LIBCPP_HAS_THREADS 54243da90eSArthur O'Dwyer unique_lock<mutex> __lk(__mut_); 55243da90eSArthur O'Dwyer # endif 56243da90eSArthur O'Dwyer __unsync_.release(); 57243da90eSArthur O'Dwyer } 58243da90eSArthur O'Dwyer 59243da90eSArthur O'Dwyer _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __unsync_.upstream_resource(); } 60243da90eSArthur O'Dwyer 61243da90eSArthur O'Dwyer _LIBCPP_HIDE_FROM_ABI pool_options options() const { return __unsync_.options(); } 62243da90eSArthur O'Dwyer 63243da90eSArthur O'Dwyer protected: 64bfb12231SNikolas Klauser _LIBCPP_HIDE_FROM_ABI_VIRTUAL void* do_allocate(size_t __bytes, size_t __align) override { 65c6f3b7bcSNikolas Klauser # if _LIBCPP_HAS_THREADS 66243da90eSArthur O'Dwyer unique_lock<mutex> __lk(__mut_); 67243da90eSArthur O'Dwyer # endif 68243da90eSArthur O'Dwyer return __unsync_.allocate(__bytes, __align); 69243da90eSArthur O'Dwyer } 70243da90eSArthur O'Dwyer 71bfb12231SNikolas Klauser _LIBCPP_HIDE_FROM_ABI_VIRTUAL void do_deallocate(void* __p, size_t __bytes, size_t __align) override { 72c6f3b7bcSNikolas Klauser # if _LIBCPP_HAS_THREADS 73243da90eSArthur O'Dwyer unique_lock<mutex> __lk(__mut_); 74243da90eSArthur O'Dwyer # endif 75243da90eSArthur O'Dwyer return __unsync_.deallocate(__p, __bytes, __align); 76243da90eSArthur O'Dwyer } 77243da90eSArthur O'Dwyer 78243da90eSArthur O'Dwyer bool do_is_equal(const memory_resource& __other) const noexcept override; // key function 79243da90eSArthur O'Dwyer 80243da90eSArthur O'Dwyer private: 81c6f3b7bcSNikolas Klauser # if _LIBCPP_HAS_THREADS 82243da90eSArthur O'Dwyer mutex __mut_; 83243da90eSArthur O'Dwyer # endif 84243da90eSArthur O'Dwyer unsynchronized_pool_resource __unsync_; 85243da90eSArthur O'Dwyer }; 86243da90eSArthur O'Dwyer 87243da90eSArthur O'Dwyer } // namespace pmr 88243da90eSArthur O'Dwyer 89243da90eSArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD 90243da90eSArthur O'Dwyer 914f15267dSNikolas Klauser #endif // _LIBCPP_STD_VER >= 17 92243da90eSArthur O'Dwyer 93243da90eSArthur O'Dwyer #endif // _LIBCPP___MEMORY_RESOURCE_SYNCHRONIZED_POOL_RESOURCE_H 94