Create a malloc.h portability header
authorChristopher Dykes <cdykes@fb.com>
Thu, 31 Mar 2016 17:44:33 +0000 (10:44 -0700)
committerFacebook Github Bot 7 <facebook-github-bot-7-bot@fb.com>
Thu, 31 Mar 2016 17:50:27 +0000 (10:50 -0700)
Summary:Let's break OSX!

Alright, maybe not. Neither OSX nor Windows define malloc_usable_size, so we implement them based on what is available on the respective platforms.
This moves the implementation for OSX out of Portability.h and into the new header, so it likely breaks something on OSX, although I'm not sure what.

Reviewed By: yfeldblum

Differential Revision: D3019938

fb-gh-sync-id: df95faef09535098fb73b7b3479d7a73f6b49712
fbshipit-source-id: df95faef09535098fb73b7b3479d7a73f6b49712

folly/Makefile.am
folly/Portability.h
folly/portability/Malloc.cpp [new file with mode: 0755]
folly/portability/Malloc.h [new file with mode: 0755]

index 4f3f4d6060c565ed93f20d90bfb4cd50e635c95c..0e0357c0acd3aedf9d9fbe2a5d5da518b0ad1f25 100644 (file)
@@ -273,6 +273,7 @@ nobase_follyinclude_HEADERS = \
        portability/Environment.h \
        portability/GFlags.h \
        portability/IOVec.h \
+       portability/Malloc.h \
        portability/Memory.h \
        portability/String.h \
        portability/Strings.h \
@@ -414,6 +415,7 @@ libfolly_la_SOURCES = \
        MacAddress.cpp \
        MemoryMapping.cpp \
        portability/Environment.cpp \
+       portability/Malloc.cpp \
        portability/String.cpp \
        portability/Strings.cpp \
        portability/SysFile.cpp \
index 5bca1f54c8bdbd7f260cd847b62ca761c4f1dace..7de1d8debaccf25e81cd074605cab513c776c7fa 100644 (file)
 
 #include <folly/CPortability.h>
 
-#ifdef __APPLE__
-# include <malloc/malloc.h>
-#endif
-
 #if FOLLY_HAVE_SCHED_H
  #include <sched.h>
 #endif
@@ -355,13 +351,6 @@ using namespace FOLLY_GFLAGS_NAMESPACE;
 #include <TargetConditionals.h>
 #endif
 
-// MacOS doesn't have malloc_usable_size()
-#if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
-inline size_t malloc_usable_size(void* ptr) {
-  return malloc_size(ptr);
-}
-#endif
-
 // RTTI may not be enabled for this compilation unit.
 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
     (defined(_MSC_VER) && defined(_CPPRTTI))
diff --git a/folly/portability/Malloc.cpp b/folly/portability/Malloc.cpp
new file mode 100755 (executable)
index 0000000..402ee09
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/portability/Malloc.h>
+
+#if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
+#include <malloc/malloc.h>
+
+extern "C" size_t malloc_usable_size(void* ptr) {
+  return malloc_size(ptr);
+}
+#elif defined(_WIN32)
+extern "C" size_t malloc_usable_size(void* addr) {
+  return _msize(addr);
+}
+#endif
diff --git a/folly/portability/Malloc.h b/folly/portability/Malloc.h
new file mode 100755 (executable)
index 0000000..69b3704
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <malloc.h>
+
+#include <folly/portability/Config.h>
+
+#if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
+// MacOS doesn't have malloc_usable_size()
+extern "C" size_t malloc_usable_size(void* ptr);
+#elif defined(_WIN32)
+extern "C" size_t malloc_usable_size(void* ptr);
+#endif