diff --git a/mozilla/netwerk/cache/src/nsDiskCacheMap.cpp b/mozilla/netwerk/cache/src/nsDiskCacheMap.cpp new file mode 100644 index 00000000000..7b4c4eaea2b --- /dev/null +++ b/mozilla/netwerk/cache/src/nsDiskCacheMap.cpp @@ -0,0 +1,106 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is nsDiskCacheMap.cpp, released March 23, 2001. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are + * Copyright (C) 2001 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Patrick C. Beard + */ + +#include "nsDiskCacheMap.h" +#include "nsIInputStream.h" +#include "nsIOutputStream.h" + +nsDiskCacheMap::nsDiskCacheMap() +{ +} + +nsDiskCacheMap::~nsDiskCacheMap() +{ +} + +nsDiskCacheRecord* nsDiskCacheMap::GetRecord(PRUint32 hashNumber) +{ + PRUint32 index = (hashNumber & (kBucketsPerTable - 1)); + nsDiskCacheBucket& bucket = mBuckets[index]; + nsDiskCacheRecord* oldestRecord = &bucket.mRecords[0]; + + for (int i = 0; i < kRecordsPerBucket; ++i) { + nsDiskCacheRecord* record = &bucket.mRecords[i]; + if (record->HashNumber() == 0 || record->HashNumber() == hashNumber) + return record; + if (record->EvictionRank() > oldestRecord->EvictionRank()) + oldestRecord = record; + } + + // record not found, so must evict a record. + oldestRecord->SetHashNumber(0); + oldestRecord->SetEvictionRank(0); + return oldestRecord; +} + +nsresult nsDiskCacheMap::Read(nsIInputStream* input) +{ + // XXX need a header, etc. + PRUint32 count; + nsresult rv = input->Read((char*)&mBuckets, sizeof(mBuckets), &count); + if (NS_FAILED(rv)) return rv; + + // unswap all of the active records. + for (int b = 0; b < kBucketsPerTable; ++b) { + nsDiskCacheBucket& bucket = mBuckets[b]; + for (int r = 0; r < kRecordsPerBucket; ++r) { + nsDiskCacheRecord* record = &bucket.mRecords[r]; + if (record->HashNumber() == 0) + break; + record->Unswap(); + } + } + + return NS_OK; +} + +nsresult nsDiskCacheMap::Write(nsIOutputStream* output) +{ + // swap all of the active records. + for (int b = 0; b < kBucketsPerTable; ++b) { + nsDiskCacheBucket& bucket = mBuckets[b]; + for (int r = 0; r < kRecordsPerBucket; ++r) { + nsDiskCacheRecord* record = &bucket.mRecords[r]; + if (record->HashNumber() == 0) + break; + record->Swap(); + } + } + + // XXX need a header, etc. + PRUint32 count; + nsresult rv = output->Write((char*)&mBuckets, sizeof(mBuckets), &count); + + // unswap all of the active records. + for (int b = 0; b < kBucketsPerTable; ++b) { + nsDiskCacheBucket& bucket = mBuckets[b]; + for (int r = 0; r < kRecordsPerBucket; ++r) { + nsDiskCacheRecord* record = &bucket.mRecords[r]; + if (record->HashNumber() == 0) + break; + record->Unswap(); + } + } + + return rv; +} diff --git a/mozilla/netwerk/cache/src/nsDiskCacheMap.h b/mozilla/netwerk/cache/src/nsDiskCacheMap.h new file mode 100644 index 00000000000..74e36d336b9 --- /dev/null +++ b/mozilla/netwerk/cache/src/nsDiskCacheMap.h @@ -0,0 +1,54 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is nsDiskCacheMap.h, released March 23, 2001. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are + * Copyright (C) 2001 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Patrick C. Beard + */ + +#ifndef _nsDiskCacheMap_h_ + +#include "nsDiskCache.h" +#include "nsError.h" + +class nsIInputStream; +class nsIOutputStream; + +// XXX initial capacity, enough for 8192 distint entries. +const PRUint32 kRecordsPerBucket = 256; +const PRUint32 kBucketsPerTable = (1 << 4); // must be a power of 2! + +class nsDiskCacheMap { +public: + nsDiskCacheMap(); + ~nsDiskCacheMap(); + + nsDiskCacheRecord* GetRecord(PRUint32 hashNumber); + + nsresult Read(nsIInputStream* input); + nsresult Write(nsIOutputStream* output); + +private: + // XXX need a bitmap? + struct nsDiskCacheBucket { + nsDiskCacheRecord mRecords[kRecordsPerBucket]; + }; + nsDiskCacheBucket mBuckets[kBucketsPerTable]; +}; + +#endif // _nsDiskCacheMap_h_