Debian Codegraph @

js/src/ds/Bitmap.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 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 ds_Bitmap_h
8 #define ds_Bitmap_h
9
10 #include "mozilla/Array.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/MemoryChecking.h"
14
15 #include <algorithm>
16 #include <stddef.h>
17 #include <stdint.h>
18
19 #include "js/AllocPolicy.h"
20 #include "js/HashTable.h"
21 #include "js/HeapAPI.h"
22 #include "js/Vector.h"
23
24 // This file provides two classes for representing bitmaps.
25 //
26 // DenseBitmap is an array of words of bits, with size linear in the maximum
27 // bit which has been set on it.
28 //
29 // SparseBitmap provides a reasonably simple, reasonably efficient (in time and
30 // space) implementation of a sparse bitmap. The basic representation is a hash
31 // table whose entries are fixed length malloc'ed blocks of bits.
32
33 namespace js {
34
35 class DenseBitmap {
36 using Data = Vector<uintptr_t, 0, SystemAllocPolicy>;
37
38 Data data;
39
40 public:
41 size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
42 return data.sizeOfExcludingThis(mallocSizeOf);
43 }
44
45 bool ensureSpace(size_t numWords) {
46 MOZ_ASSERT(data.empty());
47 return data.appendN(0, numWords);
48 }
49
50 size_t numWords() const { return data.length(); }
51 uintptr_t word(size_t i) const { return data[i]; }
52 uintptr_t& word(size_t i) { return data[i]; }
53
54 template <typename T>
55 typename std::enable_if_t<std::is_convertible_v<T, uintptr_t>, void>
56 copyBitsFrom(size_t wordStart, size_t numWords, T* source) {
57 MOZ_ASSERT(wordStart + numWords <= data.length());
58 for (size_t i = 0; i < numWords; i++) {
59 data[wordStart + i] = source[i];
60 }
61 }
62
63 template <typename T>
64 typename std::enable_if_t<std::is_convertible_v<T, uintptr_t>, void>
65 bitwiseOrRangeInto(size_t wordStart, size_t numWords, T* target) const {
66 for (size_t i = 0; i < numWords; i++) {
67 target[i] |= data[wordStart + i];
68 }
69 }
70 };
71
72 class SparseBitmap {
73 // The number of words of bits to use for each block mainly affects the
74 // memory usage of the bitmap. To minimize overhead, bitmaps which are
75 // expected to be fairly dense should have a large block size, and bitmaps
76 // which are expected to be very sparse should have a small block size.
77 static const size_t WordsInBlock = 4096 / sizeof(uintptr_t);
78
79 using BitBlock = mozilla::Array<uintptr_t, WordsInBlock>;
80 using Data =
81 HashMap<size_t, BitBlock*, DefaultHasher<size_t>, SystemAllocPolicy>;
82
83 Data data;
84
85 MOZ_ALWAYS_INLINE static size_t blockStartWord(size_t word) {
86 return word & ~(WordsInBlock - 1);
87 }
88
89 MOZ_ALWAYS_INLINE static uintptr_t bitMask(size_t bit) {
90 return uintptr_t(1) << (bit % JS_BITS_PER_WORD);
91 }
92
93 // Return the number of words in a BitBlock starting at |blockWord| which
94 // are in |other|.
95 static size_t wordIntersectCount(size_t blockWord, const DenseBitmap& other) {
96 long count = other.numWords() - blockWord;
97 return static_cast<size_t>(std::clamp(count, 0l, (long)WordsInBlock));
98 }
99
100 BitBlock& createBlock(Data::AddPtr p, size_t blockId,
101 AutoEnterOOMUnsafeRegion& oomUnsafe);
102
103 BitBlock* createBlock(Data::AddPtr p, size_t blockId);
104
105 MOZ_ALWAYS_INLINE BitBlock* getBlock(size_t blockId) const {
106 Data::Ptr p = data.lookup(blockId);
107 return p ? p->value() : nullptr;
108 }
109
110 MOZ_ALWAYS_INLINE const BitBlock* readonlyThreadsafeGetBlock(
111 size_t blockId) const {
112 Data::Ptr p = data.readonlyThreadsafeLookup(blockId);
113 return p ? p->value() : nullptr;
114 }
115
116 MOZ_ALWAYS_INLINE BitBlock& getOrCreateBlock(size_t blockId) {
117 // The lookupForAdd() needs protection against injected OOMs, as does
118 // the add() within createBlock().
119 AutoEnterOOMUnsafeRegion oomUnsafe;
120 Data::AddPtr p = data.lookupForAdd(blockId);
121 if (p) {
122 return *p->value();
123 }
124 return createBlock(p, blockId, oomUnsafe);
125 }
126
127 MOZ_ALWAYS_INLINE BitBlock* getOrCreateBlockFallible(size_t blockId) {
128 Data::AddPtr p = data.lookupForAdd(blockId);
129 if (p) {
130 return p->value();
131 }
132 return createBlock(p, blockId);
133 }
134
135 public:
136 ~SparseBitmap();
137
138 size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf);
139
140 MOZ_ALWAYS_INLINE void setBit(size_t bit) {
141 size_t word = bit / JS_BITS_PER_WORD;
142 size_t blockWord = blockStartWord(word);
143 BitBlock& block = getOrCreateBlock(blockWord / WordsInBlock);
144 block[word - blockWord] |= bitMask(bit);
145 }
146
147 MOZ_ALWAYS_INLINE bool setBitFallible(size_t bit) {
148 size_t word = bit / JS_BITS_PER_WORD;
149 size_t blockWord = blockStartWord(word);
150 BitBlock* block = getOrCreateBlockFallible(blockWord / WordsInBlock);
151 if (!block) {
152 return false;
153 }
154 (*block)[word - blockWord] |= bitMask(bit);
155 return true;
156 }
157
158 bool getBit(size_t bit) const;
159 bool readonlyThreadsafeGetBit(size_t bit) const;
160
161 void bitwiseAndWith(const DenseBitmap& other);
162 void bitwiseOrWith(const SparseBitmap& other);
163 void bitwiseOrInto(DenseBitmap& other) const;
164
165 // Currently, the following APIs only supports a range of words that is in a
166 // single bit block.
167
168 template <typename T>
169 typename std::enable_if_t<std::is_convertible_v<T, uintptr_t>, void>
170 bitwiseAndRangeWith(size_t wordStart, size_t numWords, T* source) {
171 size_t blockWord = blockStartWord(wordStart);
172
173 // We only support using a single bit block in this API.
174 MOZ_ASSERT(numWords &&
175 (blockWord == blockStartWord(wordStart + numWords - 1)));
176
177 BitBlock* block = getBlock(blockWord / WordsInBlock);
178 if (block) {
179 for (size_t i = 0; i < numWords; i++) {
180 (*block)[wordStart - blockWord + i] &= source[i];
181 }
182 }
183 }
184
185 template <typename T>
186 typename std::enable_if_t<std::is_convertible_v<T, uintptr_t>, void>
187 bitwiseOrRangeInto(size_t wordStart, size_t numWords, T* target) const {
188 size_t blockWord = blockStartWord(wordStart);
189
190 // We only support using a single bit block in this API.
191 MOZ_ASSERT(numWords &&
192 (blockWord == blockStartWord(wordStart + numWords - 1)));
193
194 BitBlock* block = getBlock(blockWord / WordsInBlock);
195 if (block) {
196 for (size_t i = 0; i < numWords; i++) {
197 target[i] |= (*block)[wordStart - blockWord + i];
198 }
199 }
200 }
201 };
202
203 } // namespace js
204
205 #endif // ds_Bitmap_h