CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
array_pool.h
1
3#pragma once
4
5#include <cstdint>
6#include <vector>
7#include <memory>
8#include <mutex>
9#include <system/object.h>
10#include <system/shared_ptr.h>
11#include <system/array.h>
12#include <system/exceptions.h>
13
14namespace System {
15namespace Buffers {
16
19template <typename T>
20class ArrayPool final : public System::Object
21{
22 typedef ArrayPool<T> ThisType;
24 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
25 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
26
27public:
32 explicit ArrayPool(int32_t maxArrayLength = 1024 * 1024,
33 int32_t maxArraysPerBucket = 50)
34 : m_maxArrayLength(maxArrayLength)
35 , m_maxArraysPerBucket(maxArraysPerBucket)
36 {
37 if (maxArrayLength <= 0 || maxArraysPerBucket <= 0)
38 {
39 throw ArgumentOutOfRangeException(u"maxArrayLength / maxArraysPerBucket");
40 }
41
42 const int32_t bucketCount = GetBucketIndex(maxArrayLength) + 1;
43 m_buckets.reserve(bucketCount);
44 for (int32_t i = 0; i < bucketCount; ++i)
45 {
46 m_buckets.push_back(std::make_unique<Bucket>(16 << i));
47 }
48 }
49
54 {
55 return MakeDefaultPool();
56 }
57
62 static SharedPtr<ArrayPool<T>> Create(int32_t maxArrayLength, int32_t maxArraysPerBucket)
63 {
64 return MakeObject<ArrayPool<T>>(maxArrayLength, maxArraysPerBucket);
65 }
66
68 ArrayPtr<T> Rent(int32_t minimumLength)
69 {
70 if (minimumLength < 0)
71 {
72 throw ArgumentOutOfRangeException(u"minimumLength");
73 }
74 if (minimumLength == 0)
75 {
76 return System::MakeArray<T>(0);
77 }
78
79 if (minimumLength > m_maxArrayLength)
80 {
81 return System::MakeArray<T>(minimumLength);
82 }
83
84 Bucket& bucket = *m_buckets[GetBucketIndex(minimumLength)];
85 {
86 std::lock_guard<std::mutex> guard(bucket.m_mutex);
87 if (!bucket.m_arrays.empty())
88 {
89 ArrayPtr<T> buffer = std::move(bucket.m_arrays.back());
90 bucket.m_arrays.pop_back();
91 return buffer;
92 }
93 }
94
95 return System::MakeArray<T>(bucket.m_bufferLength);
96 }
97
104 void Return(const ArrayPtr<T>& array, bool clearArray = false)
105 {
106 if (array == nullptr || array->get_Length() == 0)
107 {
108 return;
109 }
110
111 const int32_t len = array->get_Length();
112 const int32_t index = GetBucketIndex(len);
113 if (len > m_maxArrayLength ||
114 index >= static_cast<int32_t>(m_buckets.size()) ||
115 m_buckets[index]->m_bufferLength != len)
116 {
117 return; // not owned by the pool -> let reference counting / GC handle it
118 }
119
120 Bucket& bucket = *m_buckets[index];
121 {
122 std::lock_guard<std::mutex> guard(bucket.m_mutex);
123 if (clearArray)
124 {
125 System::Array<T>::Clear(array, 0, len);
126 }
127 if (bucket.m_arrays.size() < static_cast<size_t>(m_maxArraysPerBucket))
128 {
129 bucket.m_arrays.push_back(array);
130 }
131 }
132 }
133
134private:
137 static SharedPtr<ArrayPool<T>> MakeDefaultPool()
138 {
139 static SharedPtr<ArrayPool<T>> s_sharedInstance = MakeObject<ArrayPool<T>>();
140 return s_sharedInstance;
141 }
142
143 struct Bucket
144 {
145 int32_t m_bufferLength;
146 std::vector<ArrayPtr<T>> m_arrays;
147 std::mutex m_mutex;
148
149 explicit Bucket(int32_t length)
150 : m_bufferLength(length)
151 {
152 }
153 };
154
155 int32_t m_maxArrayLength;
156 int32_t m_maxArraysPerBucket;
157 std::vector<std::unique_ptr<Bucket>> m_buckets;
158
159 int32_t GetBucketIndex(int32_t minLength) const
160 {
161 int32_t size = 16, index = 0;
162 while (size < minLength && size < m_maxArrayLength)
163 {
164 size <<= 1;
165 ++index;
166 }
167 return index;
168 }
169};
170
171} // namespace Buffers
172} // namespace System
virtual void Clear() override
Not supported because the array represented by the current object is read-only.
Definition: array.h:501
Provides a thread-safe pool for reusing managed array instances (ArrayPtr<T>).
Definition: array_pool.h:21
ArrayPool(int32_t maxArrayLength=1024 *1024, int32_t maxArraysPerBucket=50)
Creates an ArrayPool instance.
Definition: array_pool.h:32
static SharedPtr< ArrayPool< T > > Create(int32_t maxArrayLength, int32_t maxArraysPerBucket)
Creates a new pool instance with custom configuration options.
Definition: array_pool.h:62
void Return(const ArrayPtr< T > &array, bool clearArray=false)
Returns a previously rented buffer back to the pool.
Definition: array_pool.h:104
static SharedPtr< ArrayPool< T > > get_Shared()
Retrieves the process-wide shared pool instance. The instance is created lazily on first access and c...
Definition: array_pool.h:53
ArrayPtr< T > Rent(int32_t minimumLength)
Obtains a buffer that is at least minimumLength long.
Definition: array_pool.h:68
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:51
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Definition: db_command.h:9
System::ExceptionWrapper< Details_ArgumentOutOfRangeException > ArgumentOutOfRangeException
Definition: throw_helper.h:24