include/am-utility.h
author David Anderson <dvander@alliedmods.net>
Fri Sep 06 13:54:31 2013 -0700 (2013-09-06)
changeset 18 9b30f80f6e49
parent 10 0ac292cc1467
permissions -rw-r--r--
Eliminate Newborn/NoAddRef (bug 5907, r=ds).
     1 // vim: set sts=8 ts=2 sw=2 tw=99 et:
     2 //
     3 // Copyright (C) 2013, David Anderson and AlliedModders LLC
     4 // All rights reserved.
     5 // 
     6 // Redistribution and use in source and binary forms, with or without
     7 // modification, are permitted provided that the following conditions are met:
     8 // 
     9 //  * Redistributions of source code must retain the above copyright notice, this
    10 //    list of conditions and the following disclaimer.
    11 //  * Redistributions in binary form must reproduce the above copyright notice,
    12 //    this list of conditions and the following disclaimer in the documentation
    13 //    and/or other materials provided with the distribution.
    14 //  * Neither the name of AlliedModders LLC nor the names of its contributors
    15 //    may be used to endorse or promote products derived from this software
    16 //    without specific prior written permission.
    17 //
    18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    28 // POSSIBILITY OF SUCH DAMAGE.
    29 
    30 #ifndef _include_amtl_utility_h_
    31 #define _include_amtl_utility_h_
    32 
    33 #include <assert.h>
    34 #include <stddef.h>
    35 #include <stdlib.h>
    36 #include <stdint.h>
    37 #if defined(_MSC_VER)
    38 # include <intrin.h>
    39 #endif
    40 
    41 #define KE_32BIT
    42 
    43 #if defined(_MSC_VER)
    44 # pragma warning(disable:4355)
    45 #endif
    46 
    47 namespace ke {
    48 
    49 static const size_t kMallocAlignment = sizeof(void *) * 2;
    50 
    51 static const size_t kKB = 1024;
    52 static const size_t kMB = 1024 * kKB;
    53 static const size_t kGB = 1024 * kMB;
    54 
    55 typedef uint8_t * Address;
    56 
    57 template <typename T> T
    58 ReturnAndVoid(T &t)
    59 {
    60     T saved = t;
    61     t = T();
    62     return saved;
    63 }
    64 
    65 // Wrapper that automatically deletes its contents. The pointer can be taken
    66 // to avoid destruction.
    67 template <typename T>
    68 class AutoPtr
    69 {
    70     T *t_;
    71 
    72   public:
    73     AutoPtr()
    74       : t_(NULL)
    75     {
    76     }
    77     explicit AutoPtr(T *t)
    78       : t_(t)
    79     {
    80     }
    81     ~AutoPtr() {
    82         delete t_;
    83     }
    84     T *take() {
    85         return ReturnAndVoid(t_);
    86     }
    87     T *operator *() const {
    88         return t_;
    89     }
    90     T *operator ->() const {
    91         return t_;
    92     }
    93     operator T *() const {
    94         return t_;
    95     }
    96     void operator =(T *t) {
    97         delete t_;
    98         t_ = t;
    99     }
   100     bool operator !() const {
   101         return !t_;
   102     }
   103 };
   104 
   105 // Wrapper that automatically deletes its contents. The pointer can be taken
   106 // to avoid destruction.
   107 template <typename T>
   108 class AutoArray
   109 {
   110     T *t_;
   111 
   112   public:
   113     AutoArray()
   114       : t_(NULL)
   115     {
   116     }
   117     explicit AutoArray(T *t)
   118       : t_(t)
   119     {
   120     }
   121     ~AutoArray() {
   122         delete [] t_;
   123     }
   124     T *take() {
   125         return ReturnAndVoid(t_);
   126     }
   127     T *operator *() const {
   128         return t_;
   129     }
   130     T &operator [](size_t index) {
   131       return t_[index];
   132     }
   133     const T &operator [](size_t index) const {
   134       return t_[index];
   135     }
   136     operator T *() const {
   137         return t_;
   138     }
   139     void operator =(T *t) {
   140         delete [] t_;
   141         t_ = t;
   142     }
   143     bool operator !() const {
   144         return !t_;
   145     }
   146 };
   147 
   148 static inline size_t
   149 Log2(size_t number)
   150 {
   151     assert(number != 0);
   152 
   153 #ifdef _MSC_VER
   154     unsigned long rval;
   155 # ifdef _M_IX86
   156     _BitScanReverse(&rval, number);
   157 # elif _M_X64
   158     _BitScanReverse64(&rval, number);
   159 # endif
   160     return rval;
   161 #else
   162     size_t bit;
   163     asm("bsr %1, %0\n"
   164         : "=r" (bit)
   165         : "rm" (number));
   166     return bit;
   167 #endif
   168 }
   169 
   170 static inline size_t
   171 FindRightmostBit(size_t number)
   172 {
   173     assert(number != 0);
   174 
   175 #ifdef _MSC_VER
   176     unsigned long rval;
   177 # ifdef _M_IX86
   178     _BitScanForward(&rval, number);
   179 # elif _M_X64
   180     _BitScanForward64(&rval, number);
   181 # endif
   182     return rval;
   183 #else
   184     size_t bit;
   185     asm("bsf %1, %0\n"
   186         : "=r" (bit)
   187         : "rm" (number));
   188     return bit;
   189 #endif
   190 }
   191 
   192 static inline bool
   193 IsPowerOfTwo(size_t value)
   194 {
   195     if (value == 0)
   196         return false;
   197     return !(value & (value - 1));
   198 }
   199 
   200 static inline size_t
   201 Align(size_t count, size_t alignment)
   202 {
   203     assert(IsPowerOfTwo(alignment));
   204     return count + (alignment - (count % alignment)) % alignment;
   205 }
   206 
   207 static inline bool
   208 IsUint32AddSafe(unsigned a, unsigned b)
   209 {
   210     if (!a || !b)
   211         return true;
   212     size_t log2_a = Log2(a);
   213     size_t log2_b = Log2(b);
   214     return (log2_a < sizeof(unsigned) * 8) &&
   215            (log2_b < sizeof(unsigned) * 8);
   216 }
   217 
   218 static inline bool
   219 IsUintPtrAddSafe(size_t a, size_t b)
   220 {
   221     if (!a || !b)
   222         return true;
   223     size_t log2_a = Log2(a);
   224     size_t log2_b = Log2(b);
   225     return (log2_a < sizeof(size_t) * 8) &&
   226            (log2_b < sizeof(size_t) * 8);
   227 }
   228 
   229 static inline bool
   230 IsUint32MultiplySafe(unsigned a, unsigned b)
   231 {
   232     if (a <= 1 || b <= 1)
   233         return true;
   234 
   235     size_t log2_a = Log2(a);
   236     size_t log2_b = Log2(b);
   237     return log2_a + log2_b <= sizeof(unsigned) * 8;
   238 }
   239 
   240 static inline bool
   241 IsUintPtrMultiplySafe(size_t a, size_t b)
   242 {
   243     if (a <= 1 || b <= 1)
   244         return true;
   245 
   246     size_t log2_a = Log2(a);
   247     size_t log2_b = Log2(b);
   248     return log2_a + log2_b <= sizeof(size_t) * 8;
   249 }
   250 
   251 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
   252 #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
   253 
   254 #define IS_ALIGNED(addr, alignment)    (!(uintptr_t(addr) & ((alignment) - 1)))
   255 
   256 template <typename T>
   257 static inline bool
   258 IsAligned(T addr, size_t alignment)
   259 {
   260     assert(IsPowerOfTwo(alignment));
   261     return !(uintptr_t(addr) & (alignment - 1));
   262 }
   263 
   264 static inline Address
   265 AlignedBase(Address addr, size_t alignment)
   266 {
   267     assert(IsPowerOfTwo(alignment));
   268     return Address(uintptr_t(addr) & ~(alignment - 1));
   269 }
   270 
   271 template <typename T> static inline T
   272 Min(const T &t1, const T &t2)
   273 {
   274     return t1 < t2 ? t1 : t2;
   275 }
   276 
   277 template <typename T> static inline T
   278 Max(const T &t1, const T &t2)
   279 {
   280     return t1 > t2 ? t1 : t2;
   281 }
   282 
   283 template <typename T>
   284 class StorageBuffer
   285 {
   286  public:
   287   T *address() {
   288     return reinterpret_cast<T *>(buffer_);
   289   }
   290   const T *address() const {
   291     return reinterpret_cast<const T *>(buffer_);
   292   }
   293 
   294  private:
   295   union {
   296     char buffer_[sizeof(T)];
   297     uint64_t aligned_;
   298   };
   299 };
   300 
   301 #if __cplusplus >= 201103L
   302 # define KE_CXX11
   303 #endif
   304 
   305 #if defined(KE_CXX11)
   306 # define KE_DELETE delete
   307 # define KE_OVERRIDE override
   308 #else
   309 # define KE_DELETE
   310 # define KE_OVERRIDE
   311 #endif
   312 
   313 #if defined(_MSC_VER)
   314 # define KE_SIZET_FMT           "%Iu"
   315 #elif defined(__GNUC__)
   316 # define KE_SIZET_FMT           "%zu"
   317 #else
   318 # error "Implement format specifier string"
   319 #endif
   320 
   321 #if defined(__GNUC__)
   322 # define KE_CRITICAL_LIKELY(x)  __builtin_expect(!!(x), 1)
   323 #else
   324 # define KE_CRITICAL_LIKELY(x)  x
   325 #endif
   326 
   327 }
   328 
   329 #endif // _include_amtl_utility_h_