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-10-05 02:12:29 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       10258 : LabelBlockManager::LabelBlockManager() {
      15       10258 :     refcount_ = 0;
      16       10258 : }
      17             : 
      18       10258 : LabelBlockManager::~LabelBlockManager() {
      19       10258 :     assert(blocks_.size() == 0);
      20       10258 : }
      21             : 
      22        1792 : LabelBlockPtr LabelBlockManager::LocateBlock(uint32_t first, uint32_t last) {
      23        1792 :     std::scoped_lock lock(mutex_);
      24             : 
      25        1791 :     for (LabelBlockList::iterator it = blocks_.begin();
      26        1974 :          it != blocks_.end(); ++it) {
      27         847 :         LabelBlock *block = *it;
      28         847 :         if (block->first_ == first && block->last_ == last) {
      29         664 :             return LabelBlockPtr(block);
      30             :         }
      31             :     }
      32             : 
      33        1126 :     LabelBlock *block = new LabelBlock(this, first, last);
      34        1128 :     blocks_.push_back(block);
      35        1126 :     return LabelBlockPtr(block);
      36        1792 : }
      37             : 
      38        1128 : void LabelBlockManager::RemoveBlock(LabelBlock *block) {
      39        1128 :     for (LabelBlockList::iterator it = blocks_.begin();
      40        1218 :          it != blocks_.end(); ++it) {
      41        1218 :         if (*it == block) {
      42        1128 :             blocks_.erase(it);
      43        1128 :             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        6436 : LabelBlock::LabelBlock(uint32_t first, uint32_t last)
      55        6436 :     : block_manager_(NULL),
      56        6436 :       first_(first),
      57        6436 :       last_(last),
      58        6436 :       prev_pos_(BitSet::npos) {
      59        6436 :       refcount_ = 0;
      60        6436 : }
      61             : 
      62        1126 : LabelBlock::LabelBlock(
      63        1126 :         LabelBlockManager *block_manager, uint32_t first, uint32_t last)
      64        1126 :     : block_manager_(block_manager),
      65        1128 :       first_(first),
      66        1128 :       last_(last),
      67        1128 :       prev_pos_(BitSet::npos) {
      68        1128 :       refcount_ = 0;
      69        1128 : }
      70             : 
      71        7564 : LabelBlock::~LabelBlock() {
      72        7564 :     assert(used_bitset_.empty());
      73        7564 :     if (block_manager_)
      74        1128 :         block_manager_->RemoveBlock(this);
      75        7564 : }
      76             : 
      77        5560 : uint32_t LabelBlock::AllocateLabel() {
      78        5560 :     std::scoped_lock lock(mutex_);
      79             : 
      80             :     size_t pos;
      81        6102 :     for (int idx = 0; idx < 2; prev_pos_ = BitSet::npos, idx++) {
      82        6090 :         if (prev_pos_ == BitSet::npos) {
      83        2171 :             pos = used_bitset_.find_first_clear();
      84             :         } else {
      85        3919 :             pos = used_bitset_.find_next_clear(prev_pos_);
      86             :         }
      87             : 
      88        6090 :         if (first_ + pos <= last_) {
      89        5548 :             used_bitset_.set(pos);
      90        5548 :             prev_pos_ = pos;
      91        5548 :             return static_cast<uint32_t>(first_ + pos);
      92             :         }
      93             :     }
      94             : 
      95          12 :     return 0;
      96        5560 : }
      97             : 
      98        5548 : void LabelBlock::ReleaseLabel(uint32_t value) {
      99        5548 :     std::scoped_lock lock(mutex_);
     100             : 
     101        5548 :     assert(value >= first_ && value <= last_);
     102        5548 :     size_t pos = value - first_;
     103        5548 :     used_bitset_.reset(pos);
     104        5548 : }
     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