summaryrefslogtreecommitdiff
path: root/ace/Cache_Manager_T.cpp
blob: ffabdfa75f5d3158bca2ddd903b05566e4cedd33 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// $Id$

#ifndef ACE_CACHE_MANAGER_T_CPP
#define ACE_CACHE_MANAGER_T_CPP

#include "ace_Cache_Manager_T.h"
#include "ace_Cache_Hash_T.h"
#include "ace_Cache_Heap_T.h"

ACE_RCSID(ace, Cache_Manager_T, "$Id$")

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC>
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::ACE_Cache_Manager (ACE_Allocator *alloc,
                     ACE_Cache_Object_Factory *cof,
                     size_t hashsize,
                     size_t maxsize,
                     size_t maxobjsize,
                     size_t minobjsize,
                     size_t highwater,
                     size_t lowwater,
                     int timetolive,
                     int counted)
  : allocator_ (alloc),
    factory_ (cof),
    hashsize_ (hashsize),
    maxsize_ (maxsize),
    maxobjsize_ (maxobjsize),
    minobjsize_ (minobjsize),
    highwater_ (highwater),
    lowwater_ (lowwater),
    waterlevel_ (0),
    timetolive_ (timetolive),
    counted_ (counted),
    hash_ (0),
    heap_ (0)
{
  // Some sanity checking needed here --
  if (this->lowwater_ > this->highwater_)
    this->lowwater_ = this->highwater_ / 2;

  // @@ James, please don't use "magic numbers" like 1024.  Make sure
  // you use the same symbolic constants as the other places...
  if (this->maxobjsize_ > (this->highwater_ - this->lowwater_) * 1024)
    this->maxobjsize_ = (this->highwater_ - this->lowwater_) * (1024/2);

  if (this->minobjsize_ > this->maxobjsize_)
    this->minobjsize_ = this->maxobjsize_ / 2;

  if (this->allocator_ == 0)
    this->allocator_ = ACE_Allocator::instance ();

  if (this->factory_ == 0)
    this->factory_ = Object_Factory::instance ();

  ACE_NEW_MALLOC (this->hash_,
                  (Cache_Hash *)
                  this->allocator_->malloc (sizeof (Cache_Hash)),
                  Cache_Hash (alloc, hashsize));
  // @@ James, please check the ACE_NEW_MALLOC macro -- it should do
  // this check and bailout if this->hash_ == 0...

  if (this->hash_ == 0)
    {
      this->hashsize_ = 0;
      return;
    }

  ACE_NEW_MALLOC (this->heap_,
                  (Cache_Heap *)
                  this->allocator_->malloc (sizeof (Cache_Heap)),
                  Cache_Heap (alloc, maxsize));

  // @@ James, please check the ACE_NEW_MALLOC macro -- it should do
  // this check and bailout if this->hash_ == 0...

  if (this->heap_ == 0)
    {
      this->maxsize_ = 0;

      ACE_DES_FREE (this->hash_, this->allocator_->free, Cache_Hash);
      this->hash_ = 0;
      this->hashsize_ = 0;
    }
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::open (ACE_Allocator *alloc,
        ACE_Cache_Object_Factory *cof,
        size_t hashsize,
        size_t maxsize,
        size_t maxobjsize,
        size_t minobjsize,
        size_t highwater,
        size_t lowwater,
        int timetolive,
        int counted)
{
  this->close ();

  this->allocator_ = alloc;
  this->factory_ = cof;
  this->hashsize_ = hashsize;
  this->maxsize_ = maxsize;
  this->maxobjsize_ = maxobjsize;
  this->minobjsize_ = minobjsize;
  this->highwater_ = highwater;
  this->lowwater_ = lowwater;
  this->waterlevel_ = 0;
  this->timetolive_ = timetolive;
  this->counted_ = counted;

  // Some sanity checking needed here --
  if (this->lowwater_ > this->highwater_)
    this->lowwater_ = this->highwater_ / 2;

  if (this->maxobjsize_ > (this->highwater_ - this->lowwater_) * 1024)
    this->maxobjsize_ = (this->highwater_ - this->lowwater_) * (1024/2);

  if (this->minobjsize_ > this->maxobjsize_)
    this->minobjsize_ = this->maxobjsize_ / 2;

  if (this->allocator_ == 0)
    this->allocator_ = ACE_Allocator::instance ();

  if (this->factory_ == 0)
    this->factory_ = Object_Factory::instance ();

  // @@ James, please use the ACE_NEW_MALLOC_RETURN macro here.
  this->hash_ = (Cache_Hash *) this->allocator_->malloc (sizeof (Cache_Hash));
  if (this->hash_ == 0)
    {
      errno = ENOMEM;
      this->hashsize_ = 0;

      return -1;
    }

  // @@ James, please use the ACE_NEW_*_RETURN macro here.
  new (this->hash_) Cache_Hash (alloc, hashsize);

  // @@ James, please use the ACE_NEW_MALLOC_RETURN macro here.
  this->heap_ = (Cache_Heap *) this->allocator_->malloc (sizeof (Cache_Heap));

  if (this->heap_ == 0)
    {
      errno = ENOMEM;
      this->maxsize_ = 0;

      ACE_DES_FREE (this->hash_, this->allocator_->free, Cache_Hash);
      this->hash_ = 0;
      this->hashsize_ = 0;

      return -1;
    }

  // @@ James, please use the ACE_NEW_*_RETURN macro here.
  new (this->heap_) Cache_Heap (alloc, maxsize);

  return 0;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC>
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>::~ACE_Cache_Manager (void)
{
  this->close ();
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>::close (void)
{
  while (this->waterlevel_ > 0)
    this->FLUSH_i ();

  if (this->hash_)
    {
      ACE_DES_FREE (this->hash_, this->allocator_->free, Cache_Hash);
      this->hash_ = 0;
    }

  if (this->heap_)
    {
      ACE_DES_FREE (this->heap_, this->allocator_->free, Cache_Heap);
      this->heap_ = 0;
    }

  return 0;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::GET_i (const KEY &key, ACE_Cache_Object *&object)
{
  int result = this->hash_->find (key, object);

  if (result == 0)
    this->TAKE (object);
  else
    object = 0;

  return result;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::PUT_i (const KEY &key, const void *data, size_t size, ACE_Cache_Object *&obj)
{
  int result = 0;

  this->FLUSH_i (key);

  result = this->MAKE (data, size, obj);
  if (result == -1)
    return -1;

  result = this->hash_->bind (key, obj);
  if (result == -1)
    {
      this->DROP_i (obj);
      return -1;
    }

  result = this->heap_->insert (key, obj);
  if (result == -1)
    {
      this->hash_->unbind (key);
      this->DROP_i (obj);
      return -1;
    }

  this->waterlevel_ += size;

  // Acquire this one for the putter.
  this->TAKE (obj);

  return 0;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::FLUSH_i (const KEY &key)
{
  ACE_Cache_Object *temp_object;

  int result = this->hash_->unbind (key, temp_object);
  if (result == 0)
    {
      this->waterlevel_ -= temp_object->size ();
      this->heap_->remove (temp_object->heap_item ());
      this->DROP_i (temp_object);
    }

  return result;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::FLUSH_i (void)
{
  KEY temp_key;
  ACE_Cache_Object *temp_object;

  int result = this->heap_->remove (temp_key, temp_object);
  if (result == 0)
    {
      result = this->hash_->unbind (temp_key);
      this->waterlevel_ -= temp_object->size ();
      this->DROP_i (temp_object);
    }

  return result;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::DROP_i (ACE_Cache_Object *const &obj)
{
  int result = obj->release ();

  if (result == 0)
    {
      if (obj->count () == 0)
        this->factory_->destroy (obj);
    }
  else
    result = this->heap_->adjust (obj->heap_item ());

  return result;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::GET (const KEY &key, ACE_Cache_Object *&object)
{
  ACE_Read_Guard<ACE_SYNCH_RW_MUTEX> g (this->lock_);

  return this->GET_i (key, object);
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::PUT (const KEY &key, const void *data, size_t size, ACE_Cache_Object *&obj)
{
  ACE_Write_Guard<ACE_SYNCH_RW_MUTEX> g (this->lock_);

  return this->PUT_i (key, data, size, obj);
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::MAKE (const void *data, size_t size, ACE_Cache_Object *&obj)
{
  // verify object is within cacheable range
  // @@ James, please don't use these magic numbers...
  if (size/1024 > this->maxobjsize_ || size/1024 < this->minobjsize_)
    return -1;

  // make sure heap has enough room
  if (this->heap_->is_full () && this->FLUSH_i () == -1)
    return -1;

  // make sure we have sufficient memory
  if (this->waterlevel_ + size > this->highwater_ * (1024 * 1024))
    do
      if (this->FLUSH_i () == -1)
        return -1;
    while (this->waterlevel_ > this->lowwater_ * (1024 * 1024));

  obj = this->factory_->create (data, size);
  if (this->TAKE (obj) == -1)
    {
      this->factory_->destroy (obj);
      obj = 0;
      return -1;
    }

  return 0;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::TAKE (ACE_Cache_Object *const &obj)
{
  if (obj == 0)
    return -1;

  return obj->acquire ();
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::DROP (ACE_Cache_Object *const &obj)
{
  if (obj == 0)
    return -1;

  {
    ACE_Write_Guard<ACE_SYNCH_RW_MUTEX> g (this->lock_);

    return this->DROP_i (obj);
  }
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
ACE_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::FLUSH (void)
{
  ACE_Write_Guard<ACE_SYNCH_RW_MUTEX> g (this->lock_);

  return this->FLUSH_i ();
}


template <class KEY, class DATA, class CACHE_MANAGER>
ACE_Cache_Proxy<KEY, DATA, CACHE_MANAGER>
::ACE_Cache_Proxy (const KEY &key, Cache_Manager *manager)
  : object_ (0),
    manager_ (manager)
{
  if (this->manager_ == 0)
    this->manager_ = Cache_Manager_Singleton::instance ();

  int result = this->manager_->GET (key, this->object_);
  if (result == -1)
    this->object_ = 0;
}

template <class KEY, class DATA, class CACHE_MANAGER>
ACE_Cache_Proxy<KEY, DATA, CACHE_MANAGER>
::ACE_Cache_Proxy (const KEY &key, DATA *data, size_t size,
                   Cache_Manager *manager)
  : object_ (0),
    manager_ (manager)
{
  if (this->manager_ == 0)
    this->manager_ = Cache_Manager_Singleton::instance ();

  int result = this->manager_->PUT (key, data, size, this->object_);
  if (result == -1)
    this->object_ = 0;
}

template <class KEY, class DATA, class CACHE_MANAGER>
ACE_Cache_Proxy<KEY, DATA, CACHE_MANAGER>::~ACE_Cache_Proxy (void)
{
  this->manager_->DROP (this->object_);
  this->object_ = 0;
}

template <class KEY, class DATA, class CACHE_MANAGER> DATA *
ACE_Cache_Proxy<KEY, DATA, CACHE_MANAGER>::data (void) const
{
  return (DATA *) this->object_->data ();
}

template <class KEY, class DATA, class CACHE_MANAGER>
ACE_Cache_Proxy<KEY, DATA, CACHE_MANAGER>::operator DATA * (void) const
{
  return this->data ();
}

#endif /* ACE_CACHE_MANAGER_T_CPP */