SHOGUN  v3.2.0
SGRefObject.cpp
浏览该文件的文档.
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/base/init.h>
15 #include <shogun/io/SGIO.h>
16 
17 #include <stdlib.h>
18 #include <stdio.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 #endif
79 
80 void SGRefObject::init()
81 {
82 #ifdef TRACE_MEMORY_ALLOCS
83  if (sg_mallocs)
84  {
85  int32_t idx=sg_mallocs->index_of(this);
86  if (idx>-1)
87  {
88  MemoryBlock* b=sg_mallocs->get_element_ptr(idx);
89  b->set_sgobject();
90  }
91  }
92 #endif
93 }
Class SGRefObject is a reference count based memory management class.
Definition: SGRefObject.h:46
int32_t ref_count()
Definition: RefCount.cpp:31
virtual const char * get_name() const =0
int32_t unref()
Definition: RefCount.cpp:18
#define SG_REF(x)
Definition: SGRefObject.h:34
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:40
int32_t ref()
Definition: RefCount.cpp:5
int32_t index_of(const K &key)
Definition: Map.h:152
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:16
#define SG_SGCDEBUG(...)
Definition: SGIO.h:165
virtual ~SGRefObject()
Definition: SGRefObject.cpp:37

SHOGUN Machine Learning Toolbox - Documentation