| 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 |
#ifndef gc_MallocedBlockCache_h |
| 9 |
#define gc_MallocedBlockCache_h |
| 10 |
|
| 11 |
#include "ds/PointerAndUint7.h" |
| 12 |
#include "js/AllocPolicy.h" |
| 13 |
#include "js/Vector.h" |
| 14 |
#include "util/Poison.h" |
| 15 |
|
| 16 |
namespace js { |
| 17 |
namespace gc { |
| 18 |
|
| 19 |
// MallocedBlockCache implements a lightweight wrapper around js_malloc/js_free. |
| 20 |
// |
| 21 |
// Blocks are requested by ::alloc and must be returned with ::free, at which |
| 22 |
// point the cache may decide to hold on to the block rather than hand it back |
| 23 |
// to js_free. Subsequent ::alloc calls may be satisfied from the cached |
| 24 |
// blocks rather than calling js_malloc. The mechanism is designed to be much |
| 25 |
// cheaper than calling js_malloc/js_free directly. One consequence is that |
| 26 |
// there is no locking; it is essential therefore to use each cache only from |
| 27 |
// a single thread. |
| 28 |
// |
| 29 |
// The intended use is for lightweight management of OOL (malloc'd) storage |
| 30 |
// associated with WasmStructObject and WasmArrayObject. The mechanism is |
| 31 |
// general and potentially has other uses. Blocks of size STEP * NUM_LISTS |
| 32 |
// and larger are never cached, though. |
| 33 |
// |
| 34 |
// Request sizes are rounded up to a multiple of STEP. There are NUM_LISTS-1 |
| 35 |
// free lists, with a "List ID" indicating the multiple of STEP stored on the |
| 36 |
// list. So for example, blocks of size 3 * STEP (after rounding up) are |
| 37 |
// stored on the list with ID 3. List ID 0 indicates blocks which are too |
| 38 |
// large to live on any freelist. With the default settings, this gives |
| 39 |
// separate freelists for blocks of size 16, 32, 48, .. 496. Blocks of size |
| 40 |
// zero are not supported, and `lists[0]` will always be empty. |
| 41 |
// |
| 42 |
// Allocation of a block produces not only the block's address but also its |
| 43 |
// List ID. When freeing, both values must be presented, because there is |
| 44 |
// otherwise no way for ::free to know the size of the original allocation, |
| 45 |
// and hence which freelist it should go on. For this reason, the ::alloc and |
| 46 |
// ::free methods produce and take a `PointerAndUint7`, not a `void*`. |
| 47 |
// |
| 48 |
// Resizing of blocks is not supported. |
| 49 |
|
| 50 |
class MallocedBlockCache { |
| 51 |
public: |
| 52 |
static const size_t STEP = 16; |
| 53 |
|
| 54 |
static const size_t NUM_LISTS = 32; |
| 55 |
// This limitation exists because allocation returns a PointerAndUint7, and |
| 56 |
// a List-ID value (viz, 0 .. NUM_LISTS-1) is stored in the Uint7 part. |
| 57 |
static_assert(NUM_LISTS <= (1 << 7)); |
| 58 |
|
| 59 |
// list[0] must always remain empty. List ID 0 indicates a block which |
| 60 |
// cannot participate in the freelist machinery because it is too large. |
| 61 |
// |
| 62 |
// list[i], for 1 <= i < NUM_LISTS, holds blocks of size i * STEP only. |
| 63 |
// All requests are rounded up to multiple of SIZE. |
| 64 |
// |
| 65 |
// We do not expect to be required to issue or accept blocks of size zero. |
| 66 |
static const size_t OVERSIZE_BLOCK_LIST_ID = 0; |
| 67 |
using MallocedBlockVector = Vector<void*, 0, SystemAllocPolicy>; |
| 68 |
|
| 69 |
MallocedBlockVector lists[NUM_LISTS]; |
| 70 |
|
| 71 |
~MallocedBlockCache(); |
| 72 |
|
| 73 |
static inline size_t listIDForSize(size_t size); |
| 74 |
|
| 75 |
// Allocation and freeing. Use `alloc` to allocate. `allocSlow` is |
| 76 |
// `alloc`s fallback path. Do not call it directly, since it doesn't handle |
| 77 |
// all cases by itself. |
| 78 |
[[nodiscard]] inline PointerAndUint7 alloc(size_t size); |
| 79 |
[[nodiscard]] MOZ_NEVER_INLINE PointerAndUint7 allocSlow(size_t size); |
| 80 |
|
| 81 |
inline void free(PointerAndUint7 blockAndListID); |
| 82 |
|
| 83 |
// Allows users to gradually hand blocks back to js_free, so as to avoid |
| 84 |
// space leaks in long-running scenarios. The specified percentage of |
| 85 |
// blocks in each list is discarded. |
| 86 |
void preen(double percentOfBlocksToDiscard); |
| 87 |
|
| 88 |
// Return all blocks in the cache to js_free. |
| 89 |
void clear(); |
| 90 |
|
| 91 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 92 |
}; |
| 93 |
|
| 94 |
/* static */ |
| 95 |
inline size_t MallocedBlockCache::listIDForSize(size_t size) { |
| 96 |
// Figure out which free list can give us a block of size `size`, after it |
| 97 |
// has been rounded up to a multiple of `step`. |
| 98 |
// |
| 99 |
// Example mapping for STEP = 16 and NUM_LISTS = 8, after rounding up: |
| 100 |
// 0 never holds any blocks (denotes "too large") |
| 101 |
// 1 holds blocks of size 16 |
| 102 |
// 2 holds blocks of size 32 |
| 103 |
// 3 holds blocks of size 48 |
| 104 |
// 4 holds blocks of size 64 |
| 105 |
// 5 holds blocks of size 80 |
| 106 |
// 6 holds blocks of size 96 |
| 107 |
// 7 holds blocks of size 112 |
| 108 |
// |
| 109 |
// For a request of size n: |
| 110 |
// * if n == 0, fail |
| 111 |
// * else |
| 112 |
// round n up to a multiple of STEP |
| 113 |
// let i = n / STEP |
| 114 |
// if i >= NUM_LISTS |
| 115 |
// alloc direct from js_malloc, and return listID = 0 |
| 116 |
// if lists[i] is nonempty, use lists[i] and return listID = i. |
| 117 |
// else |
| 118 |
// let p = js_malloc(n) |
| 119 |
// return p and listID = i. |
| 120 |
|
| 121 |
// We're never expected to handle zero-sized blocks. |
| 122 |
MOZ_ASSERT(size > 0); |
| 123 |
|
| 124 |
size = js::RoundUp(size, STEP); |
| 125 |
size_t i = size / STEP; |
| 126 |
MOZ_ASSERT(i > 0); |
| 127 |
|
| 128 |
if (i >= NUM_LISTS) { |
| 129 |
return OVERSIZE_BLOCK_LIST_ID; |
| 130 |
} |
| 131 |
|
| 132 |
return i; |
| 133 |
} |
| 134 |
|
| 135 |
inline PointerAndUint7 MallocedBlockCache::alloc(size_t size) { |
| 136 |
size_t i = listIDForSize(size); |
| 137 |
MOZ_ASSERT(i < NUM_LISTS); |
| 138 |
|
| 139 |
// Fast path: try to pull a block from the relevant list. |
| 140 |
if (MOZ_LIKELY( |
| 141 |
i != OVERSIZE_BLOCK_LIST_ID && // "block is small enough to cache" |
| 142 |
!lists[i].empty())) { // "a cached block is available" |
| 143 |
// Check that i is the right list |
| 144 |
MOZ_ASSERT(i * STEP == js::RoundUp(size, STEP)); |
| 145 |
void* block = lists[i].popCopy(); |
| 146 |
return PointerAndUint7(block, i); |
| 147 |
} |
| 148 |
|
| 149 |
// Fallback path for all other cases. |
| 150 |
return allocSlow(size); |
| 151 |
} |
| 152 |
|
| 153 |
inline void MallocedBlockCache::free(PointerAndUint7 blockAndListID) { |
| 154 |
// This is a whole lot simpler than the ::alloc case, since we are given the |
| 155 |
// listId and don't have to compute it (not that we have any way to). |
| 156 |
void* block = blockAndListID.pointer(); |
| 157 |
uint32_t listID = blockAndListID.uint7(); |
| 158 |
MOZ_ASSERT(block); |
| 159 |
MOZ_ASSERT(listID < NUM_LISTS); |
| 160 |
if (MOZ_UNLIKELY(listID == OVERSIZE_BLOCK_LIST_ID)) { |
| 161 |
// It was too large for recycling; go straight to js_free. |
| 162 |
js_free(block); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
// Put it back on list `listId`, first poisoning it for safety. |
| 167 |
memset(block, JS_NOTINUSE_TRAILER_PATTERN, listID * STEP); |
| 168 |
MOZ_MAKE_MEM_UNDEFINED(block, listID * STEP); |
| 169 |
if (MOZ_UNLIKELY(!lists[listID].append(block))) { |
| 170 |
// OOM'd while doing admin. Hand it off to js_free and forget about the |
| 171 |
// OOM. |
| 172 |
js_free(block); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
} // namespace gc |
| 177 |
} // namespace js |
| 178 |
|
| 179 |
#endif // gc_MallocedBlockCache_h |