Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/cache/nsMemoryCacheDevice.h
Line
Count
Source (jump to first uncovered line)
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 "PLDHashTable.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() override;
27
    virtual nsresult        Shutdown() override;
28
29
    virtual const char *    GetDeviceID(void) override;
30
31
    virtual nsresult        BindEntry( nsCacheEntry * entry ) override;
32
    virtual nsCacheEntry *  FindEntry( nsCString * key, bool *collision ) override;
33
    virtual void            DoomEntry( nsCacheEntry * entry ) override;
34
    virtual nsresult        DeactivateEntry( nsCacheEntry * entry ) override;
35
36
    virtual nsresult OpenInputStreamForEntry(nsCacheEntry *     entry,
37
                                             nsCacheAccessMode  mode,
38
                                             uint32_t           offset,
39
                                             nsIInputStream **  result) override;
40
41
    virtual nsresult OpenOutputStreamForEntry(nsCacheEntry *     entry,
42
                                              nsCacheAccessMode  mode,
43
                                              uint32_t           offset,
44
                                              nsIOutputStream ** result) override;
45
46
    virtual nsresult GetFileForEntry( nsCacheEntry *    entry,
47
                                      nsIFile **        result ) override;
48
49
    virtual nsresult OnDataSizeChange( nsCacheEntry * entry,
50
                                       int32_t deltaSize ) override;
51
52
    virtual nsresult Visit( nsICacheVisitor * visitor ) override;
53
54
    virtual nsresult EvictEntries(const char * clientID) override;
55
    nsresult EvictPrivateEntries();
56
57
    void             SetCapacity(int32_t  capacity);
58
    void             SetMaxEntrySize(int32_t  maxSizeInKilobytes);
59
60
    bool             EntryIsTooBig(int64_t entrySize);
61
62
    size_t           TotalSize();
63
64
private:
65
    friend class nsMemoryCacheDeviceInfo;
66
    enum      { DELETE_ENTRY        = true,
67
                DO_NOT_DELETE_ENTRY = false };
68
69
    void      AdjustMemoryLimits( int32_t  softLimit, int32_t  hardLimit);
70
    void      EvictEntry( nsCacheEntry * entry , bool deleteEntry);
71
    void      EvictEntriesIfNecessary();
72
    int       EvictionList(nsCacheEntry * entry, int32_t  deltaSize);
73
74
    typedef bool (*EvictionMatcherFn)(nsCacheEntry* entry, void* args);
75
    nsresult DoEvictEntries(EvictionMatcherFn matchFn, void* args);
76
77
#ifdef DEBUG
78
    void      CheckEntryCount();
79
#endif
80
    /*
81
     *  Data members
82
     */
83
    enum {
84
        kQueueCount = 24   // entries > 2^23 (8Mb) start in last queue
85
    };
86
87
    nsCacheEntryHashTable  mMemCacheEntries;
88
    bool                   mInitialized;
89
90
    PRCList                mEvictionList[kQueueCount];
91
92
    int32_t                mHardLimit;
93
    int32_t                mSoftLimit;
94
95
    int32_t                mTotalSize;
96
    int32_t                mInactiveSize;
97
98
    int32_t                mEntryCount;
99
    int32_t                mMaxEntryCount;
100
    int32_t                mMaxEntrySize; // internal unit is bytes
101
102
    // XXX what other stats do we want to keep?
103
};
104
105
106
/******************************************************************************
107
 * nsMemoryCacheDeviceInfo - used to call nsIVisitor for about:cache
108
 ******************************************************************************/
109
class nsMemoryCacheDeviceInfo : public nsICacheDeviceInfo {
110
public:
111
    NS_DECL_ISUPPORTS
112
    NS_DECL_NSICACHEDEVICEINFO
113
114
    explicit nsMemoryCacheDeviceInfo(nsMemoryCacheDevice* device)
115
        :   mDevice(device)
116
0
    {
117
0
    }
118
119
private:
120
0
    virtual ~nsMemoryCacheDeviceInfo() = default;
121
    nsMemoryCacheDevice* mDevice;
122
};
123
124
125
#endif // _nsMemoryCacheDevice_h_