1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H
10 #define _LIBCPP___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H
12 #include <__availability>
14 #include <__memory/addressof.h>
15 #include <__memory_resource/memory_resource.h>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
22 #if _LIBCPP_STD_VER >= 17
24 _LIBCPP_BEGIN_NAMESPACE_STD
28 // [mem.res.monotonic.buffer]
30 class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resource
: public memory_resource
{
31 static const size_t __default_buffer_capacity
= 1024;
32 static const size_t __default_buffer_alignment
= 16;
34 struct __chunk_footer
{
35 __chunk_footer
* __next_
;
39 _LIBCPP_HIDE_FROM_ABI
size_t __allocation_size() {
40 return (reinterpret_cast<char*>(this) - __start_
) + sizeof(*this);
42 void* __try_allocate_from_chunk(size_t, size_t);
45 struct __initial_descriptor
{
52 void* __try_allocate_from_chunk(size_t, size_t);
56 _LIBCPP_HIDE_FROM_ABI
monotonic_buffer_resource()
57 : monotonic_buffer_resource(nullptr, __default_buffer_capacity
, get_default_resource()) {}
59 _LIBCPP_HIDE_FROM_ABI
explicit monotonic_buffer_resource(size_t __initial_size
)
60 : monotonic_buffer_resource(nullptr, __initial_size
, get_default_resource()) {}
62 _LIBCPP_HIDE_FROM_ABI
monotonic_buffer_resource(void* __buffer
, size_t __buffer_size
)
63 : monotonic_buffer_resource(__buffer
, __buffer_size
, get_default_resource()) {}
65 _LIBCPP_HIDE_FROM_ABI
explicit monotonic_buffer_resource(memory_resource
* __upstream
)
66 : monotonic_buffer_resource(nullptr, __default_buffer_capacity
, __upstream
) {}
68 _LIBCPP_HIDE_FROM_ABI
monotonic_buffer_resource(size_t __initial_size
, memory_resource
* __upstream
)
69 : monotonic_buffer_resource(nullptr, __initial_size
, __upstream
) {}
71 _LIBCPP_HIDE_FROM_ABI
monotonic_buffer_resource(void* __buffer
, size_t __buffer_size
, memory_resource
* __upstream
)
72 : __res_(__upstream
) {
73 __initial_
.__start_
= static_cast<char*>(__buffer
);
74 if (__buffer
!= nullptr) {
75 __initial_
.__cur_
= static_cast<char*>(__buffer
) + __buffer_size
;
76 __initial_
.__end_
= static_cast<char*>(__buffer
) + __buffer_size
;
78 __initial_
.__cur_
= nullptr;
79 __initial_
.__size_
= __buffer_size
;
84 monotonic_buffer_resource(const monotonic_buffer_resource
&) = delete;
86 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
~monotonic_buffer_resource() override
{ release(); }
88 monotonic_buffer_resource
& operator=(const monotonic_buffer_resource
&) = delete;
90 _LIBCPP_HIDE_FROM_ABI
void release() {
91 if (__initial_
.__start_
!= nullptr)
92 __initial_
.__cur_
= __initial_
.__end_
;
93 while (__chunks_
!= nullptr) {
94 __chunk_footer
* __next
= __chunks_
->__next_
;
95 __res_
->deallocate(__chunks_
->__start_
, __chunks_
->__allocation_size(), __chunks_
->__align_
);
100 _LIBCPP_HIDE_FROM_ABI memory_resource
* upstream_resource() const { return __res_
; }
103 void* do_allocate(size_t __bytes
, size_t __alignment
) override
; // key function
105 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
void do_deallocate(void*, size_t, size_t) override
{}
107 _LIBCPP_HIDE_FROM_ABI_VIRTUAL
bool do_is_equal(const memory_resource
& __other
) const _NOEXCEPT override
{
108 return this == std::addressof(__other
);
112 __initial_descriptor __initial_
;
113 __chunk_footer
* __chunks_
;
114 memory_resource
* __res_
;
119 _LIBCPP_END_NAMESPACE_STD
121 #endif // _LIBCPP_STD_VER >= 17
123 #endif // _LIBCPP___MEMORY_RESOURCE_MONOTONIC_BUFFER_RESOURCE_H