LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - label_block.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 68 69 98.6 %
Date: 2026-08-24 02:06:43 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #include "base/label_block.h"
       6             : 
       7             : #include <stdio.h>
       8             : 
       9             : #include <cassert>
      10             : #include <string>
      11             : 
      12             : using std::string;
      13             : 
      14       10057 : LabelBlockManager::LabelBlockManager() {
      15       10057 :     refcount_ = 0;
      16       10057 : }
      17             : 
      18       10057 : LabelBlockManager::~LabelBlockManager() {
      19       10057 :     assert(blocks_.size() == 0);
      20       10057 : }
      21             : 
      22        1791 : LabelBlockPtr LabelBlockManager::LocateBlock(uint32_t first, uint32_t last) {
      23        1791 :     std::scoped_lock lock(mutex_);
      24             : 
      25        1792 :     for (LabelBlockList::iterator it = blocks_.begin();
      26        1974 :          it != blocks_.end(); ++it) {
      27         483 :         LabelBlock *block = *it;
      28         483 :         if (block->first_ == first && block->last_ == last) {
      29         301 :             return LabelBlockPtr(block);
      30             :         }
      31             :     }
      32             : 
      33        1491 :     LabelBlock *block = new LabelBlock(this, first, last);
      34        1491 :     blocks_.push_back(block);
      35        1491 :     return LabelBlockPtr(block);
      36        1792 : }
      37             : 
      38        1491 : void LabelBlockManager::RemoveBlock(LabelBlock *block) {
      39        1491 :     for (LabelBlockList::iterator it = blocks_.begin();
      40        1582 :          it != blocks_.end(); ++it) {
      41        1582 :         if (*it == block) {
      42        1491 :             blocks_.erase(it);
      43        1491 :             return;
      44             :         }
      45             :     }
      46           0 :     assert(false);
      47             : }
      48             : 
      49        1067 : size_t LabelBlockManager::size() {
      50        1067 :     std::scoped_lock lock(mutex_);
      51        2134 :     return blocks_.size();
      52        1067 : }
      53             : 
      54        6484 : LabelBlock::LabelBlock(uint32_t first, uint32_t last)
      55        6484 :     : block_manager_(NULL),
      56        6484 :       first_(first),
      57        6484 :       last_(last),
      58        6484 :       prev_pos_(BitSet::npos) {
      59        6484 :       refcount_ = 0;
      60        6484 : }
      61             : 
      62        1491 : LabelBlock::LabelBlock(
      63        1491 :         LabelBlockManager *block_manager, uint32_t first, uint32_t last)
      64        1491 :     : block_manager_(block_manager),
      65        1491 :       first_(first),
      66        1491 :       last_(last),
      67        1491 :       prev_pos_(BitSet::npos) {
      68        1491 :       refcount_ = 0;
      69        1491 : }
      70             : 
      71        7975 : LabelBlock::~LabelBlock() {
      72        7975 :     assert(used_bitset_.empty());
      73        7975 :     if (block_manager_)
      74        1491 :         block_manager_->RemoveBlock(this);
      75        7975 : }
      76             : 
      77        5684 : uint32_t LabelBlock::AllocateLabel() {
      78        5684 :     std::scoped_lock lock(mutex_);
      79             : 
      80             :     size_t pos;
      81        6237 :     for (int idx = 0; idx < 2; prev_pos_ = BitSet::npos, idx++) {
      82        6225 :         if (prev_pos_ == BitSet::npos) {
      83        2191 :             pos = used_bitset_.find_first_clear();
      84             :         } else {
      85        4034 :             pos = used_bitset_.find_next_clear(prev_pos_);
      86             :         }
      87             : 
      88        6225 :         if (first_ + pos <= last_) {
      89        5672 :             used_bitset_.set(pos);
      90        5672 :             prev_pos_ = pos;
      91        5672 :             return static_cast<uint32_t>(first_ + pos);
      92             :         }
      93             :     }
      94             : 
      95          12 :     return 0;
      96        5684 : }
      97             : 
      98        5672 : void LabelBlock::ReleaseLabel(uint32_t value) {
      99        5672 :     std::scoped_lock lock(mutex_);
     100             : 
     101        5672 :     assert(value >= first_ && value <= last_);
     102        5672 :     size_t pos = value - first_;
     103        5672 :     used_bitset_.reset(pos);
     104        5672 : }
     105             : 
     106           8 : string LabelBlock::ToString() const {
     107             :     char repr[32];
     108           8 :     snprintf(repr, sizeof(repr), "%u-%u", first_, last_);
     109           8 :     return repr;
     110             : }

Generated by: LCOV version 1.14