|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * |
|
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 |
|
8 #ifndef _nsDiskCacheBinding_h_ |
|
9 #define _nsDiskCacheBinding_h_ |
|
10 |
|
11 #include "mozilla/MemoryReporting.h" |
|
12 #include "nspr.h" |
|
13 #include "pldhash.h" |
|
14 |
|
15 #include "nsISupports.h" |
|
16 #include "nsCacheEntry.h" |
|
17 |
|
18 #include "nsDiskCacheMap.h" |
|
19 #include "nsDiskCacheStreams.h" |
|
20 |
|
21 |
|
22 /****************************************************************************** |
|
23 * nsDiskCacheBinding |
|
24 * |
|
25 * Created for disk cache specific data and stored in nsCacheEntry.mData as |
|
26 * an nsISupports. Also stored in nsDiskCacheHashTable, with collisions |
|
27 * linked by the PRCList. |
|
28 * |
|
29 *****************************************************************************/ |
|
30 |
|
31 class nsDiskCacheDeviceDeactivateEntryEvent; |
|
32 |
|
33 class nsDiskCacheBinding : public nsISupports, public PRCList { |
|
34 public: |
|
35 NS_DECL_THREADSAFE_ISUPPORTS |
|
36 |
|
37 nsDiskCacheBinding(nsCacheEntry* entry, nsDiskCacheRecord * record); |
|
38 virtual ~nsDiskCacheBinding(); |
|
39 |
|
40 nsresult EnsureStreamIO(); |
|
41 bool IsActive() { return mCacheEntry != nullptr;} |
|
42 |
|
43 // XXX make friends |
|
44 public: |
|
45 nsCacheEntry* mCacheEntry; // back pointer to parent nsCacheEntry |
|
46 nsDiskCacheRecord mRecord; |
|
47 nsDiskCacheStreamIO* mStreamIO; // strong reference |
|
48 bool mDoomed; // record is not stored in cache map |
|
49 uint8_t mGeneration; // possibly just reservation |
|
50 |
|
51 // If set, points to a pending event which will deactivate |mCacheEntry|. |
|
52 // If not set then either |mCacheEntry| is not deactivated, or it has been |
|
53 // deactivated but the device returned it from FindEntry() before the event |
|
54 // fired. In both two latter cases this binding is to be considered valid. |
|
55 nsDiskCacheDeviceDeactivateEntryEvent *mDeactivateEvent; |
|
56 }; |
|
57 |
|
58 |
|
59 /****************************************************************************** |
|
60 * Utility Functions |
|
61 *****************************************************************************/ |
|
62 |
|
63 nsDiskCacheBinding * GetCacheEntryBinding(nsCacheEntry * entry); |
|
64 |
|
65 |
|
66 |
|
67 /****************************************************************************** |
|
68 * nsDiskCacheBindery |
|
69 * |
|
70 * Used to keep track of nsDiskCacheBinding associated with active/bound (and |
|
71 * possibly doomed) entries. Lookups on 4 byte disk hash to find collisions |
|
72 * (which need to be doomed, instead of just evicted. Collisions are linked |
|
73 * using a PRCList to keep track of current generation number. |
|
74 * |
|
75 * Used to detect hash number collisions, and find available generation numbers. |
|
76 * |
|
77 * Not all nsDiskCacheBinding have a generation number. |
|
78 * |
|
79 * Generation numbers may be aquired late, or lost (when data fits in block file) |
|
80 * |
|
81 * Collisions can occur: |
|
82 * BindEntry() - hashnumbers collide (possibly different keys) |
|
83 * |
|
84 * Generation number required: |
|
85 * DeactivateEntry() - metadata written to disk, may require file |
|
86 * GetFileForEntry() - force data to require file |
|
87 * writing to stream - data size may require file |
|
88 * |
|
89 * Binding can be kept in PRCList in order of generation numbers. |
|
90 * Binding with no generation number can be Appended to PRCList (last). |
|
91 * |
|
92 *****************************************************************************/ |
|
93 |
|
94 class nsDiskCacheBindery { |
|
95 public: |
|
96 nsDiskCacheBindery(); |
|
97 ~nsDiskCacheBindery(); |
|
98 |
|
99 nsresult Init(); |
|
100 void Reset(); |
|
101 |
|
102 nsDiskCacheBinding * CreateBinding(nsCacheEntry * entry, |
|
103 nsDiskCacheRecord * record); |
|
104 |
|
105 nsDiskCacheBinding * FindActiveBinding(uint32_t hashNumber); |
|
106 void RemoveBinding(nsDiskCacheBinding * binding); |
|
107 bool ActiveBindings(); |
|
108 |
|
109 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
|
110 |
|
111 private: |
|
112 nsresult AddBinding(nsDiskCacheBinding * binding); |
|
113 |
|
114 // member variables |
|
115 static const PLDHashTableOps ops; |
|
116 PLDHashTable table; |
|
117 bool initialized; |
|
118 }; |
|
119 |
|
120 #endif /* _nsDiskCacheBinding_h_ */ |