Coverage Report

Created: 2023-12-11 06:17

/proc/self/cwd/src/common/utils/ref_counter.h
Line
Count
Source (jump to first uncovered line)
1
//-----------------------------------------------------------------------------
2
// Copyright 2022 Google LLC
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//     https://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//-----------------------------------------------------------------------------
16
#ifndef SRC_COMMON_UTILS_REF_COUNTER_H_
17
#define SRC_COMMON_UTILS_REF_COUNTER_H_
18
19
#include <cstddef>
20
21
#include "src/common/logging/logging.h"
22
23
namespace raksha {
24
25
// Note that this is not a thread-safe class.
26
class RefCounter {
27
 public:
28
0
  RefCounter() : count_(0) {}
29
124k
  RefCounter(size_t init) : count_(init) {}
30
31
0
  operator size_t() const { return count_; }
32
33
  // Adds the given increment and returns the old value.
34
249k
  size_t FetchAdd(size_t increment) {
35
249k
    size_t old = count_;
36
249k
    count_ += increment;
37
249k
    return old;
38
249k
  }
39
40
  // Subtracts the given increment and returns the old value.
41
249k
  size_t FetchSub(size_t decrement) {
42
249k
    CHECK(count_ >= decrement) << "FetchSub can decrement below zero.";
43
249k
    size_t old = count_;
44
249k
    count_ -= decrement;
45
249k
    return old;
46
249k
  }
47
48
 private:
49
  size_t count_;
50
};
51
52
}  // namespace raksha
53
54
#endif  // SRC_COMMON_UTILS_REF_COUNTER_H_