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