/dep/acelite/ace/Caching_Utility_T.cpp

https://github.com/LORDofDOOM/MMOTBC · C++ · 500 lines · 363 code · 70 blank · 67 comment · 70 complexity · 517afaa96e9d624a200b69a23607f2de MD5 · raw file

  1. // $Id: Caching_Utility_T.cpp 92264 2010-10-19 18:12:46Z olli $
  2. #ifndef ACE_CACHING_UTILITY_T_CPP
  3. #define ACE_CACHING_UTILITY_T_CPP
  4. #include "ace/Caching_Utility_T.h"
  5. #if !defined (ACE_LACKS_PRAGMA_ONCE)
  6. #pragma once
  7. #endif /* ACE_LACKS_PRAGMA_ONCE */
  8. #include "ace/ACE.h"
  9. #include "ace/Min_Max.h"
  10. #include "ace/OS_Memory.h"
  11. #include "ace/Recyclable.h"
  12. //////////////////////////////////////////////////////////////////////////////
  13. ACE_BEGIN_VERSIONED_NAMESPACE_DECL
  14. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  15. ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Pair_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
  16. bool delete_cleanup_strategy)
  17. : cleanup_strategy_ (cleanup_strategy),
  18. delete_cleanup_strategy_ (delete_cleanup_strategy)
  19. {
  20. if (cleanup_strategy == 0)
  21. {
  22. ACE_NEW (this->cleanup_strategy_,
  23. CLEANUP_STRATEGY);
  24. this->delete_cleanup_strategy_ = true;
  25. }
  26. }
  27. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  28. ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Pair_Caching_Utility (void)
  29. {
  30. if (this->delete_cleanup_strategy_)
  31. delete this->cleanup_strategy_;
  32. }
  33. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
  34. ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
  35. double purge_percent)
  36. {
  37. // Check that the purge_percent is non-zero.
  38. if (ACE::is_equal (purge_percent, 0.0))
  39. return 0;
  40. // Get the number of entries in the container.
  41. size_t current_map_size = container.current_size ();
  42. // Also whether the number of entries in the cache!
  43. // Oops! then there is no way out but exiting. So return an error.
  44. if (current_map_size == 0)
  45. return 0;
  46. // Calculate the no of entries to remove from the cache depending
  47. // upon the <purge_percent>.
  48. size_t const entries_to_remove
  49. = ACE_MAX (static_cast<size_t> (1),
  50. static_cast<size_t> (static_cast<double> (purge_percent)
  51. / 100 * current_map_size));
  52. KEY *key_to_remove = 0;
  53. VALUE *value_to_remove = 0;
  54. for (size_t i = 0; i < entries_to_remove ; ++i)
  55. {
  56. this->minimum (container,
  57. key_to_remove,
  58. value_to_remove);
  59. // Simply verifying that the key is non-zero.
  60. // This is important for strategies where the minimum
  61. // entry cant be found due to constraints on the type of entry
  62. // to remove.
  63. if (key_to_remove == 0)
  64. return 0;
  65. if (this->cleanup_strategy_->cleanup (container,
  66. key_to_remove,
  67. value_to_remove) == -1)
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
  73. ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
  74. KEY *&key_to_remove,
  75. VALUE *&value_to_remove)
  76. {
  77. // Starting values.
  78. ITERATOR iter = container.begin ();
  79. ITERATOR end = container.end ();
  80. ATTRIBUTES min = (*iter).int_id_.second;
  81. key_to_remove = &(*iter).ext_id_;
  82. value_to_remove = &(*iter).int_id_;
  83. // The iterator moves thru the container searching for the entry
  84. // with the lowest ATTRIBUTES.
  85. for (++iter;
  86. iter != end;
  87. ++iter)
  88. {
  89. if (min > (*iter).int_id_.second)
  90. {
  91. // Ah! an item with lower ATTTRIBUTES...
  92. min = (*iter).int_id_.second;
  93. key_to_remove = &(*iter).ext_id_;
  94. value_to_remove = &(*iter).int_id_;
  95. }
  96. }
  97. }
  98. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  99. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  100. ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Recyclable_Handler_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
  101. bool delete_cleanup_strategy)
  102. : cleanup_strategy_ (cleanup_strategy),
  103. delete_cleanup_strategy_ (delete_cleanup_strategy)
  104. {
  105. if (cleanup_strategy == 0)
  106. {
  107. ACE_NEW (this->cleanup_strategy_,
  108. CLEANUP_STRATEGY);
  109. this->delete_cleanup_strategy_ = true;
  110. }
  111. }
  112. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  113. ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Recyclable_Handler_Caching_Utility (void)
  114. {
  115. if (this->delete_cleanup_strategy_)
  116. delete this->cleanup_strategy_;
  117. }
  118. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
  119. ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
  120. double purge_percent)
  121. {
  122. // Check that the purge_percent is non-zero.
  123. if (ACE::is_equal (purge_percent, 0.0))
  124. return 0;
  125. // Get the number of entries in the container.
  126. size_t current_map_size = container.current_size ();
  127. // Also whether the number of entries in the cache is just one!
  128. // Oops! then there is no way out but exiting. So return an error.
  129. // if (current_map_size <= 1)
  130. if (current_map_size == 0)
  131. return 0;
  132. // Calculate the no of entries to remove from the cache depending
  133. // upon the <purge_percent>.
  134. size_t const entries_to_remove
  135. = ACE_MAX (static_cast<size_t> (1),
  136. static_cast<size_t> (static_cast<double> (purge_percent)
  137. / 100 * current_map_size));
  138. KEY *key_to_remove = 0;
  139. VALUE *value_to_remove = 0;
  140. for (size_t i = 0; i < entries_to_remove ; ++i)
  141. {
  142. this->minimum (container,
  143. key_to_remove,
  144. value_to_remove);
  145. // Simply verifying that the key is non-zero.
  146. // This is important for strategies where the minimum
  147. // entry cant be found due to constraints on the type of entry
  148. // to remove.
  149. if (key_to_remove == 0)
  150. return 0;
  151. if (this->cleanup_strategy_->cleanup (container,
  152. key_to_remove,
  153. value_to_remove) == -1)
  154. return -1;
  155. }
  156. return 0;
  157. }
  158. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
  159. ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
  160. KEY *&key_to_remove,
  161. VALUE *&value_to_remove)
  162. {
  163. // Starting values.
  164. ITERATOR end = container.end ();
  165. ITERATOR iter = container.begin ();
  166. ATTRIBUTES min = (*iter).int_id_.second;
  167. key_to_remove = 0;
  168. value_to_remove = 0;
  169. // Found the minimum entry to be purged?
  170. int found = 0;
  171. // The iterator moves thru the container searching for the entry
  172. // with the lowest ATTRIBUTES.
  173. for (;
  174. iter != end;
  175. ++iter)
  176. {
  177. // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
  178. // the first entry which can be purged. This is the minimum with
  179. // which you will compare the rest of the purgable entries.
  180. if ((*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE ||
  181. (*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE)
  182. {
  183. if (found == 0)
  184. {
  185. min = (*iter).int_id_.second;
  186. key_to_remove = &(*iter).ext_id_;
  187. value_to_remove = &(*iter).int_id_;
  188. found = 1;
  189. }
  190. else
  191. {
  192. // Ah! an entry with lower ATTTRIBUTES...
  193. if (min > (*iter).int_id_.second)
  194. {
  195. min = (*iter).int_id_.second;
  196. key_to_remove = &(*iter).ext_id_;
  197. value_to_remove = &(*iter).int_id_;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. ////////////////////////////////////////////////////////////////////////////////
  204. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  205. ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Refcounted_Recyclable_Handler_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
  206. bool delete_cleanup_strategy)
  207. : cleanup_strategy_ (cleanup_strategy),
  208. delete_cleanup_strategy_ (delete_cleanup_strategy),
  209. marked_as_closed_entries_ (0)
  210. {
  211. if (cleanup_strategy == 0)
  212. {
  213. ACE_NEW (this->cleanup_strategy_,
  214. CLEANUP_STRATEGY);
  215. this->delete_cleanup_strategy_ = true;
  216. }
  217. }
  218. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  219. ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Refcounted_Recyclable_Handler_Caching_Utility (void)
  220. {
  221. if (this->delete_cleanup_strategy_)
  222. delete this->cleanup_strategy_;
  223. }
  224. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
  225. ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
  226. double purge_percent)
  227. {
  228. // Check that the purge_percent is non-zero.
  229. if (ACE::is_equal (purge_percent, 0.0))
  230. return 0;
  231. // Get the number of entries in the container which can be considered for purging.
  232. size_t const available_entries =
  233. container.current_size () - this->marked_as_closed_entries_;
  234. // Also whether the number of entries in the cache zero.
  235. // Oops! then there is no way out but exiting.
  236. if (available_entries <= 0)
  237. return 0;
  238. // Calculate the no of entries to remove from the cache depending
  239. // upon the <purge_percent>.
  240. size_t entries_to_remove
  241. = ACE_MAX (static_cast<size_t> (1),
  242. static_cast<size_t> (static_cast<double> (purge_percent)
  243. / 100 * available_entries));
  244. if (entries_to_remove >= available_entries || entries_to_remove == 0)
  245. entries_to_remove = available_entries - 1;
  246. KEY *key_to_remove = 0;
  247. VALUE *value_to_remove = 0;
  248. for (size_t i = 0; i < entries_to_remove ; ++i)
  249. {
  250. this->minimum (container,
  251. key_to_remove,
  252. value_to_remove);
  253. // Simply verifying that the key is non-zero.
  254. // This is important for strategies where the minimum
  255. // entry cant be found due to constraints on the type of entry
  256. // to remove.
  257. if (key_to_remove == 0)
  258. return 0;
  259. if (this->cleanup_strategy_->cleanup (container,
  260. key_to_remove,
  261. value_to_remove) == -1)
  262. return -1;
  263. ++this->marked_as_closed_entries_;
  264. }
  265. return 0;
  266. }
  267. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
  268. ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
  269. KEY *&key_to_remove,
  270. VALUE *&value_to_remove)
  271. {
  272. // Starting values.
  273. ITERATOR end = container.end ();
  274. ITERATOR iter = container.begin ();
  275. ATTRIBUTES min = (*iter).int_id_.second ();
  276. key_to_remove = 0;
  277. value_to_remove = 0;
  278. // Found the minimum entry to be purged?
  279. int found = 0;
  280. // The iterator moves thru the container searching for the entry
  281. // with the lowest ATTRIBUTES.
  282. for (;
  283. iter != end;
  284. ++iter)
  285. {
  286. // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
  287. // the first entry which can be purged. This is the minimum with
  288. // which you will compare the rest of the purgable entries.
  289. if ((*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE ||
  290. (*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE)
  291. {
  292. if (found == 0)
  293. {
  294. min = (*iter).int_id_.second ();
  295. key_to_remove = &(*iter).ext_id_;
  296. value_to_remove = &(*iter).int_id_;
  297. found = 1;
  298. }
  299. else
  300. {
  301. // Ah! an entry with lower ATTTRIBUTES...
  302. if (min > (*iter).int_id_.second ())
  303. {
  304. min = (*iter).int_id_.second ();
  305. key_to_remove = &(*iter).ext_id_;
  306. value_to_remove = &(*iter).int_id_;
  307. }
  308. }
  309. }
  310. }
  311. }
  312. ////////////////////////////////////////////////////////////////////////////////
  313. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  314. ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Handler_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
  315. bool delete_cleanup_strategy)
  316. : cleanup_strategy_ (cleanup_strategy),
  317. delete_cleanup_strategy_ (delete_cleanup_strategy)
  318. {
  319. if (cleanup_strategy == 0)
  320. {
  321. ACE_NEW (this->cleanup_strategy_,
  322. CLEANUP_STRATEGY);
  323. this->delete_cleanup_strategy_ = true;
  324. }
  325. }
  326. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  327. ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Handler_Caching_Utility (void)
  328. {
  329. if (this->delete_cleanup_strategy_)
  330. delete this->cleanup_strategy_;
  331. }
  332. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
  333. ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
  334. double purge_percent)
  335. {
  336. // Check that the purge_percent is non-zero.
  337. if (ACE::is_equal (purge_percent, 0.0))
  338. return 0;
  339. // Get the number of entries in the container.
  340. size_t current_map_size = container.current_size ();
  341. // Also whether the number of entries in the cache is just one!
  342. // Oops! then there is no way out but exiting. So return an error.
  343. if (current_map_size == 0)
  344. return 0;
  345. // Calculate the no of entries to remove from the cache depending
  346. // upon the <purge_percent>.
  347. size_t entries_to_remove
  348. = ACE_MAX (static_cast<size_t> (1),
  349. static_cast<size_t> (static_cast<double> (purge_percent)
  350. / 100 * current_map_size));
  351. KEY *key_to_remove = 0;
  352. VALUE *value_to_remove = 0;
  353. for (size_t i = 0; i < entries_to_remove ; ++i)
  354. {
  355. this->minimum (container,
  356. key_to_remove,
  357. value_to_remove);
  358. if (this->cleanup_strategy_->cleanup (container,
  359. key_to_remove,
  360. value_to_remove) == -1)
  361. return -1;
  362. }
  363. return 0;
  364. }
  365. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
  366. ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
  367. KEY *&key_to_remove,
  368. VALUE *&value_to_remove)
  369. {
  370. // Starting values.
  371. ITERATOR iter = container.begin ();
  372. ITERATOR end = container.end ();
  373. ATTRIBUTES min = (*iter).int_id_->caching_attributes ();
  374. key_to_remove = &(*iter).ext_id_;
  375. value_to_remove = &(*iter).int_id_;
  376. // The iterator moves thru the container searching for the entry
  377. // with the lowest ATTRIBUTES.
  378. for (++iter;
  379. iter != end;
  380. ++iter)
  381. {
  382. if (min > (*iter).int_id_->caching_attributes () &&
  383. (*iter).int_id_->active () != 1)
  384. {
  385. // Ah! an item with lower ATTTRIBUTES...
  386. min = (*iter).int_id_->caching_attributes ();
  387. key_to_remove = &(*iter).ext_id_;
  388. value_to_remove = &(*iter).int_id_;
  389. }
  390. }
  391. }
  392. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  393. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  394. ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Null_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
  395. bool delete_cleanup_strategy)
  396. : cleanup_strategy_ (cleanup_strategy),
  397. delete_cleanup_strategy_ (delete_cleanup_strategy)
  398. {
  399. if (cleanup_strategy == 0)
  400. {
  401. ACE_NEW (this->cleanup_strategy_,
  402. CLEANUP_STRATEGY);
  403. this->delete_cleanup_strategy_ = true;
  404. }
  405. }
  406. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
  407. ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Null_Caching_Utility (void)
  408. {
  409. if (this->delete_cleanup_strategy_)
  410. delete this->cleanup_strategy_;
  411. }
  412. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
  413. ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
  414. double purge_percent)
  415. {
  416. ACE_UNUSED_ARG (container);
  417. ACE_UNUSED_ARG (purge_percent);
  418. return 0;
  419. }
  420. template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
  421. ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
  422. KEY *&key_to_remove,
  423. VALUE *&value_to_remove)
  424. {
  425. ACE_UNUSED_ARG (container);
  426. ACE_UNUSED_ARG (key_to_remove);
  427. ACE_UNUSED_ARG (value_to_remove);
  428. }
  429. ACE_END_VERSIONED_NAMESPACE_DECL
  430. #endif /* ACE_CACHING_UTILITY_T_CPP */