Android does not always provide posix_memalign
authorMichael Lee <mzlee@fb.com>
Wed, 2 Mar 2016 16:28:19 +0000 (08:28 -0800)
committerFacebook Github Bot 0 <facebook-github-bot-0-bot@fb.com>
Wed, 2 Mar 2016 16:35:42 +0000 (08:35 -0800)
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
folly/portability/Stdlib.cpp [new file with mode: 0644]
folly/portability/Stdlib.h [new file with mode: 0644]
folly/test/RangeTest.cpp

index 0dc1d29e44bb0f6511f75d9ad49abf4fe4bf681a..75a739ae1797eca4d1ae5afb69bc64d4ce246eed 100644 (file)
@@ -268,6 +268,7 @@ nobase_follyinclude_HEADERS = \
        portability/Constexpr.h \
        portability/Environment.h \
        portability/GFlags.h \
        portability/Constexpr.h \
        portability/Environment.h \
        portability/GFlags.h \
+       portability/Stdlib.h \
        portability/Syscall.h \
        portability/SysTime.h \
        portability/SysUio.h \
        portability/Syscall.h \
        portability/SysTime.h \
        portability/SysUio.h \
@@ -397,6 +398,7 @@ libfolly_la_SOURCES = \
        MacAddress.cpp \
        MemoryMapping.cpp \
        portability/Environment.cpp \
        MacAddress.cpp \
        MemoryMapping.cpp \
        portability/Environment.cpp \
+       portability/Stdlib.cpp \
        portability/SysTime.cpp \
        portability/Time.cpp \
        Random.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 (file)
index 0000000..e3f275b
--- /dev/null
@@ -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 <folly/portability/Stdlib.h>
+
+#include <errno.h>
+
+#if defined(__ANDROID__)
+
+#include <android/api-level.h>
+
+#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 (file)
index 0000000..2e7c1c0
--- /dev/null
@@ -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 <cstdlib>
+
+extern int posix_memalign(void** memptr, size_t alignment, size_t size);
index 104fcac02ee5a9cf2e30a650f74c10e4e6efbd84..aed47dc2ef13a05dce1c461cee99edf9e0013da8 100644 (file)
 
 #include <folly/Range.h>
 
 
 #include <folly/Range.h>
 
+#include <folly/portability/Stdlib.h>
+
 #include <sys/mman.h>
 #include <array>
 #include <sys/mman.h>
 #include <array>
-#include <cstdlib>
 #include <iterator>
 #include <limits>
 #include <random>
 #include <iterator>
 #include <limits>
 #include <random>