Back to index

lightning-sunbird  0.9+nobinonly
nsMemoryCacheDevice.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is nsMemoryCacheDevice.h, released
00017  * February 20, 2001.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2001
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Gordon Sheridan, 20-February-2001
00026  *   Brian Ryner <bryner@brianryner.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #ifndef _nsMemoryCacheDevice_h_
00043 #define _nsMemoryCacheDevice_h_
00044 
00045 #include "nsCacheDevice.h"
00046 #include "pldhash.h"
00047 #include "nsCacheEntry.h"
00048 
00049 
00050 class nsMemoryCacheDeviceInfo;
00051 
00052 /******************************************************************************
00053  * nsMemoryCacheDevice
00054  ******************************************************************************/
00055 class nsMemoryCacheDevice : public nsCacheDevice
00056 {
00057 public:
00058     nsMemoryCacheDevice();
00059     virtual ~nsMemoryCacheDevice();
00060 
00061     virtual nsresult        Init();
00062     virtual nsresult        Shutdown();
00063 
00064     virtual const char *    GetDeviceID(void);
00065 
00066     virtual nsresult        BindEntry( nsCacheEntry * entry );
00067     virtual nsCacheEntry *  FindEntry( nsCString * key, PRBool *collision );
00068     virtual void            DoomEntry( nsCacheEntry * entry );
00069     virtual nsresult        DeactivateEntry( nsCacheEntry * entry );
00070 
00071     virtual nsresult OpenInputStreamForEntry(nsCacheEntry *     entry,
00072                                              nsCacheAccessMode  mode,
00073                                              PRUint32           offset,
00074                                              nsIInputStream **  result);
00075 
00076     virtual nsresult OpenOutputStreamForEntry(nsCacheEntry *     entry,
00077                                               nsCacheAccessMode  mode,
00078                                               PRUint32           offset,
00079                                               nsIOutputStream ** result);
00080 
00081     virtual nsresult GetFileForEntry( nsCacheEntry *    entry,
00082                                       nsIFile **        result );
00083 
00084     virtual nsresult OnDataSizeChange( nsCacheEntry * entry, PRInt32 deltaSize );
00085 
00086     virtual nsresult Visit( nsICacheVisitor * visitor );
00087 
00088     virtual nsresult EvictEntries(const char * clientID);
00089     
00090     void             SetCapacity(PRInt32  capacity);
00091  
00092 private:
00093     friend class nsMemoryCacheDeviceInfo;
00094     enum      { DELETE_ENTRY        = PR_TRUE,
00095                 DO_NOT_DELETE_ENTRY = PR_FALSE };
00096 
00097     void      AdjustMemoryLimits( PRInt32  softLimit, PRInt32  hardLimit);
00098     void      EvictEntry( nsCacheEntry * entry , PRBool deleteEntry);
00099     void      EvictEntriesIfNecessary();
00100     int       EvictionList(nsCacheEntry * entry, PRInt32  deltaSize);
00101 
00102 #ifdef DEBUG
00103     void      CheckEntryCount();
00104 #endif
00105     /*
00106      *  Data members
00107      */
00108     enum {
00109         kQueueCount = 24   // entries > 2^23 (8Mb) start in last queue
00110     };
00111 
00112     nsCacheEntryHashTable  mMemCacheEntries;
00113     PRBool                 mInitialized;
00114 
00115     PRCList                mEvictionList[kQueueCount];
00116     PRInt32                mEvictionThreshold;
00117 
00118     PRInt32                mHardLimit;
00119     PRInt32                mSoftLimit;
00120 
00121     PRInt32                mTotalSize;
00122     PRInt32                mInactiveSize;
00123 
00124     PRInt32                mEntryCount;
00125     PRInt32                mMaxEntryCount;
00126 
00127     // XXX what other stats do we want to keep?
00128 };
00129 
00130 
00131 /******************************************************************************
00132  * nsMemoryCacheDeviceInfo - used to call nsIVisitor for about:cache
00133  ******************************************************************************/
00134 class nsMemoryCacheDeviceInfo : public nsICacheDeviceInfo {
00135 public:
00136     NS_DECL_ISUPPORTS
00137     NS_DECL_NSICACHEDEVICEINFO
00138 
00139     nsMemoryCacheDeviceInfo(nsMemoryCacheDevice* device)
00140         :   mDevice(device)
00141     {
00142     }
00143 
00144     virtual ~nsMemoryCacheDeviceInfo() {}
00145     
00146 private:
00147     nsMemoryCacheDevice* mDevice;
00148 };
00149 
00150 
00151 #endif // _nsMemoryCacheDevice_h_