9#include <system/object.h>
10#include <system/shared_ptr.h>
11#include <system/array.h>
12#include <system/exceptions.h>
24 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
25 RTTI_INFO_TEMPLATE_CLASS(
ThisType, ThisTypeBaseTypesInfo);
32 explicit ArrayPool(int32_t maxArrayLength = 1024 * 1024,
33 int32_t maxArraysPerBucket = 50)
34 : m_maxArrayLength(maxArrayLength)
35 , m_maxArraysPerBucket(maxArraysPerBucket)
37 if (maxArrayLength <= 0 || maxArraysPerBucket <= 0)
42 const int32_t bucketCount = GetBucketIndex(maxArrayLength) + 1;
43 m_buckets.reserve(bucketCount);
44 for (int32_t i = 0; i < bucketCount; ++i)
46 m_buckets.push_back(std::make_unique<Bucket>(16 << i));
55 return MakeDefaultPool();
64 return MakeObject<ArrayPool<T>>(maxArrayLength, maxArraysPerBucket);
70 if (minimumLength < 0)
74 if (minimumLength == 0)
76 return System::MakeArray<T>(0);
79 if (minimumLength > m_maxArrayLength)
81 return System::MakeArray<T>(minimumLength);
84 Bucket& bucket = *m_buckets[GetBucketIndex(minimumLength)];
86 std::lock_guard<std::mutex> guard(bucket.m_mutex);
87 if (!bucket.m_arrays.empty())
89 ArrayPtr<T> buffer = std::move(bucket.m_arrays.back());
90 bucket.m_arrays.pop_back();
95 return System::MakeArray<T>(bucket.m_bufferLength);
106 if (array ==
nullptr || array->get_Length() == 0)
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)
120 Bucket& bucket = *m_buckets[
index];
122 std::lock_guard<std::mutex> guard(bucket.m_mutex);
127 if (bucket.m_arrays.size() <
static_cast<size_t>(m_maxArraysPerBucket))
129 bucket.m_arrays.push_back(array);
140 return s_sharedInstance;
145 int32_t m_bufferLength;
146 std::vector<ArrayPtr<T>> m_arrays;
149 explicit Bucket(int32_t length)
150 : m_bufferLength(length)
155 int32_t m_maxArrayLength;
156 int32_t m_maxArraysPerBucket;
157 std::vector<std::unique_ptr<Bucket>> m_buckets;
159 int32_t GetBucketIndex(int32_t minLength)
const
161 int32_t size = 16,
index = 0;
162 while (size < minLength && size < m_maxArrayLength)
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