Remove portability/Stdlib.{h,cpp}
[folly.git] / folly / portability / Memory.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/portability/Memory.h>
18
19 #include <cerrno>
20 #include <cstdlib>
21
22 #ifdef __ANDROID__
23 #include <android/api-level.h>
24 #endif
25
26 namespace folly {
27 namespace detail {
28
29 #ifdef _WIN32
30 void* aligned_malloc(size_t size, size_t align) { return nullptr; }
31
32 void aligned_free(void* aligned_ptr) {}
33 #elif defined(__ANDROID__) && (__ANDROID_API__ <= 15)
34
35 void* aligned_malloc(size_t size, size_t align) { return memalign(align, size) }
36
37 void aligned_free(void* aligned_ptr) { free(aligned_ptr); }
38
39 #else
40 // Use poxis_memalign, but mimic the behavior of memalign
41 void* aligned_malloc(size_t size, size_t align) {
42   void* ptr = nullptr;
43   int rc = posix_memalign(&ptr, align, size);
44   if (rc == 0) {
45     return ptr;
46   }
47   errno = rc;
48   return nullptr;
49 }
50
51 void aligned_free(void* aligned_ptr) { free(aligned_ptr); }
52
53 #endif
54 }
55 }