Move ExpressionList to a jc::Vector.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jitcraft_utility_h_
20 #define _include_jitcraft_utility_h_
29 # pragma warning(disable:4355)
34 static const size_t kMallocAlignment = sizeof(void *) * 2;
36 typedef uint8_t uint8;
37 typedef int32_t int32;
38 typedef uint32_t uint32;
40 typedef uint8 * Address;
42 static const size_t kKB = 1024;
43 static const size_t kMB = 1024 * kKB;
44 static const size_t kGB = 1024 * kMB;
66 void operator =(T *t) {
73 // Bob Jenkin's one-at-a-time hash function[1].
75 // [1] http://burtleburtle.net/bob/hash/doobs.html
76 class CharacterStreamHasher
81 CharacterStreamHasher()
91 void add(const char *s, size_t length) {
92 for (size_t i = 0; i < length; i++)
105 HashCharSequence(const char *s, size_t length)
107 CharacterStreamHasher hasher;
108 hasher.add(s, length);
109 return hasher.result();
119 _BitScanReverse(&rval, number);
131 FindRightmostBit(size_t number)
137 _BitScanForward(&rval, number);
149 IsPowerOfTwo(size_t value)
153 return !(value & (value - 1));
157 Align(size_t count, size_t alignment)
159 assert(IsPowerOfTwo(alignment));
160 return count + (alignment - (count % alignment)) % alignment;
164 IsUint32AddSafe(unsigned a, unsigned b)
168 unsigned log2_a = Log2(a);
169 unsigned log2_b = Log2(b);
170 return (log2_a < sizeof(unsigned) * 8) &&
171 (log2_b < sizeof(unsigned) * 8);
175 IsUintPtrAddSafe(size_t a, size_t b)
179 size_t log2_a = Log2(a);
180 size_t log2_b = Log2(b);
181 return (log2_a < sizeof(size_t) * 8) &&
182 (log2_b < sizeof(size_t) * 8);
186 IsUint32MultiplySafe(unsigned a, unsigned b)
188 if (a <= 1 || b <= 1)
191 unsigned log2_a = Log2(a);
192 unsigned log2_b = Log2(b);
193 return log2_a + log2_b <= sizeof(unsigned) * 8;
197 IsUintPtrMultiplySafe(size_t a, size_t b)
199 if (a <= 1 || b <= 1)
202 size_t log2_a = Log2(a);
203 size_t log2_b = Log2(b);
204 return log2_a + log2_b <= sizeof(size_t) * 8;
207 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
208 #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
210 #define IS_ALIGNED(addr, alignment) (!(uintptr_t(addr) & ((alignment) - 1)))
212 template <typename T>
214 IsAligned(T addr, size_t alignment)
216 assert(IsPowerOfTwo(alignment));
217 return !(uintptr_t(addr) & (alignment - 1));
220 static inline Address
221 AlignedBase(Address addr, size_t alignment)
223 assert(IsPowerOfTwo(alignment));
224 return Address(uintptr_t(addr) & ~(alignment - 1));
227 template <typename T> static inline T
228 Min(const T &t1, const T &t2)
230 return t1 < t2 ? t1 : t2;
235 #endif // _include_jitcraft_utility_h_