|
26 | 26 | #include "arrow/result.h" |
27 | 27 | #include "arrow/status.h" |
28 | 28 | #include "arrow/type_fwd.h" |
| 29 | +#include "arrow/util/macros.h" |
29 | 30 | #include "arrow/util/visibility.h" |
30 | 31 |
|
31 | 32 | namespace arrow { |
@@ -245,6 +246,64 @@ class ARROW_EXPORT ProxyMemoryPool : public MemoryPool { |
245 | 246 | std::unique_ptr<ProxyMemoryPoolImpl> impl_; |
246 | 247 | }; |
247 | 248 |
|
| 249 | +/// EXPERIMENTAL MemoryPool wrapper with an upper limit |
| 250 | +class ARROW_EXPORT CappedMemoryPool : public MemoryPool { |
| 251 | + public: |
| 252 | + CappedMemoryPool(MemoryPool* wrapped_pool, int64_t bytes_allocated_limit) |
| 253 | + : wrapped_(wrapped_pool), bytes_allocated_limit_(bytes_allocated_limit) {} |
| 254 | + |
| 255 | + using MemoryPool::Allocate; |
| 256 | + using MemoryPool::Reallocate; |
| 257 | + |
| 258 | + Status Allocate(int64_t size, int64_t alignment, uint8_t** out) override { |
| 259 | + const auto attempted = size + wrapped_->bytes_allocated(); |
| 260 | + if (ARROW_PREDICT_FALSE(attempted >= bytes_allocated_limit_)) { |
| 261 | + return OutOfMemory(attempted); |
| 262 | + } |
| 263 | + return wrapped_->Allocate(size, alignment, out); |
| 264 | + } |
| 265 | + |
| 266 | + Status Reallocate(int64_t old_size, int64_t new_size, int64_t alignment, |
| 267 | + uint8_t** ptr) override { |
| 268 | + const auto attempted = new_size - old_size + wrapped_->bytes_allocated(); |
| 269 | + if (ARROW_PREDICT_FALSE(attempted >= bytes_allocated_limit_)) { |
| 270 | + return OutOfMemory(attempted); |
| 271 | + } |
| 272 | + return wrapped_->Reallocate(old_size, new_size, alignment, ptr); |
| 273 | + } |
| 274 | + |
| 275 | + void Free(uint8_t* buffer, int64_t size, int64_t alignment) override { |
| 276 | + return wrapped_->Free(buffer, size, alignment); |
| 277 | + } |
| 278 | + |
| 279 | + void ReleaseUnused() override { wrapped_->ReleaseUnused(); } |
| 280 | + |
| 281 | + void PrintStats() override { wrapped_->PrintStats(); } |
| 282 | + |
| 283 | + int64_t bytes_allocated() const override { return wrapped_->bytes_allocated(); } |
| 284 | + |
| 285 | + int64_t max_memory() const override { return wrapped_->max_memory(); } |
| 286 | + |
| 287 | + int64_t total_bytes_allocated() const override { |
| 288 | + return wrapped_->total_bytes_allocated(); |
| 289 | + } |
| 290 | + |
| 291 | + int64_t num_allocations() const override { return wrapped_->num_allocations(); } |
| 292 | + |
| 293 | + std::string backend_name() const override { return wrapped_->backend_name(); } |
| 294 | + |
| 295 | + private: |
| 296 | + Status OutOfMemory(int64_t value) { |
| 297 | + return Status::OutOfMemory( |
| 298 | + "MemoryPool bytes_allocated cap exceeded: " |
| 299 | + "limit=", |
| 300 | + bytes_allocated_limit_, ", attempted=", value); |
| 301 | + } |
| 302 | + |
| 303 | + MemoryPool* wrapped_; |
| 304 | + int64_t bytes_allocated_limit_; |
| 305 | +}; |
| 306 | + |
248 | 307 | /// \brief Return a process-wide memory pool based on the system allocator. |
249 | 308 | ARROW_EXPORT MemoryPool* system_memory_pool(); |
250 | 309 |
|
|
0 commit comments