nsDiskCacheBinding.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef _nsDiskCacheBinding_h_
  7. #define _nsDiskCacheBinding_h_
  8. #include "mozilla/MemoryReporting.h"
  9. #include "nspr.h"
  10. #include "PLDHashTable.h"
  11. #include "nsISupports.h"
  12. #include "nsCacheEntry.h"
  13. #include "nsDiskCacheMap.h"
  14. #include "nsDiskCacheStreams.h"
  15. /******************************************************************************
  16. * nsDiskCacheBinding
  17. *
  18. * Created for disk cache specific data and stored in nsCacheEntry.mData as
  19. * an nsISupports. Also stored in nsDiskCacheHashTable, with collisions
  20. * linked by the PRCList.
  21. *
  22. *****************************************************************************/
  23. class nsDiskCacheDeviceDeactivateEntryEvent;
  24. class nsDiskCacheBinding : public nsISupports, public PRCList {
  25. virtual ~nsDiskCacheBinding();
  26. public:
  27. NS_DECL_THREADSAFE_ISUPPORTS
  28. nsDiskCacheBinding(nsCacheEntry* entry, nsDiskCacheRecord * record);
  29. nsresult EnsureStreamIO();
  30. bool IsActive() { return mCacheEntry != nullptr;}
  31. // XXX make friends
  32. public:
  33. nsCacheEntry* mCacheEntry; // back pointer to parent nsCacheEntry
  34. nsDiskCacheRecord mRecord;
  35. nsDiskCacheStreamIO* mStreamIO; // strong reference
  36. bool mDoomed; // record is not stored in cache map
  37. uint8_t mGeneration; // possibly just reservation
  38. // If set, points to a pending event which will deactivate |mCacheEntry|.
  39. // If not set then either |mCacheEntry| is not deactivated, or it has been
  40. // deactivated but the device returned it from FindEntry() before the event
  41. // fired. In both two latter cases this binding is to be considered valid.
  42. nsDiskCacheDeviceDeactivateEntryEvent *mDeactivateEvent;
  43. };
  44. /******************************************************************************
  45. * Utility Functions
  46. *****************************************************************************/
  47. nsDiskCacheBinding * GetCacheEntryBinding(nsCacheEntry * entry);
  48. /******************************************************************************
  49. * nsDiskCacheBindery
  50. *
  51. * Used to keep track of nsDiskCacheBinding associated with active/bound (and
  52. * possibly doomed) entries. Lookups on 4 byte disk hash to find collisions
  53. * (which need to be doomed, instead of just evicted. Collisions are linked
  54. * using a PRCList to keep track of current generation number.
  55. *
  56. * Used to detect hash number collisions, and find available generation numbers.
  57. *
  58. * Not all nsDiskCacheBinding have a generation number.
  59. *
  60. * Generation numbers may be aquired late, or lost (when data fits in block file)
  61. *
  62. * Collisions can occur:
  63. * BindEntry() - hashnumbers collide (possibly different keys)
  64. *
  65. * Generation number required:
  66. * DeactivateEntry() - metadata written to disk, may require file
  67. * GetFileForEntry() - force data to require file
  68. * writing to stream - data size may require file
  69. *
  70. * Binding can be kept in PRCList in order of generation numbers.
  71. * Binding with no generation number can be Appended to PRCList (last).
  72. *
  73. *****************************************************************************/
  74. class nsDiskCacheBindery {
  75. public:
  76. nsDiskCacheBindery();
  77. ~nsDiskCacheBindery();
  78. void Init();
  79. void Reset();
  80. nsDiskCacheBinding * CreateBinding(nsCacheEntry * entry,
  81. nsDiskCacheRecord * record);
  82. nsDiskCacheBinding * FindActiveBinding(uint32_t hashNumber);
  83. void RemoveBinding(nsDiskCacheBinding * binding);
  84. bool ActiveBindings();
  85. size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf);
  86. private:
  87. nsresult AddBinding(nsDiskCacheBinding * binding);
  88. // member variables
  89. static const PLDHashTableOps ops;
  90. PLDHashTable table;
  91. bool initialized;
  92. static const uint32_t kInitialTableLength = 0;
  93. };
  94. #endif /* _nsDiskCacheBinding_h_ */