1 //
2 // Copyright (c) 2020 The Khronos Group 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 #ifndef HARNESS_ALLOC_H_
18 #define HARNESS_ALLOC_H_
19 
20 #if defined(__linux__) || defined(linux) || defined(__APPLE__)
21 #if defined(__ANDROID__)
22 #include <malloc.h>
23 #else
24 #include <stdlib.h>
25 #endif
26 #endif
27 
28 #if defined(__MINGW32__)
29 #include "mingw_compat.h"
30 #endif
31 
align_malloc(size_t size,size_t alignment)32 static void* align_malloc(size_t size, size_t alignment)
33 {
34 #if defined(_WIN32) && defined(_MSC_VER)
35     return _aligned_malloc(size, alignment);
36 #elif defined(__linux__) || defined(linux) || defined(__APPLE__)
37     void* ptr = NULL;
38 #if defined(__ANDROID__)
39     ptr = memalign(alignment, size);
40     if (ptr) return ptr;
41 #else
42     if (alignment < sizeof(void*))
43     {
44         alignment = sizeof(void*);
45     }
46     if (0 == posix_memalign(&ptr, alignment, size)) return ptr;
47 #endif
48     return NULL;
49 #elif defined(__MINGW32__)
50     return __mingw_aligned_malloc(size, alignment);
51 #else
52 #error "Please add support OS for aligned malloc"
53 #endif
54 }
55 
align_free(void * ptr)56 static void align_free(void* ptr)
57 {
58 #if defined(_WIN32) && defined(_MSC_VER)
59     _aligned_free(ptr);
60 #elif defined(__linux__) || defined(linux) || defined(__APPLE__)
61     return free(ptr);
62 #elif defined(__MINGW32__)
63     return __mingw_aligned_free(ptr);
64 #else
65 #error "Please add support OS for aligned free"
66 #endif
67 }
68 
69 #endif // #ifndef HARNESS_ALLOC_H_
70