blob: 95d2299973f93fcbd7c53ad35ede5ae8ef59cccf (
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() : m_refCount(0), m_ptr(0) {}
RefCount(T *ptr) : m_ptr(ptr) { m_refCount = new int(1); }
RefCount(const RefCount<T> &other) { cpy(other); }
~RefCount() {
if (m_refCount)
dispose();
}
RefCount &operator=(const RefCount<T> &other) {
if (this == &other)
return *this;
if (m_refCount)
dispose();
cpy(other);
return *this;
}
void init(T *ptr) {
assert(!m_ptr && "overrunning non NULL pointer");
assert(!m_refCount && "overrunning non NULL pointer");
m_refCount = 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(m_refCount && "NULL ref counter");
assert(*m_refCount && "zero ref counter");
}
void cpy(const RefCount<T> &other) {
m_refCount = other.m_refCount;
m_ptr = other.m_ptr;
if (m_refCount)
++*m_refCount;
}
void dispose() {
sanity();
if (0 == --*m_refCount) {
delete m_refCount;
delete m_ptr;
m_ptr = 0;
m_refCount = 0;
}
}
int *m_refCount;
T *m_ptr;
}; // End RefCount
} // namespace SPIR
#endif // SPIRV_MANGLER_REFCOUNT_H
|