Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / apps / JAWS2 / JAWS / Cache_Heap_T.cpp
blob99185b8280a534870601c39f6f6a0be9ac0cb4c4
1 #ifndef JAWS_CACHE_HEAP_T_CPP
2 #define JAWS_CACHE_HEAP_T_CPP
4 #include "JAWS/Cache_Heap_T.h"
5 #include "JAWS/Cache_Manager_T.h"
7 template <class EXT_ID, class FACT, class H_FN, class E_FN>
8 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::JAWS_Cache_Heap (ACE_Allocator *alloc,
9 size_t maxsize)
10 : allocator_ (alloc),
11 maxsize_ (maxsize),
12 size_ (0)
14 if (this->allocator_ == 0)
15 this->allocator_ = ACE_Allocator::instance ();
17 size_t memsize
18 = this->maxsize_ * sizeof (Cache_Heap_Item *);
20 this->heap_ = (Cache_Heap_Item **) this->allocator_->malloc (memsize);
21 if (this->heap_)
23 for (size_t i = 0; i < this->maxsize_; i++)
24 this->heap_[i] = 0;
26 else
28 this->maxsize_ = 0;
29 // should indicate something
33 template <class EXT_ID, class FACT, class H_FN, class E_FN>
34 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::~JAWS_Cache_Heap (void)
36 if (this->heap_ != 0)
38 for (size_t i = 0; i < this->maxsize_; i++)
40 if (this->heap_[i])
42 ACE_DES_FREE_TEMPLATE4(this->heap_[i], this->allocator_->free,
43 JAWS_Cache_Heap_Item,
44 EXT_ID, FACT, H_FN, E_FN);
46 this->heap_[i] = 0;
49 this->allocator_->free (this->heap_);
50 this->heap_ = 0;
53 this->allocator_ = 0;
56 template <class EXT_ID, class FACT, class H_FN, class E_FN> int
57 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::is_empty (void) const
59 return (this->size_ == 0);
62 template <class EXT_ID, class FACT, class H_FN, class E_FN> int
63 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::is_full (void) const
65 return (this->size_ == this->maxsize_);
68 template <class EXT_ID, class FACT, class H_FN, class E_FN> size_t
69 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::size (void) const
71 return this->size_;
74 template <class EXT_ID, class FACT, class H_FN, class E_FN> size_t
75 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::maxsize (void) const
77 return this->maxsize_;
80 template <class EXT_ID, class FACT, class H_FN, class E_FN> int
81 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::maxsize (Cache_Manager *cm,
82 size_t new_maxsize)
84 int result = -1;
86 size_t memsize
87 = new_maxsize * sizeof (Cache_Heap_Item *);
89 Cache_Heap_Item **new_heap
90 = (Cache_Heap_Item **) this->allocator_->malloc (memsize);
91 if (new_heap)
93 while (new_maxsize < this->size_)
94 cm->FLUSH_i ();
96 for (size_t i = 0; i < new_maxsize; i++)
97 if (i < this->size_)
98 new_heap[i] = this->heap_[i];
99 else
100 new_heap[i] = 0;
102 Cache_Heap_Item ** volatile temp = this->heap_;
103 this->heap_ = new_heap;
104 this->maxsize_ = new_maxsize;
105 this->allocator_->free (temp);
106 result = 0;
109 return result;
112 template <class EXT_ID, class FACT, class H_FN, class E_FN> void
113 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::insert_i (Cache_Heap_Item *item)
115 /* ASSERT: this->size_ < this->maxsize_ */
117 size_t i;
119 for (i = this->size_ + 1; i > 1; i /= 2)
121 if (item->priority () > this->heap_[i/2 - 1]->priority ())
122 break;
124 this->heap_[i-1] = this->heap_[i/2 - 1];
125 this->heap_[i-1]->heap_idx_ = i-1;
128 this->heap_[i-1] = item;
129 this->heap_[i-1]->heap_idx_ = i-1;
130 this->size_++;
133 template <class EXT_ID, class FACT, class H_FN, class E_FN> int
134 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::insert (const EXT_ID &ext_id,
135 JAWS_Cache_Object *const &int_id)
137 if (this->is_full ())
138 return -1;
140 Cache_Heap_Item *item;
141 ACE_NEW_MALLOC_RETURN (item,
142 (Cache_Heap_Item *)
143 this->allocator_->malloc (sizeof (Cache_Heap_Item)),
144 Cache_Heap_Item (ext_id, int_id), -1);
146 this->insert_i (item);
148 return 0;
151 template <class EXT_ID, class FACT, class H_FN, class E_FN> void
152 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::remove_i (void)
154 /* ASSERT: this->size_ > 0 */
155 this->size_--;
156 Cache_Heap_Item *temp = this->heap_[this->size_];
157 this->heap_[this->size_] = 0;
159 size_t i = 1;
160 while (2*i <= this->size_)
162 size_t child = 2*i;
163 if ((child < this->size_)
164 && (this->heap_[2*i]->priority ()
165 < this->heap_[2*i - 1]->priority ()))
166 child = 2*i + 1;
168 if (temp->priority () < this->heap_[child-1]->priority ())
169 break;
171 this->heap_[i-1] = this->heap_[child-1];
172 this->heap_[i-1]->heap_idx_ = i-1;
173 i = child;
176 if (this->size_ > 0)
178 this->heap_[i-1] = temp;
179 this->heap_[i-1]->heap_idx_ = i-1;
183 template <class EXT_ID, class FACT, class H_FN, class E_FN> void
184 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::remove_i (size_t pos)
186 Cache_Heap_Item *item = this->heap_[pos];
188 if (pos > 0)
190 int i = pos + 1;
193 this->heap_[i-1] = this->heap_[i/2 - 1];
194 this->heap_[i-1]->heap_idx_ = i-1;
195 i /= 2;
197 while (i > 1);
200 this->heap_[0] = item;
202 this->remove_i ();
205 template <class EXT_ID, class FACT, class H_FN, class E_FN> int
206 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::remove (EXT_ID &ext_id,
207 JAWS_Cache_Object *&int_id)
209 if (this->is_empty ())
210 return -1;
212 Cache_Heap_Item *item = this->heap_[0];
213 item->int_id_->heap_item (0);
215 this->remove_i ();
217 ext_id = item->ext_id_;
218 int_id = item->int_id_;
220 ACE_DES_FREE_TEMPLATE4(item, this->allocator_->free,
221 JAWS_Cache_Heap_Item,
222 EXT_ID, FACT, H_FN, E_FN);
224 item = 0;
225 return 0;
228 template <class EXT_ID, class FACT, class H_FN, class E_FN> int
229 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::remove (void *item)
231 if (item == 0)
232 return 0;
234 Cache_Heap_Item *real_item = (Cache_Heap_Item *) item;
236 // Make sure the item is where it thinks it is.
237 if (this->heap_[real_item->heap_idx_] != real_item)
238 return -1;
240 real_item->int_id_->heap_item (0);
241 this->remove_i (real_item->heap_idx_);
243 ACE_DES_FREE_TEMPLATE4(real_item, this->allocator_->free,
244 JAWS_Cache_Heap_Item,
245 EXT_ID, FACT, H_FN, E_FN);
247 real_item = 0;
249 return 0;
252 template <class EXT_ID, class FACT, class H_FN, class E_FN> int
253 JAWS_Cache_Heap<EXT_ID,FACT,H_FN,E_FN>::adjust (void *item)
255 if (item == 0)
256 return 0;
258 Cache_Heap_Item *real_item = (Cache_Heap_Item *) item;
260 // Make sure the item is where it thinks it is.
261 if (this->heap_[real_item->heap_idx_] != real_item)
262 return -1;
264 this->remove_i (real_item->heap_idx_);
265 this->insert_i (real_item);
267 return 0;
271 template <class EXT_ID, class FACT, class H_FN, class E_FN>
272 JAWS_Cache_Heap_Item<EXT_ID,FACT,H_FN,E_FN>::
273 JAWS_Cache_Heap_Item (const EXT_ID &ext_id, JAWS_Cache_Object *const &int_id)
274 : ext_id_ (ext_id),
275 int_id_ (int_id),
276 heap_idx_ (0)
278 this->int_id_->heap_item (this);
281 template <class EXT_ID, class FACT, class H_FN, class E_FN> unsigned int
282 JAWS_Cache_Heap_Item<EXT_ID,FACT,H_FN,E_FN>::priority (void)
284 return this->int_id_->priority ();
288 #endif /* JAWS_CACHE_HEAP_T_CPP */