mfbt/double-conversion/utils.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 // Copyright 2010 the V8 project authors. All rights reserved.
michael@0 2 // Redistribution and use in source and binary forms, with or without
michael@0 3 // modification, are permitted provided that the following conditions are
michael@0 4 // met:
michael@0 5 //
michael@0 6 // * Redistributions of source code must retain the above copyright
michael@0 7 // notice, this list of conditions and the following disclaimer.
michael@0 8 // * Redistributions in binary form must reproduce the above
michael@0 9 // copyright notice, this list of conditions and the following
michael@0 10 // disclaimer in the documentation and/or other materials provided
michael@0 11 // with the distribution.
michael@0 12 // * Neither the name of Google Inc. nor the names of its
michael@0 13 // contributors may be used to endorse or promote products derived
michael@0 14 // from this software without specific prior written permission.
michael@0 15 //
michael@0 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 27
michael@0 28 #ifndef DOUBLE_CONVERSION_UTILS_H_
michael@0 29 #define DOUBLE_CONVERSION_UTILS_H_
michael@0 30
michael@0 31 #include <stdlib.h>
michael@0 32 #include <string.h>
michael@0 33
michael@0 34 #include "mozilla/Assertions.h"
michael@0 35 #ifndef ASSERT
michael@0 36 #define ASSERT(condition) MOZ_ASSERT(condition)
michael@0 37 #endif
michael@0 38 #ifndef UNIMPLEMENTED
michael@0 39 #define UNIMPLEMENTED() MOZ_CRASH()
michael@0 40 #endif
michael@0 41 #ifndef UNREACHABLE
michael@0 42 #define UNREACHABLE() MOZ_CRASH()
michael@0 43 #endif
michael@0 44
michael@0 45 // Double operations detection based on target architecture.
michael@0 46 // Linux uses a 80bit wide floating point stack on x86. This induces double
michael@0 47 // rounding, which in turn leads to wrong results.
michael@0 48 // An easy way to test if the floating-point operations are correct is to
michael@0 49 // evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then
michael@0 50 // the result is equal to 89255e-22.
michael@0 51 // The best way to test this, is to create a division-function and to compare
michael@0 52 // the output of the division with the expected result. (Inlining must be
michael@0 53 // disabled.)
michael@0 54 // On Linux,x86 89255e-22 != Div_double(89255.0/1e22)
michael@0 55 #if defined(_M_X64) || defined(__x86_64__) || \
michael@0 56 defined(__ARMEL__) || defined(__avr32__) || \
michael@0 57 defined(__hppa__) || defined(__ia64__) || \
michael@0 58 defined(__mips__) || \
michael@0 59 defined(__powerpc__) || defined(__ppc__) || defined(__ppc64__) || \
michael@0 60 defined(__sparc__) || defined(__sparc) || defined(__s390__) || \
michael@0 61 defined(__SH4__) || defined(__alpha__) || \
michael@0 62 defined(_MIPS_ARCH_MIPS32R2) || \
michael@0 63 defined(__AARCH64EL__)
michael@0 64 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
michael@0 65 #elif defined(_M_IX86) || defined(__i386__) || defined(__i386)
michael@0 66 #if defined(_WIN32)
michael@0 67 // Windows uses a 64bit wide floating point stack.
michael@0 68 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
michael@0 69 #else
michael@0 70 #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
michael@0 71 #endif // _WIN32
michael@0 72 #else
michael@0 73 #error Target architecture was not detected as supported by Double-Conversion.
michael@0 74 #endif
michael@0 75
michael@0 76
michael@0 77 #include <stdint.h>
michael@0 78
michael@0 79 // The following macro works on both 32 and 64-bit platforms.
michael@0 80 // Usage: instead of writing 0x1234567890123456
michael@0 81 // write UINT64_2PART_C(0x12345678,90123456);
michael@0 82 #define UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
michael@0 83
michael@0 84
michael@0 85 // The expression ARRAY_SIZE(a) is a compile-time constant of type
michael@0 86 // size_t which represents the number of elements of the given
michael@0 87 // array. You should only use ARRAY_SIZE on statically allocated
michael@0 88 // arrays.
michael@0 89 #ifndef ARRAY_SIZE
michael@0 90 #define ARRAY_SIZE(a) \
michael@0 91 ((sizeof(a) / sizeof(*(a))) / \
michael@0 92 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
michael@0 93 #endif
michael@0 94
michael@0 95 // A macro to disallow the evil copy constructor and operator= functions
michael@0 96 // This should be used in the private: declarations for a class
michael@0 97 #ifndef DISALLOW_COPY_AND_ASSIGN
michael@0 98 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
michael@0 99 TypeName(const TypeName&); \
michael@0 100 void operator=(const TypeName&)
michael@0 101 #endif
michael@0 102
michael@0 103 // A macro to disallow all the implicit constructors, namely the
michael@0 104 // default constructor, copy constructor and operator= functions.
michael@0 105 //
michael@0 106 // This should be used in the private: declarations for a class
michael@0 107 // that wants to prevent anyone from instantiating it. This is
michael@0 108 // especially useful for classes containing only static methods.
michael@0 109 #ifndef DISALLOW_IMPLICIT_CONSTRUCTORS
michael@0 110 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
michael@0 111 TypeName(); \
michael@0 112 DISALLOW_COPY_AND_ASSIGN(TypeName)
michael@0 113 #endif
michael@0 114
michael@0 115 namespace double_conversion {
michael@0 116
michael@0 117 static const int kCharSize = sizeof(char);
michael@0 118
michael@0 119 // Returns the maximum of the two parameters.
michael@0 120 template <typename T>
michael@0 121 static T Max(T a, T b) {
michael@0 122 return a < b ? b : a;
michael@0 123 }
michael@0 124
michael@0 125
michael@0 126 // Returns the minimum of the two parameters.
michael@0 127 template <typename T>
michael@0 128 static T Min(T a, T b) {
michael@0 129 return a < b ? a : b;
michael@0 130 }
michael@0 131
michael@0 132
michael@0 133 inline int StrLength(const char* string) {
michael@0 134 size_t length = strlen(string);
michael@0 135 ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
michael@0 136 return static_cast<int>(length);
michael@0 137 }
michael@0 138
michael@0 139 // This is a simplified version of V8's Vector class.
michael@0 140 template <typename T>
michael@0 141 class Vector {
michael@0 142 public:
michael@0 143 Vector() : start_(NULL), length_(0) {}
michael@0 144 Vector(T* data, int length) : start_(data), length_(length) {
michael@0 145 ASSERT(length == 0 || (length > 0 && data != NULL));
michael@0 146 }
michael@0 147
michael@0 148 // Returns a vector using the same backing storage as this one,
michael@0 149 // spanning from and including 'from', to but not including 'to'.
michael@0 150 Vector<T> SubVector(int from, int to) {
michael@0 151 ASSERT(to <= length_);
michael@0 152 ASSERT(from < to);
michael@0 153 ASSERT(0 <= from);
michael@0 154 return Vector<T>(start() + from, to - from);
michael@0 155 }
michael@0 156
michael@0 157 // Returns the length of the vector.
michael@0 158 int length() const { return length_; }
michael@0 159
michael@0 160 // Returns whether or not the vector is empty.
michael@0 161 bool is_empty() const { return length_ == 0; }
michael@0 162
michael@0 163 // Returns the pointer to the start of the data in the vector.
michael@0 164 T* start() const { return start_; }
michael@0 165
michael@0 166 // Access individual vector elements - checks bounds in debug mode.
michael@0 167 T& operator[](int index) const {
michael@0 168 ASSERT(0 <= index && index < length_);
michael@0 169 return start_[index];
michael@0 170 }
michael@0 171
michael@0 172 T& first() { return start_[0]; }
michael@0 173
michael@0 174 T& last() { return start_[length_ - 1]; }
michael@0 175
michael@0 176 private:
michael@0 177 T* start_;
michael@0 178 int length_;
michael@0 179 };
michael@0 180
michael@0 181
michael@0 182 // Helper class for building result strings in a character buffer. The
michael@0 183 // purpose of the class is to use safe operations that checks the
michael@0 184 // buffer bounds on all operations in debug mode.
michael@0 185 class StringBuilder {
michael@0 186 public:
michael@0 187 StringBuilder(char* buffer, int size)
michael@0 188 : buffer_(buffer, size), position_(0) { }
michael@0 189
michael@0 190 ~StringBuilder() { if (!is_finalized()) Finalize(); }
michael@0 191
michael@0 192 int size() const { return buffer_.length(); }
michael@0 193
michael@0 194 // Get the current position in the builder.
michael@0 195 int position() const {
michael@0 196 ASSERT(!is_finalized());
michael@0 197 return position_;
michael@0 198 }
michael@0 199
michael@0 200 // Reset the position.
michael@0 201 void Reset() { position_ = 0; }
michael@0 202
michael@0 203 // Add a single character to the builder. It is not allowed to add
michael@0 204 // 0-characters; use the Finalize() method to terminate the string
michael@0 205 // instead.
michael@0 206 void AddCharacter(char c) {
michael@0 207 ASSERT(c != '\0');
michael@0 208 ASSERT(!is_finalized() && position_ < buffer_.length());
michael@0 209 buffer_[position_++] = c;
michael@0 210 }
michael@0 211
michael@0 212 // Add an entire string to the builder. Uses strlen() internally to
michael@0 213 // compute the length of the input string.
michael@0 214 void AddString(const char* s) {
michael@0 215 AddSubstring(s, StrLength(s));
michael@0 216 }
michael@0 217
michael@0 218 // Add the first 'n' characters of the given string 's' to the
michael@0 219 // builder. The input string must have enough characters.
michael@0 220 void AddSubstring(const char* s, int n) {
michael@0 221 ASSERT(!is_finalized() && position_ + n < buffer_.length());
michael@0 222 ASSERT(static_cast<size_t>(n) <= strlen(s));
michael@0 223 memmove(&buffer_[position_], s, n * kCharSize);
michael@0 224 position_ += n;
michael@0 225 }
michael@0 226
michael@0 227
michael@0 228 // Add character padding to the builder. If count is non-positive,
michael@0 229 // nothing is added to the builder.
michael@0 230 void AddPadding(char c, int count) {
michael@0 231 for (int i = 0; i < count; i++) {
michael@0 232 AddCharacter(c);
michael@0 233 }
michael@0 234 }
michael@0 235
michael@0 236 // Finalize the string by 0-terminating it and returning the buffer.
michael@0 237 char* Finalize() {
michael@0 238 ASSERT(!is_finalized() && position_ < buffer_.length());
michael@0 239 buffer_[position_] = '\0';
michael@0 240 // Make sure nobody managed to add a 0-character to the
michael@0 241 // buffer while building the string.
michael@0 242 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
michael@0 243 position_ = -1;
michael@0 244 ASSERT(is_finalized());
michael@0 245 return buffer_.start();
michael@0 246 }
michael@0 247
michael@0 248 private:
michael@0 249 Vector<char> buffer_;
michael@0 250 int position_;
michael@0 251
michael@0 252 bool is_finalized() const { return position_ < 0; }
michael@0 253
michael@0 254 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
michael@0 255 };
michael@0 256
michael@0 257 // The type-based aliasing rule allows the compiler to assume that pointers of
michael@0 258 // different types (for some definition of different) never alias each other.
michael@0 259 // Thus the following code does not work:
michael@0 260 //
michael@0 261 // float f = foo();
michael@0 262 // int fbits = *(int*)(&f);
michael@0 263 //
michael@0 264 // The compiler 'knows' that the int pointer can't refer to f since the types
michael@0 265 // don't match, so the compiler may cache f in a register, leaving random data
michael@0 266 // in fbits. Using C++ style casts makes no difference, however a pointer to
michael@0 267 // char data is assumed to alias any other pointer. This is the 'memcpy
michael@0 268 // exception'.
michael@0 269 //
michael@0 270 // Bit_cast uses the memcpy exception to move the bits from a variable of one
michael@0 271 // type of a variable of another type. Of course the end result is likely to
michael@0 272 // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
michael@0 273 // will completely optimize BitCast away.
michael@0 274 //
michael@0 275 // There is an additional use for BitCast.
michael@0 276 // Recent gccs will warn when they see casts that may result in breakage due to
michael@0 277 // the type-based aliasing rule. If you have checked that there is no breakage
michael@0 278 // you can use BitCast to cast one pointer type to another. This confuses gcc
michael@0 279 // enough that it can no longer see that you have cast one pointer type to
michael@0 280 // another thus avoiding the warning.
michael@0 281 template <class Dest, class Source>
michael@0 282 inline Dest BitCast(const Source& source) {
michael@0 283 static_assert(sizeof(Dest) == sizeof(Source),
michael@0 284 "BitCast's source and destination types must be the same size");
michael@0 285
michael@0 286 Dest dest;
michael@0 287 memmove(&dest, &source, sizeof(dest));
michael@0 288 return dest;
michael@0 289 }
michael@0 290
michael@0 291 template <class Dest, class Source>
michael@0 292 inline Dest BitCast(Source* source) {
michael@0 293 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
michael@0 294 }
michael@0 295
michael@0 296 } // namespace double_conversion
michael@0 297
michael@0 298 #endif // DOUBLE_CONVERSION_UTILS_H_

mercurial