00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #ifdef __GNUC__
00073 #pragma interface
00074 #endif
00075
00076 #ifndef _util_ref_ref_h
00077 #define _util_ref_ref_h
00078
00079 #include <iostream>
00080 #include <stdlib.h>
00081 #include <limits.h>
00082
00083 #include <util/ref/identity.h>
00084
00085 #ifdef HAVE_CONFIG_H
00086 #include <scconfig.h>
00087 #endif
00088
00089 #ifdef REF_OPTIMIZE
00090 #ifndef REF_CHECK_STACK
00091 # define REF_CHECK_STACK 0
00092 #endif
00093 #ifndef REF_MANAGE
00094 # define REF_MANAGE 0
00095 #endif
00096 #ifndef REF_CHECK_MAX_NREF
00097 # define REF_CHECK_MAX_NREF 0
00098 #endif
00099 #ifndef REF_CHECK_MIN_NREF
00100 # define REF_CHECK_MIN_NREF 0
00101 #endif
00102 #endif
00103
00104 #ifdef SUNMOS
00105 #ifndef REF_CHECK_STACK
00106 #define REF_CHECK_STACK 0
00107 #endif
00108 #else
00109 #ifndef REF_CHECK_STACK
00110 #define REF_CHECK_STACK 0
00111 #endif
00112 #endif
00113
00114 #ifndef REF_MANAGE
00115 #define REF_MANAGE 1
00116 #endif
00117
00118 #ifndef REF_CHECK_MAX_NREF
00119 #define REF_CHECK_MAX_NREF 1
00120 #endif
00121
00122 #ifndef REF_CHECK_MIN_NREF
00123 #define REF_CHECK_MIN_NREF 1
00124 #endif
00125
00126 #ifndef REF_USE_LOCKS
00127 # if HAVE_STHREAD || HAVE_CREATETHREAD || HAVE_PTHREAD
00128 # define REF_USE_LOCKS 1
00129 # endif
00130 #endif
00131
00132 #ifndef REF_ALWAYS_USE_LOCKS
00133 # define REF_ALWAYS_USE_LOCKS 1
00134 #endif
00135
00136 #if REF_CHECK_STACK
00137 #include <unistd.h>
00138 #ifndef HAVE_SBRK_DEC
00139 extern "C" void * sbrk(ssize_t);
00140 #endif
00141 #define DO_REF_CHECK_STACK(p) (((void*) (p) > sbrk(0)) && (p)->managed())
00142 #else // REF_CHECK_STACK
00143 #define DO_REF_CHECK_STACK(p) (0)
00144 #endif // REF_CHECK_STACK
00145
00146 #if REF_MANAGE
00147 #define DO_REF_UNMANAGE(p) ((p)->unmanage())
00148 #else // REF_MANAGE
00149 #define DO_REF_UNMANAGE(p)
00150 #endif // REF_MANAGE
00151
00152 #if REF_USE_LOCKS
00153 #define __REF_LOCK__(p) p->lock_ptr()
00154 #define __REF_UNLOCK__(p) p->unlock_ptr()
00155 #if REF_ALWAYS_USE_LOCKS
00156 #define __REF_INITLOCK__() use_locks(true)
00157 #else
00158 #define __REF_INITLOCK__() ref_lock_ = 0xff
00159 #endif
00160 #else
00161 #define __REF_LOCK__(p)
00162 #define __REF_UNLOCK__(p)
00163 #define __REF_INITLOCK__()
00164 #endif
00165
00166 namespace sc {
00167
00168 typedef unsigned long refcount_t;
00169
00194 class RefCount: public Identity {
00195 private:
00196 #if REF_MANAGE
00197 # define REF_MAX_NREF (UINT_MAX - 1)
00198 # define REF_MANAGED_CODE UINT_MAX
00199 #else
00200 # define REF_MAX_NREF UINT_MAX
00201 #endif
00202 unsigned int _reference_count_;
00203 #if REF_USE_LOCKS
00204 unsigned char ref_lock_;
00205 #endif
00206
00207 void error(const char*) const;
00208 void too_many_refs() const;
00209 void not_enough_refs() const;
00210 protected:
00211 RefCount(): _reference_count_(0) {
00212 __REF_INITLOCK__();
00213
00214 }
00215 RefCount(const RefCount&): _reference_count_(0) {
00216 __REF_INITLOCK__();
00217
00218 }
00219
00220
00221 RefCount& operator=(const RefCount&) { return *this; }
00222 public:
00223 virtual ~RefCount();
00224
00226 int lock_ptr() const;
00228 int unlock_ptr() const;
00229
00231 void use_locks(bool inVal);
00232
00234 refcount_t nreference() const {
00235 # if REF_MANAGE
00236 if (!managed()) return 1;
00237 # endif
00238 return _reference_count_;
00239 }
00240
00242 refcount_t reference() {
00243 # if REF_MANAGE
00244 if (!managed()) return 1;
00245 # endif
00246 __REF_LOCK__(this);
00247 # if REF_CHECK_MAX_NREF
00248 if (_reference_count_ >= REF_MAX_NREF) too_many_refs();
00249 # endif
00250 _reference_count_++;
00251 refcount_t r = _reference_count_;
00252 __REF_UNLOCK__(this);
00253 return r;
00254 }
00255
00257 refcount_t dereference() {
00258 # if REF_MANAGE
00259 if (!managed()) return 1;
00260 # endif
00261 __REF_LOCK__(this);
00262 # if REF_CHECK_MIN_NREF
00263 if (_reference_count_ == 0) not_enough_refs();
00264 # endif
00265 _reference_count_--;
00266 refcount_t r = _reference_count_;
00267 __REF_UNLOCK__(this);
00268 return r;
00269 }
00270
00271 #if REF_MANAGE
00272 int managed() const {
00273 return _reference_count_ != REF_MANAGED_CODE;
00274 }
00280 void unmanage() {
00281 _reference_count_ = REF_MANAGED_CODE;
00282 }
00283 #else // REF_MANAGE
00285 int managed() const { return 1; }
00286 #endif // REF_MANAGE
00287 };
00288
00292 class RefBase {
00293 protected:
00295 void warn ( const char * msg) const;
00297 void warn_ref_to_stack() const;
00299 void warn_skip_stack_delete() const;
00301 void warn_bad_ref_count() const;
00303 void ref_info(RefCount*p,std::ostream& os) const;
00304 void ref_info(std::ostream& os) const;
00305 void check_pointer() const;
00306 void reference(RefCount *);
00307 int dereference(RefCount *);
00308 public:
00309 RefBase() {};
00310 virtual ~RefBase();
00312 virtual RefCount* parentpointer() const = 0;
00315 void require_nonnull() const;
00316 };
00317
00331 template <class T>
00332 class Ref : public RefBase {
00333 public:
00334 typedef T element_type;
00335 private:
00336 T* p;
00337 public:
00339 Ref(): p(0) {}
00341 Ref(T*a) : p(0)
00342 {
00343 if (a) {
00344 p = a;
00345 reference(p);
00346 }
00347 }
00349 Ref(const Ref<T> &a) : p(0)
00350 {
00351 if (a.pointer()) {
00352 p = a.pointer();
00353 reference(p);
00354 }
00355 }
00357 template <class A> Ref(const Ref<A> &a): p(0)
00358 {
00359 if (a.pointer()) {
00360 p = a.pointer();
00361 reference(p);
00362 }
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00377 ~Ref()
00378 {
00379 clear();
00380 }
00383 T* operator->() const { return p; }
00385 T* pointer() const { return p; }
00387 RefCount *parentpointer() const { return p; }
00388
00389 operator T*() const { return p; }
00392 T& operator *() const { return *p; };
00395 int null() const { return p == 0; }
00397 int nonnull() const { return p != 0; }
00400 template <class A> int operator==(const Ref<A>&a) const
00401 { return eq(p,a.pointer()); }
00402 template <class A> int operator>=(const Ref<A>&a) const
00403 { return ge(p,a.pointer()); }
00404 template <class A> int operator<=(const Ref<A>&a) const
00405 { return le(p,a.pointer()); }
00406 template <class A> int operator>(const Ref<A>&a) const
00407 { return gt(p,a.pointer()); }
00408 template <class A> int operator<(const Ref<A>&a) const
00409 { return lt(p,a.pointer()); }
00410 template <class A> int operator!=(const Ref<A>&a) const
00411 { return ne(p,a.pointer()); }
00414 int compare(const Ref<T> &a) const {
00415 return eq(p,a.p)?0:((lt(p,a.p)?-1:1));
00416 }
00418 void clear()
00419 {
00420 if (p) {
00421 int ref = dereference(p);
00422 if (ref == 0)
00423 delete p;
00424 p = 0;
00425 }
00426 }
00428 Ref<T>& operator=(const Ref<T> & c)
00429 {
00430 T *cp = c.pointer();
00431 if (cp) {
00432 cp->reference();
00433 clear();
00434 p=cp;
00435 }
00436 else {
00437 clear();
00438 }
00439 return *this;
00440 }
00442 template <class A> Ref<T>& operator=(const Ref<A> & c)
00443 {
00444 A *cp = c.pointer();
00445 if (cp) {
00446 cp->reference();
00447 clear();
00448 p=cp;
00449 }
00450 else {
00451 clear();
00452 }
00453 return *this;
00454 }
00456 Ref<T>& operator<<(const RefBase&a) {
00457 T* cr = dynamic_cast<T*>(a.parentpointer());
00458 if (cr) {
00459 reference(cr);
00460 clear();
00461 }
00462 p = cr;
00463 return *this;
00464 }
00468 Ref<T>& operator<<(RefCount *a) {
00469 T* cr = dynamic_cast<T*>(a);
00470 if (cr) assign_pointer(cr);
00471 else if (a && a->nreference() <= 0) delete a;
00472 return *this;
00473 }
00475 Ref<T>& operator=(T* cr)
00476 {
00477 assign_pointer(cr);
00478 return *this;
00479 }
00481 void assign_pointer(T* cr)
00482 {
00483 if (cr) {
00484 if (DO_REF_CHECK_STACK(cr)) {
00485 DO_REF_UNMANAGE(cr);
00486 warn_ref_to_stack();
00487 }
00488 cr->reference();
00489 }
00490 clear();
00491 p = cr;
00492 }
00494 void check_pointer() const
00495 {
00496 if (p && p->nreference() <= 0) {
00497 warn_bad_ref_count();
00498 }
00499 }
00501 void ref_info(std::ostream& os) const
00502 {
00503 RefBase::ref_info(p,os);
00504 }
00506 void warn(const char*s) const { RefBase::warn(s); }
00507 };
00508
00514 template <typename T, typename EqualTo = std::equal_to<T> >
00515 struct RefObjectEqual {
00516 bool operator()(const Ref<T>& obj1, const Ref<T>& obj2) {
00517 EqualTo equal_functor;
00518 return equal_functor(*obj1,*obj2);
00519 }
00520 };
00521
00522 }
00523
00524 #endif
00525
00526
00527
00528
00529
00530
00531