blob: 0ff1d81d6f621356e4126af180bc28614aad1faa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
//===--------------------------- Refcount.h ------------------------------===//
//
// SPIR Tools
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===---------------------------------------------------------------------===//
/*
* Contributed by: Intel Corporation
*/
#ifndef SPIRV_MANGLER_REFCOUNT_H
#define SPIRV_MANGLER_REFCOUNT_H
#include <assert.h>
namespace SPIR {
template <typename T> class RefCount {
public:
RefCount() : Count(0), m_ptr(0) {}
RefCount(T *ptr) : m_ptr(ptr) { Count = new int(1); }
RefCount(const RefCount<T> &other) { cpy(other); }
~RefCount() {
if (Count)
dispose();
}
RefCount &operator=(const RefCount<T> &other) {
if (this == &other)
return *this;
if (Count)
dispose();
cpy(other);
return *this;
}
void init(T *ptr) {
assert(!m_ptr && "overrunning non NULL pointer");
assert(!Count && "overrunning non NULL pointer");
Count = new int(1);
m_ptr = ptr;
}
bool isNull() const { return (!m_ptr); }
// Pointer access
const T &operator*() const {
sanity();
return *m_ptr;
}
T &operator*() {
sanity();
return *m_ptr;
}
operator T *() { return m_ptr; }
operator const T *() const { return m_ptr; }
T *operator->() { return m_ptr; }
const T *operator->() const { return m_ptr; }
private:
void sanity() const {
assert(m_ptr && "NULL pointer");
assert(Count && "NULL ref counter");
assert(*Count && "zero ref counter");
}
void cpy(const RefCount<T> &other) {
Count = other.Count;
m_ptr = other.m_ptr;
if (Count)
++*Count;
}
void dispose() {
sanity();
if (0 == --*Count) {
delete Count;
delete m_ptr;
m_ptr = 0;
Count = 0;
}
}
int *Count;
T *m_ptr;
}; // End RefCount
} // namespace SPIR
#endif // SPIRV_MANGLER_REFCOUNT_H
|