js/src/ds/BitArray.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef ds_BitArray_h
michael@0 8 #define ds_BitArray_h
michael@0 9
michael@0 10 #include "mozilla/TemplateLib.h"
michael@0 11
michael@0 12 #include <limits.h>
michael@0 13
michael@0 14 #include "jstypes.h"
michael@0 15
michael@0 16 namespace js {
michael@0 17
michael@0 18 template <size_t nbits>
michael@0 19 class BitArray
michael@0 20 {
michael@0 21 private:
michael@0 22 static const size_t bitsPerElement = sizeof(uintptr_t) * CHAR_BIT;
michael@0 23 static const size_t numSlots = nbits / bitsPerElement + (nbits % bitsPerElement == 0 ? 0 : 1);
michael@0 24 static const size_t paddingBits = (numSlots * bitsPerElement) - nbits;
michael@0 25 static_assert(paddingBits < bitsPerElement, "More padding bits than expected.");
michael@0 26 static const uintptr_t paddingMask = uintptr_t(-1) >> paddingBits;
michael@0 27
michael@0 28 uintptr_t map[numSlots];
michael@0 29
michael@0 30 public:
michael@0 31 void clear(bool value) {
michael@0 32 memset(map, value ? 0xFF : 0, sizeof(map));
michael@0 33 if (value)
michael@0 34 map[numSlots - 1] &= paddingMask;
michael@0 35 }
michael@0 36
michael@0 37 inline bool get(size_t offset) const {
michael@0 38 uintptr_t index, mask;
michael@0 39 getMarkWordAndMask(offset, &index, &mask);
michael@0 40 return map[index] & mask;
michael@0 41 }
michael@0 42
michael@0 43 void set(size_t offset) {
michael@0 44 uintptr_t index, mask;
michael@0 45 getMarkWordAndMask(offset, &index, &mask);
michael@0 46 map[index] |= mask;
michael@0 47 }
michael@0 48
michael@0 49 void unset(size_t offset) {
michael@0 50 uintptr_t index, mask;
michael@0 51 getMarkWordAndMask(offset, &index, &mask);
michael@0 52 map[index] &= ~mask;
michael@0 53 }
michael@0 54
michael@0 55 bool isAllClear() const {
michael@0 56 for (size_t i = 0; i < numSlots; i++) {
michael@0 57 if (map[i])
michael@0 58 return false;
michael@0 59 }
michael@0 60 return true;
michael@0 61 }
michael@0 62
michael@0 63 private:
michael@0 64 inline void getMarkWordAndMask(size_t offset,
michael@0 65 uintptr_t *indexp, uintptr_t *maskp) const {
michael@0 66 static_assert(bitsPerElement == 32 || bitsPerElement == 64,
michael@0 67 "unexpected bitsPerElement value");
michael@0 68 *indexp = offset / bitsPerElement;
michael@0 69 *maskp = uintptr_t(1) << (offset % bitsPerElement);
michael@0 70 }
michael@0 71 };
michael@0 72
michael@0 73 } /* namespace js */
michael@0 74
michael@0 75 #endif /* ds_BitArray_h */

mercurial