Home | History | Annotate | Download | only in scudo
      1 //===-- scudo_utils.h -------------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 ///
     10 /// Header for scudo_utils.cpp.
     11 ///
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef SCUDO_UTILS_H_
     15 #define SCUDO_UTILS_H_
     16 
     17 #include <string.h>
     18 
     19 #include "sanitizer_common/sanitizer_common.h"
     20 
     21 namespace __scudo {
     22 
     23 template <class Dest, class Source>
     24 inline Dest bit_cast(const Source& source) {
     25   static_assert(sizeof(Dest) == sizeof(Source), "Sizes are not equal!");
     26   Dest dest;
     27   memcpy(&dest, &source, sizeof(dest));
     28   return dest;
     29 }
     30 
     31 void dieWithMessage(const char *Format, ...);
     32 
     33 enum  CPUFeature {
     34   SSE4_2 = 0,
     35   ENUM_CPUFEATURE_MAX
     36 };
     37 bool testCPUFeature(CPUFeature feature);
     38 
     39 // Tiny PRNG based on https://en.wikipedia.org/wiki/Xorshift#xorshift.2B
     40 // The state (128 bits) will be stored in thread local storage.
     41 struct Xorshift128Plus {
     42  public:
     43   Xorshift128Plus();
     44   u64 Next() {
     45     u64 x = State_0_;
     46     const u64 y = State_1_;
     47     State_0_ = y;
     48     x ^= x << 23;
     49     State_1_ = x ^ y ^ (x >> 17) ^ (y >> 26);
     50     return State_1_ + y;
     51   }
     52  private:
     53   u64 State_0_;
     54   u64 State_1_;
     55 };
     56 
     57 } // namespace __scudo
     58 
     59 #endif  // SCUDO_UTILS_H_
     60