src/Utility.h
changeset 251 ca99a81745fe
parent 195 c1ba166f3fc4
child 256 3c184218462d
     1.1 --- a/src/Utility.h	Sat Dec 29 00:55:38 2012 -0800
     1.2 +++ b/src/Utility.h	Tue Jan 01 15:34:07 2013 -0800
     1.3 @@ -21,6 +21,7 @@
     1.4  
     1.5  #include <assert.h>
     1.6  #include <stddef.h>
     1.7 +#include <stdlib.h>
     1.8  #include "../include/keima_util.h"
     1.9  #ifdef _MSC_VER
    1.10  # include <intrin.h>
    1.11 @@ -47,28 +48,33 @@
    1.12  static const size_t kGB = 1024 * kMB;
    1.13  
    1.14  template <typename T>
    1.15 -class AutoVec
    1.16 +class AutoFree
    1.17  {
    1.18      T *t_;
    1.19  
    1.20    public:
    1.21 -    AutoVec()
    1.22 +    AutoFree()
    1.23        : t_(NULL)
    1.24      {
    1.25      }
    1.26 -    AutoVec(T *t)
    1.27 +    AutoFree(T *t)
    1.28        : t_(t)
    1.29      {
    1.30      }
    1.31 -    ~AutoVec() {
    1.32 -        delete [] t_;
    1.33 +    ~AutoFree() {
    1.34 +        free(t_);
    1.35      }
    1.36 -    operator T *() {
    1.37 +    T *take() {
    1.38 +        T *t = t_;
    1.39 +        t_ = NULL;
    1.40 +        return t;
    1.41 +    }
    1.42 +    T *operator *() const {
    1.43          return t_;
    1.44      }
    1.45      void operator =(T *t) {
    1.46          if (t_)
    1.47 -            delete [] t_;
    1.48 +            free(t_);
    1.49          t_ = t;
    1.50      }
    1.51  };
    1.52 @@ -116,11 +122,11 @@
    1.53  static inline uint32
    1.54  HashInt32(int32 a)
    1.55  {
    1.56 -    a = (a ^ 61) ^ (a >> 16);
    1.57 -    a = a + (a << 3);
    1.58 -    a = a ^ (a >> 4);
    1.59 -    a = a * 0x27d4eb2d;
    1.60 -    a = a ^ (a >> 15);
    1.61 +    a = (a ^ 61) ^ (a >> 16);
    1.62 +    a = a + (a << 3);
    1.63 +    a = a ^ (a >> 4);
    1.64 +    a = a * 0x27d4eb2d;
    1.65 +    a = a ^ (a >> 15);
    1.66      return a;
    1.67  }
    1.68  
    1.69 @@ -128,12 +134,12 @@
    1.70  static inline uint32
    1.71  HashInt64(int64 key)
    1.72  {
    1.73 -  key = (~key) + (key << 18); // key = (key << 18) - key - 1;
    1.74 -  key = key ^ (uint64(key) >> 31);
    1.75 -  key = key * 21; // key = (key + (key << 2)) + (key << 4);
    1.76 -  key = key ^ (uint64(key) >> 11);
    1.77 -  key = key + (key << 6);
    1.78 -  key = key ^ (uint64(key) >> 22);
    1.79 +  key = (~key) + (key << 18); // key = (key << 18) - key - 1;
    1.80 +  key = key ^ (uint64(key) >> 31);
    1.81 +  key = key * 21; // key = (key + (key << 2)) + (key << 4);
    1.82 +  key = key ^ (uint64(key) >> 11);
    1.83 +  key = key + (key << 6);
    1.84 +  key = key ^ (uint64(key) >> 22);
    1.85    return uint32(key);
    1.86  }
    1.87  
    1.88 @@ -161,11 +167,11 @@
    1.89  # endif
    1.90      return rval;
    1.91  #else
    1.92 -	size_t bit;
    1.93 -	asm("bsr %1, %0\n"
    1.94 -		: "=r" (bit)
    1.95 -		: "rm" (number));
    1.96 -	return bit;
    1.97 +    size_t bit;
    1.98 +    asm("bsr %1, %0\n"
    1.99 +        : "=r" (bit)
   1.100 +        : "rm" (number));
   1.101 +    return bit;
   1.102  #endif
   1.103  }
   1.104  
   1.105 @@ -183,11 +189,11 @@
   1.106  # endif
   1.107      return rval;
   1.108  #else
   1.109 -	size_t bit;
   1.110 -	asm("bsf %1, %0\n"
   1.111 -		: "=r" (bit)
   1.112 -		: "rm" (number));
   1.113 -	return bit;
   1.114 +    size_t bit;
   1.115 +    asm("bsf %1, %0\n"
   1.116 +        : "=r" (bit)
   1.117 +        : "rm" (number));
   1.118 +    return bit;
   1.119  #endif
   1.120  }
   1.121  
   1.122 @@ -253,7 +259,7 @@
   1.123  #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
   1.124  #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
   1.125  
   1.126 -#define IS_ALIGNED(addr, alignment)	(!(uintptr_t(addr) & ((alignment) - 1)))
   1.127 +#define IS_ALIGNED(addr, alignment)    (!(uintptr_t(addr) & ((alignment) - 1)))
   1.128  
   1.129  template <typename T>
   1.130  static inline bool