2001-02-23 16:18:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
|
2012-11-22 05:14:00 +04:00
|
|
|
#include "nsCache.h"
|
2001-02-23 16:18:01 +03:00
|
|
|
#include "nspr.h"
|
|
|
|
#include "nsCacheEntry.h"
|
|
|
|
#include "nsCacheEntryDescriptor.h"
|
|
|
|
#include "nsCacheMetaData.h"
|
|
|
|
#include "nsCacheRequest.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2001-02-23 16:18:01 +03:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsICacheService.h"
|
2001-06-06 06:30:07 +04:00
|
|
|
#include "nsCacheService.h"
|
2001-04-30 05:18:34 +04:00
|
|
|
#include "nsCacheDevice.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "nsHashKeys.h"
|
2013-03-28 21:38:04 +04:00
|
|
|
#include "mozilla/VisualEventTracer.h"
|
2001-03-04 03:11:30 +03:00
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
using namespace mozilla;
|
2001-03-04 03:11:30 +03:00
|
|
|
|
2012-08-08 04:06:06 +04:00
|
|
|
nsCacheEntry::nsCacheEntry(const nsACString & key,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool streamBased,
|
2001-02-28 06:54:16 +03:00
|
|
|
nsCacheStoragePolicy storagePolicy)
|
2001-02-23 16:18:01 +03:00
|
|
|
: mKey(key),
|
2001-03-29 09:54:58 +04:00
|
|
|
mFetchCount(0),
|
|
|
|
mLastFetched(0),
|
2005-09-01 04:20:14 +04:00
|
|
|
mLastModified(0),
|
2010-06-24 10:55:19 +04:00
|
|
|
mExpirationTime(nsICache::NO_EXPIRATION_TIME),
|
2001-02-26 17:42:50 +03:00
|
|
|
mFlags(0),
|
2010-09-08 02:39:28 +04:00
|
|
|
mPredictedDataSize(-1),
|
2001-02-26 17:42:50 +03:00
|
|
|
mDataSize(0),
|
2012-07-30 18:20:58 +04:00
|
|
|
mCacheDevice(nullptr),
|
|
|
|
mCustomDevice(nullptr),
|
|
|
|
mData(nullptr)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-11-07 01:51:58 +03:00
|
|
|
MOZ_COUNT_CTOR(nsCacheEntry);
|
2001-03-05 10:17:58 +03:00
|
|
|
PR_INIT_CLIST(this);
|
2001-02-23 16:18:01 +03:00
|
|
|
PR_INIT_CLIST(&mRequestQ);
|
|
|
|
PR_INIT_CLIST(&mDescriptorQ);
|
|
|
|
|
2001-02-28 06:54:16 +03:00
|
|
|
if (streamBased) MarkStreamBased();
|
2001-03-21 01:42:03 +03:00
|
|
|
SetStoragePolicy(storagePolicy);
|
2012-05-24 19:31:53 +04:00
|
|
|
|
|
|
|
MarkPublic();
|
2013-03-28 21:38:04 +04:00
|
|
|
|
|
|
|
MOZ_EVENT_TRACER_NAME_OBJECT(this, key.BeginReading());
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsCacheEntry::~nsCacheEntry()
|
|
|
|
{
|
2001-11-07 01:51:58 +03:00
|
|
|
MOZ_COUNT_DTOR(nsCacheEntry);
|
2001-05-03 06:35:26 +04:00
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
if (mData)
|
|
|
|
nsCacheService::ReleaseObject_Locked(mData, mThread);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-03-08 08:37:00 +03:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
nsresult
|
|
|
|
nsCacheEntry::Create( const char * key,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool streamBased,
|
2001-05-19 03:24:09 +04:00
|
|
|
nsCacheStoragePolicy storagePolicy,
|
|
|
|
nsCacheDevice * device,
|
|
|
|
nsCacheEntry ** result)
|
|
|
|
{
|
2012-08-08 04:06:06 +04:00
|
|
|
nsCacheEntry* entry = new nsCacheEntry(nsCString(key),
|
|
|
|
streamBased,
|
|
|
|
storagePolicy);
|
2001-05-19 03:24:09 +04:00
|
|
|
entry->SetCacheDevice(device);
|
|
|
|
*result = entry;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-06-06 06:30:07 +04:00
|
|
|
|
2001-03-08 08:37:00 +03:00
|
|
|
void
|
2001-03-29 09:54:58 +04:00
|
|
|
nsCacheEntry::Fetched()
|
2001-03-08 08:37:00 +03:00
|
|
|
{
|
2001-03-29 09:54:58 +04:00
|
|
|
mLastFetched = SecondsFromPRTime(PR_Now());
|
|
|
|
++mFetchCount;
|
2001-03-29 10:50:38 +04:00
|
|
|
MarkEntryDirty();
|
2001-03-07 12:27:36 +03:00
|
|
|
}
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-04-30 05:18:34 +04:00
|
|
|
const char *
|
|
|
|
nsCacheEntry::GetDeviceID()
|
|
|
|
{
|
|
|
|
if (mCacheDevice) return mCacheDevice->GetDeviceID();
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2001-04-30 05:18:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
void
|
|
|
|
nsCacheEntry::TouchData()
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-03-21 01:42:03 +03:00
|
|
|
mLastModified = SecondsFromPRTime(PR_Now());
|
2001-03-29 09:54:58 +04:00
|
|
|
MarkDataDirty();
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
void
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheEntry::SetData(nsISupports * data)
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
2006-06-28 03:13:48 +04:00
|
|
|
if (mData) {
|
|
|
|
nsCacheService::ReleaseObject_Locked(mData, mThread);
|
2012-07-30 18:20:58 +04:00
|
|
|
mData = nullptr;
|
2006-06-28 03:13:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
NS_ADDREF(mData = data);
|
|
|
|
mThread = do_GetCurrentThread();
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
void
|
|
|
|
nsCacheEntry::TouchMetaData()
|
|
|
|
{
|
|
|
|
mLastModified = SecondsFromPRTime(PR_Now());
|
|
|
|
MarkMetaDataDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
/**
|
|
|
|
* cache entry states
|
|
|
|
* 0 descriptors (new entry)
|
|
|
|
* 0 descriptors (existing, bound entry)
|
|
|
|
* n descriptors (existing, bound entry) valid
|
|
|
|
* n descriptors (existing, bound entry) not valid (wait until valid or doomed)
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsresult
|
2001-03-05 10:17:58 +03:00
|
|
|
nsCacheEntry::RequestAccess(nsCacheRequest * request, nsCacheAccessMode *accessGranted)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2010-10-26 02:47:40 +04:00
|
|
|
|
|
|
|
if (IsDoomed()) return NS_ERROR_CACHE_ENTRY_DOOMED;
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
if (!IsInitialized()) {
|
|
|
|
// brand new, unbound entry
|
2001-03-05 10:17:58 +03:00
|
|
|
if (request->IsStreamBased()) MarkStreamBased();
|
2001-02-23 16:18:01 +03:00
|
|
|
MarkInitialized();
|
2001-03-05 10:17:58 +03:00
|
|
|
|
|
|
|
*accessGranted = request->AccessRequested() & nsICache::ACCESS_WRITE;
|
|
|
|
NS_ASSERTION(*accessGranted, "new cache entry for READ-ONLY request");
|
|
|
|
PR_APPEND_LINK(request, &mRequestQ);
|
2001-02-23 16:18:01 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-03-05 10:17:58 +03:00
|
|
|
if (IsStreamData() != request->IsStreamBased()) {
|
2001-02-24 04:02:37 +03:00
|
|
|
*accessGranted = nsICache::ACCESS_NONE;
|
2001-03-05 10:17:58 +03:00
|
|
|
return request->IsStreamBased() ?
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_ERROR_CACHE_DATA_IS_NOT_STREAM : NS_ERROR_CACHE_DATA_IS_STREAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PR_CLIST_IS_EMPTY(&mDescriptorQ)) {
|
2001-03-05 10:17:58 +03:00
|
|
|
// 1st descriptor for existing bound entry
|
|
|
|
*accessGranted = request->AccessRequested();
|
2001-03-07 12:27:36 +03:00
|
|
|
if (*accessGranted & nsICache::ACCESS_WRITE) {
|
|
|
|
MarkInvalid();
|
|
|
|
} else {
|
|
|
|
MarkValid();
|
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
} else {
|
|
|
|
// nth request for existing, bound entry
|
2001-03-05 10:17:58 +03:00
|
|
|
*accessGranted = request->AccessRequested() & ~nsICache::ACCESS_WRITE;
|
2001-02-23 16:18:01 +03:00
|
|
|
if (!IsValid())
|
|
|
|
rv = NS_ERROR_CACHE_WAIT_FOR_VALIDATION;
|
|
|
|
}
|
2001-03-05 10:17:58 +03:00
|
|
|
PR_APPEND_LINK(request,&mRequestQ);
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-05 10:17:58 +03:00
|
|
|
nsresult
|
|
|
|
nsCacheEntry::CreateDescriptor(nsCacheRequest * request,
|
|
|
|
nsCacheAccessMode accessGranted,
|
|
|
|
nsICacheEntryDescriptor ** result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(request && result);
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2001-03-05 10:17:58 +03:00
|
|
|
nsCacheEntryDescriptor * descriptor =
|
|
|
|
new nsCacheEntryDescriptor(this, accessGranted);
|
|
|
|
|
2001-03-08 08:37:00 +03:00
|
|
|
// XXX check request is on q
|
|
|
|
PR_REMOVE_AND_INIT_LINK(request); // remove request regardless of success
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (descriptor == nullptr)
|
2001-03-05 10:17:58 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
PR_APPEND_LINK(descriptor, &mDescriptorQ);
|
|
|
|
|
2010-10-26 02:47:40 +04:00
|
|
|
CACHE_LOG_DEBUG((" descriptor %p created for request %p on entry %p\n",
|
|
|
|
descriptor, request, this));
|
|
|
|
|
2001-03-05 10:17:58 +03:00
|
|
|
NS_ADDREF(*result = descriptor);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-26 17:42:50 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2001-02-26 17:42:50 +03:00
|
|
|
nsCacheEntry::RemoveRequest(nsCacheRequest * request)
|
|
|
|
{
|
2001-03-04 03:11:30 +03:00
|
|
|
// XXX if debug: verify this request belongs to this entry
|
2001-03-05 10:17:58 +03:00
|
|
|
PR_REMOVE_AND_INIT_LINK(request);
|
2001-02-26 17:42:50 +03:00
|
|
|
|
|
|
|
// return true if this entry should stay active
|
|
|
|
return !((PR_CLIST_IS_EMPTY(&mRequestQ)) &&
|
|
|
|
(PR_CLIST_IS_EMPTY(&mDescriptorQ)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-12-14 21:03:01 +04:00
|
|
|
nsCacheEntry::RemoveDescriptor(nsCacheEntryDescriptor * descriptor,
|
|
|
|
bool * doomEntry)
|
2001-02-26 17:42:50 +03:00
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_ASSERTION(descriptor->CacheEntry() == this, "### Wrong cache entry!!");
|
2012-01-26 15:23:45 +04:00
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
*doomEntry = descriptor->ClearCacheEntry();
|
2001-02-26 17:42:50 +03:00
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
PR_REMOVE_AND_INIT_LINK(descriptor);
|
2012-01-26 15:23:45 +04:00
|
|
|
|
2001-02-26 17:42:50 +03:00
|
|
|
if (!PR_CLIST_IS_EMPTY(&mDescriptorQ))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true; // stay active if we still have open descriptors
|
2001-02-26 17:42:50 +03:00
|
|
|
|
|
|
|
if (PR_CLIST_IS_EMPTY(&mRequestQ))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false; // no descriptors or requests, we can deactivate
|
2001-02-26 17:42:50 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true; // find next best request to give a descriptor to
|
2001-02-26 17:42:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-13 18:43:17 +03:00
|
|
|
void
|
2012-12-14 21:03:01 +04:00
|
|
|
nsCacheEntry::DetachDescriptors()
|
2001-03-13 18:43:17 +03:00
|
|
|
{
|
|
|
|
nsCacheEntryDescriptor * descriptor =
|
|
|
|
(nsCacheEntryDescriptor *)PR_LIST_HEAD(&mDescriptorQ);
|
|
|
|
|
|
|
|
while (descriptor != &mDescriptorQ) {
|
2001-03-21 01:42:03 +03:00
|
|
|
nsCacheEntryDescriptor * nextDescriptor =
|
2001-03-13 18:43:17 +03:00
|
|
|
(nsCacheEntryDescriptor *)PR_NEXT_LINK(descriptor);
|
2012-01-26 15:23:45 +04:00
|
|
|
|
2001-03-13 18:43:17 +03:00
|
|
|
descriptor->ClearCacheEntry();
|
|
|
|
PR_REMOVE_AND_INIT_LINK(descriptor);
|
2001-03-21 01:42:03 +03:00
|
|
|
descriptor = nextDescriptor;
|
2001-03-13 18:43:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
void
|
|
|
|
nsCacheEntry::GetDescriptors(
|
|
|
|
nsTArray<nsRefPtr<nsCacheEntryDescriptor> > &outDescriptors)
|
|
|
|
{
|
|
|
|
nsCacheEntryDescriptor * descriptor =
|
|
|
|
(nsCacheEntryDescriptor *)PR_LIST_HEAD(&mDescriptorQ);
|
|
|
|
|
|
|
|
while (descriptor != &mDescriptorQ) {
|
|
|
|
nsCacheEntryDescriptor * nextDescriptor =
|
|
|
|
(nsCacheEntryDescriptor *)PR_NEXT_LINK(descriptor);
|
|
|
|
|
|
|
|
outDescriptors.AppendElement(descriptor);
|
|
|
|
descriptor = nextDescriptor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheEntryInfo - for implementing about:cache
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsCacheEntryInfo, nsICacheEntryInfo)
|
2001-03-21 01:42:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryInfo::GetClientID(char ** clientID)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(clientID);
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
return ClientIDFromCacheKey(*mCacheEntry->Key(), clientID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-30 05:18:34 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryInfo::GetDeviceID(char ** deviceID)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(deviceID);
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
2007-05-15 00:09:20 +04:00
|
|
|
|
|
|
|
*deviceID = NS_strdup(mCacheEntry->GetDeviceID());
|
2001-04-30 05:18:34 +04:00
|
|
|
return *deviceID ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
NS_IMETHODIMP
|
2005-01-13 06:25:28 +03:00
|
|
|
nsCacheEntryInfo::GetKey(nsACString &key)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
return ClientKeyFromCacheKey(*mCacheEntry->Key(), key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryInfo::GetFetchCount(int32_t * fetchCount)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(fetchCount);
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
*fetchCount = mCacheEntry->FetchCount();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryInfo::GetLastFetched(uint32_t * lastFetched)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(lastFetched);
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
*lastFetched = mCacheEntry->LastFetched();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryInfo::GetLastModified(uint32_t * lastModified)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(lastModified);
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
*lastModified = mCacheEntry->LastModified();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryInfo::GetExpirationTime(uint32_t * expirationTime)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(expirationTime);
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
*expirationTime = mCacheEntry->ExpirationTime();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryInfo::GetDataSize(uint32_t * dataSize)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(dataSize);
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
*dataSize = mCacheEntry->DataSize();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsCacheEntryInfo::IsStreamBased(bool * result)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
*result = mCacheEntry->IsStreamData();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-13 18:43:17 +03:00
|
|
|
/******************************************************************************
|
2001-02-23 16:18:01 +03:00
|
|
|
* nsCacheEntryHashTable
|
2001-03-13 18:43:17 +03:00
|
|
|
*****************************************************************************/
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2013-11-20 22:17:01 +04:00
|
|
|
const PLDHashTableOps
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntryHashTable::ops =
|
|
|
|
{
|
|
|
|
HashKey,
|
|
|
|
MatchEntry,
|
|
|
|
MoveEntry,
|
2015-01-14 06:02:35 +03:00
|
|
|
ClearEntry
|
2001-02-23 16:18:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
nsCacheEntryHashTable::nsCacheEntryHashTable()
|
2015-02-11 01:39:49 +03:00
|
|
|
: initialized(false)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-11-07 01:51:58 +03:00
|
|
|
MOZ_COUNT_CTOR(nsCacheEntryHashTable);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsCacheEntryHashTable::~nsCacheEntryHashTable()
|
|
|
|
{
|
2001-11-07 01:51:58 +03:00
|
|
|
MOZ_COUNT_DTOR(nsCacheEntryHashTable);
|
2015-02-11 01:39:49 +03:00
|
|
|
if (initialized)
|
|
|
|
Shutdown();
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-11 01:39:49 +03:00
|
|
|
nsresult
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntryHashTable::Init()
|
|
|
|
{
|
2015-02-11 01:39:49 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
initialized = PL_DHashTableInit(&table, &ops,
|
|
|
|
sizeof(nsCacheEntryHashTableEntry),
|
|
|
|
fallible, 256);
|
|
|
|
|
|
|
|
if (!initialized) rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return rv;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-05-03 06:35:26 +04:00
|
|
|
void
|
|
|
|
nsCacheEntryHashTable::Shutdown()
|
|
|
|
{
|
2015-02-11 01:39:49 +03:00
|
|
|
if (initialized) {
|
2001-05-03 06:35:26 +04:00
|
|
|
PL_DHashTableFinish(&table);
|
2015-02-11 01:39:49 +03:00
|
|
|
initialized = false;
|
2001-05-03 06:35:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
nsCacheEntry *
|
|
|
|
nsCacheEntryHashTable::GetEntry( const nsCString * key)
|
|
|
|
{
|
2015-02-11 01:39:49 +03:00
|
|
|
NS_ASSERTION(initialized, "nsCacheEntryHashTable not initialized");
|
|
|
|
if (!initialized) return nullptr;
|
2015-01-23 08:06:55 +03:00
|
|
|
|
|
|
|
PLDHashEntryHdr *hashEntry = PL_DHashTableSearch(&table, key);
|
|
|
|
return hashEntry ? ((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry
|
|
|
|
: nullptr;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-03-08 08:37:00 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
nsresult
|
|
|
|
nsCacheEntryHashTable::AddEntry( nsCacheEntry *cacheEntry)
|
|
|
|
{
|
|
|
|
PLDHashEntryHdr *hashEntry;
|
|
|
|
|
2015-02-11 01:39:49 +03:00
|
|
|
NS_ASSERTION(initialized, "nsCacheEntryHashTable not initialized");
|
|
|
|
if (!initialized) return NS_ERROR_NOT_INITIALIZED;
|
2001-04-24 06:22:27 +04:00
|
|
|
if (!cacheEntry) return NS_ERROR_NULL_POINTER;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2015-02-03 01:48:58 +03:00
|
|
|
hashEntry = PL_DHashTableAdd(&table, &(cacheEntry->mKey), fallible);
|
2001-03-13 01:41:28 +03:00
|
|
|
#ifndef DEBUG_dougt
|
2001-02-27 08:35:53 +03:00
|
|
|
NS_ASSERTION(((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry == 0,
|
2001-03-13 18:43:17 +03:00
|
|
|
"### nsCacheEntryHashTable::AddEntry - entry already used");
|
2001-03-13 01:41:28 +03:00
|
|
|
#endif
|
2001-02-23 16:18:01 +03:00
|
|
|
((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry = cacheEntry;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-03-08 08:37:00 +03:00
|
|
|
|
|
|
|
void
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntryHashTable::RemoveEntry( nsCacheEntry *cacheEntry)
|
|
|
|
{
|
2015-02-11 01:39:49 +03:00
|
|
|
NS_ASSERTION(initialized, "nsCacheEntryHashTable not initialized");
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ASSERTION(cacheEntry, "### cacheEntry == nullptr");
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2015-02-11 01:39:49 +03:00
|
|
|
if (!initialized) return; // NS_ERROR_NOT_INITIALIZED
|
2001-04-24 06:22:27 +04:00
|
|
|
|
2001-03-13 18:43:17 +03:00
|
|
|
#if DEBUG
|
2001-03-04 03:11:30 +03:00
|
|
|
// XXX debug code to make sure we have the entry we're trying to remove
|
2012-08-08 04:06:06 +04:00
|
|
|
nsCacheEntry *check = GetEntry(&(cacheEntry->mKey));
|
2001-03-13 18:43:17 +03:00
|
|
|
NS_ASSERTION(check == cacheEntry, "### Attempting to remove unknown cache entry!!!");
|
|
|
|
#endif
|
2015-01-06 05:27:28 +03:00
|
|
|
PL_DHashTableRemove(&table, &(cacheEntry->mKey));
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-03-08 08:37:00 +03:00
|
|
|
|
2001-03-10 03:43:20 +03:00
|
|
|
void
|
2008-03-11 11:08:05 +03:00
|
|
|
nsCacheEntryHashTable::VisitEntries( PLDHashEnumerator etor, void *arg)
|
2001-03-10 03:43:20 +03:00
|
|
|
{
|
2015-02-11 01:39:49 +03:00
|
|
|
NS_ASSERTION(initialized, "nsCacheEntryHashTable not initialized");
|
|
|
|
if (!initialized) return; // NS_ERROR_NOT_INITIALIZED
|
2008-03-11 11:08:05 +03:00
|
|
|
PL_DHashTableEnumerate(&table, etor, arg);
|
2001-03-10 03:43:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-08 08:37:00 +03:00
|
|
|
/**
|
2001-02-23 16:18:01 +03:00
|
|
|
* hash table operation callback functions
|
|
|
|
*/
|
2001-02-26 17:42:50 +03:00
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
PLDHashNumber
|
2001-02-24 07:22:19 +03:00
|
|
|
nsCacheEntryHashTable::HashKey( PLDHashTable *table, const void *key)
|
|
|
|
{
|
2012-03-13 02:53:18 +04:00
|
|
|
return HashString(*static_cast<const nsCString *>(key));
|
2001-02-24 07:22:19 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntryHashTable::MatchEntry(PLDHashTable * /* table */,
|
|
|
|
const PLDHashEntryHdr * hashEntry,
|
|
|
|
const void * key)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ASSERTION(key != nullptr, "### nsCacheEntryHashTable::MatchEntry : null key");
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntry *cacheEntry = ((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry;
|
|
|
|
|
2012-08-08 04:06:06 +04:00
|
|
|
return cacheEntry->mKey.Equals(*(nsCString *)key);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
void
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntryHashTable::MoveEntry(PLDHashTable * /* table */,
|
|
|
|
const PLDHashEntryHdr *from,
|
|
|
|
PLDHashEntryHdr *to)
|
|
|
|
{
|
|
|
|
((nsCacheEntryHashTableEntry *)to)->cacheEntry =
|
|
|
|
((nsCacheEntryHashTableEntry *)from)->cacheEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
void
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntryHashTable::ClearEntry(PLDHashTable * /* table */,
|
|
|
|
PLDHashEntryHdr * hashEntry)
|
|
|
|
{
|
|
|
|
((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry = 0;
|
|
|
|
}
|