ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RefCountedObject.h
Go to the documentation of this file.
1/*
2
3 @copyright
4
5 <pre>
6
7 Copyright 2018 Infineon Technologies AG
8
9 This file is part of ETISS tool, see <https://github.com/tum-ei-eda/etiss>.
10
11 The initial version of this software has been created with the funding support by the German Federal
12 Ministry of Education and Research (BMBF) in the project EffektiV under grant 01IS13022.
13
14 Redistribution and use in source and binary forms, with or without modification, are permitted
15 provided that the following conditions are met:
16
17 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
18 the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
21 and the following disclaimer in the documentation and/or other materials provided with the distribution.
22
23 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
24 or promote products derived from this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
27 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
29 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 POSSIBILITY OF SUCH DAMAGE.
34
35 </pre>
36
37 @author Chair of Electronic Design Automation, TUM
38
39 @version 0.1
40
41*/
42
43#ifndef REFCOUNTEDOBJECT_H_INCLUDED
44#define REFCOUNTEDOBJECT_H_INCLUDED
45
46#include "etiss/Misc.h"
47#include <type_traits>
48
49namespace etiss
50{
51
52template <typename allocatorT = void>
53class RefCountedObject;
54
55// declare templated access functions
56template <typename T, typename allocatorT>
58typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, bool>::type helper_decRef(
59 T *ptr);
60template <typename T, typename allocatorT>
61typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, void>::type helper_incRef(
62 T *ptr);
63
64template <typename T, typename allocatorT>
65typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, allocatorT *&>::type
67
68template <typename allocatorT>
71{
72 template <typename T_, typename allocatorT_>
73 friend typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT_>, T_>::value, bool>::type
74 helper_decRef(T_ *ptr);
75 template <typename T_, typename allocatorT_>
76 friend typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT_>, T_>::value, void>::type
77 helper_incRef(T_ *ptr);
78 template <typename T_, typename allocatorT_>
79 friend
80 typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT_>, T_>::value, allocatorT_ *&>::type
82
83 public:
84 typedef allocatorT refcount_allocatorT;
85 inline RefCountedObject() : refcount(0), allocator(0) {}
86 inline RefCountedObject(const RefCountedObject &cpy) : refcount(0), allocator(0) {}
88 inline virtual ~RefCountedObject() {}
89
90 private:
91 mutable size_t refcount;
92 mutable allocatorT *allocator;
93};
94
95template <typename T, typename allocatorT>
96typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, bool>::type helper_decRef(
97 T *ptr)
98{
99 if (unlikely(ptr == 0))
100 return false;
101#if DEBUG
102 if (ptr->refcount == 0)
103 throw std::bad_alloc();
104#endif
105 ptr->refcount--;
106 return ptr->refcount == 0;
107}
108template <typename T, typename allocatorT>
109typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, void>::type helper_incRef(
110 T *ptr)
111{
112 if (unlikely(ptr == 0))
113 return;
114#if DEBUG
115 if (ptr->refcount + 1 < ptr->refcount)
116 throw std::runtime_error("reference counting failed (overflow)");
117#endif
118 ptr->refcount++;
119}
120template <typename T, typename allocatorT>
121typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, allocatorT *&>::type
123{
124 return ptr->allocator;
125}
126
127template <typename T>
130inline void incRef(T *ptr)
131{
132 helper_incRef<T, typename T::refcount_allocatorT>(ptr);
133}
134
135template <typename T>
136typename std::enable_if<std::is_base_of<etiss::RefCountedObject<void>, T>::value, bool>::type decRef(T *ptr)
137{
138 bool ret = helper_decRef<T, void>(ptr);
139 if (unlikely(ret))
140 {
141 delete ptr;
142 ptr = 0;
143 }
144 return ret;
145}
146
147} // namespace etiss
148
149#endif // REFCOUNTEDOBJECT_H_INCLUDED
general configuration and logging
#define unlikely(x)
Definition types.h:74
base refcount class
friend std::enable_if< std::is_base_of< etiss::RefCountedObject< allocatorT_ >, T_ >::value, allocatorT_ *& >::type helper_allocator_ptr_ref(T_ *ptr)
friend std::enable_if< std::is_base_of< etiss::RefCountedObject< allocatorT_ >, T_ >::value, void >::type helper_incRef(T_ *ptr)
RefCountedObject(const RefCountedObject &cpy)
RefCountedObject(RefCountedObject &&cpy)
friend std::enable_if< std::is_base_of< etiss::RefCountedObject< allocatorT_ >, T_ >::value, bool >::type helper_decRef(T_ *ptr)
Page Table Entry (PTE) defines the composition of Page Frame Number (PFN) and relavant flags.
Definition Benchmark.h:53
void incRef(T *ptr)
std::enable_if< std::is_base_of< etiss::RefCountedObject< allocatorT >, T >::value, allocatorT *& >::type helper_allocator_ptr_ref(T *ptr)
std::enable_if< std::is_base_of< etiss::RefCountedObject< allocatorT >, T >::value, bool >::type helper_decRef(T *ptr)
std::enable_if< std::is_base_of< etiss::RefCountedObject< ObjectPool< T > >, T >::value, bool >::type decRef(T *ptr)
Definition ObjectPool.h:217
std::enable_if< std::is_base_of< etiss::RefCountedObject< allocatorT >, T >::value, void >::type helper_incRef(T *ptr)