fix IOBuf self move-assignment
[folly.git] / folly / Portability.h
index b227ba0da9ecf2f9018b7481bb67f870ab584c9a..68d7d969e9d8e4eb19274e3adb84258a241dff07 100644 (file)
@@ -42,14 +42,72 @@ struct MaxAlign { char c; } __attribute__((aligned));
 # error Cannot define MaxAlign on this platform
 #endif
 
+// compiler specific attribute translation
+// msvc should come first, so if clang is in msvc mode it gets the right defines
+
+// NOTE: this will only do checking in msvc with versions that support /analyze
+#if _MSC_VER
+# ifdef _USE_ATTRIBUTES_FOR_SAL
+#    undef _USE_ATTRIBUTES_FOR_SAL
+# endif
+# define _USE_ATTRIBUTES_FOR_SAL 1
+# include <sal.h>
+# define FOLLY_PRINTF_FORMAT _Printf_format_string_
+# define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
+#else
+# define FOLLY_PRINTF_FORMAT /**/
+# define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
+  __attribute__((format(printf, format_param, dots_param)))
+#endif
 
 // noreturn
-#if defined(__clang__) || defined(__GNUC__)
+#if defined(_MSC_VER)
+# define FOLLY_NORETURN __declspec(noreturn)
+#elif defined(__clang__) || defined(__GNUC__)
 # define FOLLY_NORETURN __attribute__((noreturn))
 #else
 # define FOLLY_NORETURN
 #endif
 
+// noinline
+#ifdef _MSC_VER
+# define FOLLY_NOINLINE __declspec(noinline)
+#elif defined(__clang__) || defined(__GNUC__)
+# define FOLLY_NOINLINE __attribute__((noinline))
+#else
+# define FOLLY_NOINLINE
+#endif
+
+// always inline
+#ifdef _MSC_VER
+# define FOLLY_ALWAYS_INLINE __forceinline
+#elif defined(__clang__) || defined(__GNUC__)
+# define FOLLY_ALWAYS_INLINE inline __attribute__((always_inline))
+#else
+# define FOLLY_ALWAYS_INLINE
+#endif
+
+// detection for 64 bit
+#if defined(__x86_64__) || defined(_M_X64)
+# define FOLLY_X64 1
+#else
+# define FOLLY_X64 0
+#endif
+
+// packing is very ugly in msvc
+#ifdef _MSC_VER
+# define FOLLY_PACK_ATTR /**/
+# define FOLLY_PACK_PUSH __pragma(pack(push, 1))
+# define FOLLY_PACK_POP __pragma(pack(pop))
+#elif defined(__clang__) || defined(__GNUC__)
+# define FOLLY_PACK_ATTR __attribute__((packed))
+# define FOLLY_PACK_PUSH /**/
+# define FOLLY_PACK_POP /**/
+#else
+# define FOLLY_PACK_ATTR /**/
+# define FOLLY_PACK_PUSH /**/
+# define FOLLY_PACK_POP /**/
+#endif
 
 // portable version check
 #ifndef __GNUC_PREREQ
@@ -74,6 +132,18 @@ struct MaxAlign { char c; } __attribute__((aligned));
 # endif
 #endif
 
+/* Platform specific TLS support
+ * gcc implements __thread
+ * msvc implements __declspec(thread)
+ * the semantics are the same (but remember __thread is broken on apple)
+ */
+#if defined(_MSC_VER)
+# define FOLLY_TLS __declspec(thread)
+#elif defined(__GNUC__) || defined(__clang__)
+# define FOLLY_TLS __thread
+#else
+# error cannot define platform specific thread local storage
+#endif
 
 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
@@ -123,11 +193,34 @@ struct MaxAlign { char c; } __attribute__((aligned));
 // to Boost otherwise.
 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
 #include <type_traits>
-#define FOLLY_IS_TRIVIALLY_COPYABLE(T)      (std::is_trivially_copyable<T>::value)
+#define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
+  (std::is_trivially_copyable<T>::value)
 #else
 #include <boost/type_traits.hpp>
-#define FOLLY_IS_TRIVIALLY_COPYABLE(T)      (boost::has_trivial_copy<T>::value)
+#define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
+  (boost::has_trivial_copy<T>::value &&                  \
+   boost::has_trivial_destructor<T>::value)
 #endif
 #endif // __cplusplus
 
+// MSVC specific defines
+// mainly for posix compat
+#ifdef _MSC_VER
+
+// this definition is in a really silly place with a silly name
+// and ifdefing it every time we want it is painful
+#include <basetsd.h>
+typedef SSIZE_T ssize_t;
+
+// sprintf semantics are not exactly identical
+// but current usage is not a problem
+# define snprintf _snprintf
+
+// semantics here are identical
+# define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
+
+// compiler specific to compiler specific
+# define __PRETTY_FUNCTION__ __FUNCSIG__
+#endif
+
 #endif // FOLLY_PORTABILITY_H_