SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGRefObject.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2008-2009 Soeren Sonnenburg
8  * Written (W) 2011-2013 Heiko Strathmann
9  * Written (W) 2013 Thoralf Klein
10  * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max Planck Society
11  */
12 
13 #include <shogun/lib/config.h>
14 #include <shogun/lib/memory.h>
15 
17 #include <shogun/io/SGIO.h>
18 #include <shogun/lib/RefCount.h>
19 
20 using namespace shogun;
21 
23 {
24  init();
25  m_refcount = new RefCount(0);
26 
27  SG_SGCDEBUG("SGRefObject created (%p)\n", this)
28 }
29 
31 {
32  init();
33  m_refcount = orig.m_refcount;
34  SG_REF(this);
35 }
36 
38 {
39  SG_SGCDEBUG("SGRefObject destroyed (%p)\n", this)
40  delete m_refcount;
41 }
42 
43 #ifdef USE_REFERENCE_COUNTING
44 int32_t SGRefObject::ref()
45 {
46  int32_t count = m_refcount->ref();
47  SG_SGCDEBUG("ref() refcount %ld obj %s (%p) increased\n", count, this->get_name(), this)
48  return m_refcount->ref_count();
49 }
50 
51 int32_t SGRefObject::ref_count()
52 {
53  int32_t count = m_refcount->ref_count();
54  SG_SGCDEBUG("ref_count(): refcount %d, obj %s (%p)\n", count, this->get_name(), this)
55  return m_refcount->ref_count();
56 }
57 
58 int32_t SGRefObject::unref()
59 {
60  int32_t count = m_refcount->unref();
61  if (count<=0)
62  {
63  SG_SGCDEBUG("unref() refcount %ld, obj %s (%p) destroying\n", count, this->get_name(), this)
64  delete this;
65  return 0;
66  }
67  else
68  {
69  SG_SGCDEBUG("unref() refcount %ld obj %s (%p) decreased\n", count, this->get_name(), this)
70  return m_refcount->ref_count();
71  }
72 }
73 #endif //USE_REFERENCE_COUNTING
74 
75 #ifdef TRACE_MEMORY_ALLOCS
76 #include <shogun/lib/Map.h>
77 extern CMap<void*, shogun::MemoryBlock>* sg_mallocs;
78 
79 static void SGRefObject::list_memory_allocs()
80 {
81  shogun::list_memory_allocs();
82 }
83 #endif
84 
85 void SGRefObject::init()
86 {
87 #ifdef TRACE_MEMORY_ALLOCS
88  if (sg_mallocs)
89  {
90  int32_t idx=sg_mallocs->index_of(this);
91  if (idx>-1)
92  {
93  MemoryBlock* b=sg_mallocs->get_element_ptr(idx);
94  b->set_sgobject();
95  }
96  }
97 #endif
98 }

SHOGUN Machine Learning Toolbox - Documentation