mfbt/IntegerTypeTraits.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 /* Helpers to manipulate integer types that don't fit in TypeTraits.h */
michael@0 8
michael@0 9 #ifndef mozilla_IntegerTypeTraits_h
michael@0 10 #define mozilla_IntegerTypeTraits_h
michael@0 11
michael@0 12 #include "mozilla/TypeTraits.h"
michael@0 13 #include <stdint.h>
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16
michael@0 17 namespace detail {
michael@0 18
michael@0 19 /**
michael@0 20 * StdintTypeForSizeAndSignedness returns the stdint integer type
michael@0 21 * of given size (can be 1, 2, 4 or 8) and given signedness
michael@0 22 * (false means unsigned, true means signed).
michael@0 23 */
michael@0 24 template<size_t Size, bool Signedness>
michael@0 25 struct StdintTypeForSizeAndSignedness;
michael@0 26
michael@0 27 template<>
michael@0 28 struct StdintTypeForSizeAndSignedness<1, true>
michael@0 29 { typedef int8_t Type; };
michael@0 30
michael@0 31 template<>
michael@0 32 struct StdintTypeForSizeAndSignedness<1, false>
michael@0 33 { typedef uint8_t Type; };
michael@0 34
michael@0 35 template<>
michael@0 36 struct StdintTypeForSizeAndSignedness<2, true>
michael@0 37 { typedef int16_t Type; };
michael@0 38
michael@0 39 template<>
michael@0 40 struct StdintTypeForSizeAndSignedness<2, false>
michael@0 41 { typedef uint16_t Type; };
michael@0 42
michael@0 43 template<>
michael@0 44 struct StdintTypeForSizeAndSignedness<4, true>
michael@0 45 { typedef int32_t Type; };
michael@0 46
michael@0 47 template<>
michael@0 48 struct StdintTypeForSizeAndSignedness<4, false>
michael@0 49 { typedef uint32_t Type; };
michael@0 50
michael@0 51 template<>
michael@0 52 struct StdintTypeForSizeAndSignedness<8, true>
michael@0 53 { typedef int64_t Type; };
michael@0 54
michael@0 55 template<>
michael@0 56 struct StdintTypeForSizeAndSignedness<8, false>
michael@0 57 { typedef uint64_t Type; };
michael@0 58
michael@0 59 } // namespace detail
michael@0 60
michael@0 61 template<size_t Size>
michael@0 62 struct UnsignedStdintTypeForSize
michael@0 63 : detail::StdintTypeForSizeAndSignedness<Size, false>
michael@0 64 {};
michael@0 65
michael@0 66 template<typename IntegerType>
michael@0 67 struct PositionOfSignBit
michael@0 68 {
michael@0 69 static_assert(IsIntegral<IntegerType>::value, "PositionOfSignBit is only for integral types");
michael@0 70 // 8 here should be CHAR_BIT from limits.h, but the world has moved on.
michael@0 71 static const size_t value = 8 * sizeof(IntegerType) - 1;
michael@0 72 };
michael@0 73
michael@0 74 /**
michael@0 75 * MinValue returns the minimum value of the given integer type as a
michael@0 76 * compile-time constant, which std::numeric_limits<IntegerType>::min()
michael@0 77 * cannot do in c++98.
michael@0 78 */
michael@0 79 template<typename IntegerType>
michael@0 80 struct MinValue
michael@0 81 {
michael@0 82 private:
michael@0 83 static_assert(IsIntegral<IntegerType>::value, "MinValue is only for integral types");
michael@0 84
michael@0 85 typedef typename MakeUnsigned<IntegerType>::Type UnsignedIntegerType;
michael@0 86 static const size_t PosOfSignBit = PositionOfSignBit<IntegerType>::value;
michael@0 87
michael@0 88 public:
michael@0 89 // Bitwise ops may return a larger type, that's why we cast explicitly.
michael@0 90 // In C++, left bit shifts on signed values is undefined by the standard
michael@0 91 // unless the shifted value is representable.
michael@0 92 // Notice that signed-to-unsigned conversions are always well-defined in
michael@0 93 // the standard as the value congruent to 2**n, as expected. By contrast,
michael@0 94 // unsigned-to-signed is only well-defined if the value is representable.
michael@0 95 static const IntegerType value =
michael@0 96 IsSigned<IntegerType>::value
michael@0 97 ? IntegerType(UnsignedIntegerType(1) << PosOfSignBit)
michael@0 98 : IntegerType(0);
michael@0 99 };
michael@0 100
michael@0 101 /**
michael@0 102 * MaxValue returns the maximum value of the given integer type as a
michael@0 103 * compile-time constant, which std::numeric_limits<IntegerType>::max()
michael@0 104 * cannot do in c++98.
michael@0 105 */
michael@0 106 template<typename IntegerType>
michael@0 107 struct MaxValue
michael@0 108 {
michael@0 109 static_assert(IsIntegral<IntegerType>::value, "MaxValue is only for integral types");
michael@0 110
michael@0 111 // Tricksy, but covered by the CheckedInt unit test.
michael@0 112 // Relies on the type of MinValue<IntegerType>::value
michael@0 113 // being IntegerType.
michael@0 114 static const IntegerType value = ~MinValue<IntegerType>::value;
michael@0 115 };
michael@0 116
michael@0 117 } // namespace mozilla
michael@0 118
michael@0 119 #endif // mozilla_IntegerTypeTraits_h

mercurial