зеркало из https://github.com/mozilla/gecko-dev.git
Added back in nsOpaqueKey, used in nsMemCache. Went back to 37 as the magic number (prime).
This commit is contained in:
Родитель
4dcd2b675d
Коммит
d43828f23c
|
@ -83,7 +83,7 @@ nsMemCache::GetDescription(PRUnichar * *aDescription)
|
|||
NS_IMETHODIMP
|
||||
nsMemCache::Contains(const char *aKey, PRUint32 aKeyLength, PRBool *aFound)
|
||||
{
|
||||
nsCStringKey *opaqueKey = new nsCStringKey(aKey, aKeyLength);
|
||||
nsOpaqueKey *opaqueKey = new nsOpaqueKey(aKey, aKeyLength);
|
||||
if (!opaqueKey)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
*aFound = mHashTable->Exists(opaqueKey);
|
||||
|
@ -97,11 +97,11 @@ nsMemCache::GetCachedNetData(const char *aKey, PRUint32 aKeyLength,
|
|||
{
|
||||
nsresult rv;
|
||||
nsMemCacheRecord* record = 0;
|
||||
nsCStringKey *opaqueKey2 = 0;
|
||||
nsCStringKey *opaqueKey3 = 0;
|
||||
nsCStringKey *opaqueKey;
|
||||
nsOpaqueKey *opaqueKey2 = 0;
|
||||
nsOpaqueKey *opaqueKey3 = 0;
|
||||
nsOpaqueKey *opaqueKey;
|
||||
|
||||
opaqueKey = new nsCStringKey(aKey, aKeyLength);
|
||||
opaqueKey = new nsOpaqueKey(aKey, aKeyLength);
|
||||
if (!opaqueKey)
|
||||
goto out_of_memory;
|
||||
record = (nsMemCacheRecord*)mHashTable->Get(opaqueKey);
|
||||
|
@ -119,13 +119,13 @@ nsMemCache::GetCachedNetData(const char *aKey, PRUint32 aKeyLength,
|
|||
if (NS_FAILED(rv)) goto out_of_memory;
|
||||
|
||||
// Index the record by opaque key
|
||||
opaqueKey2 = new nsCStringKey(record->mKey, record->mKeyLength);
|
||||
opaqueKey2 = new nsOpaqueKey(record->mKey, record->mKeyLength);
|
||||
if (!opaqueKey2) goto out_of_memory;
|
||||
mHashTable->Put(opaqueKey2, record);
|
||||
|
||||
// Index the record by it's record ID
|
||||
char *recordIDbytes = NS_REINTERPRET_CAST(char *, &record->mRecordID);
|
||||
opaqueKey3 = new nsCStringKey(recordIDbytes,
|
||||
opaqueKey3 = new nsOpaqueKey(recordIDbytes,
|
||||
sizeof record->mRecordID);
|
||||
if (!opaqueKey3) {
|
||||
// Clean up the first record from the hash table
|
||||
|
@ -157,7 +157,7 @@ NS_IMETHODIMP
|
|||
nsMemCache::GetCachedNetDataByID(PRInt32 RecordID,
|
||||
nsINetDataCacheRecord* *aRecord)
|
||||
{
|
||||
nsCStringKey opaqueKey(NS_REINTERPRET_CAST(const char *, &RecordID),
|
||||
nsOpaqueKey opaqueKey(NS_REINTERPRET_CAST(const char *, &RecordID),
|
||||
sizeof RecordID);
|
||||
*aRecord = (nsINetDataCacheRecord*)mHashTable->Get(&opaqueKey);
|
||||
if (*aRecord) {
|
||||
|
@ -173,12 +173,12 @@ nsMemCache::Delete(nsMemCacheRecord* aRecord)
|
|||
nsMemCacheRecord *removedRecord;
|
||||
|
||||
char *recordIDbytes = NS_REINTERPRET_CAST(char *, &aRecord->mRecordID);
|
||||
nsCStringKey opaqueRecordIDKey(recordIDbytes,
|
||||
nsOpaqueKey opaqueRecordIDKey(recordIDbytes,
|
||||
sizeof aRecord->mRecordID);
|
||||
removedRecord = (nsMemCacheRecord*)mHashTable->Remove(&opaqueRecordIDKey);
|
||||
NS_ASSERTION(removedRecord == aRecord, "memory cache database inconsistent");
|
||||
|
||||
nsCStringKey opaqueKey(aRecord->mKey, aRecord->mKeyLength);
|
||||
nsOpaqueKey opaqueKey(aRecord->mKey, aRecord->mKeyLength);
|
||||
removedRecord = (nsMemCacheRecord*)mHashTable->Remove(&opaqueKey);
|
||||
NS_ASSERTION(removedRecord == aRecord, "memory cache database inconsistent");
|
||||
|
||||
|
@ -234,19 +234,19 @@ HashEntryConverter(nsHashKey *aKey, void *aValue,
|
|||
void *unused, nsISupports **retval)
|
||||
{
|
||||
nsMemCacheRecord *record;
|
||||
nsCStringKey *opaqueKey;
|
||||
nsOpaqueKey *opaqueKey;
|
||||
|
||||
record = (nsMemCacheRecord*)aValue;
|
||||
opaqueKey = (nsCStringKey*)aKey;
|
||||
opaqueKey = (nsOpaqueKey*)aKey;
|
||||
|
||||
// Hash table keys that index cache entries by their record ID
|
||||
// shouldn't be enumerated.
|
||||
if ((opaqueKey->GetStringLength() == sizeof(PRInt32))) {
|
||||
if ((opaqueKey->GetBufferLength() == sizeof(PRInt32))) {
|
||||
|
||||
#ifdef DEBUG
|
||||
PRInt32 recordID;
|
||||
record->GetRecordID(&recordID);
|
||||
NS_ASSERTION(*((PRInt32*)opaqueKey->GetString()) == recordID,
|
||||
NS_ASSERTION(*((PRInt32*)opaqueKey->GetBuffer()) == recordID,
|
||||
"Key has incorrect key length");
|
||||
#endif
|
||||
return NS_ERROR_FAILURE;
|
||||
|
|
|
@ -508,8 +508,8 @@ PRUint32 nsCRT::HashCode(const char* str, PRUint32* resultingStrLen)
|
|||
if (str) {
|
||||
char ch;
|
||||
while ((ch = *str++) != 0) {
|
||||
// FYI: hc = hc*33 + ch
|
||||
hc = ((hc << 5) + hc) + ch;
|
||||
// FYI: hc = hc*37 + ch
|
||||
hc = ((hc << 5) + (hc << 2) + hc) + ch;
|
||||
len++;
|
||||
}
|
||||
}
|
||||
|
@ -524,8 +524,8 @@ PRUint32 nsCRT::HashCode(const PRUnichar* str, PRUint32* resultingStrLen)
|
|||
if (str) {
|
||||
PRUnichar ch;
|
||||
while ((ch = *str++) != 0) {
|
||||
// FYI: hc = hc*33 + ch
|
||||
hc = ((hc << 5) + hc) + ch;
|
||||
// FYI: hc = hc*37 + ch
|
||||
hc = ((hc << 5) + (hc << 2) + hc) + ch;
|
||||
len++;
|
||||
}
|
||||
}
|
||||
|
@ -539,8 +539,8 @@ PRUint32 nsCRT::BufferHashCode(const char* buf, PRUint32 len)
|
|||
PRUint32 hc = 0;
|
||||
for (PRUint32 i = 0; i < len; i++) {
|
||||
char ch = *buf++;
|
||||
// FYI: hc = hc*33 + ch
|
||||
hc = ((hc << 5) + hc) + ch;
|
||||
// FYI: hc = hc*37 + ch
|
||||
hc = ((hc << 5) + (hc << 2) + hc) + ch;
|
||||
}
|
||||
return hc;
|
||||
}
|
||||
|
|
|
@ -384,7 +384,6 @@ nsCStringKey::nsCStringKey(const nsCString& str)
|
|||
: mStr((char*)str.GetBuffer()), mStrLen(str.Length()), mOwnership(OWN_CLONE)
|
||||
{
|
||||
NS_ASSERTION(mStr, "null string key");
|
||||
NS_ASSERTION(mStrLen == nsCRT::strlen(mStr), "bad string length");
|
||||
#ifdef DEBUG
|
||||
mKeyType = CStringKey;
|
||||
#endif
|
||||
|
@ -395,8 +394,6 @@ nsCStringKey::nsCStringKey(const char* str, PRInt32 strLen, Ownership own)
|
|||
: mStr((char*)str), mStrLen(strLen), mOwnership(own)
|
||||
{
|
||||
NS_ASSERTION(mStr, "null string key");
|
||||
// if (mStrLen == -1)
|
||||
// mStrLen = nsCRT::strlen(str);
|
||||
#ifdef DEBUG
|
||||
mKeyType = CStringKey;
|
||||
#endif
|
||||
|
@ -453,7 +450,6 @@ nsStringKey::nsStringKey(const nsString& str)
|
|||
: mStr((PRUnichar*)str.GetUnicode()), mStrLen(str.Length()), mOwnership(OWN_CLONE)
|
||||
{
|
||||
NS_ASSERTION(mStr, "null string key");
|
||||
NS_ASSERTION(mStrLen == nsCRT::strlen(mStr), "bad string length");
|
||||
#ifdef DEBUG
|
||||
mKeyType = StringKey;
|
||||
#endif
|
||||
|
@ -511,6 +507,60 @@ nsStringKey::Clone() const
|
|||
return new nsStringKey(str, mStrLen, OWN);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
nsOpaqueKey::nsOpaqueKey(const char* str, PRUint32 strLen, Ownership own)
|
||||
: mBuf((char*)str), mBufLen(strLen), mOwnership(own)
|
||||
{
|
||||
NS_ASSERTION(mBuf, "null buffer");
|
||||
#ifdef DEBUG
|
||||
mKeyType = OpaqueKey;
|
||||
#endif
|
||||
MOZ_COUNT_CTOR(nsOpaqueKey);
|
||||
}
|
||||
|
||||
nsOpaqueKey::~nsOpaqueKey(void)
|
||||
{
|
||||
if (mOwnership == OWN)
|
||||
nsMemory::Free(mBuf);
|
||||
MOZ_COUNT_DTOR(nsOpaqueKey);
|
||||
}
|
||||
|
||||
PRUint32
|
||||
nsOpaqueKey::HashCode(void) const
|
||||
{
|
||||
return nsCRT::BufferHashCode(mBuf, mBufLen);
|
||||
}
|
||||
|
||||
PRBool
|
||||
nsOpaqueKey::Equals(const nsHashKey* aKey) const
|
||||
{
|
||||
NS_ASSERTION(aKey->GetKeyType() == OpaqueKey, "mismatched key types");
|
||||
nsOpaqueKey* other = (nsOpaqueKey*)aKey;
|
||||
if (mBufLen != other->mBufLen)
|
||||
return PR_FALSE;
|
||||
return nsCRT::memcmp(mBuf, other->mBuf, mBufLen) == 0;
|
||||
}
|
||||
|
||||
nsHashKey*
|
||||
nsOpaqueKey::Clone() const
|
||||
{
|
||||
if (mOwnership == NEVER_OWN)
|
||||
return new nsOpaqueKey(mBuf, mBufLen, NEVER_OWN);
|
||||
|
||||
// Since this might hold binary data OR a string, we ensure that the
|
||||
// clone string is zero terminated, but don't assume that the source
|
||||
// string was so terminated.
|
||||
|
||||
PRUint32 len = mBufLen * sizeof(char);
|
||||
char* str = (char*)nsMemory::Alloc(len + sizeof(char));
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
nsCRT::memcpy(str, mBuf, len);
|
||||
str[len] = 0;
|
||||
return new nsOpaqueKey(str, mBufLen, OWN);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// nsObjectHashtable: an nsHashtable where the elements are C++ objects to be
|
||||
// deleted
|
||||
|
|
|
@ -60,7 +60,8 @@ public:
|
|||
VoidKey,
|
||||
IDKey,
|
||||
CStringKey,
|
||||
StringKey
|
||||
StringKey,
|
||||
OpaqueKey
|
||||
};
|
||||
nsHashKeyType GetKeyType() const { return mKeyType; }
|
||||
protected:
|
||||
|
@ -260,11 +261,10 @@ public:
|
|||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// nsStringKey: Where keys are PRUnichar* or char*
|
||||
// Some uses: hashing ProgIDs, filenames, URIs
|
||||
|
||||
#include "nsString.h"
|
||||
|
||||
// for null-terminated c-strings
|
||||
class NS_COM nsCStringKey : public nsHashKey {
|
||||
public:
|
||||
|
||||
|
@ -274,9 +274,6 @@ public:
|
|||
OWN // 'str' to be free'd in key dtor. Clones make their own copy.
|
||||
};
|
||||
|
||||
// If strLen is not passed, and defaults to -1, the assumption here is that str
|
||||
// does not contain embedded nulls, i.e. nsCRT::strlen will be used to determine the
|
||||
// length.
|
||||
nsCStringKey(const char* str, PRInt32 strLen = -1, Ownership own = OWN_CLONE);
|
||||
nsCStringKey(const nsCString& str);
|
||||
~nsCStringKey(void);
|
||||
|
@ -296,6 +293,7 @@ protected:
|
|||
Ownership mOwnership;
|
||||
};
|
||||
|
||||
// for null-terminated unicode strings
|
||||
class NS_COM nsStringKey : public nsHashKey {
|
||||
public:
|
||||
|
||||
|
@ -305,10 +303,7 @@ public:
|
|||
OWN // 'str' to be free'd in key dtor. Clones make their own copy.
|
||||
};
|
||||
|
||||
// If strLen is not passed, and defaults to -1, the assumption here is that str
|
||||
// does not contain embedded nulls, i.e. nsCRT::strlen will be used to determine the
|
||||
// length.
|
||||
nsStringKey(const PRUnichar* str, PRInt32 strLen = -1, Ownership own = OWN_CLONE);
|
||||
nsStringKey(const PRUnichar* str, PRInt32 strLen = -1, Ownership own = OWN_CLONE);
|
||||
nsStringKey(const nsString& str);
|
||||
~nsStringKey(void);
|
||||
|
||||
|
@ -327,6 +322,34 @@ protected:
|
|||
Ownership mOwnership;
|
||||
};
|
||||
|
||||
// for opaque buffers of data which may contain nulls
|
||||
class NS_COM nsOpaqueKey : public nsHashKey {
|
||||
public:
|
||||
|
||||
enum Ownership {
|
||||
NEVER_OWN, // 'buf' is very long lived, even clones don't need to copy it.
|
||||
OWN_CLONE, // 'buf' is as long lived as this key. But clones make a copy.
|
||||
OWN // 'buf' to be free'd in key dtor. Clones make their own copy.
|
||||
};
|
||||
|
||||
nsOpaqueKey(const char* buf, PRUint32 bufLen, Ownership own = OWN_CLONE);
|
||||
~nsOpaqueKey(void);
|
||||
|
||||
PRUint32 HashCode(void) const;
|
||||
PRBool Equals(const nsHashKey* aKey) const;
|
||||
nsHashKey* Clone() const;
|
||||
|
||||
// For when the owner of the hashtable wants to peek at the actual
|
||||
// string in the key. No copy is made, so be careful.
|
||||
const char* GetBuffer() const { return mBuf; }
|
||||
PRUint32 GetBufferLength() const { return mBufLen; }
|
||||
|
||||
protected:
|
||||
char* mBuf;
|
||||
PRUint32 mBufLen;
|
||||
Ownership mOwnership;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#endif
|
||||
|
|
Загрузка…
Ссылка в новой задаче