Support MSVC, which does not have VLAs, in folly/io/async/AsyncSocket.cpp
authorYedidya Feldblum <yfeldblum@fb.com>
Fri, 18 Sep 2015 08:37:13 +0000 (01:37 -0700)
committerfacebook-github-bot-1 <folly-bot@fb.com>
Fri, 18 Sep 2015 09:20:17 +0000 (02:20 -0700)
Summary: [Folly] Support MSVC, which does not have VLAs, in `folly/io/async/AsyncSocket.cpp`.

We use VLAs in compilers that have them, and fixed-size arrays in compilers that do not.

Reviewed By: @JoelMarcey

Differential Revision: D2450689

folly/configure.ac
folly/io/async/AsyncSocket.cpp

index 538d81089f2585d2cfe4a920d061ab5222a88d7d..8d4b07a46d446fe360ce3cb7abf9cf89e57940c3 100644 (file)
@@ -365,6 +365,10 @@ AC_CACHE_CHECK(
     [folly_cv_prog_cc_xsi_strerror_r=yes],
     [folly_cv_prog_cc_xsi_strerror_r=no])])
 
+if test "$folly_cv_prog_cc_xsi_strerror_r" = "yes"; then
+  AC_DEFINE([HAVE_XSI_STRERROR_R], [1], [Define to 1 if the runtime supports XSI-style strerror_r])
+fi
+
 AC_CACHE_CHECK(
   [for ext/random and __gnu_cxx::sfmt19937],
   [folly_cv_prog_cc_have_extrandom_sfmt19937],
@@ -379,9 +383,26 @@ AC_CACHE_CHECK(
     [folly_cv_prog_cc_have_extrandom_sfmt19937=yes],
     [folly_cv_prog_cc_have_extrandom_sfmt19937=no])])
 
-if test "$folly_cv_prog_cc_xsi_strerror_r" = "yes"; then
-  AC_DEFINE([HAVE_XSI_STRERROR_R], [1], [Define to 1 if the runtime supports XSI-style strerror_r])
-fi
+AC_CACHE_CHECK(
+  [for VLA (variable-length array) support],
+  [folly_cv_prog_cc_have_vla],
+  [AC_COMPILE_IFELSE(
+    [AC_LANG_SOURCE[
+      int main(int argc, char** argv) {
+        unsigned size = argc;
+        char data[size];
+        return 0;
+      }
+    ]],
+    [folly_cv_prog_cc_have_vla=yes],
+    [folly_cv_prog_cc_have_vla=no])])
+
+test "$folly_cv_prog_cc_have_vla" = yes && have_vla=1 || have_vla=0
+AC_DEFINE_UNQUOTED(
+  [HAVE_VLA],
+  [$have_vla],
+  [Define to 1 if the compiler has VLA (variable-length array) support,
+   otherwise define to 0])
 
 # Checks for library functions.
 AC_CHECK_FUNCS([getdelim \
index e7c9c4510499da09cb3a16e0d57c330825bcd8eb..a603e709f67b455d4f8d6f3d93e1a5e35d6dd560 100644 (file)
@@ -31,6 +31,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
+#include <boost/preprocessor/control/if.hpp>
 
 using std::string;
 using std::unique_ptr;
@@ -622,9 +623,10 @@ void AsyncSocket::writev(WriteCallback* callback,
 
 void AsyncSocket::writeChain(WriteCallback* callback, unique_ptr<IOBuf>&& buf,
                               WriteFlags flags) {
+  constexpr size_t kSmallSizeMax = 64;
   size_t count = buf->countChainElements();
-  if (count <= 64) {
-    iovec vec[count];
+  if (count <= kSmallSizeMax) {
+    iovec vec[BOOST_PP_IF(FOLLY_HAVE_VLA, count, kSmallSizeMax)];
     writeChainImpl(callback, vec, count, std::move(buf), flags);
   } else {
     iovec* vec = new iovec[count];