Sync from sourcemod.
1 // vim: set sts=8 ts=2 sw=2 tw=99 et:
3 // Copyright (C) 2013, David Anderson and AlliedModders LLC
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
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.
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.
30 #ifndef _include_amtl_utility_h_
31 #define _include_amtl_utility_h_
44 # pragma warning(disable:4355)
49 static const size_t kMallocAlignment = sizeof(void *) * 2;
51 static const size_t kKB = 1024;
52 static const size_t kMB = 1024 * kKB;
53 static const size_t kGB = 1024 * kMB;
55 typedef uint8_t * Address;
57 template <typename T> T
65 // Wrapper that automatically deletes its contents. The pointer can be taken
66 // to avoid destruction.
77 explicit AutoPtr(T *t)
85 return ReturnAndVoid(t_);
87 T *operator *() const {
90 T *operator ->() const {
93 operator T *() const {
96 void operator =(T *t) {
100 bool operator !() const {
105 // Wrapper that automatically deletes its contents. The pointer can be taken
106 // to avoid destruction.
107 template <typename T>
117 explicit AutoArray(T *t)
125 return ReturnAndVoid(t_);
127 T *operator *() const {
130 T &operator [](size_t index) {
133 const T &operator [](size_t index) const {
136 operator T *() const {
139 void operator =(T *t) {
143 bool operator !() const {
156 _BitScanReverse(&rval, number);
158 _BitScanReverse64(&rval, number);
171 FindRightmostBit(size_t number)
178 _BitScanForward(&rval, number);
180 _BitScanForward64(&rval, number);
193 IsPowerOfTwo(size_t value)
197 return !(value & (value - 1));
201 Align(size_t count, size_t alignment)
203 assert(IsPowerOfTwo(alignment));
204 return count + (alignment - (count % alignment)) % alignment;
208 IsUint32AddSafe(unsigned a, unsigned b)
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);
219 IsUintPtrAddSafe(size_t a, size_t b)
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);
230 IsUint32MultiplySafe(unsigned a, unsigned b)
232 if (a <= 1 || b <= 1)
235 size_t log2_a = Log2(a);
236 size_t log2_b = Log2(b);
237 return log2_a + log2_b <= sizeof(unsigned) * 8;
241 IsUintPtrMultiplySafe(size_t a, size_t b)
243 if (a <= 1 || b <= 1)
246 size_t log2_a = Log2(a);
247 size_t log2_b = Log2(b);
248 return log2_a + log2_b <= sizeof(size_t) * 8;
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])
254 #define IS_ALIGNED(addr, alignment) (!(uintptr_t(addr) & ((alignment) - 1)))
256 template <typename T>
258 IsAligned(T addr, size_t alignment)
260 assert(IsPowerOfTwo(alignment));
261 return !(uintptr_t(addr) & (alignment - 1));
264 static inline Address
265 AlignedBase(Address addr, size_t alignment)
267 assert(IsPowerOfTwo(alignment));
268 return Address(uintptr_t(addr) & ~(alignment - 1));
271 template <typename T> static inline T
272 Min(const T &t1, const T &t2)
274 return t1 < t2 ? t1 : t2;
277 template <typename T> static inline T
278 Max(const T &t1, const T &t2)
280 return t1 > t2 ? t1 : t2;
283 template <typename T>
288 return reinterpret_cast<T *>(buffer_);
290 const T *address() const {
291 return reinterpret_cast<const T *>(buffer_);
296 char buffer_[sizeof(T)];
301 #if __cplusplus >= 201103L
305 #if defined(KE_CXX11)
306 # define KE_DELETE delete
307 # define KE_OVERRIDE override
313 #if defined(_MSC_VER)
314 # define KE_SIZET_FMT "%Iu"
315 #elif defined(__GNUC__)
316 # define KE_SIZET_FMT "%zu"
318 # error "Implement format specifier string"
321 #if defined(__GNUC__)
322 # define KE_CRITICAL_LIKELY(x) __builtin_expect(!!(x), 1)
324 # define KE_CRITICAL_LIKELY(x) x
329 #endif // _include_amtl_utility_h_