nsDiskCacheBinding.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include "mozilla/MemoryReporting.h"
  7. #include "nsCache.h"
  8. #include <limits.h>
  9. #include "nscore.h"
  10. #include "nsDiskCacheBinding.h"
  11. #include "nsCacheService.h"
  12. using namespace mozilla;
  13. /******************************************************************************
  14. * static hash table callback functions
  15. *
  16. *****************************************************************************/
  17. struct HashTableEntry : PLDHashEntryHdr {
  18. nsDiskCacheBinding * mBinding;
  19. };
  20. static PLDHashNumber
  21. HashKey(const void *key)
  22. {
  23. return (PLDHashNumber) NS_PTR_TO_INT32(key);
  24. }
  25. static bool
  26. MatchEntry(const PLDHashEntryHdr * header,
  27. const void * key)
  28. {
  29. HashTableEntry * hashEntry = (HashTableEntry *) header;
  30. return (hashEntry->mBinding->mRecord.HashNumber() == (PLDHashNumber) NS_PTR_TO_INT32(key));
  31. }
  32. static void
  33. MoveEntry(PLDHashTable * /* table */,
  34. const PLDHashEntryHdr * src,
  35. PLDHashEntryHdr * dst)
  36. {
  37. ((HashTableEntry *)dst)->mBinding = ((HashTableEntry *)src)->mBinding;
  38. }
  39. static void
  40. ClearEntry(PLDHashTable * /* table */,
  41. PLDHashEntryHdr * header)
  42. {
  43. ((HashTableEntry *)header)->mBinding = nullptr;
  44. }
  45. /******************************************************************************
  46. * Utility Functions
  47. *****************************************************************************/
  48. nsDiskCacheBinding *
  49. GetCacheEntryBinding(nsCacheEntry * entry)
  50. {
  51. return (nsDiskCacheBinding *) entry->Data();
  52. }
  53. /******************************************************************************
  54. * nsDiskCacheBinding
  55. *****************************************************************************/
  56. NS_IMPL_ISUPPORTS0(nsDiskCacheBinding)
  57. nsDiskCacheBinding::nsDiskCacheBinding(nsCacheEntry* entry, nsDiskCacheRecord * record)
  58. : mCacheEntry(entry)
  59. , mStreamIO(nullptr)
  60. , mDeactivateEvent(nullptr)
  61. {
  62. NS_ASSERTION(record->ValidRecord(), "bad record");
  63. PR_INIT_CLIST(this);
  64. mRecord = *record;
  65. mDoomed = entry->IsDoomed();
  66. mGeneration = record->Generation(); // 0 == uninitialized, or data & meta using block files
  67. }
  68. nsDiskCacheBinding::~nsDiskCacheBinding()
  69. {
  70. // Grab the cache lock since the binding is stored in nsCacheEntry::mData
  71. // and it is released using nsCacheService::ReleaseObject_Locked() which
  72. // releases the object outside the cache lock.
  73. nsCacheServiceAutoLock lock;
  74. NS_ASSERTION(PR_CLIST_IS_EMPTY(this), "binding deleted while still on list");
  75. if (!PR_CLIST_IS_EMPTY(this))
  76. PR_REMOVE_LINK(this); // XXX why are we still on a list?
  77. // sever streamIO/binding link
  78. if (mStreamIO) {
  79. if (NS_FAILED(mStreamIO->ClearBinding()))
  80. nsCacheService::DoomEntry(mCacheEntry);
  81. NS_RELEASE(mStreamIO);
  82. }
  83. }
  84. nsresult
  85. nsDiskCacheBinding::EnsureStreamIO()
  86. {
  87. if (!mStreamIO) {
  88. mStreamIO = new nsDiskCacheStreamIO(this);
  89. if (!mStreamIO) return NS_ERROR_OUT_OF_MEMORY;
  90. NS_ADDREF(mStreamIO);
  91. }
  92. return NS_OK;
  93. }
  94. /******************************************************************************
  95. * nsDiskCacheBindery
  96. *
  97. * Keeps track of bound disk cache entries to detect for collisions.
  98. *
  99. *****************************************************************************/
  100. const PLDHashTableOps nsDiskCacheBindery::ops =
  101. {
  102. HashKey,
  103. MatchEntry,
  104. MoveEntry,
  105. ClearEntry
  106. };
  107. nsDiskCacheBindery::nsDiskCacheBindery()
  108. : table(&ops, sizeof(HashTableEntry), kInitialTableLength)
  109. , initialized(false)
  110. {
  111. }
  112. nsDiskCacheBindery::~nsDiskCacheBindery()
  113. {
  114. Reset();
  115. }
  116. void
  117. nsDiskCacheBindery::Init()
  118. {
  119. table.ClearAndPrepareForLength(kInitialTableLength);
  120. initialized = true;
  121. }
  122. void
  123. nsDiskCacheBindery::Reset()
  124. {
  125. if (initialized) {
  126. table.ClearAndPrepareForLength(kInitialTableLength);
  127. initialized = false;
  128. }
  129. }
  130. nsDiskCacheBinding *
  131. nsDiskCacheBindery::CreateBinding(nsCacheEntry * entry,
  132. nsDiskCacheRecord * record)
  133. {
  134. NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
  135. nsCOMPtr<nsISupports> data = entry->Data();
  136. if (data) {
  137. NS_ERROR("cache entry already has bind data");
  138. return nullptr;
  139. }
  140. nsDiskCacheBinding * binding = new nsDiskCacheBinding(entry, record);
  141. if (!binding) return nullptr;
  142. // give ownership of the binding to the entry
  143. entry->SetData(binding);
  144. // add binding to collision detection system
  145. nsresult rv = AddBinding(binding);
  146. if (NS_FAILED(rv)) {
  147. entry->SetData(nullptr);
  148. return nullptr;
  149. }
  150. return binding;
  151. }
  152. /**
  153. * FindActiveEntry : to find active colliding entry so we can doom it
  154. */
  155. nsDiskCacheBinding *
  156. nsDiskCacheBindery::FindActiveBinding(uint32_t hashNumber)
  157. {
  158. NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
  159. // find hash entry for key
  160. auto hashEntry = static_cast<HashTableEntry*>
  161. (table.Search((void*)(uintptr_t)hashNumber));
  162. if (!hashEntry) return nullptr;
  163. // walk list looking for active entry
  164. NS_ASSERTION(hashEntry->mBinding, "hash entry left with no binding");
  165. nsDiskCacheBinding * binding = hashEntry->mBinding;
  166. while (binding->mCacheEntry->IsDoomed()) {
  167. binding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
  168. if (binding == hashEntry->mBinding) return nullptr;
  169. }
  170. return binding;
  171. }
  172. /**
  173. * AddBinding
  174. *
  175. * Called from FindEntry() if we read an entry off of disk
  176. * - it may already have a generation number
  177. * - a generation number conflict is an error
  178. *
  179. * Called from BindEntry()
  180. * - a generation number needs to be assigned
  181. */
  182. nsresult
  183. nsDiskCacheBindery::AddBinding(nsDiskCacheBinding * binding)
  184. {
  185. NS_ENSURE_ARG_POINTER(binding);
  186. NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
  187. // find hash entry for key
  188. auto hashEntry = static_cast<HashTableEntry*>
  189. (table.Add((void*)(uintptr_t)binding->mRecord.HashNumber(), fallible));
  190. if (!hashEntry)
  191. return NS_ERROR_OUT_OF_MEMORY;
  192. if (hashEntry->mBinding == nullptr) {
  193. hashEntry->mBinding = binding;
  194. if (binding->mGeneration == 0)
  195. binding->mGeneration = 1; // if generation uninitialized, set it to 1
  196. return NS_OK;
  197. }
  198. // insert binding in generation order
  199. nsDiskCacheBinding * p = hashEntry->mBinding;
  200. bool calcGeneration = (binding->mGeneration == 0); // do we need to calculate generation?
  201. if (calcGeneration) binding->mGeneration = 1; // initialize to 1 if uninitialized
  202. while (1) {
  203. if (binding->mGeneration < p->mGeneration) {
  204. // here we are
  205. PR_INSERT_BEFORE(binding, p);
  206. if (hashEntry->mBinding == p)
  207. hashEntry->mBinding = binding;
  208. break;
  209. }
  210. if (binding->mGeneration == p->mGeneration) {
  211. if (calcGeneration) ++binding->mGeneration; // try the next generation
  212. else {
  213. NS_ERROR("### disk cache: generations collide!");
  214. return NS_ERROR_UNEXPECTED;
  215. }
  216. }
  217. p = (nsDiskCacheBinding *)PR_NEXT_LINK(p);
  218. if (p == hashEntry->mBinding) {
  219. // end of line: insert here or die
  220. p = (nsDiskCacheBinding *)PR_PREV_LINK(p); // back up and check generation
  221. if (p->mGeneration == 255) {
  222. NS_WARNING("### disk cache: generation capacity at full");
  223. return NS_ERROR_UNEXPECTED;
  224. }
  225. PR_INSERT_BEFORE(binding, hashEntry->mBinding);
  226. break;
  227. }
  228. }
  229. return NS_OK;
  230. }
  231. /**
  232. * RemoveBinding : remove binding from collision detection on deactivation
  233. */
  234. void
  235. nsDiskCacheBindery::RemoveBinding(nsDiskCacheBinding * binding)
  236. {
  237. NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
  238. if (!initialized) return;
  239. void* key = (void *)(uintptr_t)binding->mRecord.HashNumber();
  240. auto hashEntry =
  241. static_cast<HashTableEntry*>(table.Search((void*)(uintptr_t) key));
  242. if (!hashEntry) {
  243. NS_WARNING("### disk cache: binding not in hashtable!");
  244. return;
  245. }
  246. if (binding == hashEntry->mBinding) {
  247. if (PR_CLIST_IS_EMPTY(binding)) {
  248. // remove this hash entry
  249. table.Remove((void*)(uintptr_t) binding->mRecord.HashNumber());
  250. return;
  251. } else {
  252. // promote next binding to head, and unlink this binding
  253. hashEntry->mBinding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
  254. }
  255. }
  256. PR_REMOVE_AND_INIT_LINK(binding);
  257. }
  258. /**
  259. * ActiveBindings: return true if any bindings have open descriptors.
  260. */
  261. bool
  262. nsDiskCacheBindery::ActiveBindings()
  263. {
  264. NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
  265. if (!initialized) return false;
  266. for (auto iter = table.Iter(); !iter.Done(); iter.Next()) {
  267. auto entry = static_cast<HashTableEntry*>(iter.Get());
  268. nsDiskCacheBinding* binding = entry->mBinding;
  269. nsDiskCacheBinding* head = binding;
  270. do {
  271. if (binding->IsActive()) {
  272. return true;
  273. }
  274. binding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
  275. } while (binding != head);
  276. }
  277. return false;
  278. }
  279. /**
  280. * SizeOfExcludingThis: return the amount of heap memory (bytes) being used by
  281. * the bindery.
  282. */
  283. size_t
  284. nsDiskCacheBindery::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf)
  285. {
  286. NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
  287. if (!initialized) return 0;
  288. size_t size = 0;
  289. for (auto iter = table.Iter(); !iter.Done(); iter.Next()) {
  290. auto entry = static_cast<HashTableEntry*>(iter.Get());
  291. nsDiskCacheBinding* binding = entry->mBinding;
  292. nsDiskCacheBinding* head = binding;
  293. do {
  294. size += aMallocSizeOf(binding);
  295. if (binding->mStreamIO) {
  296. size += binding->mStreamIO->SizeOfIncludingThis(aMallocSizeOf);
  297. }
  298. // No good way to get at mDeactivateEvent internals for proper
  299. // size, so we use this as an estimate.
  300. if (binding->mDeactivateEvent) {
  301. size += aMallocSizeOf(binding->mDeactivateEvent);
  302. }
  303. binding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
  304. } while (binding != head);
  305. }
  306. return size;
  307. }