Coverage Report

Created: 2023-12-11 06:17

/proc/self/cwd/src/common/utils/ref_counted.h
Line
Count
Source
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_COUNTED_H_
17
#define SRC_COMMON_UTILS_REF_COUNTED_H_
18
19
#include <iostream>
20
21
#include "src/common/utils/ref_counter.h"
22
23
namespace raksha {
24
25
template <typename T>
26
class intrusive_ptr;
27
28
// Specialize this class for your type if you want custom reference counting.
29
template <typename T>
30
class RefCountManager {
31
249k
  static void Retain(T* ptr) { ptr->Retain(); }
raksha::RefCountManager<raksha::ir::types::TypeBase>::Retain(raksha::ir::types::TypeBase*)
Line
Count
Source
31
249k
  static void Retain(T* ptr) { ptr->Retain(); }
Unexecuted instantiation: raksha::RefCountManager<raksha::ir::AttributeBase const>::Retain(raksha::ir::AttributeBase const*)
32
249k
  static void Release(T* ptr) { ptr->Release(); }
raksha::RefCountManager<raksha::ir::types::TypeBase>::Release(raksha::ir::types::TypeBase*)
Line
Count
Source
32
249k
  static void Release(T* ptr) { ptr->Release(); }
Unexecuted instantiation: raksha::RefCountManager<raksha::ir::AttributeBase const>::Release(raksha::ir::AttributeBase const*)
33
34
  // So that it can call `Retain` and `Release`.
35
  friend class intrusive_ptr<T>;
36
};
37
38
// A helper class that allows a type `T` to be reference counted for an
39
// `intrusive_ptr`.
40
template <typename T>
41
class RefCounted {
42
 protected:
43
124k
  RefCounted() : count_(0) {}
44
45
  // Disable copies and moves of RefCounted objects because they should be done
46
  // by classes like intrusive_ptr, which update the reference counts
47
  // appropriately.
48
  RefCounted(const RefCounted& other) = delete;
49
  RefCounted(const RefCounted&& other) = delete;
50
  RefCounted& operator=(const RefCounted&) = delete;
51
  RefCounted& operator=(RefCounted&&) = delete;
52
53
 private:
54
249k
  void Retain() const { count_.FetchAdd(1); }
raksha::RefCounted<raksha::ir::types::TypeBase>::Retain() const
Line
Count
Source
54
249k
  void Retain() const { count_.FetchAdd(1); }
Unexecuted instantiation: raksha::RefCounted<raksha::ir::AttributeBase>::Retain() const
55
56
249k
  void Release() const {
57
249k
    size_t old = count_.FetchSub(1);
58
249k
    if (old == 1) {
59
124k
      delete this;
60
124k
    }
61
249k
  }
raksha::RefCounted<raksha::ir::types::TypeBase>::Release() const
Line
Count
Source
56
249k
  void Release() const {
57
249k
    size_t old = count_.FetchSub(1);
58
249k
    if (old == 1) {
59
124k
      delete this;
60
124k
    }
61
249k
  }
Unexecuted instantiation: raksha::RefCounted<raksha::ir::AttributeBase>::Release() const
62
63
 protected:
64
124k
  virtual ~RefCounted() {}
65
66
 private:
67
  // So that it can call `Retain` and `Release`.
68
  template <typename U>
69
  friend class RefCountManager;
70
71
  mutable RefCounter count_;
72
};
73
74
}  // namespace raksha
75
76
#endif  // SRC_COMMON_UTILS_REF_COUNTED_H_