XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // This source code is licensed under the BSD-style license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 11 | |
Marat Dukhan | d0cf9bd | 2020-04-13 20:19:19 -0700 | [diff] [blame] | 12 | #if defined(_MSC_VER) |
| 13 | #include <malloc.h> |
| 14 | #elif !defined(__GNUC__) |
| 15 | #include <alloca.h> |
| 16 | #endif |
| 17 | |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 18 | #include <xnnpack/common.h> |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 19 | #include <xnnpack/params.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 20 | |
| 21 | |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 22 | #if XNN_ARCH_ASMJS || XNN_ARCH_WASM |
| 23 | #define XNN_ALLOCATION_ALIGNMENT 4 |
Marat Dukhan | 17ec5f3 | 2019-11-22 13:34:16 -0800 | [diff] [blame] | 24 | #elif (XNN_ARCH_X86 || XNN_ARCH_X86_64) && !XNN_PLATFORM_MOBILE |
| 25 | #define XNN_ALLOCATION_ALIGNMENT 64 |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 26 | #else |
| 27 | #define XNN_ALLOCATION_ALIGNMENT 16 |
| 28 | #endif |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 29 | |
| 30 | inline static void* xnn_allocate_memory(size_t memory_size) { |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 31 | return xnn_params.allocator.allocate(xnn_params.allocator.context, memory_size); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Marat Dukhan | 1d75a54 | 2020-02-03 12:23:01 -0800 | [diff] [blame] | 34 | inline static void* xnn_allocate_zero_memory(size_t memory_size) { |
| 35 | void* memory_pointer = xnn_params.allocator.allocate(xnn_params.allocator.context, memory_size); |
| 36 | if (memory_pointer != NULL) { |
| 37 | memset(memory_pointer, 0, memory_size); |
| 38 | } |
| 39 | return memory_pointer; |
| 40 | } |
| 41 | |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 42 | inline static void* xnn_reallocate_memory(void* memory_pointer, size_t memory_size) { |
| 43 | return xnn_params.allocator.reallocate(xnn_params.allocator.context, memory_pointer, memory_size); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 46 | inline static void xnn_release_memory(void* memory_pointer) { |
| 47 | xnn_params.allocator.deallocate(xnn_params.allocator.context, memory_pointer); |
| 48 | } |
| 49 | |
| 50 | inline static void* xnn_allocate_simd_memory(size_t memory_size) { |
| 51 | return xnn_params.allocator.aligned_allocate(xnn_params.allocator.context, XNN_ALLOCATION_ALIGNMENT, memory_size); |
| 52 | } |
| 53 | |
| 54 | inline static void* xnn_allocate_zero_simd_memory(size_t memory_size) { |
| 55 | void* memory_pointer = xnn_params.allocator.aligned_allocate( |
| 56 | xnn_params.allocator.context, XNN_ALLOCATION_ALIGNMENT, memory_size); |
| 57 | if (memory_pointer != NULL) { |
| 58 | memset(memory_pointer, 0, memory_size); |
| 59 | } |
| 60 | return memory_pointer; |
| 61 | } |
| 62 | |
| 63 | inline static void xnn_release_simd_memory(void* memory_pointer) { |
| 64 | xnn_params.allocator.aligned_deallocate(xnn_params.allocator.context, memory_pointer); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 65 | } |
Marat Dukhan | d0cf9bd | 2020-04-13 20:19:19 -0700 | [diff] [blame] | 66 | |
| 67 | #if defined(__GNUC__) && defined(__BIGGEST_ALIGNMENT__) && (__BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT) |
| 68 | #define XNN_SIMD_ALLOCA(size) __builtin_alloca((size)) |
| 69 | #elif (defined(__clang_major__) && (__clang_major__ >= 4)) || \ |
| 70 | (defined(__GNUC__) && (__GNUC__ >= 5 || __GNUC__ == 4 && __GNUC_MINOR__ >= 7) && !defined(__INTEL_COMPILER)) |
| 71 | #define XNN_SIMD_ALLOCA(size) __builtin_alloca_with_align((size), XNN_ALLOCATION_ALIGNMENT) |
| 72 | #elif defined(__GNUC__) |
| 73 | #define XNN_SIMD_ALLOCA(size) \ |
| 74 | ((void*) ((((uintptr_t) __builtin_alloca((size) + XNN_ALLOCATION_ALIGNMENT)) | (XNN_ALLOCATION_ALIGNMENT - 1)) + 1)) |
| 75 | #elif defined(_MSC_VER) |
| 76 | #define XNN_SIMD_ALLOCA(size) \ |
| 77 | ((void*) ((((uintptr_t) _alloca((size) + XNN_ALLOCATION_ALIGNMENT)) | (XNN_ALLOCATION_ALIGNMENT - 1)) + 1)) |
| 78 | #else |
| 79 | #define XNN_SIMD_ALLOCA(size) \ |
| 80 | ((void*) ((((uintptr_t) alloca((size) + XNN_ALLOCATION_ALIGNMENT)) | (XNN_ALLOCATION_ALIGNMENT - 1)) + 1)) |
| 81 | #endif |