/usr/include/sc/util/ref/ref.h is in libsc-dev 2.3.1-16build1.
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | //
// ref.h --- definitions of the reference counting classes
//
// Copyright (C) 1996 Limit Point Systems, Inc.
//
// Author: Curtis Janssen <cljanss@limitpt.com>
// Maintainer: LPS
//
// This file is part of the SC Toolkit.
//
// The SC Toolkit 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, or (at your option)
// any later version.
//
// The SC Toolkit 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 the SC Toolkit; see the file COPYING.LIB. If not, write to
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
// The U.S. Government is granted a limited license as per AL 91-7.
//
// This is the main include file for the reference counting classes.
// This includes two other files: reftmpl.h and refmacr.h. The
// former is a template declaration for the reference counted classes
// and the latter is generated from the former by a perl script and
// provides CPP macros that declare reference counting classes.
//
// The behaviour of the package can be modified with the following five
// macros, each of which should be undefined, 0, or 1:
//
// REF_CHECK_STACK: If this is 1 referenced objects are checked to see if they
// reside on the stack, in which case storage for the object is not managed,
// if management is enabled. This feature can be confused by multiple threads
// and memory checking libraries.
//
// REF_MANAGE: If this is 1 the manage and unmanage members are enabled.
//
// REF_CHECK_MAX_NREF: If this is 1 the reference count is checked before
// it is incremented to make sure it isn't too big.
//
// REF_CHECK_MIN_NREF: If this is 1 the reference count is checked before
// it is decremented to make sure it isn't already zero.
//
// REF_USE_LOCKS: If this is 1 then critical regions are locked before they
// are entered. This prevents erroneous behavior when multiple threads
// share reference counted objects. This will slow down certain operations,
// so it should be set to 0 if your application does not need to be thread
// safe.
//
// If a macro is undefined, then the behaviour is architecture
// dependent--usually, the macro will be set to 1 in this case.
// For maximum efficiency and for normal operation after the program is
// debugged, compile with all of the above macros defined to zero.
// This can also be done with -DREF_OPTIMIZE.
//
// An include file can be used to set these options as well. This has
// the advantage that dependency checking will force an automatic
// recompile of all affected files if the options change. The file
// <scconfig.h> will be include if -DHAVE_CONFIG_H is specified.
//
// Note that all source code that uses references must be compiled with
// the same value REF_MANAGE. Changing this can change the storage layout
// and the interpretation of the reference count data.
#ifdef __GNUC__
#pragma interface
#endif
#ifndef _util_ref_ref_h
#define _util_ref_ref_h
#include <iostream>
#include <stdlib.h>
#include <limits.h>
#include <util/ref/identity.h>
#ifdef HAVE_CONFIG_H
#include <scconfig.h>
#endif
#ifdef REF_OPTIMIZE
#ifndef REF_CHECK_STACK
# define REF_CHECK_STACK 0
#endif
#ifndef REF_MANAGE
# define REF_MANAGE 0
#endif
#ifndef REF_CHECK_MAX_NREF
# define REF_CHECK_MAX_NREF 0
#endif
#ifndef REF_CHECK_MIN_NREF
# define REF_CHECK_MIN_NREF 0
#endif
#endif
#ifdef SUNMOS
#ifndef REF_CHECK_STACK
#define REF_CHECK_STACK 0
#endif
#else
#ifndef REF_CHECK_STACK
#define REF_CHECK_STACK 0
#endif
#endif
#ifndef REF_MANAGE
#define REF_MANAGE 1
#endif
#ifndef REF_CHECK_MAX_NREF
#define REF_CHECK_MAX_NREF 1
#endif
#ifndef REF_CHECK_MIN_NREF
#define REF_CHECK_MIN_NREF 1
#endif
#ifndef REF_USE_LOCKS
# if HAVE_STHREAD || HAVE_CREATETHREAD || HAVE_PTHREAD
# define REF_USE_LOCKS 1
# endif
#endif
#ifndef REF_ALWAYS_USE_LOCKS
# define REF_ALWAYS_USE_LOCKS 1
#endif
#if REF_CHECK_STACK
#include <unistd.h>
#ifndef HAVE_SBRK_DEC
extern "C" void * sbrk(ssize_t);
#endif
#define DO_REF_CHECK_STACK(p) (((void*) (p) > sbrk(0)) && (p)->managed())
#else // REF_CHECK_STACK
#define DO_REF_CHECK_STACK(p) (0)
#endif // REF_CHECK_STACK
#if REF_MANAGE
#define DO_REF_UNMANAGE(p) ((p)->unmanage())
#else // REF_MANAGE
#define DO_REF_UNMANAGE(p)
#endif // REF_MANAGE
#if REF_USE_LOCKS
#define __REF_LOCK__(p) p->lock_ptr()
#define __REF_UNLOCK__(p) p->unlock_ptr()
#if REF_ALWAYS_USE_LOCKS
#define __REF_INITLOCK__() use_locks(true)
#else
#define __REF_INITLOCK__() ref_lock_ = 0xff
#endif
#else
#define __REF_LOCK__(p)
#define __REF_UNLOCK__(p)
#define __REF_INITLOCK__()
#endif
namespace sc {
typedef unsigned long refcount_t;
/** The base class for all reference counted objects. If multiple
inheritance is used, RefCount must be virtually inherited from,
otherwise references to invalid memory will likely result.
Reference counting information is usually maintained by smart
pointer classes Ref, however this mechanism can be
supplemented or replaced by directly using the public
interface to RefCount.
The unmanage() member is only needed for special cases where memory
management must be turned off. For example, if a reference counted
object is created on the stack, memory management mechanisms based on
reference counting must be prohibited from deleting it. The unmanage()
member accomplishes this, but a better solution would be to allocate
the object on the heap with new and let a smart pointer manage the
memory for the object.
When using a debugger to look at reference counted objects the count is
maintained in the _reference_count_ member. However, this member is
encoded so that memory overwrites can be sometimes detected. Thus,
interpretation of _reference_count_ is not always straightforward.
*/
class RefCount: public Identity {
private:
#if REF_MANAGE
# define REF_MAX_NREF (UINT_MAX - 1)
# define REF_MANAGED_CODE UINT_MAX
#else
# define REF_MAX_NREF UINT_MAX
#endif
unsigned int _reference_count_;
#if REF_USE_LOCKS
unsigned char ref_lock_;
#endif
void error(const char*) const;
void too_many_refs() const;
void not_enough_refs() const;
protected:
RefCount(): _reference_count_(0) {
__REF_INITLOCK__();
//std::cout << "ref_lock_ = " << (int) ref_lock_ << std::endl;
}
RefCount(const RefCount&): _reference_count_(0) {
__REF_INITLOCK__();
//std::cout << "ref_lock_ = " << (int) ref_lock_ << std::endl;
}
// Assigment should not overwrite the reference count.
RefCount& operator=(const RefCount&) { return *this; }
public:
virtual ~RefCount();
/// Lock this object.
int lock_ptr() const;
/// Unlock this object.
int unlock_ptr() const;
/// start and stop using locks on this object
void use_locks(bool inVal);
/// Return the reference count.
refcount_t nreference() const {
# if REF_MANAGE
if (!managed()) return 1;
# endif
return _reference_count_;
}
/// Increment the reference count and return the new count.
refcount_t reference() {
# if REF_MANAGE
if (!managed()) return 1;
# endif
__REF_LOCK__(this);
# if REF_CHECK_MAX_NREF
if (_reference_count_ >= REF_MAX_NREF) too_many_refs();
# endif
_reference_count_++;
refcount_t r = _reference_count_;
__REF_UNLOCK__(this);
return r;
}
/// Decrement the reference count and return the new count.
refcount_t dereference() {
# if REF_MANAGE
if (!managed()) return 1;
# endif
__REF_LOCK__(this);
# if REF_CHECK_MIN_NREF
if (_reference_count_ == 0) not_enough_refs();
# endif
_reference_count_--;
refcount_t r = _reference_count_;
__REF_UNLOCK__(this);
return r;
}
#if REF_MANAGE
int managed() const {
return _reference_count_ != REF_MANAGED_CODE;
}
/** Turn off the reference counting mechanism for this object.
The value returned by nreference() will always be
1 after this is called. The ability to unmanage()
objects must be turned on at compile time by defining
REF_MANAGE. There is a slight performance penalty. */
void unmanage() {
_reference_count_ = REF_MANAGED_CODE;
}
#else // REF_MANAGE
/// Return 1 if the object is managed. Otherwise return 0.
int managed() const { return 1; }
#endif // REF_MANAGE
};
/** Provides a few utility routines common to all
Ref template instantiations.
*/
class RefBase {
protected:
/// Print a warning message.
void warn ( const char * msg) const;
/// Called when stack data is referenced.
void warn_ref_to_stack() const;
/// Called when the deletion of stack data is skipped.
void warn_skip_stack_delete() const;
/// Called when the reference count is corrupted.
void warn_bad_ref_count() const;
/// Print information about the reference.
void ref_info(RefCount*p,std::ostream& os) const;
void ref_info(std::ostream& os) const;
void check_pointer() const;
void reference(RefCount *);
int dereference(RefCount *);
public:
RefBase() {};
virtual ~RefBase();
/// Returns the DescribedClass pointer for the contained object.
virtual RefCount* parentpointer() const = 0;
/** Requires that a nonnull reference is held. If not,
the program will abort. */
void require_nonnull() const;
};
/** A template class that maintains references counts.
Several of these operations can cause a reference to an object to be
replaced by a reference to a different object. If a reference to a
nonnull object is eliminated, the object's reference count is
decremented and the object is deleted if the reference count becomes
zero.
There also may be a to convert to T*, where T is the type of the object
which Ref references. Some compilers have bugs that prevent the use of
operator T*(). The pointer() member should be used instead.
*/
template <class T>
class Ref : public RefBase {
private:
T* p;
public:
/// Create a reference to a null object.
Ref(): p(0) {}
/// Create a reference to the object a.
Ref(T*a) : p(0)
{
if (a) {
p = a;
reference(p);
}
}
/// Create a reference to the object referred to by a.
Ref(const Ref<T> &a) : p(0)
{
if (a.pointer()) {
p = a.pointer();
reference(p);
}
}
/// Create a reference to the object referred to by a.
template <class A> Ref(const Ref<A> &a): p(0)
{
if (a.pointer()) {
p = a.pointer();
reference(p);
}
}
// /** Create a reference to the object a. Do a
// dynamic_cast to convert a to the appropiate type. */
// Ref(const RefBase&a) {
// p = dynamic_cast<T*>(a.parentpointer());
// reference(p);
// }
// /** Create a reference to the object a. Do a
// dynamic_cast to convert a to the appropiate type. */
// Ref(RefCount*a): p(0) {
// operator<<(a);
// }
/** Delete this reference to the object. Decrement the object's reference
count and delete the object if the count is zero. */
~Ref()
{
clear();
}
/** Returns the reference counted object. The behaviour is undefined if
the object is null. */
T* operator->() const { return p; }
/// Returns a pointer the reference counted object.
T* pointer() const { return p; }
/// Implements the parentpointer pure virtual in the base class.
RefCount *parentpointer() const { return p; }
operator T*() const { return p; }
/** Returns a C++ reference to the reference counted object.
The behaviour is undefined if the object is null. */
T& operator *() const { return *p; };
/** Return 1 if this is a reference to a null object. Otherwise
return 0. */
int null() const { return p == 0; }
/// Return !null().
int nonnull() const { return p != 0; }
/** A variety of ordering and equivalence operators are provided using
the Identity class. */
template <class A> int operator==(const Ref<A>&a) const
{ return eq(p,a.pointer()); }
template <class A> int operator>=(const Ref<A>&a) const
{ return ge(p,a.pointer()); }
template <class A> int operator<=(const Ref<A>&a) const
{ return le(p,a.pointer()); }
template <class A> int operator>(const Ref<A>&a) const
{ return gt(p,a.pointer()); }
template <class A> int operator<(const Ref<A>&a) const
{ return lt(p,a.pointer()); }
template <class A> int operator!=(const Ref<A>&a) const
{ return ne(p,a.pointer()); }
/** Compare two objects returning -1, 0, or 1. Similar
to the C library routine strcmp. */
int compare(const Ref<T> &a) const {
return eq(p,a.p)?0:((lt(p,a.p)?-1:1));
}
/// Refer to the null object.
void clear()
{
if (p) {
int ref = dereference(p);
if (ref == 0)
delete p;
p = 0;
}
}
/// Assignment to c.
Ref<T>& operator=(const Ref<T> & c)
{
T *cp = c.pointer();
if (cp) {
cp->reference();
clear();
p=cp;
}
else {
clear();
}
return *this;
}
/// Assignment to c.
template <class A> Ref<T>& operator=(const Ref<A> & c)
{
A *cp = c.pointer();
if (cp) {
cp->reference();
clear();
p=cp;
}
else {
clear();
}
return *this;
}
/// Assignment to the object that a references using dynamic_cast.
Ref<T>& operator<<(const RefBase&a) {
T* cr = dynamic_cast<T*>(a.parentpointer());
if (cr) {
reference(cr);
clear();
}
p = cr;
return *this;
}
/** Assigns to the given base class pointer using dynamic_cast. If
the dynamic_cast fails and the argument is nonnull and has a
reference count of zero, then it is deleted. */
Ref<T>& operator<<(RefCount *a) {
T* cr = dynamic_cast<T*>(a);
if (cr) assign_pointer(cr);
else if (a && a->nreference() <= 0) delete a;
return *this;
}
/// Assignment to cr.
Ref<T>& operator=(T* cr)
{
assign_pointer(cr);
return *this;
}
/// Assignment to cr.
void assign_pointer(T* cr)
{
if (cr) {
if (DO_REF_CHECK_STACK(cr)) {
DO_REF_UNMANAGE(cr);
warn_ref_to_stack();
}
cr->reference();
}
clear();
p = cr;
}
/// Check the validity of the pointer.
void check_pointer() const
{
if (p && p->nreference() <= 0) {
warn_bad_ref_count();
}
}
/// Print information about the reference to os.
void ref_info(std::ostream& os) const
{
RefBase::ref_info(p,os);
}
/// Print a warning concerning the reference.
void warn(const char*s) const { RefBase::warn(s); }
};
}
#endif
// ///////////////////////////////////////////////////////////////////////////
// Local Variables:
// mode: c++
// c-file-style: "CLJ"
// End:
|