ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
RefCountedObject.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// This file is part of ETISS. It is licensed under the BSD 3-Clause License; you may not use this file except in
4// compliance with the License. You should have received a copy of the license along with this project. If not, see the
5// LICENSE file.
6
7#ifndef REFCOUNTEDOBJECT_H_INCLUDED
8#define REFCOUNTEDOBJECT_H_INCLUDED
9
10#include "etiss/Misc.h"
11#include <type_traits>
12
13namespace etiss
14{
15
16template <typename allocatorT = void>
17class RefCountedObject;
18
19// declare templated access functions
20template <typename T, typename allocatorT>
22typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, bool>::type helper_decRef(
23 T *ptr);
24template <typename T, typename allocatorT>
25typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, void>::type helper_incRef(
26 T *ptr);
27
28template <typename T, typename allocatorT>
29typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, allocatorT *&>::type
31
32template <typename allocatorT>
35{
36 template <typename T_, typename allocatorT_>
37 friend typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT_>, T_>::value, bool>::type
38 helper_decRef(T_ *ptr);
39 template <typename T_, typename allocatorT_>
40 friend typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT_>, T_>::value, void>::type
41 helper_incRef(T_ *ptr);
42 template <typename T_, typename allocatorT_>
43 friend
44 typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT_>, T_>::value, allocatorT_ *&>::type
46
47 public:
48 typedef allocatorT refcount_allocatorT;
49 inline RefCountedObject() : refcount(0), allocator(0) {}
50 inline RefCountedObject(const RefCountedObject &cpy) : refcount(0), allocator(0) {}
52 inline virtual ~RefCountedObject() {}
53
54 private:
55 mutable size_t refcount;
56 mutable allocatorT *allocator;
57};
58
59template <typename T, typename allocatorT>
60typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, bool>::type helper_decRef(
61 T *ptr)
62{
63 if (unlikely(ptr == 0))
64 return false;
65#if DEBUG
66 if (ptr->refcount == 0)
67 throw std::bad_alloc();
68#endif
69 ptr->refcount--;
70 return ptr->refcount == 0;
71}
72template <typename T, typename allocatorT>
73typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, void>::type helper_incRef(
74 T *ptr)
75{
76 if (unlikely(ptr == 0))
77 return;
78#if DEBUG
79 if (ptr->refcount + 1 < ptr->refcount)
80 throw std::runtime_error("reference counting failed (overflow)");
81#endif
82 ptr->refcount++;
83}
84template <typename T, typename allocatorT>
85typename std::enable_if<std::is_base_of<etiss::RefCountedObject<allocatorT>, T>::value, allocatorT *&>::type
87{
88 return ptr->allocator;
89}
90
91template <typename T>
94inline void incRef(T *ptr)
95{
96 helper_incRef<T, typename T::refcount_allocatorT>(ptr);
97}
98
99template <typename T>
100typename std::enable_if<std::is_base_of<etiss::RefCountedObject<void>, T>::value, bool>::type decRef(T *ptr)
101{
102 bool ret = helper_decRef<T, void>(ptr);
103 if (unlikely(ret))
104 {
105 delete ptr;
106 ptr = 0;
107 }
108 return ret;
109}
110
111} // namespace etiss
112
113#endif // REFCOUNTEDOBJECT_H_INCLUDED
general configuration and logging
#define unlikely(x)
Definition types.h:36
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)
forwards: include/jit/*
Definition Benchmark.h:17
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:179
std::enable_if< std::is_base_of< etiss::RefCountedObject< allocatorT >, T >::value, void >::type helper_incRef(T *ptr)