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