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 )
41 return ptr;
42 #else
43 if (alignment < sizeof(void*)) {
44 alignment = sizeof(void*);
45 }
46 if (0 == posix_memalign(&ptr, alignment, size))
47 return ptr;
48 #endif
49 return NULL;
50 #elif defined(__MINGW32__)
51 return __mingw_aligned_malloc(size, alignment);
52 #else
53 #error "Please add support OS for aligned malloc"
54 #endif
55 }
56
align_free(void * ptr)57 static void align_free(void * ptr)
58 {
59 #if defined(_WIN32) && defined(_MSC_VER)
60 _aligned_free(ptr);
61 #elif defined(__linux__) || defined (linux) || defined(__APPLE__)
62 return free(ptr);
63 #elif defined(__MINGW32__)
64 return __mingw_aligned_free(ptr);
65 #else
66 #error "Please add support OS for aligned free"
67 #endif
68 }
69
70 #endif // #ifndef HARNESS_ALLOC_H_
71
72