From 860171628db4ef08526a2bdcef7b08a0ad282c37 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Thu, 31 Mar 2016 10:44:33 -0700 Subject: [PATCH] Create a malloc.h portability header 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 | 2 ++ folly/Portability.h | 11 ----------- folly/portability/Malloc.cpp | 29 +++++++++++++++++++++++++++++ folly/portability/Malloc.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 11 deletions(-) create mode 100755 folly/portability/Malloc.cpp create mode 100755 folly/portability/Malloc.h diff --git a/folly/Makefile.am b/folly/Makefile.am index 4f3f4d60..0e0357c0 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -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 \ diff --git a/folly/Portability.h b/folly/Portability.h index 5bca1f54..7de1d8de 100644 --- a/folly/Portability.h +++ b/folly/Portability.h @@ -25,10 +25,6 @@ #include -#ifdef __APPLE__ -# include -#endif - #if FOLLY_HAVE_SCHED_H #include #endif @@ -355,13 +351,6 @@ using namespace FOLLY_GFLAGS_NAMESPACE; #include #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 index 00000000..402ee099 --- /dev/null +++ b/folly/portability/Malloc.cpp @@ -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 + +#if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE) +#include + +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 index 00000000..69b37043 --- /dev/null +++ b/folly/portability/Malloc.h @@ -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 + +#include + +#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 -- 2.34.1