netwerk/cache/nsDiskCacheBinding.cpp

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "mozilla/MemoryReporting.h"
michael@0 8 #include "nsCache.h"
michael@0 9 #include <limits.h>
michael@0 10
michael@0 11 #include "nscore.h"
michael@0 12 #include "nsDiskCacheBinding.h"
michael@0 13 #include "nsCacheService.h"
michael@0 14
michael@0 15
michael@0 16
michael@0 17 /******************************************************************************
michael@0 18 * static hash table callback functions
michael@0 19 *
michael@0 20 *****************************************************************************/
michael@0 21 struct HashTableEntry : PLDHashEntryHdr {
michael@0 22 nsDiskCacheBinding * mBinding;
michael@0 23 };
michael@0 24
michael@0 25
michael@0 26 static PLDHashNumber
michael@0 27 HashKey( PLDHashTable *table, const void *key)
michael@0 28 {
michael@0 29 return (PLDHashNumber) NS_PTR_TO_INT32(key);
michael@0 30 }
michael@0 31
michael@0 32
michael@0 33 static bool
michael@0 34 MatchEntry(PLDHashTable * /* table */,
michael@0 35 const PLDHashEntryHdr * header,
michael@0 36 const void * key)
michael@0 37 {
michael@0 38 HashTableEntry * hashEntry = (HashTableEntry *) header;
michael@0 39 return (hashEntry->mBinding->mRecord.HashNumber() == (PLDHashNumber) NS_PTR_TO_INT32(key));
michael@0 40 }
michael@0 41
michael@0 42 static void
michael@0 43 MoveEntry(PLDHashTable * /* table */,
michael@0 44 const PLDHashEntryHdr * src,
michael@0 45 PLDHashEntryHdr * dst)
michael@0 46 {
michael@0 47 ((HashTableEntry *)dst)->mBinding = ((HashTableEntry *)src)->mBinding;
michael@0 48 }
michael@0 49
michael@0 50
michael@0 51 static void
michael@0 52 ClearEntry(PLDHashTable * /* table */,
michael@0 53 PLDHashEntryHdr * header)
michael@0 54 {
michael@0 55 ((HashTableEntry *)header)->mBinding = nullptr;
michael@0 56 }
michael@0 57
michael@0 58
michael@0 59 /******************************************************************************
michael@0 60 * Utility Functions
michael@0 61 *****************************************************************************/
michael@0 62 nsDiskCacheBinding *
michael@0 63 GetCacheEntryBinding(nsCacheEntry * entry)
michael@0 64 {
michael@0 65 return (nsDiskCacheBinding *) entry->Data();
michael@0 66 }
michael@0 67
michael@0 68
michael@0 69 /******************************************************************************
michael@0 70 * nsDiskCacheBinding
michael@0 71 *****************************************************************************/
michael@0 72
michael@0 73 NS_IMPL_ISUPPORTS0(nsDiskCacheBinding)
michael@0 74
michael@0 75 nsDiskCacheBinding::nsDiskCacheBinding(nsCacheEntry* entry, nsDiskCacheRecord * record)
michael@0 76 : mCacheEntry(entry)
michael@0 77 , mStreamIO(nullptr)
michael@0 78 , mDeactivateEvent(nullptr)
michael@0 79 {
michael@0 80 NS_ASSERTION(record->ValidRecord(), "bad record");
michael@0 81 PR_INIT_CLIST(this);
michael@0 82 mRecord = *record;
michael@0 83 mDoomed = entry->IsDoomed();
michael@0 84 mGeneration = record->Generation(); // 0 == uninitialized, or data & meta using block files
michael@0 85 }
michael@0 86
michael@0 87 nsDiskCacheBinding::~nsDiskCacheBinding()
michael@0 88 {
michael@0 89 // Grab the cache lock since the binding is stored in nsCacheEntry::mData
michael@0 90 // and it is released using nsCacheService::ReleaseObject_Locked() which
michael@0 91 // releases the object outside the cache lock.
michael@0 92 nsCacheServiceAutoLock lock(LOCK_TELEM(NSDISKCACHEBINDING_DESTRUCTOR));
michael@0 93
michael@0 94 NS_ASSERTION(PR_CLIST_IS_EMPTY(this), "binding deleted while still on list");
michael@0 95 if (!PR_CLIST_IS_EMPTY(this))
michael@0 96 PR_REMOVE_LINK(this); // XXX why are we still on a list?
michael@0 97
michael@0 98 // sever streamIO/binding link
michael@0 99 if (mStreamIO) {
michael@0 100 if (NS_FAILED(mStreamIO->ClearBinding()))
michael@0 101 nsCacheService::DoomEntry(mCacheEntry);
michael@0 102 NS_RELEASE(mStreamIO);
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 nsresult
michael@0 107 nsDiskCacheBinding::EnsureStreamIO()
michael@0 108 {
michael@0 109 if (!mStreamIO) {
michael@0 110 mStreamIO = new nsDiskCacheStreamIO(this);
michael@0 111 if (!mStreamIO) return NS_ERROR_OUT_OF_MEMORY;
michael@0 112 NS_ADDREF(mStreamIO);
michael@0 113 }
michael@0 114 return NS_OK;
michael@0 115 }
michael@0 116
michael@0 117
michael@0 118 /******************************************************************************
michael@0 119 * nsDiskCacheBindery
michael@0 120 *
michael@0 121 * Keeps track of bound disk cache entries to detect for collisions.
michael@0 122 *
michael@0 123 *****************************************************************************/
michael@0 124
michael@0 125 const PLDHashTableOps nsDiskCacheBindery::ops =
michael@0 126 {
michael@0 127 PL_DHashAllocTable,
michael@0 128 PL_DHashFreeTable,
michael@0 129 HashKey,
michael@0 130 MatchEntry,
michael@0 131 MoveEntry,
michael@0 132 ClearEntry,
michael@0 133 PL_DHashFinalizeStub
michael@0 134 };
michael@0 135
michael@0 136
michael@0 137 nsDiskCacheBindery::nsDiskCacheBindery()
michael@0 138 : initialized(false)
michael@0 139 {
michael@0 140 }
michael@0 141
michael@0 142
michael@0 143 nsDiskCacheBindery::~nsDiskCacheBindery()
michael@0 144 {
michael@0 145 Reset();
michael@0 146 }
michael@0 147
michael@0 148
michael@0 149 nsresult
michael@0 150 nsDiskCacheBindery::Init()
michael@0 151 {
michael@0 152 nsresult rv = NS_OK;
michael@0 153 PL_DHashTableInit(&table, &ops, nullptr, sizeof(HashTableEntry), 0);
michael@0 154 initialized = true;
michael@0 155
michael@0 156 return rv;
michael@0 157 }
michael@0 158
michael@0 159 void
michael@0 160 nsDiskCacheBindery::Reset()
michael@0 161 {
michael@0 162 if (initialized) {
michael@0 163 PL_DHashTableFinish(&table);
michael@0 164 initialized = false;
michael@0 165 }
michael@0 166 }
michael@0 167
michael@0 168
michael@0 169 nsDiskCacheBinding *
michael@0 170 nsDiskCacheBindery::CreateBinding(nsCacheEntry * entry,
michael@0 171 nsDiskCacheRecord * record)
michael@0 172 {
michael@0 173 NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
michael@0 174 nsCOMPtr<nsISupports> data = entry->Data();
michael@0 175 if (data) {
michael@0 176 NS_ERROR("cache entry already has bind data");
michael@0 177 return nullptr;
michael@0 178 }
michael@0 179
michael@0 180 nsDiskCacheBinding * binding = new nsDiskCacheBinding(entry, record);
michael@0 181 if (!binding) return nullptr;
michael@0 182
michael@0 183 // give ownership of the binding to the entry
michael@0 184 entry->SetData(binding);
michael@0 185
michael@0 186 // add binding to collision detection system
michael@0 187 nsresult rv = AddBinding(binding);
michael@0 188 if (NS_FAILED(rv)) {
michael@0 189 entry->SetData(nullptr);
michael@0 190 return nullptr;
michael@0 191 }
michael@0 192
michael@0 193 return binding;
michael@0 194 }
michael@0 195
michael@0 196
michael@0 197 /**
michael@0 198 * FindActiveEntry : to find active colliding entry so we can doom it
michael@0 199 */
michael@0 200 nsDiskCacheBinding *
michael@0 201 nsDiskCacheBindery::FindActiveBinding(uint32_t hashNumber)
michael@0 202 {
michael@0 203 NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
michael@0 204 // find hash entry for key
michael@0 205 HashTableEntry * hashEntry;
michael@0 206 hashEntry =
michael@0 207 (HashTableEntry *) PL_DHashTableOperate(&table,
michael@0 208 (void*)(uintptr_t) hashNumber,
michael@0 209 PL_DHASH_LOOKUP);
michael@0 210 if (PL_DHASH_ENTRY_IS_FREE(hashEntry)) return nullptr;
michael@0 211
michael@0 212 // walk list looking for active entry
michael@0 213 NS_ASSERTION(hashEntry->mBinding, "hash entry left with no binding");
michael@0 214 nsDiskCacheBinding * binding = hashEntry->mBinding;
michael@0 215 while (binding->mCacheEntry->IsDoomed()) {
michael@0 216 binding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
michael@0 217 if (binding == hashEntry->mBinding) return nullptr;
michael@0 218 }
michael@0 219 return binding;
michael@0 220 }
michael@0 221
michael@0 222
michael@0 223 /**
michael@0 224 * AddBinding
michael@0 225 *
michael@0 226 * Called from FindEntry() if we read an entry off of disk
michael@0 227 * - it may already have a generation number
michael@0 228 * - a generation number conflict is an error
michael@0 229 *
michael@0 230 * Called from BindEntry()
michael@0 231 * - a generation number needs to be assigned
michael@0 232 */
michael@0 233 nsresult
michael@0 234 nsDiskCacheBindery::AddBinding(nsDiskCacheBinding * binding)
michael@0 235 {
michael@0 236 NS_ENSURE_ARG_POINTER(binding);
michael@0 237 NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
michael@0 238
michael@0 239 // find hash entry for key
michael@0 240 HashTableEntry * hashEntry;
michael@0 241 hashEntry = (HashTableEntry *)
michael@0 242 PL_DHashTableOperate(&table,
michael@0 243 (void *)(uintptr_t) binding->mRecord.HashNumber(),
michael@0 244 PL_DHASH_ADD);
michael@0 245 if (!hashEntry) return NS_ERROR_OUT_OF_MEMORY;
michael@0 246
michael@0 247 if (hashEntry->mBinding == nullptr) {
michael@0 248 hashEntry->mBinding = binding;
michael@0 249 if (binding->mGeneration == 0)
michael@0 250 binding->mGeneration = 1; // if generation uninitialized, set it to 1
michael@0 251
michael@0 252 return NS_OK;
michael@0 253 }
michael@0 254
michael@0 255
michael@0 256 // insert binding in generation order
michael@0 257 nsDiskCacheBinding * p = hashEntry->mBinding;
michael@0 258 bool calcGeneration = (binding->mGeneration == 0); // do we need to calculate generation?
michael@0 259 if (calcGeneration) binding->mGeneration = 1; // initialize to 1 if uninitialized
michael@0 260 while (1) {
michael@0 261
michael@0 262 if (binding->mGeneration < p->mGeneration) {
michael@0 263 // here we are
michael@0 264 PR_INSERT_BEFORE(binding, p);
michael@0 265 if (hashEntry->mBinding == p)
michael@0 266 hashEntry->mBinding = binding;
michael@0 267 break;
michael@0 268 }
michael@0 269
michael@0 270 if (binding->mGeneration == p->mGeneration) {
michael@0 271 if (calcGeneration) ++binding->mGeneration; // try the next generation
michael@0 272 else {
michael@0 273 NS_ERROR("### disk cache: generations collide!");
michael@0 274 return NS_ERROR_UNEXPECTED;
michael@0 275 }
michael@0 276 }
michael@0 277
michael@0 278 p = (nsDiskCacheBinding *)PR_NEXT_LINK(p);
michael@0 279 if (p == hashEntry->mBinding) {
michael@0 280 // end of line: insert here or die
michael@0 281 p = (nsDiskCacheBinding *)PR_PREV_LINK(p); // back up and check generation
michael@0 282 if (p->mGeneration == 255) {
michael@0 283 NS_WARNING("### disk cache: generation capacity at full");
michael@0 284 return NS_ERROR_UNEXPECTED;
michael@0 285 }
michael@0 286 PR_INSERT_BEFORE(binding, hashEntry->mBinding);
michael@0 287 break;
michael@0 288 }
michael@0 289 }
michael@0 290 return NS_OK;
michael@0 291 }
michael@0 292
michael@0 293
michael@0 294 /**
michael@0 295 * RemoveBinding : remove binding from collision detection on deactivation
michael@0 296 */
michael@0 297 void
michael@0 298 nsDiskCacheBindery::RemoveBinding(nsDiskCacheBinding * binding)
michael@0 299 {
michael@0 300 NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
michael@0 301 if (!initialized) return;
michael@0 302
michael@0 303 HashTableEntry * hashEntry;
michael@0 304 void * key = (void *)(uintptr_t)binding->mRecord.HashNumber();
michael@0 305
michael@0 306 hashEntry = (HashTableEntry*) PL_DHashTableOperate(&table,
michael@0 307 (void*)(uintptr_t) key,
michael@0 308 PL_DHASH_LOOKUP);
michael@0 309 if (!PL_DHASH_ENTRY_IS_BUSY(hashEntry)) {
michael@0 310 NS_WARNING("### disk cache: binding not in hashtable!");
michael@0 311 return;
michael@0 312 }
michael@0 313
michael@0 314 if (binding == hashEntry->mBinding) {
michael@0 315 if (PR_CLIST_IS_EMPTY(binding)) {
michael@0 316 // remove this hash entry
michael@0 317 PL_DHashTableOperate(&table,
michael@0 318 (void*)(uintptr_t) binding->mRecord.HashNumber(),
michael@0 319 PL_DHASH_REMOVE);
michael@0 320 return;
michael@0 321
michael@0 322 } else {
michael@0 323 // promote next binding to head, and unlink this binding
michael@0 324 hashEntry->mBinding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
michael@0 325 }
michael@0 326 }
michael@0 327 PR_REMOVE_AND_INIT_LINK(binding);
michael@0 328 }
michael@0 329
michael@0 330
michael@0 331 /**
michael@0 332 * ActiveBinding : PLDHashTable enumerate function to verify active bindings
michael@0 333 */
michael@0 334
michael@0 335 PLDHashOperator
michael@0 336 ActiveBinding(PLDHashTable * table,
michael@0 337 PLDHashEntryHdr * hdr,
michael@0 338 uint32_t number,
michael@0 339 void * arg)
michael@0 340 {
michael@0 341 nsDiskCacheBinding * binding = ((HashTableEntry *)hdr)->mBinding;
michael@0 342 NS_ASSERTION(binding, "### disk cache binding = nullptr!");
michael@0 343
michael@0 344 nsDiskCacheBinding * head = binding;
michael@0 345 do {
michael@0 346 if (binding->IsActive()) {
michael@0 347 *((bool *)arg) = true;
michael@0 348 return PL_DHASH_STOP;
michael@0 349 }
michael@0 350
michael@0 351 binding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
michael@0 352 } while (binding != head);
michael@0 353
michael@0 354 return PL_DHASH_NEXT;
michael@0 355 }
michael@0 356
michael@0 357
michael@0 358 /**
michael@0 359 * ActiveBindings : return true if any bindings have open descriptors
michael@0 360 */
michael@0 361 bool
michael@0 362 nsDiskCacheBindery::ActiveBindings()
michael@0 363 {
michael@0 364 NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
michael@0 365 if (!initialized) return false;
michael@0 366
michael@0 367 bool activeBinding = false;
michael@0 368 PL_DHashTableEnumerate(&table, ActiveBinding, &activeBinding);
michael@0 369
michael@0 370 return activeBinding;
michael@0 371 }
michael@0 372
michael@0 373 struct AccumulatorArg {
michael@0 374 size_t mUsage;
michael@0 375 mozilla::MallocSizeOf mMallocSizeOf;
michael@0 376 };
michael@0 377
michael@0 378 PLDHashOperator
michael@0 379 AccumulateHeapUsage(PLDHashTable *table, PLDHashEntryHdr *hdr, uint32_t number,
michael@0 380 void *arg)
michael@0 381 {
michael@0 382 nsDiskCacheBinding *binding = ((HashTableEntry *)hdr)->mBinding;
michael@0 383 NS_ASSERTION(binding, "### disk cache binding = nsnull!");
michael@0 384
michael@0 385 AccumulatorArg *acc = (AccumulatorArg *)arg;
michael@0 386
michael@0 387 nsDiskCacheBinding *head = binding;
michael@0 388 do {
michael@0 389 acc->mUsage += acc->mMallocSizeOf(binding);
michael@0 390
michael@0 391 if (binding->mStreamIO) {
michael@0 392 acc->mUsage += binding->mStreamIO->SizeOfIncludingThis(acc->mMallocSizeOf);
michael@0 393 }
michael@0 394
michael@0 395 /* No good way to get at mDeactivateEvent internals for proper size, so
michael@0 396 we use this as an estimate. */
michael@0 397 if (binding->mDeactivateEvent) {
michael@0 398 acc->mUsage += acc->mMallocSizeOf(binding->mDeactivateEvent);
michael@0 399 }
michael@0 400
michael@0 401 binding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
michael@0 402 } while (binding != head);
michael@0 403
michael@0 404 return PL_DHASH_NEXT;
michael@0 405 }
michael@0 406
michael@0 407 /**
michael@0 408 * SizeOfExcludingThis: return the amount of heap memory (bytes) being used by the bindery
michael@0 409 */
michael@0 410 size_t
michael@0 411 nsDiskCacheBindery::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf)
michael@0 412 {
michael@0 413 NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
michael@0 414 if (!initialized) return 0;
michael@0 415
michael@0 416 AccumulatorArg arg;
michael@0 417 arg.mUsage = 0;
michael@0 418 arg.mMallocSizeOf = aMallocSizeOf;
michael@0 419
michael@0 420 PL_DHashTableEnumerate(&table, AccumulateHeapUsage, &arg);
michael@0 421
michael@0 422 return arg.mUsage;
michael@0 423 }

mercurial