1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is nsMemoryCacheDevice.h, released
17 : * February 20, 2001.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 2001
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Gordon Sheridan, 20-February-2001
26 : * Brian Ryner <bryner@brianryner.com>
27 : *
28 : * Alternatively, the contents of this file may be used under the terms of
29 : * either the GNU General Public License Version 2 or later (the "GPL"), or
30 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 : * in which case the provisions of the GPL or the LGPL are applicable instead
32 : * of those above. If you wish to allow use of your version of this file only
33 : * under the terms of either the GPL or the LGPL, and not to allow others to
34 : * use your version of this file under the terms of the MPL, indicate your
35 : * decision by deleting the provisions above and replace them with the notice
36 : * and other provisions required by the GPL or the LGPL. If you do not delete
37 : * the provisions above, a recipient may use your version of this file under
38 : * the terms of any one of the MPL, the GPL or the LGPL.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 : #ifndef _nsMemoryCacheDevice_h_
43 : #define _nsMemoryCacheDevice_h_
44 :
45 : #include "nsCacheDevice.h"
46 : #include "pldhash.h"
47 : #include "nsCacheEntry.h"
48 :
49 :
50 : class nsMemoryCacheDeviceInfo;
51 :
52 : /******************************************************************************
53 : * nsMemoryCacheDevice
54 : ******************************************************************************/
55 : class nsMemoryCacheDevice : public nsCacheDevice
56 : {
57 : public:
58 : nsMemoryCacheDevice();
59 : virtual ~nsMemoryCacheDevice();
60 :
61 : virtual nsresult Init();
62 : virtual nsresult Shutdown();
63 :
64 : virtual const char * GetDeviceID(void);
65 :
66 : virtual nsresult BindEntry( nsCacheEntry * entry );
67 : virtual nsCacheEntry * FindEntry( nsCString * key, bool *collision );
68 : virtual void DoomEntry( nsCacheEntry * entry );
69 : virtual nsresult DeactivateEntry( nsCacheEntry * entry );
70 :
71 : virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry,
72 : nsCacheAccessMode mode,
73 : PRUint32 offset,
74 : nsIInputStream ** result);
75 :
76 : virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry,
77 : nsCacheAccessMode mode,
78 : PRUint32 offset,
79 : nsIOutputStream ** result);
80 :
81 : virtual nsresult GetFileForEntry( nsCacheEntry * entry,
82 : nsIFile ** result );
83 :
84 : virtual nsresult OnDataSizeChange( nsCacheEntry * entry, PRInt32 deltaSize );
85 :
86 : virtual nsresult Visit( nsICacheVisitor * visitor );
87 :
88 : virtual nsresult EvictEntries(const char * clientID);
89 :
90 : void SetCapacity(PRInt32 capacity);
91 : void SetMaxEntrySize(PRInt32 maxSizeInKilobytes);
92 :
93 : bool EntryIsTooBig(PRInt64 entrySize);
94 :
95 : size_t TotalSize();
96 :
97 : private:
98 : friend class nsMemoryCacheDeviceInfo;
99 : enum { DELETE_ENTRY = true,
100 : DO_NOT_DELETE_ENTRY = false };
101 :
102 : void AdjustMemoryLimits( PRInt32 softLimit, PRInt32 hardLimit);
103 : void EvictEntry( nsCacheEntry * entry , bool deleteEntry);
104 : void EvictEntriesIfNecessary();
105 : int EvictionList(nsCacheEntry * entry, PRInt32 deltaSize);
106 :
107 : #ifdef DEBUG
108 : void CheckEntryCount();
109 : #endif
110 : /*
111 : * Data members
112 : */
113 : enum {
114 : kQueueCount = 24 // entries > 2^23 (8Mb) start in last queue
115 : };
116 :
117 : nsCacheEntryHashTable mMemCacheEntries;
118 : bool mInitialized;
119 :
120 : PRCList mEvictionList[kQueueCount];
121 : PRInt32 mEvictionThreshold;
122 :
123 : PRInt32 mHardLimit;
124 : PRInt32 mSoftLimit;
125 :
126 : PRInt32 mTotalSize;
127 : PRInt32 mInactiveSize;
128 :
129 : PRInt32 mEntryCount;
130 : PRInt32 mMaxEntryCount;
131 : PRInt32 mMaxEntrySize; // internal unit is bytes
132 :
133 : // XXX what other stats do we want to keep?
134 : };
135 :
136 :
137 : /******************************************************************************
138 : * nsMemoryCacheDeviceInfo - used to call nsIVisitor for about:cache
139 : ******************************************************************************/
140 : class nsMemoryCacheDeviceInfo : public nsICacheDeviceInfo {
141 : public:
142 : NS_DECL_ISUPPORTS
143 : NS_DECL_NSICACHEDEVICEINFO
144 :
145 4 : nsMemoryCacheDeviceInfo(nsMemoryCacheDevice* device)
146 4 : : mDevice(device)
147 : {
148 4 : }
149 :
150 16 : virtual ~nsMemoryCacheDeviceInfo() {}
151 :
152 : private:
153 : nsMemoryCacheDevice* mDevice;
154 : };
155 :
156 :
157 : #endif // _nsMemoryCacheDevice_h_
|