1 #ifndef JAWS_CACHE_MANAGER_T_CPP
2 #define JAWS_CACHE_MANAGER_T_CPP
4 #include "JAWS/Cache_Manager_T.h"
5 #include "JAWS/Cache_Hash_T.h"
6 #include "JAWS/Cache_List_T.h"
8 // FUZZ: disable check_for_streams_include
9 #include "ace/streams.h"
13 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
>
14 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
15 ::JAWS_Cache_Manager (ACE_Allocator
*alloc
,
16 JAWS_Cache_Object_Factory
*cof
,
29 maxobjsize_ (maxobjsize
),
30 minobjsize_ (minobjsize
),
31 highwater_ (highwater
),
34 timetolive_ (timetolive
),
39 // Some sanity checking needed here --
40 if (this->lowwater_
> this->highwater_
)
41 this->lowwater_
= this->highwater_
/ 2;
43 if (this->maxobjsize_
> (this->highwater_
- this->lowwater_
) * 1024)
44 this->maxobjsize_
= (this->highwater_
- this->lowwater_
) * (1024/2);
46 if (this->minobjsize_
> this->maxobjsize_
)
47 this->minobjsize_
= this->maxobjsize_
/ 2;
49 if (this->allocator_
== 0)
50 this->allocator_
= ACE_Allocator::instance ();
52 if (this->factory_
== 0)
53 this->factory_
= Object_Factory::instance ();
55 ACE_NEW_MALLOC (this->hash_
,
57 this->allocator_
->malloc (sizeof (Cache_Hash
)),
58 Cache_Hash (alloc
, hashsize
));
66 ACE_NEW_MALLOC (this->heap_
,
68 this->allocator_
->malloc (sizeof (Cache_Heap
)),
69 Cache_Heap (alloc
, maxsize
));
76 ACE_DES_FREE_TEMPLATE3(this->hash_
, this->allocator_
->free
,
78 KEY
, HASH_FUNC
, EQ_FUNC
);
87 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
88 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
89 ::open (ACE_Allocator
*alloc
,
90 JAWS_Cache_Object_Factory
*cof
,
102 this->allocator_
= alloc
;
103 this->factory_
= cof
;
104 this->hashsize_
= hashsize
;
105 this->maxsize_
= maxsize
;
106 this->maxobjsize_
= maxobjsize
;
107 this->minobjsize_
= minobjsize
;
108 this->highwater_
= highwater
;
109 this->lowwater_
= lowwater
;
110 this->waterlevel_
= 0;
111 this->timetolive_
= timetolive
;
112 this->counted_
= counted
;
114 // Some sanity checking needed here --
115 if (this->lowwater_
> this->highwater_
)
116 this->lowwater_
= this->highwater_
/ 2;
118 if (this->maxobjsize_
> (this->highwater_
- this->lowwater_
) * 1024)
119 this->maxobjsize_
= (this->highwater_
- this->lowwater_
) * (1024/2);
121 if (this->minobjsize_
> this->maxobjsize_
)
122 this->minobjsize_
= this->maxobjsize_
/ 2;
124 if (this->allocator_
== 0)
125 this->allocator_
= ACE_Allocator::instance ();
127 if (this->factory_
== 0)
128 this->factory_
= Object_Factory::instance ();
130 this->hash_
= (Cache_Hash
*) this->allocator_
->malloc (sizeof (Cache_Hash
));
131 if (this->hash_
== 0)
138 new (this->hash_
) Cache_Hash (alloc
, hashsize
);
140 this->heap_
= (Cache_Heap
*) this->allocator_
->malloc (sizeof (Cache_Heap
));
141 if (this->heap_
== 0)
147 ACE_DES_FREE_TEMPLATE3(this->hash_
, this->allocator_
->free
,
149 KEY
, HASH_FUNC
, EQ_FUNC
);
157 new (this->heap_
) Cache_Heap (alloc
, maxsize
);
162 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
>
163 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>::~JAWS_Cache_Manager ()
168 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
169 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>::close ()
171 while (this->waterlevel_
> 0)
176 ACE_DES_FREE_TEMPLATE3(this->hash_
, this->allocator_
->free
,
178 KEY
, HASH_FUNC
, EQ_FUNC
);
186 ACE_DES_FREE_TEMPLATE4(this->heap_
, this->allocator_
->free
,
188 KEY
, FACTORY
, HASH_FUNC
, EQ_FUNC
);
197 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
198 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
199 ::GET_i (const KEY
&key
, JAWS_Cache_Object
*&object
)
201 int const result
= this->hash_
->find (key
, object
);
211 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
212 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
213 ::PUT_i (const KEY
&key
, const void *data
, size_t size
, JAWS_Cache_Object
*&obj
)
224 result
= this->MAKE (data
, size
, obj
);
227 if (size
/1024 <= this->maxobjsize_
)
228 cerr
<< "MAKE failed. Bummer!" << endl
;
234 obj
->internal (new KEY (key
));
237 JAWS_Cache_Object
*old_obj
;
239 result
= this->hash_
->rebind (key
, obj
, old_key
, old_obj
);
242 cerr
<< "*** hash bind error: " << key
<< endl
;
247 else if (result
== 1)
249 this->heap_
->remove (old_obj
->heap_item ());
250 this->waterlevel_
-= old_obj
->size ();
252 this->DROP_i (old_obj
);
255 result
= this->heap_
->insert (key
, obj
);
258 cerr
<< "*** heap insertion error: " << key
<< endl
;
259 this->hash_
->unbind (key
);
265 this->waterlevel_
+= size
;
267 // Acquire this one for the putter.
273 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
274 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
275 ::FLUSH_i (const KEY
&key
)
277 JAWS_Cache_Object
*temp_object
;
279 #ifdef ENTERA_VERBOSE_TRACE
280 cerr
<< "*** flush key unbinding: " << key
<< endl
;
282 int result
= this->hash_
->unbind (key
, temp_object
);
285 this->waterlevel_
-= temp_object
->size ();
286 if (this->heap_
->remove (temp_object
->heap_item ()) == -1)
287 cerr
<< "*** flush key heap remove failed: " << endl
;
288 temp_object
->release ();
289 this->DROP_i (temp_object
);
292 cerr
<< "*** flush key hash unbind failed: " << key
<< endl
;
297 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
298 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
302 JAWS_Cache_Object
*temp_object
;
304 int result
= this->heap_
->remove (temp_key
, temp_object
);
307 #ifdef ENTERA_VERBOSE_TRACE
308 cerr
<< "*** flush unbinding: " << temp_key
<< endl
;
310 result
= this->hash_
->unbind (temp_key
);
312 cerr
<< "*** flush hash unbind failed: " << temp_key
<< endl
;
314 this->waterlevel_
-= temp_object
->size ();
315 temp_object
->release ();
316 this->DROP_i (temp_object
);
320 cerr
<< "*** flush heap remove failed" << endl
;
326 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
327 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
328 ::DROP_i (JAWS_Cache_Object
*&obj
)
332 if (obj
->count () == 0)
334 KEY
*key
= (KEY
*) obj
->internal ();
335 this->factory_
->destroy (obj
);
341 result
= this->heap_
->adjust (obj
->heap_item ());
346 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
347 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
348 ::GET (const KEY
&key
, JAWS_Cache_Object
*&object
)
350 ACE_READ_GUARD_RETURN (ACE_SYNCH_RW_MUTEX
, g
,this->lock_
, -1);
352 return this->GET_i (key
, object
);
355 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
356 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
357 ::PUT (const KEY
&key
, const void *data
, size_t size
, JAWS_Cache_Object
*&obj
)
359 ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX
, g
,this->lock_
, -1);
361 return this->PUT_i (key
, data
, size
, obj
);
364 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
365 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
366 ::MAKE (const void *data
, size_t size
, JAWS_Cache_Object
*&obj
)
368 // verify object is within cacheable range
369 if (size
/1024 > this->maxobjsize_
)
372 // What we do is cache it anyway, but remove it as soon as the
373 // requester returns it.
374 obj
= this->factory_
->create (data
, size
);
377 // The above is a little tricky to implement. Think about it
379 obj
= this->factory_
->create (data
, size
);
385 if (size
/1024 < this->minobjsize_
)
388 // Don't bother to cache this.
389 cerr
<< "*** " << static_cast<unsigned int>(size
) << " is too small to cache" << endl
;
393 // make sure we have sufficient memory
394 if (this->waterlevel_
+ size
> this->highwater_
* (1024 * 1024))
398 if (this->FLUSH_i () == -1)
400 cerr
<< "*** cache flooded, flush error" << endl
;
404 while (this->waterlevel_
> this->lowwater_
* (1024 * 1024));
407 // make sure heap has enough room
408 if (this->heap_
->is_full ())
410 cerr
<< "*** heap full, flushing" << endl
;
411 if (this->FLUSH_i () == -1)
413 cerr
<< "*** heap full, flush error" << endl
;
418 obj
= this->factory_
->create (data
, size
);
419 if (this->TAKE (obj
) == -1)
421 cerr
<< "*** take error" << endl
;
422 this->factory_
->destroy (obj
);
430 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
431 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
432 ::TAKE (JAWS_Cache_Object
*const &obj
)
437 return obj
->acquire ();
440 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
441 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
442 ::DROP (JAWS_Cache_Object
*&obj
)
447 ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX
, g
, this->lock_
, -1);
449 int result
= obj
->release ();
453 if (obj
->count () == 0)
455 KEY
*key
= (KEY
*) obj
->internal ();
456 this->factory_
->destroy (obj
);
463 result
= this->DROP_i (obj
);
470 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
471 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
474 ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX
, g
, this->lock_
, -1);
476 return this->FLUSH_i ();
480 template <class KEY
, class DATA
, class CACHE_MANAGER
>
481 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>
482 ::JAWS_Cache_Proxy (const KEY
&key
, Cache_Manager
*manager
)
486 if (this->manager_
== 0)
487 this->manager_
= Cache_Manager_Singleton::instance ();
489 int const result
= this->manager_
->GET (key
, this->object_
);
494 template <class KEY
, class DATA
, class CACHE_MANAGER
>
495 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>
496 ::JAWS_Cache_Proxy (const KEY
&key
, DATA
*data
, size_t size
,
497 Cache_Manager
*manager
)
501 if (this->manager_
== 0)
502 this->manager_
= Cache_Manager_Singleton::instance ();
504 int result
= this->manager_
->PUT (key
, data
, size
, this->object_
);
509 template <class KEY
, class DATA
, class CACHE_MANAGER
>
510 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>::~JAWS_Cache_Proxy ()
512 DATA
*data
= this->data ();
513 this->manager_
->DROP (this->object_
);
514 if (this->object_
== 0)
518 template <class KEY
, class DATA
, class CACHE_MANAGER
> DATA
*
519 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>::data () const
521 return this->object_
? (DATA
*) this->object_
->data () : 0;
524 template <class KEY
, class DATA
, class CACHE_MANAGER
>
525 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>::operator DATA
* () const
527 return this->data ();
530 template <class KEY
, class DATA
, class CACHE_MANAGER
> int
531 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>::close (DATA
*)
537 #endif /* JAWS_CACHE_MANAGER_T_CPP */