Jared Duke | 13689fe | 2019-04-16 16:22:07 -0400 | [diff] [blame] | 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 | |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 16 | #include "allocator.h" |
| 17 | |
Benoit Jacob | 607e445 | 2019-08-21 15:37:39 -0400 | [diff] [blame] | 18 | #include <cstdint> |
Benoit Jacob | b21b310 | 2019-04-15 11:52:05 -0400 | [diff] [blame] | 19 | #include <cstdlib> |
| 20 | |
| 21 | #ifdef _WIN32 |
| 22 | #include <malloc.h> |
| 23 | #endif |
| 24 | |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 25 | namespace ruy { |
| 26 | |
Sean Silva | d8bbade | 2019-04-12 12:32:11 -0400 | [diff] [blame] | 27 | namespace detail { |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 28 | |
Sean Silva | c85f6d7 | 2019-12-03 13:42:15 -0500 | [diff] [blame] | 29 | void *SystemAlignedAlloc(std::ptrdiff_t num_bytes) { |
Benoit Jacob | b21b310 | 2019-04-15 11:52:05 -0400 | [diff] [blame] | 30 | #ifdef _WIN32 |
Sean Silva | c85f6d7 | 2019-12-03 13:42:15 -0500 | [diff] [blame] | 31 | return _aligned_malloc(num_bytes, kMinimumBlockAlignment); |
Benoit Jacob | b21b310 | 2019-04-15 11:52:05 -0400 | [diff] [blame] | 32 | #else |
Sean Silva | d8bbade | 2019-04-12 12:32:11 -0400 | [diff] [blame] | 33 | void *ptr; |
Sean Silva | c85f6d7 | 2019-12-03 13:42:15 -0500 | [diff] [blame] | 34 | if (posix_memalign(&ptr, kMinimumBlockAlignment, num_bytes)) { |
Sean Silva | d8bbade | 2019-04-12 12:32:11 -0400 | [diff] [blame] | 35 | return nullptr; |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 36 | } |
Sean Silva | d8bbade | 2019-04-12 12:32:11 -0400 | [diff] [blame] | 37 | return ptr; |
Benoit Jacob | b21b310 | 2019-04-15 11:52:05 -0400 | [diff] [blame] | 38 | #endif |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Sean Silva | c85f6d7 | 2019-12-03 13:42:15 -0500 | [diff] [blame] | 41 | void SystemAlignedFree(void *ptr) { |
Benoit Jacob | b21b310 | 2019-04-15 11:52:05 -0400 | [diff] [blame] | 42 | #ifdef _WIN32 |
| 43 | _aligned_free(ptr); |
| 44 | #else |
| 45 | free(ptr); |
| 46 | #endif |
| 47 | } |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 48 | |
Sean Silva | d8bbade | 2019-04-12 12:32:11 -0400 | [diff] [blame] | 49 | } // namespace detail |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 50 | |
| 51 | } // namespace ruy |