Coverage Report

Created: 2024-02-25 06:37

/src/ntopng/include/Bitmask.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 * (C) 2013-24 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 *
20
 */
21
22
/**
23
 * @file Bitmask.h
24
 *
25
 * @brief      Bitmask class implementation.
26
 * @details    A Bitmask instance represents a simple bitmask, where bits can be
27
 * set, cleared and read.
28
 */
29
30
#ifndef _BITMASK_H_
31
#define _BITMASK_H_
32
33
#include "ntop_includes.h"
34
35
/*
36
  NOTE
37
38
  Bitmask is not thread safe and locking is not a bad idea
39
  however we believe that (on most architectures) writing a
40
  bimask byte while reading it won't be a problem.
41
 */
42
43
class Bitmask {
44
 private:
45
  u_int32_t tot_elems; /**< The bitmask size in bits */
46
  u_int32_t num_elems; /**< The bitmask size in bytes */
47
  u_int32_t *bits;     /**< The bitmask */
48
49
  void bitmask_set(u_int32_t n);
50
  void bitmask_clr(u_int32_t n);
51
  bool bitmask_isset(u_int32_t n);
52
  void bitmask_clr_all();
53
54
 public:
55
  Bitmask(u_int32_t num_tot_elems);
56
  ~Bitmask();
57
58
  /**
59
   * Sets a bit in the bitmask.
60
   * @param bit The bit position.
61
   */
62
48
  inline void set_bit(u_int32_t bit) { bitmask_set(bit); }
63
64
  /**
65
   * Clears a bit in the bitmask.
66
   * @param bit The bit position.
67
   */
68
1
  inline void clear_bit(u_int32_t bit) { bitmask_clr(bit); }
69
70
  /**
71
   * Checks if a bit is set.
72
   * @param bit The bit position.
73
   * @return True if the bit is set, false otherwise.
74
   */
75
8.61k
  inline bool is_set_bit(u_int32_t bit) {
76
8.61k
    return (bitmask_isset(bit) ? true : false);
77
8.61k
  }
78
79
  /**
80
   * Clears all the bits in the bitmask.
81
   */
82
0
  inline void clear_all_bits() { bitmask_clr_all(); }
83
84
  void print();
85
};
86
87
#endif /* _BITMASK_H_ */