Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / apps / JAWS2 / JAWS / Cache_Object.cpp
blobb850dbf723027c6b3df38d93100a5e8e08d7acc7
1 #include "JAWS/Cache_Object.h"
3 #include "ace/Malloc_Base.h"
4 #include "ace/Guard_T.h"
5 #include "ace/OS_NS_time.h"
7 JAWS_Cache_Object::JAWS_Cache_Object (const void *data, size_t size)
8 : internal_ (0),
9 data_ (data),
10 size_ (size)
12 this->first_access_ = ACE_OS::time ((time_t *)0);
13 this->new_last_access_ = this->last_access_ = this->first_access_;
16 JAWS_Cache_Object::~JAWS_Cache_Object ()
18 this->data_ = 0;
19 this->size_ = 0;
22 void *
23 JAWS_Cache_Object::internal () const
25 return this->internal_;
28 void
29 JAWS_Cache_Object::internal (void *item)
31 this->internal_ = item;
34 const void *
35 JAWS_Cache_Object::data () const
37 return this->data_;
40 size_t
41 JAWS_Cache_Object::size () const
43 return this->size_;
46 unsigned int
47 JAWS_Cache_Object::count () const
49 return this->count_i ();
52 int
53 JAWS_Cache_Object::acquire ()
55 this->new_last_access_ = ACE_OS::time ((time_t *)0);
56 return this->acquire_i ();
59 int
60 JAWS_Cache_Object::release ()
62 this->last_access_ = this->new_last_access_;
63 return this->release_i ();
66 time_t
67 JAWS_Cache_Object::last_access () const
69 return this->last_access_;
72 time_t
73 JAWS_Cache_Object::first_access () const
75 return this->first_access_;
78 unsigned int
79 JAWS_Cache_Object::priority () const
81 return this->priority_i ();
84 void *
85 JAWS_Cache_Object::heap_item () const
87 return this->heap_item_;
90 void
91 JAWS_Cache_Object::heap_item (void *item)
93 this->heap_item_ = item;
97 JAWS_Referenced_Cache_Object::
98 JAWS_Referenced_Cache_Object (const void *data, size_t size)
99 : JAWS_Cache_Object (data, size),
100 lock_adapter_ (count_)
104 JAWS_Referenced_Cache_Object::~JAWS_Referenced_Cache_Object ()
108 ACE_Lock &
109 JAWS_Referenced_Cache_Object::lock ()
111 return this->lock_adapter_;
114 unsigned int
115 JAWS_Referenced_Cache_Object::count_i () const
117 if (this->count_.tryacquire_write () == 0)
118 return 0;
120 return 1;
124 JAWS_Referenced_Cache_Object::acquire_i ()
126 return this->count_.acquire_read ();
130 JAWS_Referenced_Cache_Object::release_i ()
132 return this->count_.release ();
135 unsigned int
136 JAWS_Referenced_Cache_Object::priority_i () const
138 unsigned int priority = ~(0U);
139 double delta
140 = ACE_OS::difftime (this->last_access (), this->first_access ());
142 if (delta >= 0.0 && delta < ~(0U))
143 priority = (unsigned) delta;
145 return priority;
149 JAWS_Counted_Cache_Object::
150 JAWS_Counted_Cache_Object (const void *data, size_t size)
151 : JAWS_Cache_Object (data, size),
152 count_ (0),
153 new_count_ (0),
154 lock_adapter_ (lock_)
158 JAWS_Counted_Cache_Object::~JAWS_Counted_Cache_Object ()
162 ACE_Lock &
163 JAWS_Counted_Cache_Object::lock ()
165 return this->lock_adapter_;
168 unsigned int
169 JAWS_Counted_Cache_Object::count_i () const
171 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, g ,this->lock_, 0);
173 return this->count_;
177 JAWS_Counted_Cache_Object::acquire_i ()
179 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, g ,this->lock_, 0);
181 ++this->new_count_;
182 return 0;
186 JAWS_Counted_Cache_Object::release_i ()
188 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, g ,this->lock_, 0);
190 --this->new_count_;
191 this->count_ = this->new_count_;
192 return 0;
195 unsigned int
196 JAWS_Counted_Cache_Object::priority_i () const
198 return this->count_i ();
201 JAWS_Cache_Object_Factory::JAWS_Cache_Object_Factory (ACE_Allocator *alloc)
202 : allocator_ (alloc)
204 if (this->allocator_ == 0)
205 this->allocator_ = ACE_Allocator::instance ();
208 JAWS_Cache_Object_Factory::~JAWS_Cache_Object_Factory ()
213 JAWS_Cache_Object_Factory::open (ACE_Allocator *alloc)
215 this->allocator_ = alloc;
217 if (this->allocator_ == 0)
218 this->allocator_ = ACE_Allocator::instance ();
220 return 0;
223 JAWS_Referenced_Cache_Object_Factory
224 ::JAWS_Referenced_Cache_Object_Factory (ACE_Allocator *alloc)
225 : JAWS_Cache_Object_Factory (alloc)
229 JAWS_Referenced_Cache_Object_Factory
230 ::~JAWS_Referenced_Cache_Object_Factory ()
234 JAWS_Cache_Object *
235 JAWS_Referenced_Cache_Object_Factory::create (const void *data, size_t size)
237 JAWS_Referenced_Cache_Object *obj;
239 size_t obj_size = sizeof (JAWS_Referenced_Cache_Object);
240 ACE_NEW_MALLOC_RETURN (obj,
241 (JAWS_Referenced_Cache_Object *)
242 this->allocator_->malloc (obj_size),
243 JAWS_Referenced_Cache_Object (data, size), 0);
245 return obj;
248 void
249 JAWS_Referenced_Cache_Object_Factory::destroy (JAWS_Cache_Object *obj)
251 JAWS_Referenced_Cache_Object *rco = (JAWS_Referenced_Cache_Object *) obj;
252 ACE_DES_FREE (rco, this->allocator_->free, JAWS_Referenced_Cache_Object);
255 JAWS_Counted_Cache_Object_Factory
256 ::JAWS_Counted_Cache_Object_Factory (ACE_Allocator *alloc)
257 : JAWS_Cache_Object_Factory (alloc)
261 JAWS_Counted_Cache_Object_Factory
262 ::~JAWS_Counted_Cache_Object_Factory ()
266 JAWS_Cache_Object *
267 JAWS_Counted_Cache_Object_Factory::create (const void *data, size_t size)
269 JAWS_Counted_Cache_Object *obj;
271 size_t obj_size = sizeof (JAWS_Counted_Cache_Object);
272 ACE_NEW_MALLOC_RETURN (obj,
273 (JAWS_Counted_Cache_Object *)
274 this->allocator_->malloc (obj_size),
275 JAWS_Counted_Cache_Object (data, size), 0);
277 return obj;
280 void
281 JAWS_Counted_Cache_Object_Factory::destroy (JAWS_Cache_Object *obj)
283 JAWS_Counted_Cache_Object *cco = (JAWS_Counted_Cache_Object *) obj;
284 ACE_DES_FREE (cco, this->allocator_->free, JAWS_Counted_Cache_Object);