summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Refcountable.cpp
blob: b39aded25c992d515fb52a7de2f502f1c70ccaa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "orbsvcs/Notify/Refcountable.h"
#include "tao/debug.h"
#include "orbsvcs/Log_Macros.h"
#include "ace/Guard_T.h"

#if (TAO_NOTIFY_REFCOUNT_DIAGNOSTICS != 0)

#include <map>
#include <string>
#include <typeinfo.h>

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

/**
 * @class TAO_Notify_Tracker
 *
 * @brief A debugging tool to track the creation of TAO_Notify_Refcountable objects.
 *
 *        Usage:
 *        Set USE_TAO_NOTIFY_TRACKER defined above to 1
 *        At any significant point to check object usage
 *          TAO_Notify_Tracker::get_instance().dump();
 */
class TAO_Notify_Tracker
{
public:
  struct Entry
  {
    TAO_Notify_Refcountable*  obj;
    std::string               class_name;
  };

  static TAO_Notify_Tracker& get_instance();

  void add( TAO_Notify_Refcountable* p);

  void remove( const TAO_Notify_Refcountable* p);

  Entry find( const TAO_Notify_Refcountable* p) const;

  void dump( const char* title = 0 );

private:
  TAO_Notify_Tracker();
  ~TAO_Notify_Tracker();

  friend class std::auto_ptr< TAO_Notify_Tracker >;
  static std::auto_ptr< TAO_Notify_Tracker > s_instance;
  mutable TAO_SYNCH_MUTEX lock_;
  typedef std::map<int, Entry> EntityMap;
  EntityMap map_;
  int id_counter_;
  int last_dump_count_;
};

void
TAO_Notify_Refcountable::diagnostic_dump( const char* title )
{
  TAO_Notify_Tracker::get_instance().dump( title );
}

#endif /* TAO_NOTIFY_REFCOUNT_DIAGNOSTICS */


TAO_Notify_Refcountable::TAO_Notify_Refcountable ()
{
}

TAO_Notify_Refcountable::~TAO_Notify_Refcountable ()
{
#if (TAO_NOTIFY_REFCOUNT_DIAGNOSTICS != 0)
  TAO_Notify_Tracker::Entry e = TAO_Notify_Tracker::get_instance().find( this );
  if ( e.obj != 0 )
  {
    ORBSVCS_DEBUG ((LM_DEBUG,"object:%x %s(%d) with refcount:%d destroyed incorrectly.\n",
        e.obj, e.class_name, e.obj->ref_id_, e.obj->refcount_.value() ));

    if ( e.obj != this || e.obj->ref_id_ != this->ref_id_ )
    {
      ORBSVCS_DEBUG ((LM_DEBUG, "  with an ID mismatch %x->%d != %x->%d!\n",
        this, ref_id_, e.obj, e.obj->ref_id_));
    }
    TAO_Notify_Tracker::get_instance().remove( this );
  }
#endif
  CORBA::ULong refcount = this->refcount_.value();
  ACE_ASSERT( refcount == 0 );
  ACE_UNUSED_ARG(refcount);
}


CORBA::ULong
TAO_Notify_Refcountable::_incr_refcnt ()
{
  CORBA::Long refcount = ++this->refcount_;
  if (TAO_debug_level > 1 )
  {
    ORBSVCS_DEBUG ((LM_DEBUG,"object:%x incr refcount = %d\n", this, refcount ));
  }
#if (TAO_NOTIFY_REFCOUNT_DIAGNOSTICS != 0)
  // Stack-instantiated-non-servants should never have _incr_refcnt called.
  // We do not care about stack-instances. Stack-instantiated servants break
  // the tracker.
  if ( refcount == 1 )
  {
    TAO_Notify_Tracker::get_instance().add( this );
  }
#endif
  return refcount;
}

CORBA::ULong
TAO_Notify_Refcountable::_decr_refcnt ()
{
  CORBA::Long refcount = --this->refcount_;

  if (TAO_debug_level > 1 )
  {
    ORBSVCS_DEBUG ((LM_DEBUG,"object:%x decr refcount = %d\n", this, refcount ));
  }

  ACE_ASSERT(refcount >= 0);

  if (refcount == 0)
  {
    #if ( USE_TAO_NOTIFY_TRACKER != 0 )
      TAO_Notify_Tracker::get_instance().remove( this );
    #endif
    this->release ();
  }
  return refcount;
}

#if (TAO_NOTIFY_REFCOUNT_DIAGNOSTICS != 0)

std::auto_ptr< TAO_Notify_Tracker > TAO_Notify_Tracker::s_instance;

TAO_Notify_Tracker::TAO_Notify_Tracker()
: id_counter_(0)
, last_dump_count_(0)
{
}


TAO_Notify_Tracker::~TAO_Notify_Tracker()
{
  dump( "destruction" );
}


void
TAO_Notify_Tracker::dump( const char* title )
{
  ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, this->lock_);

  ORBSVCS_DEBUG ((LM_DEBUG,"\nTAO_Notify_Tracker: %s\n", (title ? title : "dump")));

  EntityMap::const_iterator iter( map_.begin() );
  while ( iter != map_.end() )
  {
    ORBSVCS_DEBUG ((LM_DEBUG,"object:%x %s(%d) with refcount:%d.\n",
      iter->second.obj, iter->second.class_name, iter->first, iter->second.obj->refcount_.value() ));
    iter++;
  }

  EntityMap::size_type current_count = map_.size();
  int diff = ( current_count - last_dump_count_ );
  ORBSVCS_DEBUG ((LM_DEBUG,"Count: %u\nDifference: %+d\n", current_count, diff ));
  last_dump_count_ = current_count;
}


TAO_Notify_Tracker&
TAO_Notify_Tracker::get_instance()
{
  if ( s_instance.get() == 0 )
  {
    s_instance.reset( new TAO_Notify_Tracker );
  }
  return *s_instance;
}


void
TAO_Notify_Tracker::add( TAO_Notify_Refcountable* p )
{
  if ( p == 0 ) return;

  ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, this->lock_);
  int id = ++id_counter_;

  Entry e = { p, typeid(*p).name() };
  std::pair< EntityMap::iterator, bool > result =
    map_.insert( std::make_pair( id, e ) );
  EntityMap::iterator& it = result.first;
  if ( result.second == false )
  {
    ORBSVCS_DEBUG ((LM_DEBUG,"object:%x %s(%d) added twice.\n",
      it->second.obj, it->second.class_name.c_str(), it->first ));
  }
  else
  {
    p->ref_id_ = id;
  }
}


void
TAO_Notify_Tracker::remove( const TAO_Notify_Refcountable* p )
{
  if ( p == 0 ) return;

  ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, this->lock_);
  int ref_id = p->ref_id_;
  EntityMap::iterator iter( map_.find( ref_id ) );
  if ( iter == map_.end() )
  {
    const char* class_name = typeid(*iter->second.obj).name();
    ORBSVCS_DEBUG ((LM_DEBUG,"object:%x %s(%d) not found.\n",
      p, class_name, ref_id ));
  }
  else
  {
    map_.erase( iter );
  }
}


TAO_Notify_Tracker::Entry
TAO_Notify_Tracker::find( const TAO_Notify_Refcountable* p) const
{
  Entry e = { 0, "" };
  if ( p == 0 ) return e;

  ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, this->lock_);
  int ref_id = p->ref_id_;
  EntityMap::const_iterator iter( map_.find( ref_id ) );
  if ( iter != map_.end() )
  {
    return iter->second;
  }
  return e;
}

TAO_END_VERSIONED_NAMESPACE_DECL

#endif /* TAO_NOTIFY_REFCOUNT_DIAGNOSTICS */