Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/cache/nsDiskCacheBinding.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "mozilla/MemoryReporting.h"
8
#include "nsCache.h"
9
#include <limits.h>
10
11
#include "nscore.h"
12
#include "nsDiskCacheBinding.h"
13
#include "nsCacheService.h"
14
15
using namespace mozilla;
16
17
/******************************************************************************
18
 *  static hash table callback functions
19
 *
20
 *****************************************************************************/
21
struct HashTableEntry : PLDHashEntryHdr {
22
    nsDiskCacheBinding *  mBinding;
23
};
24
25
26
static PLDHashNumber
27
HashKey(const void *key)
28
0
{
29
0
    return (PLDHashNumber) NS_PTR_TO_INT32(key);
30
0
}
31
32
33
static bool
34
MatchEntry(const PLDHashEntryHdr *       header,
35
           const void *                  key)
36
0
{
37
0
    HashTableEntry * hashEntry = (HashTableEntry *) header;
38
0
    return (hashEntry->mBinding->mRecord.HashNumber() == (PLDHashNumber) NS_PTR_TO_INT32(key));
39
0
}
40
41
static void
42
MoveEntry(PLDHashTable *           /* table */,
43
          const PLDHashEntryHdr *     src,
44
          PLDHashEntryHdr       *     dst)
45
0
{
46
0
    new (KnownNotNull, dst) HashTableEntry(std::move(*(HashTableEntry*)src));
47
0
    // No need to delete `src`.
48
0
}
49
50
51
static void
52
ClearEntry(PLDHashTable *      /* table */,
53
           PLDHashEntryHdr *      header)
54
0
{
55
0
    ((HashTableEntry *)header)->mBinding = nullptr;
56
0
}
57
58
59
/******************************************************************************
60
 *  Utility Functions
61
 *****************************************************************************/
62
nsDiskCacheBinding *
63
GetCacheEntryBinding(nsCacheEntry * entry)
64
0
{
65
0
    return (nsDiskCacheBinding *) entry->Data();
66
0
}
67
68
69
/******************************************************************************
70
 *  nsDiskCacheBinding
71
 *****************************************************************************/
72
73
NS_IMPL_ISUPPORTS0(nsDiskCacheBinding)
74
75
nsDiskCacheBinding::nsDiskCacheBinding(nsCacheEntry* entry, nsDiskCacheRecord * record)
76
    :   mCacheEntry(entry)
77
    ,   mStreamIO(nullptr)
78
    ,   mDeactivateEvent(nullptr)
79
0
{
80
0
    NS_ASSERTION(record->ValidRecord(), "bad record");
81
0
    PR_INIT_CLIST(this);
82
0
    mRecord     = *record;
83
0
    mDoomed     = entry->IsDoomed();
84
0
    mGeneration = record->Generation();    // 0 == uninitialized, or data & meta using block files
85
0
}
86
87
nsDiskCacheBinding::~nsDiskCacheBinding()
88
0
{
89
0
    // Grab the cache lock since the binding is stored in nsCacheEntry::mData
90
0
    // and it is released using nsCacheService::ReleaseObject_Locked() which
91
0
    // releases the object outside the cache lock.
92
0
    nsCacheServiceAutoLock lock;
93
0
94
0
    NS_ASSERTION(PR_CLIST_IS_EMPTY(this), "binding deleted while still on list");
95
0
    if (!PR_CLIST_IS_EMPTY(this))
96
0
        PR_REMOVE_LINK(this);       // XXX why are we still on a list?
97
0
98
0
    // sever streamIO/binding link
99
0
    if (mStreamIO) {
100
0
        if (NS_FAILED(mStreamIO->ClearBinding()))
101
0
            nsCacheService::DoomEntry(mCacheEntry);
102
0
        NS_RELEASE(mStreamIO);
103
0
    }
104
0
}
105
106
nsresult
107
nsDiskCacheBinding::EnsureStreamIO()
108
0
{
109
0
    if (!mStreamIO) {
110
0
        mStreamIO = new nsDiskCacheStreamIO(this);
111
0
        if (!mStreamIO)  return NS_ERROR_OUT_OF_MEMORY;
112
0
        NS_ADDREF(mStreamIO);
113
0
    }
114
0
    return NS_OK;
115
0
}
116
117
118
/******************************************************************************
119
 *  nsDiskCacheBindery
120
 *
121
 *  Keeps track of bound disk cache entries to detect for collisions.
122
 *
123
 *****************************************************************************/
124
125
const PLDHashTableOps nsDiskCacheBindery::ops =
126
{
127
    HashKey,
128
    MatchEntry,
129
    MoveEntry,
130
    ClearEntry
131
};
132
133
134
nsDiskCacheBindery::nsDiskCacheBindery()
135
    : table(&ops, sizeof(HashTableEntry), kInitialTableLength)
136
    , initialized(false)
137
0
{
138
0
}
139
140
141
nsDiskCacheBindery::~nsDiskCacheBindery()
142
0
{
143
0
    Reset();
144
0
}
145
146
147
void
148
nsDiskCacheBindery::Init()
149
0
{
150
0
    table.ClearAndPrepareForLength(kInitialTableLength);
151
0
    initialized = true;
152
0
}
153
154
void
155
nsDiskCacheBindery::Reset()
156
0
{
157
0
    if (initialized) {
158
0
        table.ClearAndPrepareForLength(kInitialTableLength);
159
0
        initialized = false;
160
0
    }
161
0
}
162
163
164
nsDiskCacheBinding *
165
nsDiskCacheBindery::CreateBinding(nsCacheEntry *       entry,
166
                                  nsDiskCacheRecord *  record)
167
0
{
168
0
    NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
169
0
    nsCOMPtr<nsISupports> data = entry->Data();
170
0
    if (data) {
171
0
        NS_ERROR("cache entry already has bind data");
172
0
        return nullptr;
173
0
    }
174
0
175
0
    nsDiskCacheBinding * binding = new nsDiskCacheBinding(entry, record);
176
0
    if (!binding)  return nullptr;
177
0
178
0
    // give ownership of the binding to the entry
179
0
    entry->SetData(binding);
180
0
181
0
    // add binding to collision detection system
182
0
    nsresult rv = AddBinding(binding);
183
0
    if (NS_FAILED(rv)) {
184
0
        entry->SetData(nullptr);
185
0
        return nullptr;
186
0
    }
187
0
188
0
    return binding;
189
0
}
190
191
192
/**
193
 *  FindActiveEntry :  to find active colliding entry so we can doom it
194
 */
195
nsDiskCacheBinding *
196
nsDiskCacheBindery::FindActiveBinding(uint32_t  hashNumber) const
197
0
{
198
0
    NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
199
0
    // find hash entry for key
200
0
    auto hashEntry = static_cast<HashTableEntry*>
201
0
        (table.Search((void*)(uintptr_t)hashNumber));
202
0
    if (!hashEntry) return nullptr;
203
0
204
0
    // walk list looking for active entry
205
0
    NS_ASSERTION(hashEntry->mBinding, "hash entry left with no binding");
206
0
    nsDiskCacheBinding * binding = hashEntry->mBinding;
207
0
    while (binding->mCacheEntry->IsDoomed()) {
208
0
        binding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
209
0
        if (binding == hashEntry->mBinding)  return nullptr;
210
0
    }
211
0
    return binding;
212
0
}
213
214
215
/**
216
 *  AddBinding
217
 *
218
 *  Called from FindEntry() if we read an entry off of disk
219
 *      - it may already have a generation number
220
 *      - a generation number conflict is an error
221
 *
222
 *  Called from BindEntry()
223
 *      - a generation number needs to be assigned
224
 */
225
nsresult
226
nsDiskCacheBindery::AddBinding(nsDiskCacheBinding * binding)
227
0
{
228
0
    NS_ENSURE_ARG_POINTER(binding);
229
0
    NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
230
0
231
0
    // find hash entry for key
232
0
    auto hashEntry = static_cast<HashTableEntry*>
233
0
        (table.Add((void*)(uintptr_t)binding->mRecord.HashNumber(), fallible));
234
0
    if (!hashEntry)
235
0
        return NS_ERROR_OUT_OF_MEMORY;
236
0
237
0
    if (hashEntry->mBinding == nullptr) {
238
0
        hashEntry->mBinding = binding;
239
0
        if (binding->mGeneration == 0)
240
0
            binding->mGeneration = 1;   // if generation uninitialized, set it to 1
241
0
242
0
        return NS_OK;
243
0
    }
244
0
245
0
246
0
    // insert binding in generation order
247
0
    nsDiskCacheBinding * p  = hashEntry->mBinding;
248
0
    bool     calcGeneration = (binding->mGeneration == 0);  // do we need to calculate generation?
249
0
    if (calcGeneration)  binding->mGeneration = 1;          // initialize to 1 if uninitialized
250
0
    while (true) {
251
0
252
0
        if (binding->mGeneration < p->mGeneration) {
253
0
            // here we are
254
0
            PR_INSERT_BEFORE(binding, p);
255
0
            if (hashEntry->mBinding == p)
256
0
                hashEntry->mBinding = binding;
257
0
            break;
258
0
        }
259
0
260
0
        if (binding->mGeneration == p->mGeneration) {
261
0
            if (calcGeneration)  ++binding->mGeneration;    // try the next generation
262
0
            else {
263
0
                NS_ERROR("### disk cache: generations collide!");
264
0
                return NS_ERROR_UNEXPECTED;
265
0
            }
266
0
        }
267
0
268
0
        p = (nsDiskCacheBinding *)PR_NEXT_LINK(p);
269
0
        if (p == hashEntry->mBinding) {
270
0
            // end of line: insert here or die
271
0
            p = (nsDiskCacheBinding *)PR_PREV_LINK(p);  // back up and check generation
272
0
            if (p->mGeneration == 255) {
273
0
                NS_WARNING("### disk cache: generation capacity at full");
274
0
                return NS_ERROR_UNEXPECTED;
275
0
            }
276
0
            PR_INSERT_BEFORE(binding, hashEntry->mBinding);
277
0
            break;
278
0
        }
279
0
    }
280
0
    return NS_OK;
281
0
}
282
283
284
/**
285
 *  RemoveBinding :  remove binding from collision detection on deactivation
286
 */
287
void
288
nsDiskCacheBindery::RemoveBinding(nsDiskCacheBinding * binding)
289
0
{
290
0
    NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
291
0
    if (!initialized)   return;
292
0
293
0
    void* key = (void *)(uintptr_t)binding->mRecord.HashNumber();
294
0
    auto hashEntry =
295
0
        static_cast<HashTableEntry*>(table.Search((void*)(uintptr_t) key));
296
0
    if (!hashEntry) {
297
0
        NS_WARNING("### disk cache: binding not in hashtable!");
298
0
        return;
299
0
    }
300
0
301
0
    if (binding == hashEntry->mBinding) {
302
0
        if (PR_CLIST_IS_EMPTY(binding)) {
303
0
            // remove this hash entry
304
0
            table.Remove((void*)(uintptr_t) binding->mRecord.HashNumber());
305
0
            return;
306
0
307
0
        }
308
0
        // promote next binding to head, and unlink this binding
309
0
        hashEntry->mBinding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
310
0
    }
311
0
    PR_REMOVE_AND_INIT_LINK(binding);
312
0
}
313
314
/**
315
 * ActiveBindings: return true if any bindings have open descriptors.
316
 */
317
bool
318
nsDiskCacheBindery::ActiveBindings()
319
0
{
320
0
    NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
321
0
    if (!initialized) return false;
322
0
323
0
    for (auto iter = table.Iter(); !iter.Done(); iter.Next()) {
324
0
        auto entry = static_cast<HashTableEntry*>(iter.Get());
325
0
        nsDiskCacheBinding* binding = entry->mBinding;
326
0
        nsDiskCacheBinding* head = binding;
327
0
        do {
328
0
            if (binding->IsActive()) {
329
0
                return true;
330
0
            }
331
0
            binding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
332
0
        } while (binding != head);
333
0
    }
334
0
335
0
    return false;
336
0
}
337
338
/**
339
 * SizeOfExcludingThis: return the amount of heap memory (bytes) being used by
340
 * the bindery.
341
 */
342
size_t
343
nsDiskCacheBindery::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf)
344
0
{
345
0
    NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
346
0
    if (!initialized) return 0;
347
0
348
0
    size_t size = 0;
349
0
350
0
    for (auto iter = table.Iter(); !iter.Done(); iter.Next()) {
351
0
        auto entry = static_cast<HashTableEntry*>(iter.Get());
352
0
        nsDiskCacheBinding* binding = entry->mBinding;
353
0
354
0
        nsDiskCacheBinding* head = binding;
355
0
        do {
356
0
            size += aMallocSizeOf(binding);
357
0
            if (binding->mStreamIO) {
358
0
                size += binding->mStreamIO->SizeOfIncludingThis(aMallocSizeOf);
359
0
            }
360
0
361
0
            // No good way to get at mDeactivateEvent internals for proper
362
0
            // size, so we use this as an estimate.
363
0
            if (binding->mDeactivateEvent) {
364
0
                size += aMallocSizeOf(binding->mDeactivateEvent);
365
0
            }
366
0
            binding = (nsDiskCacheBinding *)PR_NEXT_LINK(binding);
367
0
        } while (binding != head);
368
0
    }
369
0
370
0
    return size;
371
0
}