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>
11
12 #include <xnnpack/common.h>
13 #include <xnnpack/params.h>
14
15
16 #if XNN_ARCH_ASMJS || XNN_ARCH_WASM
17 #define XNN_ALLOCATION_ALIGNMENT 4
18 #elif (XNN_ARCH_X86 || XNN_ARCH_X86_64) && !XNN_PLATFORM_MOBILE
19 #define XNN_ALLOCATION_ALIGNMENT 64
20 #else
21 #define XNN_ALLOCATION_ALIGNMENT 16
22 #endif
23
xnn_allocate_memory(size_t memory_size)24 inline static void* xnn_allocate_memory(size_t memory_size) {
25 return xnn_params.allocator.allocate(xnn_params.allocator.context, memory_size);
26 }
27
xnn_allocate_zero_memory(size_t memory_size)28 inline static void* xnn_allocate_zero_memory(size_t memory_size) {
29 void* memory_pointer = xnn_params.allocator.allocate(xnn_params.allocator.context, memory_size);
30 if (memory_pointer != NULL) {
31 memset(memory_pointer, 0, memory_size);
32 }
33 return memory_pointer;
34 }
35
xnn_reallocate_memory(void * memory_pointer,size_t memory_size)36 inline static void* xnn_reallocate_memory(void* memory_pointer, size_t memory_size) {
37 return xnn_params.allocator.reallocate(xnn_params.allocator.context, memory_pointer, memory_size);
38 }
39
xnn_release_memory(void * memory_pointer)40 inline static void xnn_release_memory(void* memory_pointer) {
41 xnn_params.allocator.deallocate(xnn_params.allocator.context, memory_pointer);
42 }
43
xnn_allocate_simd_memory(size_t memory_size)44 inline static void* xnn_allocate_simd_memory(size_t memory_size) {
45 return xnn_params.allocator.aligned_allocate(xnn_params.allocator.context, XNN_ALLOCATION_ALIGNMENT, memory_size);
46 }
47
xnn_allocate_zero_simd_memory(size_t memory_size)48 inline static void* xnn_allocate_zero_simd_memory(size_t memory_size) {
49 void* memory_pointer = xnn_params.allocator.aligned_allocate(
50 xnn_params.allocator.context, XNN_ALLOCATION_ALIGNMENT, memory_size);
51 if (memory_pointer != NULL) {
52 memset(memory_pointer, 0, memory_size);
53 }
54 return memory_pointer;
55 }
56
xnn_release_simd_memory(void * memory_pointer)57 inline static void xnn_release_simd_memory(void* memory_pointer) {
58 xnn_params.allocator.aligned_deallocate(xnn_params.allocator.context, memory_pointer);
59 }
60