netwerk/cache/nsMemoryCacheDevice.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:0b8027136f64
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=8 sts=4 et sw=4 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef _nsMemoryCacheDevice_h_
8 #define _nsMemoryCacheDevice_h_
9
10 #include "nsCacheDevice.h"
11 #include "pldhash.h"
12 #include "nsCacheEntry.h"
13
14
15 class nsMemoryCacheDeviceInfo;
16
17 /******************************************************************************
18 * nsMemoryCacheDevice
19 ******************************************************************************/
20 class nsMemoryCacheDevice : public nsCacheDevice
21 {
22 public:
23 nsMemoryCacheDevice();
24 virtual ~nsMemoryCacheDevice();
25
26 virtual nsresult Init();
27 virtual nsresult Shutdown();
28
29 virtual const char * GetDeviceID(void);
30
31 virtual nsresult BindEntry( nsCacheEntry * entry );
32 virtual nsCacheEntry * FindEntry( nsCString * key, bool *collision );
33 virtual void DoomEntry( nsCacheEntry * entry );
34 virtual nsresult DeactivateEntry( nsCacheEntry * entry );
35
36 virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry,
37 nsCacheAccessMode mode,
38 uint32_t offset,
39 nsIInputStream ** result);
40
41 virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry,
42 nsCacheAccessMode mode,
43 uint32_t offset,
44 nsIOutputStream ** result);
45
46 virtual nsresult GetFileForEntry( nsCacheEntry * entry,
47 nsIFile ** result );
48
49 virtual nsresult OnDataSizeChange( nsCacheEntry * entry, int32_t deltaSize );
50
51 virtual nsresult Visit( nsICacheVisitor * visitor );
52
53 virtual nsresult EvictEntries(const char * clientID);
54 nsresult EvictPrivateEntries();
55
56 void SetCapacity(int32_t capacity);
57 void SetMaxEntrySize(int32_t maxSizeInKilobytes);
58
59 bool EntryIsTooBig(int64_t entrySize);
60
61 size_t TotalSize();
62
63 private:
64 friend class nsMemoryCacheDeviceInfo;
65 enum { DELETE_ENTRY = true,
66 DO_NOT_DELETE_ENTRY = false };
67
68 void AdjustMemoryLimits( int32_t softLimit, int32_t hardLimit);
69 void EvictEntry( nsCacheEntry * entry , bool deleteEntry);
70 void EvictEntriesIfNecessary();
71 int EvictionList(nsCacheEntry * entry, int32_t deltaSize);
72
73 typedef bool (*EvictionMatcherFn)(nsCacheEntry* entry, void* args);
74 nsresult DoEvictEntries(EvictionMatcherFn matchFn, void* args);
75
76 #ifdef DEBUG
77 void CheckEntryCount();
78 #endif
79 /*
80 * Data members
81 */
82 enum {
83 kQueueCount = 24 // entries > 2^23 (8Mb) start in last queue
84 };
85
86 nsCacheEntryHashTable mMemCacheEntries;
87 bool mInitialized;
88
89 PRCList mEvictionList[kQueueCount];
90
91 int32_t mHardLimit;
92 int32_t mSoftLimit;
93
94 int32_t mTotalSize;
95 int32_t mInactiveSize;
96
97 int32_t mEntryCount;
98 int32_t mMaxEntryCount;
99 int32_t mMaxEntrySize; // internal unit is bytes
100
101 // XXX what other stats do we want to keep?
102 };
103
104
105 /******************************************************************************
106 * nsMemoryCacheDeviceInfo - used to call nsIVisitor for about:cache
107 ******************************************************************************/
108 class nsMemoryCacheDeviceInfo : public nsICacheDeviceInfo {
109 public:
110 NS_DECL_ISUPPORTS
111 NS_DECL_NSICACHEDEVICEINFO
112
113 nsMemoryCacheDeviceInfo(nsMemoryCacheDevice* device)
114 : mDevice(device)
115 {
116 }
117
118 virtual ~nsMemoryCacheDeviceInfo() {}
119
120 private:
121 nsMemoryCacheDevice* mDevice;
122 };
123
124
125 #endif // _nsMemoryCacheDevice_h_

mercurial