summaryrefslogtreecommitdiff
path: root/TAO/tao/Object_Table.cpp
blob: 3ce1cbeb39d3eef0beb0f381434904da5f714c78 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// $Id$

#include "ace/Auto_Ptr.h"
#include "tao/corba.h"
#include "tao/Object_Table.h"

ACE_RCSID(tao, Object_Table, "$Id$")

#if !defined (__ACE_INLINE__)
# include "tao/Object_Table.i"
#endif /* ! __ACE_INLINE__ */

TAO_Object_Table::TAO_Object_Table (TAO_Object_Table_Impl *impl,
                                    int delete_impl)
  : impl_ (impl),
    delete_impl_ (delete_impl)
{
  if (this->impl_ == 0)
    {
      this->impl_ =
        TAO_ORB_Core_instance ()->server_factory ()->create_object_table ();
      this->delete_impl_ = 1;
    }
}

int
TAO_Object_Table_Impl::find (const PortableServer::Servant servant,
                             PortableServer::ObjectId_out id)
{
  id.ptr () = 0;
  auto_ptr<TAO_Object_Table_Iterator_Impl> end (this->end ());

  for (auto_ptr<TAO_Object_Table_Iterator_Impl> i (this->begin ());
       !i->done (end.get ());
       i->advance ())
    {
      const TAO_Object_Table_Entry &item = i->item ();

      if (item.servant_ == servant)
        {
          if (id.ptr () != 0)
            {
              // More than one match return -1.
              delete id.ptr ();
              return -1;
            }
          // Store the match....
          ACE_NEW_RETURN (id.ptr (),
                          PortableServer::ObjectId (item.id_),
                          -1);
        }
    }

  return id.ptr () == 0 ? -1 : 0;
}

int
TAO_Object_Table_Impl::find (const PortableServer::Servant servant)
{
  PortableServer::ObjectId *id;
  PortableServer::ObjectId_out id_out (id);

  if (this->find (servant, id_out) == -1)
    return -1;

  // It was found and returned in <id>, we must release it.
  delete id;
  return 0;
}

// Template specialization....
u_long
ACE_Hash_Map_Manager<PortableServer::ObjectId, PortableServer::Servant, ACE_SYNCH_NULL_MUTEX>::hash (const PortableServer::ObjectId &id)
{
  // Based on hash_pjw function on the ACE library.
  u_long hash = 0;

  for (CORBA::ULong i = 0;
       i < id.length ();
       ++i)
    {
      hash = (hash << 4) + (id[i] * 13);

      u_long g = hash & 0xf0000000;

      if (g)
        {
          hash ^= (g >> 24);
          hash ^= g;
        }
    }

  return hash;
}

int
TAO_Dynamic_Hash_ObjTable::find (const PortableServer::ObjectId &id,
                                 PortableServer::Servant &servant)
{
  return this->hash_map_.find (id, servant);
}

int
TAO_Dynamic_Hash_ObjTable::bind (const PortableServer::ObjectId &id,
                                 PortableServer::Servant servant)
{
  return this->hash_map_.bind (id, servant);
}

int
TAO_Dynamic_Hash_ObjTable::unbind (const PortableServer::ObjectId &id,
                                   PortableServer::Servant &servant)
{
  return this->hash_map_.unbind (id, servant);
}

TAO_Dynamic_Hash_ObjTable::TAO_Dynamic_Hash_ObjTable (CORBA::ULong size)
  : hash_map_ (size == 0 ?
      ACE_static_cast (size_t, TAO_Object_Table_Impl::DEFAULT_TABLE_SIZE) :
      size)
{
}

TAO_Linear_ObjTable::TAO_Linear_ObjTable (CORBA::ULong size)
  : next_ (0),
    tablesize_ (size == 0 ?
      ACE_static_cast (size_t, TAO_Object_Table_Impl::DEFAULT_TABLE_SIZE) :
      size)
{
  ACE_NEW (table_, TAO_Object_Table_Entry[this->tablesize_]);
}

// Active Demux search strategy
// constructor

TAO_Active_Demux_ObjTable::TAO_Active_Demux_ObjTable (CORBA::ULong size)
  : TAO_Linear_ObjTable (size)
{
}

int
TAO_Linear_ObjTable::bind (const PortableServer::ObjectId &id,
                           PortableServer::Servant servant)
{
  // Check existing entries
  for (TAO_Object_Table_Entry *i = this->table_;
       i != this->table_ + this->next_;
       ++i)
    {
      if (i->is_free_)
        {
          i->id_ = id;
          i->servant_ = servant;
          i->is_free_ = 0;
          return 0;
        }
    }

  // Resize
  if (this->next_ == this->tablesize_)
    {
      int result = this->resize ();
      if (result != 0)
        return result;
    }

  // Put the entry at the end of the new section
  this->table_[this->next_].id_ = id;
  this->table_[this->next_].servant_ = servant;
  this->table_[this->next_].is_free_ = 0;

  // Increment next
  this->next_++;

  return 0;
}

int
TAO_Linear_ObjTable::find (const PortableServer::ObjectId &id,
                           PortableServer::Servant &servant)
{
  for (TAO_Object_Table_Entry *i = this->table_;
       i != this->table_ + this->next_;
       ++i)
    {
      if (i->is_free_)
        continue;
      else if (i->id_ == id)
        {
          servant = i->servant_;
          return 0;
        }
    }
  return -1;
}

int
TAO_Linear_ObjTable::unbind (const PortableServer::ObjectId &id,
                             PortableServer::Servant &servant)
{
  for (TAO_Object_Table_Entry *i = this->table_;
       i != this->table_ + this->next_;
       ++i)
    {
      if (i->is_free_)
        continue;
      else if (i->id_ == id)
        {
          servant = i->servant_;
          i->is_free_ = 1;
          return 0;
        }
    }
  return -1;
}

int
TAO_Linear_ObjTable::resize (void)
{
  if (this->tablesize_ < TAO_Linear_ObjTable::MAX_EXPONENTIAL)
    this->tablesize_ *= 2;
  else
    this->tablesize_ += TAO_Linear_ObjTable::LINEAR_INCREASE;

  TAO_Object_Table_Entry *tmp;
  ACE_NEW_RETURN (tmp,
                  TAO_Object_Table_Entry[this->tablesize_],
                  -1);

  // Copy old stuff
  for (TAO_Object_Table_Entry *i = this->table_, *j = tmp;
       i != this->table_ + this->next_;
       ++i, ++j)
    *j = *i;

  delete [] this->table_;

  this->table_ = tmp;
  return 0;
}

int
TAO_Active_Demux_ObjTable::bind (const PortableServer::ObjectId &id,
                                 PortableServer::Servant servant)
{
  CORBA::ULong index = 0;
  CORBA::ULong generation = 0;
  int result = this->parse_object_id (id, index, generation);

  if (result != 0 
      || index > this->tablesize_ 
      || this->table_[index].generation_ != generation)
    return -1;

  ACE_ASSERT (this->table_[index].is_free_ == 0);
  this->table_[index].servant_ = servant;

  return 0;
}

int
TAO_Active_Demux_ObjTable::find (const PortableServer::ObjectId &id,
                                 PortableServer::Servant &servant)
{
  CORBA::ULong index = 0;
  CORBA::ULong generation = 0;
  int result = this->parse_object_id (id, index, generation);

  if (result != 0 
      || index > this->tablesize_ 
      || this->table_[index].generation_ != generation 
      || this->table_[index].is_free_ != 0)
    return -1;

  servant = this->table_[index].servant_;

  return 0;
}

int
TAO_Active_Demux_ObjTable::unbind (const PortableServer::ObjectId &id,
                                   PortableServer::Servant &servant)
{
  CORBA::ULong index = 0;
  CORBA::ULong generation = 0;
  int result = this->parse_object_id (id, index, generation);

  if (result != 0
      || index > this->tablesize_
      || this->table_[index].generation_ != generation)
    return -1;

  servant = this->table_[index].servant_;
  this->table_[index].is_free_ = 1;

  return 0;
}

PortableServer::ObjectId *
TAO_Active_Demux_ObjTable::create_object_id (PortableServer::Servant servant,
                                             CORBA::Environment &env)
{
  // This method assumes that locks are held when it is called
  CORBA::ULong id_data[2];
  CORBA::ULong index = this->next_free ();
  id_data[TAO_Active_Demux_ObjTable::INDEX_FIELD] = index;

  // Increment generation count.
  id_data[TAO_Active_Demux_ObjTable::GENERATION_FIELD] =
    ++this->table_[index].generation_;

  // Move next along if index is not reused
  if (index == this->next_)
    this->next_++;

  PortableServer::ObjectId *id;
  CORBA::ULong size = 2 * sizeof (CORBA::ULong);
  ACE_NEW_RETURN (id,
                  PortableServer::ObjectId (size),
                  0);

  id->length (size);

  ACE_OS::memcpy (id->get_buffer (),
                  &id_data,
                  size);

  // Set the new values
  this->table_[index].id_ = *id;
  this->table_[index].servant_ = servant;
  this->table_[index].is_free_ = 0;

  return id;
}

CORBA::ULong
TAO_Active_Demux_ObjTable::next_free (void)
{
  for (;;)
    {
      for (TAO_Object_Table_Entry *i = this->table_;
           i != this->table_ + this->tablesize_;
           ++i)
        if (i->is_free_)
          return i - this->table_;

      this->resize ();
    }
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Hash_Map_Iterator_Base<PortableServer::ObjectId, PortableServer::Servant, ACE_SYNCH_NULL_MUTEX>;
template class ACE_Hash_Map_Iterator<PortableServer::ObjectId, PortableServer::Servant, ACE_SYNCH_NULL_MUTEX>;
template class ACE_Hash_Map_Reverse_Iterator<PortableServer::ObjectId, PortableServer::Servant, ACE_SYNCH_NULL_MUTEX>;
template class ACE_Hash_Map_Manager<PortableServer::ObjectId, PortableServer::Servant, ACE_SYNCH_NULL_MUTEX>;
template class ACE_Hash_Map_Entry<PortableServer::ObjectId, PortableServer::Servant>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Hash_Map_Iterator_Base<PortableServer::ObjectId, PortableServer::Servant, ACE_SYNCH_NULL_MUTEX>
#pragma instantiate ACE_Hash_Map_Iterator<PortableServer::ObjectId, PortableServer::Servant, ACE_SYNCH_NULL_MUTEX>
#pragma instantiate ACE_Hash_Map_Reverse_Iterator<PortableServer::ObjectId, PortableServer::Servant, ACE_SYNCH_NULL_MUTEX>
#pragma instantiate ACE_Hash_Map_Manager<PortableServer::ObjectId, PortableServer::Servant, ACE_SYNCH_NULL_MUTEX>
#pragma instantiate ACE_Hash_Map_Entry<PortableServer::ObjectId, PortableServer::Servant>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */