|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * */ |
| 24 | +#ifndef UNIFIEDCACHE_MOONCAKE_STORE_CC_HOST_BUFFER_POOL_H |
| 25 | +#define UNIFIEDCACHE_MOONCAKE_STORE_CC_HOST_BUFFER_POOL_H |
| 26 | + |
| 27 | +#include <chrono> |
| 28 | +#include <condition_variable> |
| 29 | +#include <cstddef> |
| 30 | +#include <memory> |
| 31 | +#include <mutex> |
| 32 | +#include "thread/index_pool.h" |
| 33 | + |
| 34 | +namespace UC::MooncakeStore { |
| 35 | + |
| 36 | +class HostBufferPool { |
| 37 | +public: |
| 38 | + HostBufferPool() = default; |
| 39 | + ~HostBufferPool() = default; |
| 40 | + |
| 41 | + void Setup(uint32_t count, size_t unitSize) |
| 42 | + { |
| 43 | + if (count == 0 || unitSize == 0) { return; } |
| 44 | + size_t totalSize = static_cast<size_t>(count) * unitSize; |
| 45 | + if (totalSize / unitSize != count) { return; } |
| 46 | + unitSize_ = unitSize; |
| 47 | + count_ = count; |
| 48 | + pool_ = std::make_unique<char[]>(totalSize); |
| 49 | + index_.Setup(count); |
| 50 | + } |
| 51 | + |
| 52 | + void* Acquire() |
| 53 | + { |
| 54 | + if (!pool_ || unitSize_ == 0) { return nullptr; } |
| 55 | + auto idx = index_.Acquire(); |
| 56 | + if (idx == IndexPool::npos) { return nullptr; } |
| 57 | + return pool_.get() + static_cast<size_t>(idx) * unitSize_; |
| 58 | + } |
| 59 | + |
| 60 | + void* AcquireWithTimeout(std::chrono::milliseconds timeout) |
| 61 | + { |
| 62 | + if (!pool_ || unitSize_ == 0) { return nullptr; } |
| 63 | + auto idx = index_.Acquire(); |
| 64 | + if (idx != IndexPool::npos) { return pool_.get() + static_cast<size_t>(idx) * unitSize_; } |
| 65 | + std::unique_lock<std::mutex> lk(cvMtx_); |
| 66 | + auto deadline = std::chrono::steady_clock::now() + timeout; |
| 67 | + while (true) { |
| 68 | + cv_.wait_until(lk, deadline); |
| 69 | + idx = index_.Acquire(); |
| 70 | + if (idx != IndexPool::npos) { |
| 71 | + return pool_.get() + static_cast<size_t>(idx) * unitSize_; |
| 72 | + } |
| 73 | + if (std::chrono::steady_clock::now() >= deadline) { return nullptr; } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + void Release(void* buf) |
| 78 | + { |
| 79 | + if (!buf || !pool_ || unitSize_ == 0) { return; } |
| 80 | + auto offset = static_cast<char*>(buf) - pool_.get(); |
| 81 | + if (offset < 0 || static_cast<size_t>(offset) >= static_cast<size_t>(count_) * unitSize_) { |
| 82 | + return; |
| 83 | + } |
| 84 | + if (static_cast<size_t>(offset) % unitSize_ != 0) { return; } |
| 85 | + auto idx = static_cast<IndexPool::Index>(static_cast<size_t>(offset) / unitSize_); |
| 86 | + index_.Release(idx); |
| 87 | + cv_.notify_one(); |
| 88 | + } |
| 89 | + |
| 90 | + size_t UnitSize() const { return unitSize_; } |
| 91 | + uint32_t Count() const { return count_; } |
| 92 | + |
| 93 | +private: |
| 94 | + std::unique_ptr<char[]> pool_; |
| 95 | + size_t unitSize_{0}; |
| 96 | + uint32_t count_{0}; |
| 97 | + IndexPool index_; |
| 98 | + std::mutex cvMtx_; |
| 99 | + std::condition_variable cv_; |
| 100 | +}; |
| 101 | + |
| 102 | +} // namespace UC::MooncakeStore |
| 103 | + |
| 104 | +#endif |
0 commit comments