Debian Codegraph @

js/src/gc/MallocedBlockCache.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sw=2 et tw=80:
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #include "gc/MallocedBlockCache.h"
9 #include "mozilla/MemoryChecking.h"
10
11 using js::PointerAndUint7;
12 using js::gc::MallocedBlockCache;
13
14 MallocedBlockCache::~MallocedBlockCache() { clear(); }
15
16 // This is the fallback path for MallocedBlockCache::alloc. Do not call this
17 // directly, since it doesn't handle all cases by itself. See ::alloc for
18 // further comments.
19 PointerAndUint7 MallocedBlockCache::allocSlow(size_t size) {
20 // We're never expected to handle zero-sized blocks.
21 MOZ_ASSERT(size > 0);
22
23 size = js::RoundUp(size, STEP);
24 size_t i = size / STEP;
25 MOZ_ASSERT(i > 0);
26
27 // Too large to cache; go straight to js_malloc.
28 if (MOZ_UNLIKELY(i >= NUM_LISTS)) {
29 void* p = js_malloc(size);
30 // If p is nullptr, that fact is carried into the PointerAndUint7, and the
31 // caller is expected to check that.
32 return PointerAndUint7(p, OVERSIZE_BLOCK_LIST_ID);
33 }
34
35 // The block is of cacheable size, but we expect the relevant list to be
36 // empty, because ::alloc will have handled the case where it wasn't.
37 MOZ_ASSERT(i >= 1 && i < NUM_LISTS);
38 // Check that i is the right list
39 MOZ_ASSERT(i * STEP == size);
40 MOZ_RELEASE_ASSERT(lists[i].empty());
41
42 // And so we have to hand the request off to js_malloc.
43 void* p = js_malloc(size);
44 if (MOZ_UNLIKELY(!p)) {
45 return PointerAndUint7(nullptr, 0); // OOM
46 }
47 return PointerAndUint7(p, i);
48 }
49
50 void MallocedBlockCache::preen(double percentOfBlocksToDiscard) {
51 MOZ_ASSERT(percentOfBlocksToDiscard >= 0.0 &&
52 percentOfBlocksToDiscard <= 100.0);
53 MOZ_ASSERT(lists[OVERSIZE_BLOCK_LIST_ID].empty());
54 for (size_t listID = 1; listID < NUM_LISTS; listID++) {
55 MallocedBlockVector& list = lists[listID];
56 size_t numToFree =
57 size_t(float(list.length()) * (percentOfBlocksToDiscard / 100.0));
58 MOZ_RELEASE_ASSERT(numToFree <= list.length());
59 while (numToFree > 0) {
60 void* block = list.popCopy();
61 MOZ_ASSERT(block);
62 js_free(block);
63 numToFree--;
64 }
65 }
66 }
67
68 void MallocedBlockCache::clear() {
69 MOZ_ASSERT(lists[OVERSIZE_BLOCK_LIST_ID].empty());
70 for (size_t i = 1; i < NUM_LISTS; i++) {
71 MallocedBlockVector& list = lists[i];
72 for (void*& block : list) {
73 MOZ_ASSERT(block);
74 js_free(block);
75 block = nullptr; // for safety
76 }
77 list.clear();
78 }
79 }
80
81 size_t MallocedBlockCache::sizeOfExcludingThis(
82 mozilla::MallocSizeOf mallocSizeOf) const {
83 MOZ_ASSERT(lists[OVERSIZE_BLOCK_LIST_ID].empty());
84 size_t nBytes = 0;
85 for (const MallocedBlockVector& list : lists) {
86 nBytes += list.sizeOfExcludingThis(mallocSizeOf);
87 // The payload size of each block in `list` is the same. Hence, we could
88 // possibly do better here (measure once and multiply by the length) if we
89 // believe that the metadata size for each block is also the same.
90 for (void* block : list) {
91 MOZ_ASSERT(block);
92 nBytes += mallocSizeOf(block);
93 }
94 }
95 return nBytes;
96 }