From b99ed996050cdce752b71ab17461d9291aa083e6 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 2 Mar 2016 08:28:19 -0800 Subject: [PATCH] Android does not always provide posix_memalign Summary: We could provide `posix_memalign` instead as part of portability, but I am not sure how to tell whether or not it will be available. Reviewed By: yfeldblum Differential Revision: D2991432 fb-gh-sync-id: 587314d43779f3b8fead2c41ed05016e6350f2ee shipit-source-id: 587314d43779f3b8fead2c41ed05016e6350f2ee --- folly/Makefile.am | 2 ++ folly/portability/Stdlib.cpp | 42 ++++++++++++++++++++++++++++++++++++ folly/portability/Stdlib.h | 21 ++++++++++++++++++ folly/test/RangeTest.cpp | 3 ++- 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 folly/portability/Stdlib.cpp create mode 100644 folly/portability/Stdlib.h diff --git a/folly/Makefile.am b/folly/Makefile.am index 0dc1d29e..75a739ae 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -268,6 +268,7 @@ nobase_follyinclude_HEADERS = \ portability/Constexpr.h \ portability/Environment.h \ portability/GFlags.h \ + portability/Stdlib.h \ portability/Syscall.h \ portability/SysTime.h \ portability/SysUio.h \ @@ -397,6 +398,7 @@ libfolly_la_SOURCES = \ MacAddress.cpp \ MemoryMapping.cpp \ portability/Environment.cpp \ + portability/Stdlib.cpp \ portability/SysTime.cpp \ portability/Time.cpp \ Random.cpp \ diff --git a/folly/portability/Stdlib.cpp b/folly/portability/Stdlib.cpp new file mode 100644 index 00000000..e3f275b0 --- /dev/null +++ b/folly/portability/Stdlib.cpp @@ -0,0 +1,42 @@ +/* + * 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 + +#include + +#if defined(__ANDROID__) + +#include + +#if (__ANDROID_API__ <= 15) + +int posix_memalign(void** memptr, size_t alignment, size_t size) { + int rc = 0; + int saved_errno = errno; + void* ptr = nullptr; + ptr = memalign(alignment, size); + if (nullptr == ptr) { + rc = errno; + } else { + *memptr = ptr; + } + errno = saved_errno; + return rc; +} + +#endif +#endif diff --git a/folly/portability/Stdlib.h b/folly/portability/Stdlib.h new file mode 100644 index 00000000..2e7c1c0b --- /dev/null +++ b/folly/portability/Stdlib.h @@ -0,0 +1,21 @@ +/* + * 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 + +extern int posix_memalign(void** memptr, size_t alignment, size_t size); diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index 104fcac0..aed47dc2 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -19,9 +19,10 @@ #include +#include + #include #include -#include #include #include #include -- 2.34.1