summaryrefslogtreecommitdiffstats
path: root/js/src/ds/Bitmap.cpp
blob: d96cf67c71162fce09ed9f29d35192da1c40e9b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ds/Bitmap.h"

#include <algorithm>

#include "js/UniquePtr.h"

using namespace js;

SparseBitmap::~SparseBitmap() {
  for (Data::Range r(data.all()); !r.empty(); r.popFront()) {
    js_delete(r.front().value());
  }
}

size_t SparseBitmap::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
  size_t size = data.shallowSizeOfExcludingThis(mallocSizeOf);
  for (Data::Range r(data.all()); !r.empty(); r.popFront()) {
    size += mallocSizeOf(r.front().value());
  }
  return size;
}

SparseBitmap::BitBlock* SparseBitmap::createBlock(Data::AddPtr p,
                                                  size_t blockId) {
  MOZ_ASSERT(!p);
  auto block = js::MakeUnique<BitBlock>();
  if (!block || !data.add(p, blockId, block.get())) {
    return nullptr;
  }
  std::fill(block->begin(), block->end(), 0);
  return block.release();
}

SparseBitmap::BitBlock& SparseBitmap::createBlock(
    Data::AddPtr p, size_t blockId, AutoEnterOOMUnsafeRegion& oomUnsafe) {
  BitBlock* block = createBlock(p, blockId);
  if (!block) {
    oomUnsafe.crash("Bitmap OOM");
  }
  return *block;
}

bool SparseBitmap::getBit(size_t bit) const {
  size_t word = bit / JS_BITS_PER_WORD;
  size_t blockWord = blockStartWord(word);

  const BitBlock* block = getBlock(blockWord / WordsInBlock);
  if (block) {
    return (*block)[word - blockWord] & bitMask(bit);
  }
  return false;
}

bool SparseBitmap::readonlyThreadsafeGetBit(size_t bit) const {
  size_t word = bit / JS_BITS_PER_WORD;
  size_t blockWord = blockStartWord(word);

  const BitBlock* block = readonlyThreadsafeGetBlock(blockWord / WordsInBlock);
  if (block) {
    return (*block)[word - blockWord] & bitMask(bit);
  }
  return false;
}

void SparseBitmap::bitwiseAndWith(const DenseBitmap& other) {
  for (Data::Enum e(data); !e.empty(); e.popFront()) {
    BitBlock& block = *e.front().value();
    size_t blockWord = e.front().key() * WordsInBlock;
    bool anySet = false;
    size_t numWords = wordIntersectCount(blockWord, other);
    for (size_t i = 0; i < numWords; i++) {
      block[i] &= other.word(blockWord + i);
      anySet |= !!block[i];
    }
    if (!anySet) {
      js_delete(&block);
      e.removeFront();
    }
  }
}

void SparseBitmap::bitwiseOrWith(const SparseBitmap& other) {
  for (Data::Range r(other.data.all()); !r.empty(); r.popFront()) {
    const BitBlock& otherBlock = *r.front().value();
    BitBlock& block = getOrCreateBlock(r.front().key());
    for (size_t i = 0; i < WordsInBlock; i++) {
      block[i] |= otherBlock[i];
    }
  }
}

void SparseBitmap::bitwiseOrInto(DenseBitmap& other) const {
  for (Data::Range r(data.all()); !r.empty(); r.popFront()) {
    BitBlock& block = *r.front().value();
    size_t blockWord = r.front().key() * WordsInBlock;
    size_t numWords = wordIntersectCount(blockWord, other);
#ifdef DEBUG
    // Any words out of range in other should be zero in this bitmap.
    for (size_t i = numWords; i < WordsInBlock; i++) {
      MOZ_ASSERT(!block[i]);
    }
#endif
    for (size_t i = 0; i < numWords; i++) {
      other.word(blockWord + i) |= block[i];
    }
  }
}