SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2008-2009 Soeren Sonnenburg 00008 * Written (W) 2011-2013 Heiko Strathmann 00009 * Written (W) 2013 Thoralf Klein 00010 * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max Planck Society 00011 */ 00012 00013 #include <shogun/base/init.h> 00014 #include <shogun/base/SGRefObject.h> 00015 #include <shogun/io/SGIO.h> 00016 00017 #include <stdlib.h> 00018 #include <stdio.h> 00019 00020 using namespace shogun; 00021 00022 SGRefObject::SGRefObject() 00023 { 00024 init(); 00025 m_refcount = new RefCount(0); 00026 00027 SG_SGCDEBUG("SGRefObject created (%p)\n", this) 00028 } 00029 00030 SGRefObject::SGRefObject(const SGRefObject& orig) 00031 { 00032 init(); 00033 m_refcount = orig.m_refcount; 00034 SG_REF(this); 00035 } 00036 00037 SGRefObject::~SGRefObject() 00038 { 00039 SG_SGCDEBUG("SGRefObject destroyed (%p)\n", this) 00040 delete m_refcount; 00041 } 00042 00043 #ifdef USE_REFERENCE_COUNTING 00044 int32_t SGRefObject::ref() 00045 { 00046 int32_t count = m_refcount->ref(); 00047 SG_SGCDEBUG("ref() refcount %ld obj %s (%p) increased\n", count, this->get_name(), this) 00048 return m_refcount->ref_count(); 00049 } 00050 00051 int32_t SGRefObject::ref_count() 00052 { 00053 int32_t count = m_refcount->ref_count(); 00054 SG_SGCDEBUG("ref_count(): refcount %d, obj %s (%p)\n", count, this->get_name(), this) 00055 return m_refcount->ref_count(); 00056 } 00057 00058 int32_t SGRefObject::unref() 00059 { 00060 int32_t count = m_refcount->unref(); 00061 if (count<=0) 00062 { 00063 SG_SGCDEBUG("unref() refcount %ld, obj %s (%p) destroying\n", count, this->get_name(), this) 00064 delete this; 00065 return 0; 00066 } 00067 else 00068 { 00069 SG_SGCDEBUG("unref() refcount %ld obj %s (%p) decreased\n", count, this->get_name(), this) 00070 return m_refcount->ref_count(); 00071 } 00072 } 00073 #endif //USE_REFERENCE_COUNTING 00074 00075 #ifdef TRACE_MEMORY_ALLOCS 00076 #include <shogun/lib/Map.h> 00077 extern CMap<void*, shogun::MemoryBlock>* sg_mallocs; 00078 #endif 00079 00080 void SGRefObject::init() 00081 { 00082 #ifdef TRACE_MEMORY_ALLOCS 00083 if (sg_mallocs) 00084 { 00085 int32_t idx=sg_mallocs->index_of(this); 00086 if (idx>-1) 00087 { 00088 MemoryBlock* b=sg_mallocs->get_element_ptr(idx); 00089 b->set_sgobject(); 00090 } 00091 } 00092 #endif 00093 }