1 /* Copyright 2019 Google LLC. All Rights Reserved.
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 
16 #include "ruy/system_aligned_alloc.h"
17 
18 #include <cstddef>
19 #include <cstdlib>
20 
21 #ifdef _WIN32
22 #include <malloc.h>
23 #endif
24 
25 namespace ruy {
26 
27 namespace detail {
28 
SystemAlignedAlloc(std::ptrdiff_t num_bytes)29 void *SystemAlignedAlloc(std::ptrdiff_t num_bytes) {
30 #ifdef _WIN32
31   return _aligned_malloc(num_bytes, kMinimumBlockAlignment);
32 #else
33   void *ptr;
34   if (posix_memalign(&ptr, kMinimumBlockAlignment, num_bytes)) {
35     return nullptr;
36   }
37   return ptr;
38 #endif
39 }
40 
SystemAlignedFree(void * ptr)41 void SystemAlignedFree(void *ptr) {
42 #ifdef _WIN32
43   _aligned_free(ptr);
44 #else
45   free(ptr);
46 #endif
47 }
48 
49 }  // namespace detail
50 
51 }  // namespace ruy
52