Fix bug in bytecode resizing.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
7 * SourcePawn 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 * SourcePawn 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 * SourcePawn. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jitcraft_utility_h_
20 #define _include_jitcraft_utility_h_
24 #include "../include/keima_util.h"
30 # pragma warning(disable:4355)
35 static const size_t kMallocAlignment = sizeof(void *) * 2;
37 typedef uint8_t uint8;
38 typedef int32_t int32;
39 typedef uint32_t uint32;
41 typedef uint8 * Address;
43 static const size_t kKB = 1024;
44 static const size_t kMB = 1024 * kKB;
45 static const size_t kGB = 1024 * kMB;
67 void operator =(T *t) {
74 // Bob Jenkin's one-at-a-time hash function[1].
76 // [1] http://burtleburtle.net/bob/hash/doobs.html
77 class CharacterStreamHasher
82 CharacterStreamHasher()
92 void add(const char *s, size_t length) {
93 for (size_t i = 0; i < length; i++)
100 hash += (hash << 15);
106 HashCharSequence(const char *s, size_t length)
108 CharacterStreamHasher hasher;
109 hasher.add(s, length);
110 return hasher.result();
121 _BitScanReverse(&rval, number);
123 _BitScanReverse64(&rval, number);
136 FindRightmostBit(size_t number)
143 _BitScanForward(&rval, number);
145 _BitScanForward64(&rval, number);
158 IsPowerOfTwo(size_t value)
162 return !(value & (value - 1));
166 Align(size_t count, size_t alignment)
168 assert(IsPowerOfTwo(alignment));
169 return count + (alignment - (count % alignment)) % alignment;
173 IsUint32AddSafe(unsigned a, unsigned b)
177 size_t log2_a = Log2(a);
178 size_t log2_b = Log2(b);
179 return (log2_a < sizeof(unsigned) * 8) &&
180 (log2_b < sizeof(unsigned) * 8);
184 IsUintPtrAddSafe(size_t a, size_t b)
188 size_t log2_a = Log2(a);
189 size_t log2_b = Log2(b);
190 return (log2_a < sizeof(size_t) * 8) &&
191 (log2_b < sizeof(size_t) * 8);
195 IsUint32MultiplySafe(unsigned a, unsigned b)
197 if (a <= 1 || b <= 1)
200 size_t log2_a = Log2(a);
201 size_t log2_b = Log2(b);
202 return log2_a + log2_b <= sizeof(unsigned) * 8;
206 IsUintPtrMultiplySafe(size_t a, size_t b)
208 if (a <= 1 || b <= 1)
211 size_t log2_a = Log2(a);
212 size_t log2_b = Log2(b);
213 return log2_a + log2_b <= sizeof(size_t) * 8;
216 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
217 #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
219 #define IS_ALIGNED(addr, alignment) (!(uintptr_t(addr) & ((alignment) - 1)))
221 template <typename T>
223 IsAligned(T addr, size_t alignment)
225 assert(IsPowerOfTwo(alignment));
226 return !(uintptr_t(addr) & (alignment - 1));
229 static inline Address
230 AlignedBase(Address addr, size_t alignment)
232 assert(IsPowerOfTwo(alignment));
233 return Address(uintptr_t(addr) & ~(alignment - 1));
236 template <typename T> static inline T
237 Min(const T &t1, const T &t2)
239 return t1 < t2 ? t1 : t2;
242 #define OFFSETOF(Class, Member) reinterpret_cast<size_t>(&((Class *)NULL)->Member)
246 #endif // _include_jitcraft_utility_h_