LCOV - code coverage report
Current view: top level - src/zerocopy - fast_buffer_pool.cc (source / functions) Hit Total Coverage
Test: vlink Lines: 27 28 96.4 %
Date: 2026-09-30 18:30:16 Functions: 3 3 100.0 %
Branches: 17 18 94.4 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2026 by Thun Lu. All rights reserved.
       3                 :            :  * Author: Thun Lu <thun.lu@zohomail.cn>
       4                 :            :  * Repo:   https://github.com/thun-res/vlink
       5                 :            :  *  _    __   __      _           __
       6                 :            :  * | |  / /  / /     (_) ____    / /__
       7                 :            :  * | | / /  / /     / / / __ \  / //_/
       8                 :            :  * | |/ /  / /___  / / / / / / / ,<
       9                 :            :  * |___/  /_____/ /_/ /_/ /_/ /_/|_|
      10                 :            :  *
      11                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
      12                 :            :  * you may not use this file except in compliance with the License.
      13                 :            :  * You may obtain a copy of the License at
      14                 :            :  *
      15                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
      16                 :            :  *
      17                 :            :  * Unless required by applicable law or agreed to in writing, software
      18                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      19                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      20                 :            :  * See the License for the specific language governing permissions and
      21                 :            :  * limitations under the License.
      22                 :            :  */
      23                 :            : 
      24                 :            : #include "./zerocopy/fast_buffer_pool.h"
      25                 :            : 
      26                 :            : #include <utility>
      27                 :            : #include <vector>
      28                 :            : 
      29                 :            : #include "./zerocopy/fast_buffer_manager.h"
      30                 :            : 
      31                 :            : namespace vlink {
      32                 :            : 
      33                 :            : namespace zerocopy {
      34                 :            : 
      35                 :          8 : bool FastBufferPool::create(size_t size, size_t depth, const FastBuffer::Config& config) noexcept {
      36                 :          8 :   std::vector<FastBuffer> slots;
      37                 :            : 
      38   [ +  +  +  +  :          8 :   if VUNLIKELY (size == 0 || depth == 0 || depth > slots.max_size()) {
          +  +  +  +  +  
                      + ]
      39                 :          3 :     return false;
      40                 :            :   }
      41                 :            : 
      42                 :          5 :   slots.reserve(depth);
      43                 :            : 
      44         [ +  + ]:         12 :   for (size_t index = 0; index < depth; ++index) {
      45         [ -  + ]:          7 :     if VUNLIKELY (!slots.emplace_back().create(size, config)) {
      46                 :          0 :       return false;
      47                 :            :     }
      48                 :            :   }
      49                 :            : 
      50                 :          5 :   slots_ = std::move(slots);
      51                 :          5 :   next_ = 0;
      52                 :          5 :   return true;
      53                 :          8 : }
      54                 :            : 
      55                 :         20 : bool FastBufferPool::acquire(FastBuffer& buffer) noexcept {
      56                 :         20 :   buffer.clear();
      57                 :            : 
      58                 :         20 :   const size_t depth = slots_.size();
      59                 :         20 :   auto& manager = FastBufferManager::get();
      60                 :            : 
      61         [ +  + ]:         30 :   for (size_t offset = 0; offset < depth; ++offset) {
      62                 :         21 :     const size_t index = (next_ + offset) % depth;
      63                 :         21 :     const FastBuffer& slot = slots_[index];
      64                 :            : 
      65         [ +  + ]:         21 :     if (!manager.reclaim(slot.buffer_)) {
      66                 :         10 :       continue;
      67                 :            :     }
      68                 :            : 
      69                 :         11 :     next_ = (index + 1) % depth;
      70                 :         11 :     return buffer.shallow_copy(slot);
      71                 :            :   }
      72                 :            : 
      73                 :          9 :   return false;
      74                 :            : }
      75                 :            : 
      76                 :          2 : void FastBufferPool::clear() noexcept {
      77                 :          2 :   slots_.clear();
      78                 :          2 :   next_ = 0;
      79                 :          2 : }
      80                 :            : 
      81                 :            : }  // namespace zerocopy
      82                 :            : 
      83                 :            : }  // namespace vlink

Generated by: LCOV version 1.14