RefCountedObject.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
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     // atomic_decr_test will only return true if the currently
00058     // executing thread is the one that sent the refcount to zero.
00059     // hence we are safe in knowing that there are no other references
00060     // on the object, and that only one thread will call
00061     // no_more_refs()
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 } // namespace oasys

Generated on Sat Sep 8 08:36:17 2007 for DTN Reference Implementation by  doxygen 1.5.3