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"
|
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
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
nsCacheEntry::nsCacheEntry(const nsACString & key,
|
|
|
|
bool streamBased,
|
2001-02-28 06:54:16 +03:00
|
|
|
nsCacheStoragePolicy storagePolicy)
|
2018-04-13 16:01:28 +03:00
|
|
|
: mKey(key),
|
|
|
|
mFetchCount(0),
|
|
|
|
mLastFetched(0),
|
|
|
|
mLastModified(0),
|
2018-06-14 11:19:07 +03:00
|
|
|
mLastValidated(0),
|
2018-04-13 16:01:28 +03:00
|
|
|
mExpirationTime(nsICache::NO_EXPIRATION_TIME),
|
|
|
|
mFlags(0),
|
|
|
|
mPredictedDataSize(-1),
|
|
|
|
mDataSize(0),
|
|
|
|
mCacheDevice(nullptr),
|
|
|
|
mCustomDevice(nullptr),
|
2018-06-14 11:19:07 +03:00
|
|
|
mData(nullptr),
|
|
|
|
mRequestQ{},
|
|
|
|
mDescriptorQ{}
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2018-04-13 16:01:28 +03:00
|
|
|
MOZ_COUNT_CTOR(nsCacheEntry);
|
|
|
|
PR_INIT_CLIST(this);
|
|
|
|
PR_INIT_CLIST(&mRequestQ);
|
|
|
|
PR_INIT_CLIST(&mDescriptorQ);
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
if (streamBased) MarkStreamBased();
|
|
|
|
SetStoragePolicy(storagePolicy);
|
2012-05-24 19:31:53 +04:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
MarkPublic();
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsCacheEntry::~nsCacheEntry()
|
|
|
|
{
|
2001-11-07 01:51:58 +03:00
|
|
|
MOZ_COUNT_DTOR(nsCacheEntry);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
if (mData)
|
2017-06-01 23:43:44 +03:00
|
|
|
nsCacheService::ReleaseObject_Locked(mData, mEventTarget);
|
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) {
|
2017-06-01 23:43:44 +03:00
|
|
|
nsCacheService::ReleaseObject_Locked(mData, mEventTarget);
|
2012-07-30 18:20:58 +04:00
|
|
|
mData = nullptr;
|
2006-06-28 03:13:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
NS_ADDREF(mData = data);
|
2017-06-01 23:43:44 +03:00
|
|
|
mEventTarget = GetCurrentThreadEventTarget();
|
2006-06-28 03:13:48 +04:00
|
|
|
}
|
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(
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<nsCacheEntryDescriptor> > &outDescriptors)
|
2012-12-14 21:03:01 +04:00
|
|
|
{
|
|
|
|
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
|
2017-10-29 14:19:48 +03:00
|
|
|
nsCacheEntryInfo::GetClientID(nsACString& aClientID)
|
2001-03-21 01:42:03 +03:00
|
|
|
{
|
2017-10-29 14:19:48 +03:00
|
|
|
if (!mCacheEntry) {
|
|
|
|
aClientID.Truncate();
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2001-03-21 01:42:03 +03:00
|
|
|
|
2017-10-29 14:19:48 +03:00
|
|
|
return ClientIDFromCacheKey(*mCacheEntry->Key(), aClientID);
|
2001-03-21 01:42:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-30 05:18:34 +04:00
|
|
|
NS_IMETHODIMP
|
2017-10-29 14:19:48 +03:00
|
|
|
nsCacheEntryInfo::GetDeviceID(nsACString& aDeviceID)
|
2001-04-30 05:18:34 +04:00
|
|
|
{
|
2017-10-29 14:19:48 +03:00
|
|
|
if (!mCacheEntry) {
|
|
|
|
aDeviceID.Truncate();
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2017-10-29 14:19:48 +03:00
|
|
|
aDeviceID.Assign(mCacheEntry->GetDeviceID());
|
|
|
|
return NS_OK;
|
2001-04-30 05:18:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
*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-05-19 07:45:30 +03:00
|
|
|
: table(&ops, sizeof(nsCacheEntryHashTableEntry), kInitialTableLength)
|
|
|
|
, 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-05-19 07:45:30 +03:00
|
|
|
void
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntryHashTable::Init()
|
|
|
|
{
|
2015-05-19 07:45:30 +03:00
|
|
|
table.ClearAndPrepareForLength(kInitialTableLength);
|
2015-04-30 02:38:29 +03:00
|
|
|
initialized = true;
|
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) {
|
2015-05-19 07:45:30 +03:00
|
|
|
table.ClearAndPrepareForLength(kInitialTableLength);
|
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 *
|
2018-07-13 13:01:53 +03:00
|
|
|
nsCacheEntryHashTable::GetEntry( const nsCString * key) const
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
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
|
|
|
|
2015-05-21 10:34:25 +03:00
|
|
|
PLDHashEntryHdr *hashEntry = table.Search(key);
|
2015-01-23 08:06:55 +03:00
|
|
|
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-09-15 00:23:12 +03:00
|
|
|
hashEntry = table.Add(&(cacheEntry->mKey), fallible);
|
2016-02-26 11:30:12 +03:00
|
|
|
|
|
|
|
if (!hashEntry)
|
|
|
|
return NS_ERROR_FAILURE;
|
2018-04-30 19:46:04 +03:00
|
|
|
NS_ASSERTION(((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry == nullptr,
|
2001-03-13 18:43:17 +03:00
|
|
|
"### nsCacheEntryHashTable::AddEntry - entry already used");
|
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-09-15 00:23:24 +03:00
|
|
|
table.Remove(&(cacheEntry->mKey));
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2015-06-18 08:31:08 +03:00
|
|
|
PLDHashTable::Iterator
|
2015-07-07 08:02:26 +03:00
|
|
|
nsCacheEntryHashTable::Iter()
|
2001-03-10 03:43:20 +03:00
|
|
|
{
|
2015-06-18 08:31:08 +03:00
|
|
|
return PLDHashTable::Iterator(&table);
|
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
|
2016-03-16 07:33:44 +03:00
|
|
|
nsCacheEntryHashTable::HashKey(const void *key)
|
2001-02-24 07:22:19 +03:00
|
|
|
{
|
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
|
2016-03-16 07:33:44 +03:00
|
|
|
nsCacheEntryHashTable::MatchEntry(const PLDHashEntryHdr * hashEntry,
|
2001-02-23 16:18:01 +03:00
|
|
|
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)
|
|
|
|
{
|
Bug 1415980 - make hash keys movable and not copyable; r=erahm
Everything that goes in a PLDHashtable (and its derivatives, like
nsTHashtable) needs to inherit from PLDHashEntryHdr. But through a lack
of enforcement, copy constructors for these derived classes didn't
explicitly invoke the copy constructor for PLDHashEntryHdr (and the
compiler didn't invoke the copy constructor for us). Instead,
PLDHashTable explicitly copied around the bits that the copy constructor
would have.
The current setup has two problems:
1) Derived classes should be using move construction, not copy
construction, since anything that's shuffling hash table keys/entries
around will be using move construction.
2) Derived classes should take responsibility for transferring bits of
superclass state around, and not rely on something else to handle that.
The second point is not a huge problem for PLDHashTable (PLDHashTable
only has to copy PLDHashEntryHdr's bits in a single place), but future
hash table implementations that might move entries around more
aggressively would have to insert compensation code all over the
place. Additionally, if moving entries is implemented via memcpy (which
is quite common), PLDHashTable copying around bits *again* is
inefficient.
Let's fix all these problems in one go, by:
1) Explicitly declaring the set of constructors that PLDHashEntryHdr
implements (and does not implement). In particular, the copy
constructor is deleted, so any derived classes that attempt to make
themselves copyable will be detected at compile time: the compiler
will complain that the superclass type is not copyable.
This change on its own will result in many compiler errors, so...
2) Change any derived classes to implement move constructors instead of
copy constructors. Note that some of these move constructors are,
strictly speaking, unnecessary, since the relevant classes are moved
via memcpy in nsTHashtable and its derivatives.
2018-09-20 18:20:36 +03:00
|
|
|
new (KnownNotNull, to) nsCacheEntryHashTableEntry(std::move(*((nsCacheEntryHashTableEntry *)from)));
|
|
|
|
// No need to destroy `from`.
|
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::ClearEntry(PLDHashTable * /* table */,
|
|
|
|
PLDHashEntryHdr * hashEntry)
|
|
|
|
{
|
2018-04-30 19:46:04 +03:00
|
|
|
((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry = nullptr;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|