1 // Copyright 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "AlignedBuf.h"
16
17 namespace android {
18
19 // Convenience function for aligned malloc across platforms
aligned_buf_alloc(size_t align,size_t size)20 void* aligned_buf_alloc(size_t align, size_t size) {
21 size_t actualAlign = std::max(align, sizeof(void*));
22 #ifdef _WIN32
23 void* res = _aligned_malloc(size, actualAlign);
24 if (!res) {
25 abort();
26 }
27 return res;
28 #else
29 void* res;
30 if (posix_memalign(&res, actualAlign, size)) {
31 abort();
32 }
33 return res;
34 #endif
35 }
36
aligned_buf_free(void * buf)37 void aligned_buf_free(void* buf) {
38 #ifdef _WIN32
39 _aligned_free(buf);
40 #else
41 free(buf);
42 #endif
43 }
44
45 } // namespace android