js/src/ds/BitArray.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * vim: set ts=8 sts=4 et sw=4 tw=99:
     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/. */
     7 #ifndef ds_BitArray_h
     8 #define ds_BitArray_h
    10 #include "mozilla/TemplateLib.h"
    12 #include <limits.h>
    14 #include "jstypes.h"
    16 namespace js {
    18 template <size_t nbits>
    19 class BitArray
    20 {
    21   private:
    22     static const size_t bitsPerElement = sizeof(uintptr_t) * CHAR_BIT;
    23     static const size_t numSlots = nbits / bitsPerElement + (nbits % bitsPerElement == 0 ? 0 : 1);
    24     static const size_t paddingBits = (numSlots * bitsPerElement) - nbits;
    25     static_assert(paddingBits < bitsPerElement, "More padding bits than expected.");
    26     static const uintptr_t paddingMask = uintptr_t(-1) >> paddingBits;
    28     uintptr_t map[numSlots];
    30   public:
    31     void clear(bool value) {
    32         memset(map, value ? 0xFF : 0, sizeof(map));
    33         if (value)
    34             map[numSlots - 1] &= paddingMask;
    35     }
    37     inline bool get(size_t offset) const {
    38         uintptr_t index, mask;
    39         getMarkWordAndMask(offset, &index, &mask);
    40         return map[index] & mask;
    41     }
    43     void set(size_t offset) {
    44         uintptr_t index, mask;
    45         getMarkWordAndMask(offset, &index, &mask);
    46         map[index] |= mask;
    47     }
    49     void unset(size_t offset) {
    50         uintptr_t index, mask;
    51         getMarkWordAndMask(offset, &index, &mask);
    52         map[index] &= ~mask;
    53     }
    55     bool isAllClear() const {
    56         for (size_t i = 0; i < numSlots; i++) {
    57             if (map[i])
    58                 return false;
    59         }
    60         return true;
    61     }
    63   private:
    64     inline void getMarkWordAndMask(size_t offset,
    65                                    uintptr_t *indexp, uintptr_t *maskp) const {
    66         static_assert(bitsPerElement == 32 || bitsPerElement == 64,
    67                       "unexpected bitsPerElement value");
    68         *indexp = offset / bitsPerElement;
    69         *maskp = uintptr_t(1) << (offset % bitsPerElement);
    70     }
    71 };
    73 } /* namespace js */
    75 #endif /* ds_BitArray_h */

mercurial