aboutsummaryrefslogtreecommitdiffstats
path: root/common/almalloc.h
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2018-12-20 11:46:40 -0800
committerChris Robinson <[email protected]>2018-12-20 11:46:40 -0800
commit08b79b9bbfa96ee6dcbc4d58bf082c9e6df911df (patch)
tree526d25e859621bf127ee69f97e342e4d923f6569 /common/almalloc.h
parent10f87c5d26cf0cf4995228ce9671b6d0aeda5b9f (diff)
Add an assume_aligned helper
Diffstat (limited to 'common/almalloc.h')
-rw-r--r--common/almalloc.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/common/almalloc.h b/common/almalloc.h
index d9b285fe..7ec07004 100644
--- a/common/almalloc.h
+++ b/common/almalloc.h
@@ -68,6 +68,21 @@ struct allocator : public std::allocator<T> {
{ }
};
+template<size_t alignment, typename T>
+inline T* assume_aligned(T *ptr) noexcept
+{
+ static_assert((alignment & (alignment-1)) == 0, "alignment must be a power of 2");
+#ifdef __GNUC__
+ return reinterpret_cast<T*>(__builtin_assume_aligned(ptr, alignment));
+#elif defined(_MSC_VER)
+ auto ptrval = reinterpret_cast<uintptr_t>(ptr);
+ if((ptrval&(alignment-1)) != 0) __assume(0);
+ return reinterpret_cast<T*>(ptrval);
+#else
+ return ptr;
+#endif
+}
+
} // namespace al
#endif /* AL_MALLOC_H */