LCOV - code coverage report
Current view: top level - src/base - object_pool.cc (source / functions) Hit Total Coverage
Test: vlink Lines: 35 35 100.0 %
Date: 2026-06-27 19:56:04 Functions: 11 11 100.0 %
Branches: 34 54 63.0 %

           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 "./base/object_pool.h"
      25                 :            : 
      26                 :            : #include <mutex>
      27                 :            : #include <sstream>
      28                 :            : #include <stdexcept>
      29                 :            : 
      30                 :            : namespace vlink {
      31                 :            : 
      32                 :            : // ObjectPoolBase
      33                 :         43 : ObjectPoolBase::ObjectPoolBase(size_t max_size, size_t initial_size, Policy policy)
      34                 :         43 :     : policy_(policy), max_size_(max_size) {
      35   [ +  +  +  +  :         43 :   if VUNLIKELY (max_size_ > 0 && initial_size > max_size_) {
                   +  + ]
      36                 :          1 :     throw_invalid_size();
      37                 :            :   }
      38                 :         42 : }
      39                 :            : 
      40                 :         24 : size_t ObjectPoolBase::borrowed() const {
      41         [ +  - ]:         24 :   std::lock_guard lock(mutex_);
      42                 :            : 
      43                 :         24 :   return borrowed_;
      44                 :         24 : }
      45                 :            : 
      46                 :          7 : size_t ObjectPoolBase::total_created() const {
      47         [ +  - ]:          7 :   std::lock_guard lock(mutex_);
      48                 :            : 
      49                 :          7 :   return total_created_;
      50                 :          7 : }
      51                 :            : 
      52                 :          3 : size_t ObjectPoolBase::max_size() const noexcept { return max_size_; }
      53                 :            : 
      54                 :          1 : void ObjectPoolBase::safe_dec_borrowed_and_live() noexcept {
      55                 :          1 :   std::lock_guard lock(mutex_);
      56                 :            : 
      57         [ +  - ]:          1 :   if (borrowed_ > 0) {
      58                 :          1 :     --borrowed_;
      59                 :            :   }
      60                 :            : 
      61         [ +  - ]:          1 :   if (live_count_ > 0) {
      62                 :          1 :     --live_count_;
      63                 :            :   }
      64                 :          1 : }
      65                 :            : 
      66                 :       1070 : bool ObjectPoolBase::should_reset_on_acquire() const noexcept {
      67   [ +  +  +  + ]:       1070 :   return policy_ == kPolicyAcquire || policy_ == kPolicyBoth;
      68                 :            : }
      69                 :            : 
      70                 :       1087 : bool ObjectPoolBase::should_reset_on_release() const noexcept {
      71   [ +  +  +  + ]:       1087 :   return policy_ == kPolicyRelease || policy_ == kPolicyBoth;
      72                 :            : }
      73                 :            : 
      74         [ +  - ]:          1 : void ObjectPoolBase::throw_invalid_size() { throw std::invalid_argument("initial_size exceeds max_size"); }
      75                 :            : 
      76                 :          1 : void ObjectPoolBase::throw_factory_null_pre_fill() {
      77         [ +  - ]:          1 :   throw std::runtime_error("FactoryCallback returned nullptr during pre-fill");
      78                 :            : }
      79                 :            : 
      80         [ +  - ]:          3 : void ObjectPoolBase::throw_factory_null() { throw std::runtime_error("FactoryCallback returned nullptr"); }
      81                 :            : 
      82                 :          4 : void ObjectPoolBase::throw_exhausted(size_t pool_size) const {
      83         [ +  - ]:          4 :   std::ostringstream oss;
      84                 :            : 
      85   [ +  -  +  -  :          4 :   oss << "ObjectPool exhausted: max_size=" << max_size_ << " live_count=" << live_count_
             +  -  +  - ]
      86   [ +  -  +  -  :          4 :       << " total_created=" << total_created_ << " borrowed=" << borrowed_ << " pool_size=" << pool_size;
          +  -  +  -  +  
                -  +  - ]
      87                 :            : 
      88   [ +  -  +  - ]:          4 :   throw std::runtime_error(oss.str());
      89                 :          4 : }
      90                 :            : 
      91                 :            : }  // namespace vlink

Generated by: LCOV version 1.14