/indra/newview/llvocache.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 156 lines · 103 code · 22 blank · 31 comment · 2 complexity · 38ec140237e525d4bcbedbb5ea32ef92 MD5 · raw file

  1. /**
  2. * @file llvocache.h
  3. * @brief Cache of objects on the viewer.
  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_LLVOCACHE_H
  27. #define LL_LLVOCACHE_H
  28. #include "lluuid.h"
  29. #include "lldatapacker.h"
  30. #include "lldlinked.h"
  31. #include "lldir.h"
  32. //---------------------------------------------------------------------------
  33. // Cache entries
  34. class LLVOCacheEntry;
  35. class LLVOCacheEntry
  36. {
  37. public:
  38. LLVOCacheEntry(U32 local_id, U32 crc, LLDataPackerBinaryBuffer &dp);
  39. LLVOCacheEntry(LLAPRFile* apr_file);
  40. LLVOCacheEntry();
  41. ~LLVOCacheEntry();
  42. U32 getLocalID() const { return mLocalID; }
  43. U32 getCRC() const { return mCRC; }
  44. S32 getHitCount() const { return mHitCount; }
  45. S32 getCRCChangeCount() const { return mCRCChangeCount; }
  46. void dump() const;
  47. BOOL writeToFile(LLAPRFile* apr_file) const;
  48. void assignCRC(U32 crc, LLDataPackerBinaryBuffer &dp);
  49. LLDataPackerBinaryBuffer *getDP(U32 crc);
  50. void recordHit();
  51. void recordDupe() { mDupeCount++; }
  52. public:
  53. typedef std::map<U32, LLVOCacheEntry*> vocache_entry_map_t;
  54. protected:
  55. U32 mLocalID;
  56. U32 mCRC;
  57. S32 mHitCount;
  58. S32 mDupeCount;
  59. S32 mCRCChangeCount;
  60. LLDataPackerBinaryBuffer mDP;
  61. U8 *mBuffer;
  62. };
  63. //
  64. //Note: LLVOCache is not thread-safe
  65. //
  66. class LLVOCache
  67. {
  68. private:
  69. struct HeaderEntryInfo
  70. {
  71. HeaderEntryInfo() : mIndex(0), mHandle(0), mTime(0) {}
  72. S32 mIndex;
  73. U64 mHandle ;
  74. U32 mTime ;
  75. };
  76. struct HeaderMetaInfo
  77. {
  78. HeaderMetaInfo() : mVersion(0){}
  79. U32 mVersion;
  80. };
  81. struct header_entry_less
  82. {
  83. bool operator()(const HeaderEntryInfo* lhs, const HeaderEntryInfo* rhs) const
  84. {
  85. if(lhs->mTime == rhs->mTime)
  86. {
  87. return lhs < rhs ;
  88. }
  89. return lhs->mTime < rhs->mTime ; // older entry in front of queue (set)
  90. }
  91. };
  92. typedef std::set<HeaderEntryInfo*, header_entry_less> header_entry_queue_t;
  93. typedef std::map<U64, HeaderEntryInfo*> handle_entry_map_t;
  94. private:
  95. LLVOCache() ;
  96. public:
  97. ~LLVOCache() ;
  98. void initCache(ELLPath location, U32 size, U32 cache_version) ;
  99. void removeCache(ELLPath location) ;
  100. void readFromCache(U64 handle, const LLUUID& id, LLVOCacheEntry::vocache_entry_map_t& cache_entry_map) ;
  101. void writeToCache(U64 handle, const LLUUID& id, const LLVOCacheEntry::vocache_entry_map_t& cache_entry_map, BOOL dirty_cache) ;
  102. void removeEntry(U64 handle) ;
  103. void setReadOnly(BOOL read_only) {mReadOnly = read_only;}
  104. private:
  105. void setDirNames(ELLPath location);
  106. // determine the cache filename for the region from the region handle
  107. void getObjectCacheFilename(U64 handle, std::string& filename);
  108. void removeFromCache(HeaderEntryInfo* entry);
  109. void readCacheHeader();
  110. void writeCacheHeader();
  111. void clearCacheInMemory();
  112. void removeCache() ;
  113. void removeEntry(HeaderEntryInfo* entry) ;
  114. void purgeEntries(U32 size);
  115. BOOL updateEntry(const HeaderEntryInfo* entry);
  116. private:
  117. BOOL mEnabled;
  118. BOOL mInitialized ;
  119. BOOL mReadOnly ;
  120. HeaderMetaInfo mMetaInfo;
  121. U32 mCacheSize;
  122. U32 mNumEntries;
  123. std::string mHeaderFileName ;
  124. std::string mObjectCacheDirName;
  125. LLVolatileAPRPool* mLocalAPRFilePoolp ;
  126. header_entry_queue_t mHeaderEntryQueue;
  127. handle_entry_map_t mHandleEntryMap;
  128. static LLVOCache* sInstance ;
  129. public:
  130. static LLVOCache* getInstance() ;
  131. static BOOL hasInstance() ;
  132. static void destroyClass() ;
  133. };
  134. #endif