Debian Codegraph @

js/src/ds/Bitmap.cpp

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 #include "ds/Bitmap.h"
8
9 #include <algorithm>
10
11 #include "js/UniquePtr.h"
12
13 using namespace js;
14
15 SparseBitmap::~SparseBitmap() {
16 for (Data::Range r(data.all()); !r.empty(); r.popFront()) {
17 js_delete(r.front().value());
18 }
19 }
20
21 size_t SparseBitmap::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
22 size_t size = data.shallowSizeOfExcludingThis(mallocSizeOf);
23 for (Data::Range r(data.all()); !r.empty(); r.popFront()) {
24 size += mallocSizeOf(r.front().value());
25 }
26 return size;
27 }
28
29 SparseBitmap::BitBlock* SparseBitmap::createBlock(Data::AddPtr p,
30 size_t blockId) {
31 MOZ_ASSERT(!p);
32 auto block = js::MakeUnique<BitBlock>();
33 if (!block || !data.add(p, blockId, block.get())) {
34 return nullptr;
35 }
36 std::fill(block->begin(), block->end(), 0);
37 return block.release();
38 }
39
40 SparseBitmap::BitBlock& SparseBitmap::createBlock(
41 Data::AddPtr p, size_t blockId, AutoEnterOOMUnsafeRegion& oomUnsafe) {
42 BitBlock* block = createBlock(p, blockId);
43 if (!block) {
44 oomUnsafe.crash("Bitmap OOM");
45 }
46 return *block;
47 }
48
49 bool SparseBitmap::getBit(size_t bit) const {
50 size_t word = bit / JS_BITS_PER_WORD;
51 size_t blockWord = blockStartWord(word);
52
53 const BitBlock* block = getBlock(blockWord / WordsInBlock);
54 if (block) {
55 return (*block)[word - blockWord] & bitMask(bit);
56 }
57 return false;
58 }
59
60 bool SparseBitmap::readonlyThreadsafeGetBit(size_t bit) const {
61 size_t word = bit / JS_BITS_PER_WORD;
62 size_t blockWord = blockStartWord(word);
63
64 const BitBlock* block = readonlyThreadsafeGetBlock(blockWord / WordsInBlock);
65 if (block) {
66 return (*block)[word - blockWord] & bitMask(bit);
67 }
68 return false;
69 }
70
71 void SparseBitmap::bitwiseAndWith(const DenseBitmap& other) {
72 for (Data::Enum e(data); !e.empty(); e.popFront()) {
73 BitBlock& block = *e.front().value();
74 size_t blockWord = e.front().key() * WordsInBlock;
75 bool anySet = false;
76 size_t numWords = wordIntersectCount(blockWord, other);
77 for (size_t i = 0; i < numWords; i++) {
78 block[i] &= other.word(blockWord + i);
79 anySet |= !!block[i];
80 }
81 if (!anySet) {
82 js_delete(&block);
83 e.removeFront();
84 }
85 }
86 }
87
88 void SparseBitmap::bitwiseOrWith(const SparseBitmap& other) {
89 for (Data::Range r(other.data.all()); !r.empty(); r.popFront()) {
90 const BitBlock& otherBlock = *r.front().value();
91 BitBlock& block = getOrCreateBlock(r.front().key());
92 for (size_t i = 0; i < WordsInBlock; i++) {
93 block[i] |= otherBlock[i];
94 }
95 }
96 }
97
98 void SparseBitmap::bitwiseOrInto(DenseBitmap& other) const {
99 for (Data::Range r(data.all()); !r.empty(); r.popFront()) {
100 BitBlock& block = *r.front().value();
101 size_t blockWord = r.front().key() * WordsInBlock;
102 size_t numWords = wordIntersectCount(blockWord, other);
103 #ifdef DEBUG
104 // Any words out of range in other should be zero in this bitmap.
105 for (size_t i = numWords; i < WordsInBlock; i++) {
106 MOZ_ASSERT(!block[i]);
107 }
108 #endif
109 for (size_t i = 0; i < numWords; i++) {
110 other.word(blockWord + i) |= block[i];
111 }
112 }
113 }