/usr/include/crystalspace-2.0/csutil/refcount.h is in libcrystalspace-dev 2.0+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | /*
Crystal Space Reference Counting Interface
Copyright (C) 2002 by Jorrit Tyberghein
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __CS_REFCOUNT_H__
#define __CS_REFCOUNT_H__
/**\file
* Reference Counting Interface
*/
#include "csextern.h"
#include "csutil/threading/atomicops.h"
#include "csutil/reftrackeraccess.h"
/**
* This is a class which provides basic reference-counting semantics. It can
* be used in conjunction with the smart pointer template class csRef (see
* <ref.h>). This class itself provides no functionality beyond
* reference counting. It is intended that you should subclass csRefCount and
* add needed functionality.
*/
class csRefCount
{
protected:
int ref_count;
// To avoid a problem with MSVC and multiple DLLs (with separate memory
// space) we have to use a virtual destructor.
// @@@ Another alternative is to force this function to be non-inline, as
// doing so will most likely achieve the same result.
virtual void Delete ()
{
delete this;
}
virtual ~csRefCount ()
{
csRefTrackerAccess::TrackDestruction (this, ref_count);
}
public:
//@{
/// Initialize object and set reference to 1.
csRefCount () : ref_count (1)
{
csRefTrackerAccess::TrackConstruction (this);
}
csRefCount (const csRefCount& other) : ref_count (1)
{
csRefTrackerAccess::TrackConstruction (this);
}
//@}
/// Increase the number of references to this object.
void IncRef ()
{
csRefTrackerAccess::TrackIncRef (this, ref_count);
ref_count++;
}
/// Decrease the number of references to this object.
void DecRef ()
{
csRefTrackerAccess::TrackDecRef (this, ref_count);
ref_count--;
if (ref_count <= 0)
Delete ();
}
/// Get the reference count (only for debugging).
int GetRefCount () const { return ref_count; }
};
namespace CS
{
namespace Utility
{
/**
* This is a class which provides basic reference-counting semantics. It can
* be used in conjunction with the smart pointer template class csRef (see
* <ref.h>). This class itself provides no functionality beyond
* reference counting. It is intended that you should subclass FastRefCount
* and add needed functionality.
*
* This class has slightly less overhead than csRefCount, but is also less
* generally useable:
* - Pointers can *not* be passed across plugin boundaries.
* - Deriving from a class C derived from \a ActualClass without using a
* virtual destructor in C can lead to the wrong destructor being
* called.
* - Using virtual methods eliminates the overhead advantage.
*/
template<typename ActualClass>
class FastRefCount
{
protected:
int ref_count;
~FastRefCount ()
{
csRefTrackerAccess::TrackDestruction (this, ref_count);
}
public:
//@{
/// Initialize object and set reference to 1.
FastRefCount () : ref_count (1)
{
csRefTrackerAccess::TrackConstruction (this);
}
FastRefCount (const FastRefCount& other) : ref_count (1)
{
csRefTrackerAccess::TrackConstruction (this);
}
//@}
/// Increase the number of references to this object.
void IncRef ()
{
csRefTrackerAccess::TrackIncRef (this, ref_count);
ref_count++;
}
/// Decrease the number of references to this object.
void DecRef ()
{
csRefTrackerAccess::TrackDecRef (this, ref_count);
ref_count--;
if (ref_count <= 0)
delete static_cast<ActualClass*> (this);
}
/// Get the reference count (only for debugging).
int GetRefCount () const { return ref_count; }
};
/**
* This class is used to hold a reference count seperate to the normal one.
* It allows the tracking of 'internal' references (such as those held by a
* collection), and the execution of some action when the ref count decrements
* to 0.
*/
class InternalRefCount
{
protected:
int internal_ref_count;
// To be implemented as needed.
virtual void InternalRemove() { return; }
virtual ~InternalRefCount ()
{
csRefTrackerAccess::TrackDestruction (this, internal_ref_count);
}
public:
/// Initialize object and set internal references to 0.
InternalRefCount () : internal_ref_count (0)
{
csRefTrackerAccess::TrackConstruction (this);
}
/// Increase the number of internal references to this object.
void InternalIncRef()
{
csRefTrackerAccess::TrackIncRef (this, internal_ref_count);
internal_ref_count++;
}
/// Decrease the number of internal references to this object.
void InternalDecRef ()
{
csRefTrackerAccess::TrackDecRef (this, internal_ref_count);
internal_ref_count--;
if (internal_ref_count <= 0)
{
InternalRemove();
}
}
/// Get the reference count (only for debugging).
int GetInternalRefCount () const { return internal_ref_count; }
};
/**
* This is a class which provides basic atomic reference-counting semantics.
* It behaves like csRefCount, with the difference that the reference count
* is increased/decreased atomically, making this class suitable for using
* reference-counted objects across threads.
*/
class AtomicRefCount
{
protected:
int32 ref_count;
// To avoid a problem with MSVC and multiple DLLs (with separate memory
// space) we have to use a virtual destructor.
// @@@ Another alternative is to force this function to be non-inline, as
// doing so will most likely achieve the same result.
virtual void Delete ()
{
delete this;
}
virtual ~AtomicRefCount ()
{
csRefTrackerAccess::TrackDestruction (this, ref_count);
}
public:
//@{
/// Initialize object and set reference to 1.
AtomicRefCount () : ref_count (1)
{
csRefTrackerAccess::TrackConstruction (this);
}
AtomicRefCount (const AtomicRefCount& other) : ref_count (1)
{
csRefTrackerAccess::TrackConstruction (this);
}
//@}
/// Increase the number of references to this object.
void IncRef ()
{
csRefTrackerAccess::TrackIncRef (this, ref_count);
CS::Threading::AtomicOperations::Increment (&ref_count);
}
/// Decrease the number of references to this object.
void DecRef ()
{
csRefTrackerAccess::TrackDecRef (this, ref_count);
if (CS::Threading::AtomicOperations::Decrement (&ref_count) == 0)
Delete ();
}
/// Get the reference count (only for debugging).
int GetRefCount () const
{ return CS::Threading::AtomicOperations::Read (&ref_count); }
};
} // namespace Utility
} // namespace CS
#endif // __CS_REFCOUNT_H__
|