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_UNSYNCHRONIZED_POOL_RESOURCE_H
10#define _LIBCPP___MEMORY_RESOURCE_UNSYNCHRONIZED_POOL_RESOURCE_H
11
12#include <__config>
13#include <__memory_resource/memory_resource.h>
14#include <__memory_resource/pool_options.h>
15#include <cstddef>
16#include <cstdint>
17
18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19#  pragma GCC system_header
20#endif
21
22#if _LIBCPP_STD_VER > 14
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26namespace pmr {
27
28// [mem.res.pool.overview]
29
30class _LIBCPP_TYPE_VIS unsynchronized_pool_resource : public memory_resource {
31  class __fixed_pool;
32
33  class __adhoc_pool {
34    struct __chunk_footer;
35    __chunk_footer* __first_;
36
37  public:
38    _LIBCPP_HIDE_FROM_ABI explicit __adhoc_pool() : __first_(nullptr) {}
39
40    void __release_ptr(memory_resource* __upstream);
41    void* __do_allocate(memory_resource* __upstream, size_t __bytes, size_t __align);
42    void __do_deallocate(memory_resource* __upstream, void* __p, size_t __bytes, size_t __align);
43  };
44
45  static const size_t __min_blocks_per_chunk = 16;
46  static const size_t __min_bytes_per_chunk  = 1024;
47  static const size_t __max_blocks_per_chunk = (size_t(1) << 20);
48  static const size_t __max_bytes_per_chunk  = (size_t(1) << 30);
49
50  static const int __log2_smallest_block_size      = 3;
51  static const size_t __smallest_block_size        = 8;
52  static const size_t __default_largest_block_size = (size_t(1) << 20);
53  static const size_t __max_largest_block_size     = (size_t(1) << 30);
54
55  size_t __pool_block_size(int __i) const;
56  int __log2_pool_block_size(int __i) const;
57  int __pool_index(size_t __bytes, size_t __align) const;
58
59public:
60  unsynchronized_pool_resource(const pool_options& __opts, memory_resource* __upstream);
61
62  _LIBCPP_HIDE_FROM_ABI unsynchronized_pool_resource()
63      : unsynchronized_pool_resource(pool_options(), get_default_resource()) {}
64
65  _LIBCPP_HIDE_FROM_ABI explicit unsynchronized_pool_resource(memory_resource* __upstream)
66      : unsynchronized_pool_resource(pool_options(), __upstream) {}
67
68  _LIBCPP_HIDE_FROM_ABI explicit unsynchronized_pool_resource(const pool_options& __opts)
69      : unsynchronized_pool_resource(__opts, get_default_resource()) {}
70
71  unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete;
72
73  _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~unsynchronized_pool_resource() override { release(); }
74
75  unsynchronized_pool_resource& operator=(const unsynchronized_pool_resource&) = delete;
76
77  void release();
78
79  _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
80
81  [[__gnu__::__pure__]] pool_options options() const;
82
83protected:
84  void* do_allocate(size_t __bytes, size_t __align) override; // key function
85
86  void do_deallocate(void* __p, size_t __bytes, size_t __align) override;
87
88  _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool do_is_equal(const memory_resource& __other) const _NOEXCEPT override {
89    return &__other == this;
90  }
91
92private:
93  memory_resource* __res_;
94  __adhoc_pool __adhoc_pool_;
95  __fixed_pool* __fixed_pools_;
96  int __num_fixed_pools_;
97  uint32_t __options_max_blocks_per_chunk_;
98};
99
100} // namespace pmr
101
102_LIBCPP_END_NAMESPACE_STD
103
104#endif // _LIBCPP_STD_VER > 14
105
106#endif // _LIBCPP___MEMORY_RESOURCE_UNSYNCHRONIZED_POOL_RESOURCE_H
107