summaryrefslogtreecommitdiff
path: root/TAO/tao/objtable.cpp
blob: 89a4893eec969ba1cb33d9a8fdeff61f2f58b1cc (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
#include "tao/corba.h"

// destructor
TAO_Object_Table::~TAO_Object_Table (void)
{
}

// Template Specialization for char*. Needed for the dynamic hash lookup
int 
ACE_Hash_Map_Manager<const char *, CORBA::Object_ptr, ACE_SYNCH_RW_MUTEX>::equal (const char *const &id1,
										 const char *const &id2)
{
  // do a string compare
  return ACE_OS::strcmp (id1, id2) == 0;
}

// Template Specialization for char *
u_long
ACE_Hash_Map_Manager<const char *, CORBA::Object_ptr, ACE_SYNCH_RW_MUTEX>::hash (const char *const &ext_id)
{
  // Use the hash_pjw hash function available in the ACE library
  return ACE::hash_pjw (ext_id);
}

TAO_Dynamic_Hash_ObjTable::TAO_Dynamic_Hash_ObjTable (CORBA::ULong size)
{
  if (size > 0)
    this->hash_.open (size);
  // else we already have a default hash map
}

TAO_Dynamic_Hash_ObjTable::~TAO_Dynamic_Hash_ObjTable (void)
{
  // we need to go thru each entry and free the space taken up by the strings
  OBJ_MAP_MANAGER::ITERATOR iterator (this->hash_);  // initialize an iterator

  for (OBJ_MAP_MANAGER::ENTRY *entry = 0;
       iterator.next (entry) != 0;
       iterator.advance ())
    {
      CORBA::string_free ((char *)entry->ext_id_); // we had allocated memory and stored
      // the string. So we free the memory
      entry->int_id_ = 0;  // we do not own this. So we just set it to 0
    }

  this->hash_.close ();
}

int
TAO_Dynamic_Hash_ObjTable::bind (const CORBA::OctetSeq &key,
				 CORBA::Object_ptr obj)
{
  // the key is an octet sequence. Hence, we cannot simply cast the buffer to a
  // char* as it may result in an arbitrary name. Hence we must first convert
  // it to a string and then save a copy of the string in the table.
  ACE_CString objkey ((char *)key.buffer, key.length);
  return this->hash_.bind (CORBA::string_dup (objkey.rep ()), obj);
}

int
TAO_Dynamic_Hash_ObjTable::find (const CORBA::OctetSeq &key,
				 CORBA::Object_ptr &obj)
{
  // the key is an octet sequence. Hence, we cannot simply cast the buffer to a
  // char* as it may result in an arbitrary name due to absence of a NULL
  // terminating character. Hence we must first convert it to a string of the
  // specified length.
  ACE_CString objkey ((char *)key.buffer, key.length);
  return this->hash_.find (objkey.rep(), obj); // no string_dup necessary here
}

// Linear search strategy
TAO_Linear_ObjTable::TAO_Linear_ObjTable (CORBA::ULong size)
  : next_ (0),
    tablesize_ (size),
    tbl_ (new TAO_Linear_ObjTable_Entry[size])
{
}

TAO_Linear_ObjTable::~TAO_Linear_ObjTable (void)
{
  delete [] this->tbl_; // this will delete each entry
}

int
TAO_Linear_ObjTable::bind (const CORBA::OctetSeq &key,
			   CORBA::Object_ptr obj)
{
  CORBA::ULong i = this->next_;

  if (i < this->tablesize_)
    {
      // store the string and the corresponding object pointer
      this->tbl_[i].opname_ = CORBA::string_alloc (key.length); // allocates one
      // more
      ACE_OS::memset (this->tbl_[i].opname_, '\0', key.length+1);
      ACE_OS::strncpy (this->tbl_[i].opname_, (char *)key.buffer, key.length);
      this->tbl_[i].obj_ = obj;
      this->next_++; // point to the next available slot
      return 0; // success
    }

  return -1; // error, size exceeded
}

// find if the key exists
int
TAO_Linear_ObjTable::find (const CORBA::OctetSeq &key,
			   CORBA::Object_ptr &obj)
{
  ACE_ASSERT (this->next_ <= this->tablesize_);

  //  ACE_CString objkey ((char *)key.buffer, key.length);
  for (CORBA::ULong i = 0; i < this->next_; i++)
    {
      // linearly search thru the table
      if (!ACE_OS::strncmp (this->tbl_[i].opname_, (char *)key.buffer, key.length))
	{
	  // keys match. Return the object pointer
	  obj = this->tbl_[i].obj_;
	  return 0; // success
	}
    }
  return -1;  // not found
}

// constructor
TAO_Linear_ObjTable_Entry::TAO_Linear_ObjTable_Entry (void)
{
  this->opname_ = 0;
  this->obj_ = 0;
}

TAO_Linear_ObjTable_Entry::~TAO_Linear_ObjTable_Entry (void)
{
  CORBA::string_free (this->opname_); // reclaim space consumed by the string
  this->obj_ = 0;  // cannot delete this as we do not own it
}

// Active Demux search strategy
// constructor
TAO_Active_Demux_ObjTable::TAO_Active_Demux_ObjTable (CORBA::ULong size)
  : next_ (0),
    tablesize_ (size),
    tbl_ (new TAO_Active_Demux_ObjTable_Entry[size])
{
}

// destructor
TAO_Active_Demux_ObjTable::~TAO_Active_Demux_ObjTable ()
{
  delete [] this->tbl_;
}


// bind the object based on the key
int
TAO_Active_Demux_ObjTable::bind (const CORBA::OctetSeq &key,
				 CORBA::Object_ptr obj)
{
  // The active demux strategy works on the assumption that the key is a
  // stringified form of an index into the table
  ACE_CString objkey ((char *)key.buffer, key.length);
  CORBA::ULong i = ACE_OS::atoi (objkey.rep ());

  if (i < this->tablesize_)
    {
      if (this->tbl_[i].obj_ != 0)
	{
	  // we are trying to overwrite a previous entry
	  return 1; // duplicate
	}
      else
	{
	  this->tbl_[i].obj_ = obj;
	  return 0;
	}
    }
  return -1; // error
}

int
TAO_Active_Demux_ObjTable::find (const CORBA::OctetSeq &key,
				 CORBA::Object_ptr& obj)
{
  ACE_CString objkey ((char *)key.buffer, key.length);
  CORBA::ULong i = ACE_OS::atoi (objkey.rep ());

  ACE_ASSERT (i < this->tablesize_); // cannot be equal to
  obj = this->tbl_[i].obj_;
  return 0; // success
}

TAO_Active_Demux_ObjTable_Entry::TAO_Active_Demux_ObjTable_Entry (void)
{
  this->obj_ = 0;
}

TAO_Active_Demux_ObjTable_Entry::~TAO_Active_Demux_ObjTable_Entry (void)
{
  this->obj_ = 0;  // cannot delete this as we do not own it
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Hash_Map_Iterator<char const*, CORBA::Object_ptr, ACE_SYNCH_RW_MUTEX>;
template class ACE_Hash_Map_Manager<char const*, CORBA::Object_ptr, ACE_SYNCH_RW_MUTEX>;
template class ACE_Hash_Map_Entry<char const*, CORBA::Object_ptr>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Hash_Map_Iterator<char const*, CORBA::Object_ptr, ACE_SYNCH_RW_MUTEX>
#pragma instantiate ACE_Hash_Map_Manager<char const*, CORBA::Object_ptr, ACE_SYNCH_RW_MUTEX>
#pragma instantiate ACE_Hash_Map_Entry<char const*, CORBA::Object_ptr>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */