diff options
author | vvvv <[email protected]> | 2024-11-01 15:41:40 +0300 |
---|---|---|
committer | vvvv <[email protected]> | 2024-11-01 15:55:52 +0300 |
commit | 3325f745e67f7f442790822b5c9c5e9996708be7 (patch) | |
tree | f7318d68bbe8990092715436444b05297ce35777 /yql/essentials/utils/limiting_allocator.cpp | |
parent | 6dce3f1c71786f2694b73b1a5155efc58f4557dd (diff) |
Moved yql/utils YQL-19206
Также была выделена жирная зависимость из yql/utils в yql/utils/network, в результате library/cpp/getopt была добавлена явно в те проекты, которые ее ранее наследовали, а не указывали явно
commit_hash:36aa4c41f807b4cbbf70a3ed7ac0a1a5079bb75d
Diffstat (limited to 'yql/essentials/utils/limiting_allocator.cpp')
-rw-r--r-- | yql/essentials/utils/limiting_allocator.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/yql/essentials/utils/limiting_allocator.cpp b/yql/essentials/utils/limiting_allocator.cpp new file mode 100644 index 00000000000..0ff84f90378 --- /dev/null +++ b/yql/essentials/utils/limiting_allocator.cpp @@ -0,0 +1,35 @@ +#include "limiting_allocator.h" + +#include <util/memory/pool.h> +#include <util/generic/yexception.h> + +namespace { +class TLimitingAllocator : public IAllocator { +public: + TLimitingAllocator(size_t limit, IAllocator* allocator) : Alloc_(allocator), Limit_(limit) {}; + TBlock Allocate(size_t len) override final { + if (Allocated_ + len > Limit_) { + throw std::runtime_error("Out of memory"); + } + Allocated_ += len; + return Alloc_->Allocate(len); + } + + void Release(const TBlock& block) override final { + Y_ENSURE(Allocated_ >= block.Len); + Allocated_ -= block.Len; + Alloc_->Release(block); + } + +private: + IAllocator* Alloc_; + size_t Allocated_ = 0; + const size_t Limit_; +}; +} + +namespace NYql { +std::unique_ptr<IAllocator> MakeLimitingAllocator(size_t limit, IAllocator* underlying) { + return std::make_unique<TLimitingAllocator>(limit, underlying); +} +} |