Make compiler-rt/trunk/lib/asan compileable with Visual Studio 2008 on Windows.
Patch by Timur Iskhodzhanov (timurrrr@google.com)
To test:
$ cl /c *.c*
in the asan directory.
The code fails to link if you omit the "/c" part but that's one of the
next steps,
as well as a few TODO's I've put into the Windows-specific code.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@149130 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_allocator.cc b/lib/asan/asan_allocator.cc
index 2ce859c..35d8352 100644
--- a/lib/asan/asan_allocator.cc
+++ b/lib/asan/asan_allocator.cc
@@ -59,15 +59,44 @@
return (a & (alignment - 1)) == 0;
}
+#ifdef _WIN32
+#include <intrin.h>
+#endif
+
static inline size_t Log2(size_t x) {
CHECK(IsPowerOfTwo(x));
+#if defined(_WIN64)
+ unsigned long ret; // NOLINT
+ _BitScanForward64(&ret, x);
+ return ret;
+#elif defined(_WIN32)
+ unsigned long ret; // NOLINT
+ _BitScanForward(&ret, x);
+ return ret;
+#else
return __builtin_ctzl(x);
+#endif
+}
+
+static inline size_t clz(size_t x) {
+#if defined(_WIN64)
+ unsigned long ret; // NOLINT
+ _BitScanReverse64(&ret, x);
+ return ret;
+#elif defined(_WIN32)
+ unsigned long ret; // NOLINT
+ _BitScanReverse(&ret, x);
+ return ret;
+#else
+ return __builtin_clzl(x);
+#endif
}
static inline size_t RoundUpToPowerOfTwo(size_t size) {
CHECK(size);
if (IsPowerOfTwo(size)) return size;
- size_t up = __WORDSIZE - __builtin_clzl(size);
+
+ size_t up = __WORDSIZE - clz(size);
CHECK(size < (1ULL << up));
CHECK(size > (1ULL << (up - 1)));
return 1UL << up;