summaryrefslogtreecommitdiffstats
path: root/netwerk/cache/nsDiskCacheBinding.h
blob: 63c2094ce69d6d36348d7ace539b076208d9a851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


#ifndef _nsDiskCacheBinding_h_
#define _nsDiskCacheBinding_h_

#include "mozilla/MemoryReporting.h"
#include "nspr.h"
#include "PLDHashTable.h"

#include "nsISupports.h"
#include "nsCacheEntry.h"

#include "nsDiskCacheMap.h"
#include "nsDiskCacheStreams.h"


/******************************************************************************
 *  nsDiskCacheBinding
 *
 *  Created for disk cache specific data and stored in nsCacheEntry.mData as
 *  an nsISupports.  Also stored in nsDiskCacheHashTable, with collisions
 *  linked by the PRCList.
 *
 *****************************************************************************/

class nsDiskCacheDeviceDeactivateEntryEvent;

class nsDiskCacheBinding : public nsISupports, public PRCList {
    virtual ~nsDiskCacheBinding();

public:
    NS_DECL_THREADSAFE_ISUPPORTS

    nsDiskCacheBinding(nsCacheEntry* entry, nsDiskCacheRecord * record);

    nsresult EnsureStreamIO();
    bool     IsActive() { return mCacheEntry != nullptr;}

// XXX make friends
public:
    nsCacheEntry*           mCacheEntry;    // back pointer to parent nsCacheEntry
    nsDiskCacheRecord       mRecord;
    nsDiskCacheStreamIO*    mStreamIO;      // strong reference
    bool                    mDoomed;        // record is not stored in cache map
    uint8_t                 mGeneration;    // possibly just reservation

    // If set, points to a pending event which will deactivate |mCacheEntry|.
    // If not set then either |mCacheEntry| is not deactivated, or it has been
    // deactivated but the device returned it from FindEntry() before the event
    // fired. In both two latter cases this binding is to be considered valid.
    nsDiskCacheDeviceDeactivateEntryEvent *mDeactivateEvent;
};


/******************************************************************************
 *  Utility Functions
 *****************************************************************************/

nsDiskCacheBinding *   GetCacheEntryBinding(nsCacheEntry * entry);



/******************************************************************************
 *  nsDiskCacheBindery
 *
 *  Used to keep track of nsDiskCacheBinding associated with active/bound (and
 *  possibly doomed) entries.  Lookups on 4 byte disk hash to find collisions
 *  (which need to be doomed, instead of just evicted.  Collisions are linked
 *  using a PRCList to keep track of current generation number.
 *
 *  Used to detect hash number collisions, and find available generation numbers.
 *
 *  Not all nsDiskCacheBinding have a generation number.
 *
 *  Generation numbers may be aquired late, or lost (when data fits in block file)
 *
 *  Collisions can occur:
 *      BindEntry()       - hashnumbers collide (possibly different keys)
 *
 *  Generation number required:
 *      DeactivateEntry() - metadata written to disk, may require file
 *      GetFileForEntry() - force data to require file
 *      writing to stream - data size may require file
 *
 *  Binding can be kept in PRCList in order of generation numbers.
 *  Binding with no generation number can be Appended to PRCList (last).
 *
 *****************************************************************************/

class nsDiskCacheBindery {
public:
    nsDiskCacheBindery();
    ~nsDiskCacheBindery();

    void                    Init();
    void                    Reset();

    nsDiskCacheBinding *    CreateBinding(nsCacheEntry *       entry,
                                          nsDiskCacheRecord *  record);

    nsDiskCacheBinding *    FindActiveBinding(uint32_t  hashNumber);
    void                    RemoveBinding(nsDiskCacheBinding * binding);
    bool                    ActiveBindings();

    size_t                 SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf);

private:
    nsresult                AddBinding(nsDiskCacheBinding * binding);

    // member variables
    static const PLDHashTableOps ops;
    PLDHashTable           table;
    bool                   initialized;

    static const uint32_t kInitialTableLength = 0;
};

#endif /* _nsDiskCacheBinding_h_ */