summaryrefslogtreecommitdiff
path: root/TAO/tao/MProfile.cpp
blob: b2cf9cf398ed0f1b994f80d2133b858619240713 (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
// This may look like C, but it's really -*- C++ -*-
// $Id$


TAO_MProfile::TAO_MProfile (CORBA::ULong sz)
  :  fwded_mprofile_(0),
     pfiles_ (0),
     current_ (0),
     size_ (sz),
     last_ (0)
{
  this->set (sz);
}

TAO_MProfile::TAO_MProfile (TAO_MProfile *mprofiles)
  :  fwded_mprofile_(0),
     pfiles_ (0),
     current_ (0),
     size_ (0),
     last_ (0)
{
  this->set (mprofiles);
}

int
TAO_MProfile::set (CORBA::ULong sz)
{
  // first release all of our profiles.
  if (this->pfiles_)
    {
      // @@ Fred, please don't put spaces in front of the ';' here.
      for (PHandle h = 0 ; h < this->size_ ; h++ )
        {
          if (this->pfiles_[h]) 
            {
              this->pfiles_[h]->_decr_refcnt ();
              this->pfiles_[h] = 0;
            }
        }
  
      // Next see if we can reuse our profile list memory Since
      // this->pfiles_ != 0, size_ > 0!  So if sz == 0 then memory is
      // deallocated.
      if (this->size_ != sz)
        {
          // we cant!
          delete [] this->pfiles_;
  
          if (this->size_ == sz)
            {
              // @@ Fred, please always use ACE_NEW_RETURN() or ACE_NEW()
              // when you allocate memory.
              this->pfiles_ = new TAO_Profile_ptr [this->size_];
              ACE_OS::memset (this->pfiles_,
                              0,
                              sizeof (TAO_Profile_ptr) *this->size_);
            }
          else // do not allocate any memory!
            this->pfiles_ = 0;
        }
    }
  else
    {
      if (this->size_ == sz)
        {
          // @@ Fred, same here.
          pfiles_ = new TAO_Profile_ptr [this->size_];
          ACE_OS::memset (this->pfiles_,
                          0,
                          sizeof (TAO_Profile_ptr) *this->size_);
        }
      else
        pfiles_ = 0;
    } // this->pfiles_

  this->last_ = 0;
  this->current_ = 0;
  this->fwded_mprofile_ = 0;

  return 1;
}
  
int
TAO_MProfile::set (TAO_MProfile *mprofile)
{
  // NOTE: We use mprofile->last_ instead of mprofile->size_ to set
  // this->size_.  This is so we can use set () to trim a profile
  // list!!

  if (! mprofile)
    return this->set ((CORBA::ULong) 0);

  this->set (mprofile->last_);

  // set indexes ...
  this->last_ = mprofile->last_;
  
  // these are set in set (ULong);
  // this->current_ = 0;
  // this->fwded_mprofile_ = 0;

  // Now reference all profiles.
  for (PHandle h = 0 ; h < this->size_ ; h++ )
    {
      if (mprofile->pfiles_[h]) 
        {
          this->pfiles_[h] = mprofile->pfiles_[h];
          this->pfiles_[h]->_incr_refcnt ();
        }
    } // for (PHandle ...)

  if (mprofile->fwded_mprofile_)
    {
      this->fwded_mprofile_ = mprofile->fwded_mprofile_;
    }

  return 1;
}

TAO_MProfile::~TAO_MProfile (void)
 {
  if (this->pfiles_)
    // @@ Fred, please make sure you use consistent style in your for
    // loop (i.e., remove certain spaces...).
    for ( PHandle h = 0 ; h < size_ ; h++ )
      if (this->pfiles_[h])
        this->pfiles_[h]->_decr_refcnt ();

  delete [] pfiles_;
  pfiles_ = 0;

}

// cyclic get next.  It will simply cycle through the complete list.

TAO_Profile *
TAO_MProfile::get_cnext (void)
{
  // @@ Fred, this express is too complicated.  Please rewrite it use
  // if/else.
  return last_ == 0 ? 0 : ((current_ < last_) ? pfiles_ [current_++] : 
                           pfiles_ [(current_ = 1, 0)]);
}

// This will return the next element until either null is found or the
// end of list.  It then continues to return NULL until rewind.

TAO_Profile *
TAO_MProfile::get_next (void)
{
  // @@ Fred, same comment here.
  return last_ == 0 ? 0 : ((current_ == last_) ? 0 : pfiles_ [current_++]);
}

TAO_Profile *
TAO_MProfile::get_cprev (void)
{
  // @@ Fred, same comment here...
  return last_ == 0 ? 0 : 
              (last_ == 1 ? pfiles_ [current_=0] :
                    (current_ > 1 ? pfiles_ [(--current_ - 1)] :
                     // @@ Fred, please don't use assignment within
                     // expressions.
                     pfiles_ [(current_ = last_, last_ - 1)]));
}

TAO_Profile *
TAO_MProfile::get_prev (void)
{
  // @@ Same comment here.
  return last_ == 0 ? 0 :
         (current_ > 1 ? pfiles_ [--current_ - 1] : 
          (TAO_Profile *)(current_ = 0, 0));
}

// does not affect the current_ setting!
TAO_Profile *
TAO_MProfile::get_profile (PHandle handle)
{
  // @@ Same comment here.
  return handle >= 0 && handle < last_ ?  pfiles_ [handle] : 0;
}
  
TAO_Profile *
TAO_MProfile::get_current_profile (void)
{
  if (last_ == 0) 
    return 0;
  else
    // @@ Fred, same comment here.
    return current_ == 0 ? pfiles_ [current_] : pfiles_ [current_-1];
}

PHandle 
TAO_MProfile::get_current_handle (void)
{
  // @@ Fred, same comment here.
  return current_ > 0 ? (current_ - 1) : 0;
  // the last profile returned!
}

void 
TAO_MProfile::rewind (void)
{
  current_ = 0;
}

int 
TAO_MProfile::add_profile (TAO_Profile *pfile)
{
  // skip by the used slots
  if (last_ == size_) // full!
    return -1;
   
  pfiles_ [last_++] = pfile;

  if (pfile && pfile->_incr_refcnt () == 0) 
    ACE_ERROR_RETURN ((LM_ERROR, 
                       "(%P|%t) Unable to increment reference count in add_profile!\n"), 
                      -1);
  return last_ - 1;
}

void
TAO_MProfile::fwded_mprofile (TAO_MProfile *fwded)
{
  this->fwded_mprofile_ = fwded;
}

TAO_MProfile *
TAO_MProfile::fwded_mprofile (void)
{
  return this->fwded_mprofile_;
}

CORBA::ULong
TAO_MProfile::profile_count (void)
{
  return this->last_;
}

TAO_Profile_ptr *
TAO_MProfile::pfiles (void) const
{
  return this->pfiles_;
}

CORBA::Boolean
TAO_MProfile::is_equivalent (TAO_MProfile *first, 
                             TAO_MProfile *second,
                             CORBA::Environment &env)
{
  // Two profile lists are equivalent iff at least one of the 
  // profiles form the first list is_equivalent to at least one
  // of the profiles from the second list!!
  TAO_Profile_ptr *pfiles1 = first->pfiles ();
  TAO_Profile_ptr *pfiles2 = second->pfiles ();
  int first_cnt = first->profile_count ();
  int second_cnt = second->profile_count ();
  
  for (PHandle h1 = 0; h1 < first_cnt;h1++)
    for (PHandle h2 = 0; h2 < second_cnt; h2++ )
      if (pfiles1[h1]->is_equivalent (pfiles2[h2], env))
        return 1;
  
  return 0;
}

CORBA::ULong
TAO_MProfile::hash (CORBA::ULong max, CORBA::Environment &env)
{
  CORBA::ULong hashval = 0;

  if (last_ == 0) 
    return 0;

  for (PHandle h=0; h < last_ ; h++) 
    hashval += pfiles_[h]->hash (max, env);

  // The above hash function return an ULong between 0 and max here we
  // simply take the average value and round
  return hashval / last_; 
}