• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(_MSC_VER)
13   #include <malloc.h>
14 #elif !defined(__GNUC__)
15   #include <alloca.h>
16 #endif
17 
18 #include <xnnpack.h>
19 #include <xnnpack/common.h>
20 #include <xnnpack/params.h>
21 
22 
23 #if XNN_ARCH_WASM
24   #define XNN_ALLOCATION_ALIGNMENT 4
25 #elif XNN_ARCH_X86 || XNN_ARCH_X86_64
26   #if XNN_PLATFORM_MOBILE
27     #define XNN_ALLOCATION_ALIGNMENT 32
28   #else
29     #define XNN_ALLOCATION_ALIGNMENT 64
30   #endif
31 #else
32   #define XNN_ALLOCATION_ALIGNMENT 16
33 #endif
34 
35 XNN_INTERNAL extern const struct xnn_allocator xnn_default_allocator;
36 
xnn_allocate_memory(size_t memory_size)37 inline static void* xnn_allocate_memory(size_t memory_size) {
38   return xnn_params.allocator.allocate(xnn_params.allocator.context, memory_size);
39 }
40 
xnn_allocate_zero_memory(size_t memory_size)41 inline static void* xnn_allocate_zero_memory(size_t memory_size) {
42   void* memory_pointer = xnn_params.allocator.allocate(xnn_params.allocator.context, memory_size);
43   if (memory_pointer != NULL) {
44     memset(memory_pointer, 0, memory_size);
45   }
46   return memory_pointer;
47 }
48 
xnn_reallocate_memory(void * memory_pointer,size_t memory_size)49 inline static void* xnn_reallocate_memory(void* memory_pointer, size_t memory_size) {
50   return xnn_params.allocator.reallocate(xnn_params.allocator.context, memory_pointer, memory_size);
51 }
52 
xnn_release_memory(void * memory_pointer)53 inline static void xnn_release_memory(void* memory_pointer) {
54   xnn_params.allocator.deallocate(xnn_params.allocator.context, memory_pointer);
55 }
56 
xnn_allocate_simd_memory(size_t memory_size)57 inline static void* xnn_allocate_simd_memory(size_t memory_size) {
58   return xnn_params.allocator.aligned_allocate(xnn_params.allocator.context, XNN_ALLOCATION_ALIGNMENT, memory_size);
59 }
60 
xnn_allocate_zero_simd_memory(size_t memory_size)61 inline static void* xnn_allocate_zero_simd_memory(size_t memory_size) {
62   void* memory_pointer = xnn_params.allocator.aligned_allocate(
63     xnn_params.allocator.context, XNN_ALLOCATION_ALIGNMENT, memory_size);
64   if (memory_pointer != NULL) {
65     memset(memory_pointer, 0, memory_size);
66   }
67   return memory_pointer;
68 }
69 
xnn_release_simd_memory(void * memory_pointer)70 inline static void xnn_release_simd_memory(void* memory_pointer) {
71   xnn_params.allocator.aligned_deallocate(xnn_params.allocator.context, memory_pointer);
72 }
73 
74 #if defined(__GNUC__) && defined(__BIGGEST_ALIGNMENT__) && (__BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT)
75   #define XNN_SIMD_ALLOCA(size) __builtin_alloca((size))
76 #elif (defined(__clang_major__) && (__clang_major__ >= 4)) || \
77     (defined(__GNUC__) && (__GNUC__ >= 5 || __GNUC__ == 4 && __GNUC_MINOR__ >= 7) && !defined(__INTEL_COMPILER))
78   #define XNN_SIMD_ALLOCA(size) __builtin_alloca_with_align((size), XNN_ALLOCATION_ALIGNMENT)
79 #elif defined(__GNUC__)
80   #define XNN_SIMD_ALLOCA(size) \
81     ((void*) ((((uintptr_t) __builtin_alloca((size) + XNN_ALLOCATION_ALIGNMENT)) | (XNN_ALLOCATION_ALIGNMENT - 1)) + 1))
82 #elif defined(_MSC_VER)
83   #define XNN_SIMD_ALLOCA(size) \
84     ((void*) ((((uintptr_t) _alloca((size) + XNN_ALLOCATION_ALIGNMENT)) | (XNN_ALLOCATION_ALIGNMENT - 1)) + 1))
85 #else
86   #define XNN_SIMD_ALLOCA(size) \
87     ((void*) ((((uintptr_t) alloca((size) + XNN_ALLOCATION_ALIGNMENT)) | (XNN_ALLOCATION_ALIGNMENT - 1)) + 1))
88 #endif
89 
90 #define XNN_DEFAULT_CODE_BUFFER_SIZE 16384  // 16kb.
91 
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95 
96 struct xnn_code_buffer {
97   // Pointer to allocated, externally managed memory.
98   void* code;
99   // Actual size of instructions (bytes). It is only safe to access code within
100   // this size.
101   size_t size;
102   // Maximum capacity of the buffer pointer to by `code`. This is the size of
103   // the currently mapped memory.
104   size_t capacity;
105 };
106 
107 // Allocates a code region and associates it with `buf`.
108 enum xnn_status xnn_allocate_code_memory(struct xnn_code_buffer* buf, size_t size);
109 // Finalize buffer, users won't need to call this directly, called by Assembler.
110 enum xnn_status xnn_finalize_code_memory(struct xnn_code_buffer* buf);
111 // Free all memory associated with `buf`.
112 enum xnn_status xnn_release_code_memory(struct xnn_code_buffer* buf);
113 
114 #ifdef __cplusplus
115 }  // extern "C"
116 #endif
117