Added MacOS, gcc, and clang support. Preliminary crappy autoconf scripts of course.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
18 #ifndef _include_jitcraft_utility_h_
19 #define _include_jitcraft_utility_h_
28 # pragma warning(disable:4355)
33 static const size_t kMallocAlignment = sizeof(void *) * 2;
35 typedef uint8_t uint8;
36 typedef int32_t int32;
37 typedef uint32_t uint32;
39 typedef uint8 * Address;
41 static const size_t kKB = 1024;
42 static const size_t kMB = 1024 * kKB;
43 static const size_t kGB = 1024 * kMB;
65 void operator =(T *t) {
79 _BitScanReverse(&rval, number);
91 IsPowerOfTwo(size_t value)
95 return !(value & (value - 1));
99 Align(size_t count, size_t alignment)
101 assert(IsPowerOfTwo(alignment));
102 return count + (alignment - (count % alignment)) % alignment;
106 IsUint32AddSafe(unsigned a, unsigned b)
108 unsigned log2_a = Log2(a);
109 unsigned log2_b = Log2(b);
110 return (log2_a < sizeof(unsigned) * 8) &&
111 (log2_b < sizeof(unsigned) * 8);
115 IsUint32MultiplySafe(unsigned a, unsigned b)
117 if (a <= 1 || b <= 1)
120 unsigned log2_a = Log2(a);
121 unsigned log2_b = Log2(b);
122 return log2_a + log2_b <= sizeof(unsigned) * 8;
125 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
126 #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
128 template <typename T>
130 IsAligned(T addr, size_t alignment)
132 assert(IsPowerOfTwo(alignment));
133 return !(uintptr_t(addr) & (alignment - 1));
136 static inline Address
137 AlignedBase(Address addr, size_t alignment)
139 assert(IsPowerOfTwo(alignment));
140 return Address(uintptr_t(addr) & ~(alignment - 1));
143 template <typename T> static inline T
144 Min(const T &t1, const T &t2)
146 return t1 < t2 ? t1 : t2;
151 #endif // _include_jitcraft_utility_h_