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
);
88 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
89 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
90 ::open (ACE_Allocator
*alloc
,
91 JAWS_Cache_Object_Factory
*cof
,
103 this->allocator_
= alloc
;
104 this->factory_
= cof
;
105 this->hashsize_
= hashsize
;
106 this->maxsize_
= maxsize
;
107 this->maxobjsize_
= maxobjsize
;
108 this->minobjsize_
= minobjsize
;
109 this->highwater_
= highwater
;
110 this->lowwater_
= lowwater
;
111 this->waterlevel_
= 0;
112 this->timetolive_
= timetolive
;
113 this->counted_
= counted
;
115 // Some sanity checking needed here --
116 if (this->lowwater_
> this->highwater_
)
117 this->lowwater_
= this->highwater_
/ 2;
119 if (this->maxobjsize_
> (this->highwater_
- this->lowwater_
) * 1024)
120 this->maxobjsize_
= (this->highwater_
- this->lowwater_
) * (1024/2);
122 if (this->minobjsize_
> this->maxobjsize_
)
123 this->minobjsize_
= this->maxobjsize_
/ 2;
125 if (this->allocator_
== 0)
126 this->allocator_
= ACE_Allocator::instance ();
128 if (this->factory_
== 0)
129 this->factory_
= Object_Factory::instance ();
131 this->hash_
= (Cache_Hash
*) this->allocator_
->malloc (sizeof (Cache_Hash
));
132 if (this->hash_
== 0)
139 new (this->hash_
) Cache_Hash (alloc
, hashsize
);
141 this->heap_
= (Cache_Heap
*) this->allocator_
->malloc (sizeof (Cache_Heap
));
142 if (this->heap_
== 0)
148 ACE_DES_FREE_TEMPLATE3(this->hash_
, this->allocator_
->free
,
150 KEY
, HASH_FUNC
, EQ_FUNC
);
159 new (this->heap_
) Cache_Heap (alloc
, maxsize
);
164 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
>
165 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>::~JAWS_Cache_Manager (void)
170 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
171 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>::close (void)
173 while (this->waterlevel_
> 0)
179 ACE_DES_FREE_TEMPLATE3(this->hash_
, this->allocator_
->free
,
181 KEY
, HASH_FUNC
, EQ_FUNC
);
191 ACE_DES_FREE_TEMPLATE4(this->heap_
, this->allocator_
->free
,
193 KEY
, FACTORY
, HASH_FUNC
, EQ_FUNC
);
203 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
204 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
205 ::GET_i (const KEY
&key
, JAWS_Cache_Object
*&object
)
207 int const result
= this->hash_
->find (key
, object
);
217 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
218 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
219 ::PUT_i (const KEY
&key
, const void *data
, size_t size
, JAWS_Cache_Object
*&obj
)
230 result
= this->MAKE (data
, size
, obj
);
233 if (size
/1024 <= this->maxobjsize_
)
234 cerr
<< "MAKE failed. Bummer!" << endl
;
240 obj
->internal (new KEY (key
));
243 JAWS_Cache_Object
*old_obj
;
245 result
= this->hash_
->rebind (key
, obj
, old_key
, old_obj
);
248 cerr
<< "*** hash bind error: " << key
<< endl
;
253 else if (result
== 1)
255 this->heap_
->remove (old_obj
->heap_item ());
256 this->waterlevel_
-= old_obj
->size ();
258 this->DROP_i (old_obj
);
261 result
= this->heap_
->insert (key
, obj
);
264 cerr
<< "*** heap insertion error: " << key
<< endl
;
265 this->hash_
->unbind (key
);
271 this->waterlevel_
+= size
;
273 // Acquire this one for the putter.
279 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
280 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
281 ::FLUSH_i (const KEY
&key
)
283 JAWS_Cache_Object
*temp_object
;
285 #ifdef ENTERA_VERBOSE_TRACE
286 cerr
<< "*** flush key unbinding: " << key
<< endl
;
288 int result
= this->hash_
->unbind (key
, temp_object
);
291 this->waterlevel_
-= temp_object
->size ();
292 if (this->heap_
->remove (temp_object
->heap_item ()) == -1)
293 cerr
<< "*** flush key heap remove failed: " << endl
;
294 temp_object
->release ();
295 this->DROP_i (temp_object
);
298 cerr
<< "*** flush key hash unbind failed: " << key
<< endl
;
303 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
304 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
308 JAWS_Cache_Object
*temp_object
;
310 int result
= this->heap_
->remove (temp_key
, temp_object
);
313 #ifdef ENTERA_VERBOSE_TRACE
314 cerr
<< "*** flush unbinding: " << temp_key
<< endl
;
316 result
= this->hash_
->unbind (temp_key
);
318 cerr
<< "*** flush hash unbind failed: " << temp_key
<< endl
;
320 this->waterlevel_
-= temp_object
->size ();
321 temp_object
->release ();
322 this->DROP_i (temp_object
);
326 cerr
<< "*** flush heap remove failed" << endl
;
332 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
333 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
334 ::DROP_i (JAWS_Cache_Object
*&obj
)
338 if (obj
->count () == 0)
340 KEY
*key
= (KEY
*) obj
->internal ();
341 this->factory_
->destroy (obj
);
347 result
= this->heap_
->adjust (obj
->heap_item ());
352 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
353 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
354 ::GET (const KEY
&key
, JAWS_Cache_Object
*&object
)
356 ACE_READ_GUARD_RETURN (ACE_SYNCH_RW_MUTEX
, g
,this->lock_
, -1);
358 return this->GET_i (key
, object
);
361 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
362 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
363 ::PUT (const KEY
&key
, const void *data
, size_t size
, JAWS_Cache_Object
*&obj
)
365 ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX
, g
,this->lock_
, -1);
367 return this->PUT_i (key
, data
, size
, obj
);
370 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
371 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
372 ::MAKE (const void *data
, size_t size
, JAWS_Cache_Object
*&obj
)
374 // verify object is within cacheable range
375 if (size
/1024 > this->maxobjsize_
)
378 // What we do is cache it anyway, but remove it as soon as the
379 // requester returns it.
380 obj
= this->factory_
->create (data
, size
);
383 // The above is a little tricky to implement. Think about it
385 obj
= this->factory_
->create (data
, size
);
391 if (size
/1024 < this->minobjsize_
)
394 // Don't bother to cache this.
395 cerr
<< "*** " << static_cast<unsigned int>(size
) << " is too small to cache" << endl
;
399 // make sure we have sufficient memory
400 if (this->waterlevel_
+ size
> this->highwater_
* (1024 * 1024))
404 if (this->FLUSH_i () == -1)
406 cerr
<< "*** cache flooded, flush error" << endl
;
410 while (this->waterlevel_
> this->lowwater_
* (1024 * 1024));
413 // make sure heap has enough room
414 if (this->heap_
->is_full ())
416 cerr
<< "*** heap full, flushing" << endl
;
417 if (this->FLUSH_i () == -1)
419 cerr
<< "*** heap full, flush error" << endl
;
424 obj
= this->factory_
->create (data
, size
);
425 if (this->TAKE (obj
) == -1)
427 cerr
<< "*** take error" << endl
;
428 this->factory_
->destroy (obj
);
436 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
437 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
438 ::TAKE (JAWS_Cache_Object
*const &obj
)
443 return obj
->acquire ();
446 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
447 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
448 ::DROP (JAWS_Cache_Object
*&obj
)
453 ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX
, g
, this->lock_
, -1);
455 int result
= obj
->release ();
459 if (obj
->count () == 0)
461 KEY
*key
= (KEY
*) obj
->internal ();
462 this->factory_
->destroy (obj
);
469 result
= this->DROP_i (obj
);
476 template <class KEY
, class FACTORY
, class HASH_FUNC
, class EQ_FUNC
> int
477 JAWS_Cache_Manager
<KEY
,FACTORY
,HASH_FUNC
,EQ_FUNC
>
480 ACE_WRITE_GUARD_RETURN (ACE_SYNCH_RW_MUTEX
, g
, this->lock_
, -1);
482 return this->FLUSH_i ();
486 template <class KEY
, class DATA
, class CACHE_MANAGER
>
487 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>
488 ::JAWS_Cache_Proxy (const KEY
&key
, Cache_Manager
*manager
)
492 if (this->manager_
== 0)
493 this->manager_
= Cache_Manager_Singleton::instance ();
495 int const result
= this->manager_
->GET (key
, this->object_
);
500 template <class KEY
, class DATA
, class CACHE_MANAGER
>
501 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>
502 ::JAWS_Cache_Proxy (const KEY
&key
, DATA
*data
, size_t size
,
503 Cache_Manager
*manager
)
507 if (this->manager_
== 0)
508 this->manager_
= Cache_Manager_Singleton::instance ();
510 int result
= this->manager_
->PUT (key
, data
, size
, this->object_
);
515 template <class KEY
, class DATA
, class CACHE_MANAGER
>
516 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>::~JAWS_Cache_Proxy (void)
518 DATA
*data
= this->data ();
519 this->manager_
->DROP (this->object_
);
520 if (this->object_
== 0)
524 template <class KEY
, class DATA
, class CACHE_MANAGER
> DATA
*
525 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>::data () const
527 return this->object_
? (DATA
*) this->object_
->data () : 0;
530 template <class KEY
, class DATA
, class CACHE_MANAGER
>
531 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>::operator DATA
* () const
533 return this->data ();
536 template <class KEY
, class DATA
, class CACHE_MANAGER
> int
537 JAWS_Cache_Proxy
<KEY
, DATA
, CACHE_MANAGER
>::close (DATA
*)
543 #endif /* JAWS_CACHE_MANAGER_T_CPP */