PageRenderTime 165ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llkeyusetracker.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 215 lines | 152 code | 24 blank | 39 comment | 14 complexity | 61acb6e9d1b5db6c6fe316c152a3bb87 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llkeyusetracker.h
  3. * @brief Declaration of the LLKeyUseTracker class.
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_KEYUSETRACKER_H
  27. #define LL_KEYUSETRACKER_H
  28. // This is a generic cache for an arbitrary data type indexed against an
  29. // arbitrary key type. The cache length is determined by expiration time
  30. // tince against last use and set size. The age of elements and the size
  31. // of the cache are queryable.
  32. //
  33. // This is implemented as a list, which makes search O(n). If you reuse this
  34. // for something with a large dataset, consider reimplementing with a Boost
  35. // multimap based on both a map(key) and priority queue(last_use).
  36. template <class TKey, class TData>
  37. class KeyUseTrackerNodeImpl
  38. {
  39. public:
  40. U64 mLastUse;
  41. U32 mUseCount;
  42. TKey mKey;
  43. TData mData;
  44. KeyUseTrackerNodeImpl( TKey k, TData d ) :
  45. mLastUse(0),
  46. mUseCount(0),
  47. mKey( k ),
  48. mData( d )
  49. {
  50. }
  51. };
  52. template <class TKey, class TData>
  53. class LLKeyUseTracker
  54. {
  55. typedef KeyUseTrackerNodeImpl<TKey,TData> TKeyUseTrackerNode;
  56. typedef std::list<TKeyUseTrackerNode *> TKeyList;
  57. TKeyList mKeyList;
  58. U64 mMemUsecs;
  59. U64 mLastExpire;
  60. U32 mMaxCount;
  61. U32 mCount;
  62. static U64 getTime()
  63. {
  64. // This function operates on a frame basis, not instantaneous.
  65. // We can rely on its output for determining first use in a
  66. // frame.
  67. return LLFrameTimer::getTotalTime();
  68. }
  69. void ageKeys()
  70. {
  71. U64 now = getTime();
  72. if( now == mLastExpire )
  73. {
  74. return;
  75. }
  76. mLastExpire = now;
  77. while( !mKeyList.empty() && (now - mKeyList.front()->mLastUse > mMemUsecs ) )
  78. {
  79. delete mKeyList.front();
  80. mKeyList.pop_front();
  81. mCount--;
  82. }
  83. }
  84. TKeyUseTrackerNode *findNode( TKey key )
  85. {
  86. ageKeys();
  87. typename TKeyList::iterator i;
  88. for( i = mKeyList.begin(); i != mKeyList.end(); i++ )
  89. {
  90. if( (*i)->mKey == key )
  91. return *i;
  92. }
  93. return NULL;
  94. }
  95. TKeyUseTrackerNode *removeNode( TKey key )
  96. {
  97. TKeyUseTrackerNode *i;
  98. i = findNode( key );
  99. if( i )
  100. {
  101. mKeyList.remove( i );
  102. mCount--;
  103. return i;
  104. }
  105. return NULL;
  106. }
  107. public:
  108. LLKeyUseTracker( U32 memory_seconds, U32 max_count ) :
  109. mLastExpire(0),
  110. mMaxCount( max_count ),
  111. mCount(0)
  112. {
  113. mMemUsecs = ((U64)memory_seconds) * 1000000;
  114. }
  115. ~LLKeyUseTracker()
  116. {
  117. // Flush list
  118. while( !mKeyList.empty() )
  119. {
  120. delete mKeyList.front();
  121. mKeyList.pop_front();
  122. mCount--;
  123. }
  124. }
  125. void markUse( TKey key, TData data )
  126. {
  127. TKeyUseTrackerNode *node = removeNode( key );
  128. if( !node )
  129. {
  130. node = new TKeyUseTrackerNode(key, data);
  131. }
  132. else
  133. {
  134. // Update data
  135. node->mData = data;
  136. }
  137. node->mLastUse = getTime();
  138. node->mUseCount++;
  139. mKeyList.push_back( node );
  140. mCount++;
  141. // Too many items? Drop head
  142. if( mCount > mMaxCount )
  143. {
  144. delete mKeyList.front();
  145. mKeyList.pop_front();
  146. mCount--;
  147. }
  148. }
  149. void forgetKey( TKey key )
  150. {
  151. TKeyUseTrackerNode *node = removeNode( key );
  152. if( node )
  153. {
  154. delete node;
  155. }
  156. }
  157. U32 getUseCount( TKey key )
  158. {
  159. TKeyUseTrackerNode *node = findNode( key );
  160. if( node )
  161. {
  162. return node->mUseCount;
  163. }
  164. return 0;
  165. }
  166. U64 getTimeSinceUse( TKey key )
  167. {
  168. TKeyUseTrackerNode *node = findNode( key );
  169. if( node )
  170. {
  171. U64 now = getTime();
  172. U64 delta = now - node->mLastUse;
  173. return (U32)( delta / 1000000 );
  174. }
  175. return 0;
  176. }
  177. TData *getLastUseData( TKey key )
  178. {
  179. TKeyUseTrackerNode *node = findNode( key );
  180. if( node )
  181. {
  182. return &node->mData;
  183. }
  184. return NULL;
  185. }
  186. U32 getKeyCount()
  187. {
  188. return mCount;
  189. }
  190. };
  191. #endif