Adding some msvc specific defines
[folly.git] / folly / Portability.h
index 1095164f4474a85c7624b348f00d0ce1145e0900..4d623eb4908d32a60806d02dc1e2eb5c0b796172 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
 #include <features.h>
 #endif
 
+#include "CPortability.h"
 
 #if FOLLY_HAVE_SCHED_H
  #include <sched.h>
@@ -41,14 +42,57 @@ 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
 
 // 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
@@ -73,6 +117,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)
@@ -84,38 +140,72 @@ struct MaxAlign { char c; } __attribute__((aligned));
 # endif
 #endif
 
+// It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
+// the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
+// up in a macro to make forward-declarations easier.
+#if FOLLY_USE_LIBCPP
+#define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
+#define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
+#else
+#define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
+#define FOLLY_NAMESPACE_STD_END       }
+#endif
 
-/* Define a convenience macro to test when address sanitizer is being used
- * across the different compilers (e.g. clang, gcc) */
-#if defined(__clang__)
-# if __has_feature(address_sanitizer)
-#  define FOLLY_SANITIZE_ADDRESS 1
-# endif
-#elif defined (__GNUC__) && \
-      (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
-      __SANITIZE_ADDRESS__
-# define FOLLY_SANITIZE_ADDRESS 1
-#endif
-
-/* Define attribute wrapper for function attribute used to disable
- * address sanitizer instrumentation. Unfortunately, this attribute
- * has issues when inlining is used, so disable that as well. */
-#ifdef FOLLY_SANITIZE_ADDRESS
-# if defined(__clang__)
-#  if __has_attribute(__no_address_safety_analysis__)
-#   define FOLLY_DISABLE_ADDRESS_SANITIZER \
-      __attribute__((__no_address_safety_analysis__, __noinline__))
-#  elif __has_attribute(__no_sanitize_address__)
-#   define FOLLY_DISABLE_ADDRESS_SANITIZER \
-      __attribute__((__no_sanitize_address__, __noinline__))
-#  endif
-# elif defined(__GNUC__)
-#  define FOLLY_DISABLE_ADDRESS_SANITIZER \
-     __attribute__((__no_address_safety_analysis__, __noinline__))
-# endif
+// Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
+// versions of these into the global namespace.
+#if FOLLY_HAVE_CLOCK_GETTIME
+#include <time.h>
+#else
+#include "folly/detail/Clock.h"
 #endif
-#ifndef FOLLY_DISABLE_ADDRESS_SANITIZER
-# define FOLLY_DISABLE_ADDRESS_SANITIZER
+
+// Provide our own std::__throw_* wrappers for platforms that don't have them
+#if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
+#include <bits/functexcept.h>
+#else
+#include "folly/detail/FunctionalExcept.h"
+#endif
+
+#if defined(__cplusplus)
+// Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
+// usage of __has_trivial_copy(), so we can't use it as a
+// least-common-denominator for C++11 implementations that don't support
+// std::is_trivially_copyable<T>.
+//
+//      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
+//
+// As a result, use std::is_trivially_copyable() where it exists, and fall back
+// 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)
+#else
+#include <boost/type_traits.hpp>
+#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_