mfbt/TemplateLib.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 /*
michael@0 8 * Reusable template meta-functions on types and compile-time values. Meta-
michael@0 9 * functions are placed inside the 'tl' namespace to avoid conflict with non-
michael@0 10 * meta functions of the same name (e.g., mozilla::tl::FloorLog2 vs.
michael@0 11 * mozilla::FloorLog2).
michael@0 12 *
michael@0 13 * When constexpr support becomes universal, we should probably use that instead
michael@0 14 * of some of these templates, for simplicity.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef mozilla_TemplateLib_h
michael@0 18 #define mozilla_TemplateLib_h
michael@0 19
michael@0 20 #include <limits.h>
michael@0 21 #include <stddef.h>
michael@0 22
michael@0 23 namespace mozilla {
michael@0 24
michael@0 25 namespace tl {
michael@0 26
michael@0 27 /** Compute min/max. */
michael@0 28 template<size_t I, size_t J>
michael@0 29 struct Min
michael@0 30 {
michael@0 31 static const size_t value = I < J ? I : J;
michael@0 32 };
michael@0 33 template<size_t I, size_t J>
michael@0 34 struct Max
michael@0 35 {
michael@0 36 static const size_t value = I > J ? I : J;
michael@0 37 };
michael@0 38
michael@0 39 /** Compute floor(log2(i)). */
michael@0 40 template<size_t I>
michael@0 41 struct FloorLog2
michael@0 42 {
michael@0 43 static const size_t value = 1 + FloorLog2<I / 2>::value;
michael@0 44 };
michael@0 45 template<> struct FloorLog2<0> { /* Error */ };
michael@0 46 template<> struct FloorLog2<1> { static const size_t value = 0; };
michael@0 47
michael@0 48 /** Compute ceiling(log2(i)). */
michael@0 49 template<size_t I>
michael@0 50 struct CeilingLog2
michael@0 51 {
michael@0 52 static const size_t value = FloorLog2<2 * I - 1>::value;
michael@0 53 };
michael@0 54
michael@0 55 /** Round up to the nearest power of 2. */
michael@0 56 template<size_t I>
michael@0 57 struct RoundUpPow2
michael@0 58 {
michael@0 59 static const size_t value = size_t(1) << CeilingLog2<I>::value;
michael@0 60 };
michael@0 61 template<>
michael@0 62 struct RoundUpPow2<0>
michael@0 63 {
michael@0 64 static const size_t value = 1;
michael@0 65 };
michael@0 66
michael@0 67 /** Compute the number of bits in the given unsigned type. */
michael@0 68 template<typename T>
michael@0 69 struct BitSize
michael@0 70 {
michael@0 71 static const size_t value = sizeof(T) * CHAR_BIT;
michael@0 72 };
michael@0 73
michael@0 74 /**
michael@0 75 * Produce an N-bit mask, where N <= BitSize<size_t>::value. Handle the
michael@0 76 * language-undefined edge case when N = BitSize<size_t>::value.
michael@0 77 */
michael@0 78 template<size_t N>
michael@0 79 struct NBitMask
michael@0 80 {
michael@0 81 // Assert the precondition. On success this evaluates to 0. Otherwise it
michael@0 82 // triggers divide-by-zero at compile time: a guaranteed compile error in
michael@0 83 // C++11, and usually one in C++98. Add this value to |value| to assure
michael@0 84 // its computation.
michael@0 85 static const size_t checkPrecondition = 0 / size_t(N < BitSize<size_t>::value);
michael@0 86 static const size_t value = (size_t(1) << N) - 1 + checkPrecondition;
michael@0 87 };
michael@0 88 template<>
michael@0 89 struct NBitMask<BitSize<size_t>::value>
michael@0 90 {
michael@0 91 static const size_t value = size_t(-1);
michael@0 92 };
michael@0 93
michael@0 94 /**
michael@0 95 * For the unsigned integral type size_t, compute a mask M for N such that
michael@0 96 * for all X, !(X & M) implies X * N will not overflow (w.r.t size_t)
michael@0 97 */
michael@0 98 template<size_t N>
michael@0 99 struct MulOverflowMask
michael@0 100 {
michael@0 101 static const size_t value =
michael@0 102 ~NBitMask<BitSize<size_t>::value - CeilingLog2<N>::value>::value;
michael@0 103 };
michael@0 104 template<> struct MulOverflowMask<0> { /* Error */ };
michael@0 105 template<> struct MulOverflowMask<1> { static const size_t value = 0; };
michael@0 106
michael@0 107 } // namespace tl
michael@0 108
michael@0 109 } // namespace mozilla
michael@0 110
michael@0 111 #endif /* mozilla_TemplateLib_h */

mercurial