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 = new RefCount(0);
34 
35  SG_SGCDEBUG("SGRefObject copied (%p)\n", this)
36 }
37 
39 {
40  SG_SGCDEBUG("SGRefObject destroyed (%p)\n", this)
41  delete m_refcount;
42 }
43 
44 #ifdef USE_REFERENCE_COUNTING
45 int32_t SGRefObject::ref()
46 {
47  int32_t count = m_refcount->ref();
48  SG_SGCDEBUG("ref() refcount %ld obj %s (%p) increased\n", count, this->get_name(), this)
49  return m_refcount->ref_count();
50 }
51 
52 int32_t SGRefObject::ref_count()
53 {
54  int32_t count = m_refcount->ref_count();
55  SG_SGCDEBUG("ref_count(): refcount %d, obj %s (%p)\n", count, this->get_name(), this)
56  return m_refcount->ref_count();
57 }
58 
59 int32_t SGRefObject::unref()
60 {
61  int32_t count = m_refcount->unref();
62  if (count<=0)
63  {
64  SG_SGCDEBUG("unref() refcount %ld, obj %s (%p) destroying\n", count, this->get_name(), this)
65  delete this;
66  return 0;
67  }
68  else
69  {
70  SG_SGCDEBUG("unref() refcount %ld obj %s (%p) decreased\n", count, this->get_name(), this)
71  return m_refcount->ref_count();
72  }
73 }
74 #endif //USE_REFERENCE_COUNTING
75 
76 #ifdef TRACE_MEMORY_ALLOCS
77 #include <shogun/lib/Map.h>
78 extern CMap<void*, shogun::MemoryBlock>* sg_mallocs;
79 
80 void SGRefObject::list_memory_allocs()
81 {
82  shogun::list_memory_allocs();
83 }
84 #endif
85 
86 void SGRefObject::init()
87 {
88 #ifdef TRACE_MEMORY_ALLOCS
89  if (sg_mallocs)
90  {
91  int32_t idx=sg_mallocs->index_of(this);
92  if (idx>-1)
93  {
94  MemoryBlock* b=sg_mallocs->get_element_ptr(idx);
95  b->set_sgobject();
96  }
97  }
98 #endif
99 }

SHOGUN Machine Learning Toolbox - Documentation