00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "RefCountedObject.h"
00019
00020 namespace oasys {
00021
00022
00023 RefCountedObject::RefCountedObject(const char* logpath)
00024 : refcount_(0),
00025 logger_("RefCountedObject", logpath)
00026 {
00027 }
00028
00029
00030 RefCountedObject::~RefCountedObject()
00031 {
00032 }
00033
00034
00035 void
00036 RefCountedObject::add_ref(const char* what1, const char* what2) const
00037 {
00038 atomic_incr(&refcount_);
00039
00040 logger_.logf(LOG_DEBUG,
00041 "refcount *%p %u -> %u add %s %s",
00042 this, refcount_.value - 1, refcount_.value, what1, what2);
00043
00044 ASSERT(refcount_.value > 0);
00045 }
00046
00047
00048 void
00049 RefCountedObject::del_ref(const char* what1, const char* what2) const
00050 {
00051 ASSERT(refcount_.value > 0);
00052
00053 logger_.logf(LOG_DEBUG,
00054 "refcount *%p %d -> %d del %s %s",
00055 this, refcount_.value, refcount_.value - 1, what1, what2);
00056
00057
00058
00059
00060
00061
00062
00063 if (atomic_decr_test(&refcount_)) {
00064 ASSERT(refcount_.value == 0);
00065 no_more_refs();
00066 }
00067 }
00068
00069
00070 void
00071 RefCountedObject::no_more_refs() const
00072 {
00073 logger_.logf(LOG_DEBUG, "no_more_refs *%p... deleting object", this);
00074 delete this;
00075 }
00076
00077
00078 int
00079 RefCountedObject::format(char* buf, size_t sz) const
00080 {
00081 return snprintf(buf, sz, "RefCountedObject %p", this);
00082 }
00083
00084 }