LCOV - code coverage report
Current view: top level - vnsw/agent/cmn - index_vector.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 36 38 94.7 %
Date: 2026-08-03 02:19:58 Functions: 43 60 71.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #ifndef agent_index_vector_h
       6             : #define agent_index_vector_h
       7             : 
       8             : #include <cassert>
       9             : #include <vector>
      10             : #include <boost/dynamic_bitset.hpp>
      11             : #include <base/logging.h>
      12             : 
      13             : // Index management + Vector holding a pointer at allocated index
      14             : template <typename EntryType>
      15             : class IndexVector {
      16             : public:
      17             :     static const size_t kGrowSize = 32;
      18             : 
      19             :     typedef std::vector<EntryType> EntryTable;
      20             : 
      21       24627 :     IndexVector() {  }
      22       24627 :     ~IndexVector() {
      23             :         // Make sure the bitmap is empty
      24       24627 :         if (bitmap_.count() != bitmap_.size()) {
      25           3 :             LOG(ERROR, "IndexVector has " << bitmap_.size() - bitmap_.count()
      26             :                 << " entries in destructor");
      27             :         }
      28       24627 :         bitmap_.clear();
      29       24627 :     }
      30             : 
      31             :     // Get entry at an index
      32        4558 :     EntryType At(size_t index) const {
      33        4558 :         if (index >= bitmap_.size()) {
      34         340 :             return EntryType();
      35             :         }
      36        4218 :         return entries_[index];
      37             :     }
      38             : 
      39             :     // Allocate a new index and store entry in vector at allocated index
      40         299 :     size_t Insert(EntryType entry) {
      41         299 :         size_t index = bitmap_.find_first();
      42         299 :         if (index == bitmap_.npos) {
      43          14 :             size_t size = bitmap_.size();
      44          14 :             bitmap_.resize(size + kGrowSize, 1);
      45          14 :             entries_.resize(size + kGrowSize);
      46          14 :             index = bitmap_.find_first();
      47             :         }
      48             : 
      49         299 :         bitmap_.set(index, 0);
      50         299 :         entries_[index] = entry;
      51         299 :         return index;
      52             :     }
      53             : 
      54         196 :     size_t InsertAtIndex(uint32_t index, EntryType entry) {
      55         196 :         size_t size = bitmap_.size();
      56         196 :         if (size == 0 || size <= index) {
      57          14 :             bitmap_.resize(index + kGrowSize, 1);
      58          14 :             entries_.resize(index + kGrowSize);
      59             :         }
      60             : 
      61             :         // TODO(prabhjot) need to enable assertion below
      62             :         // currently disabled due to some issue with MPLS
      63             :         // label allocation
      64             :         // index should not be already in use
      65             :         // assert(bitmap_[index] == 1);
      66             : 
      67         196 :         bitmap_.set(index, 0);
      68         196 :         entries_[index] = entry;
      69         196 :         return index;
      70             :     }
      71             : 
      72             :     void Update(size_t index, EntryType entry) {
      73             :         assert(index < bitmap_.size());
      74             :         assert(bitmap_[index] == 0);
      75             :         entries_[index] = entry;
      76             :     }
      77             : 
      78         492 :     void Remove(size_t index) {
      79         492 :         assert(index < bitmap_.size());
      80         492 :         assert(bitmap_[index] == 0);
      81         492 :         bitmap_.set(index);
      82         492 :         entries_[index] = EntryType();
      83         492 :     }
      84             : 
      85           0 :     bool NoneIndexSet() {
      86           0 :         return (bitmap_.count() == bitmap_.size());
      87             :     }
      88             : 
      89         493 :     size_t InUseIndexCount() {
      90         493 :         return (bitmap_.size() - bitmap_.count());
      91             :     }
      92             : 
      93             : private:
      94             :     typedef boost::dynamic_bitset<> Bitmap;
      95             :     Bitmap bitmap_;
      96             :     EntryTable entries_;
      97             :     DISALLOW_COPY_AND_ASSIGN(IndexVector);
      98             : };
      99             : 
     100             : #endif

Generated by: LCOV version 1.14