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 #include <assert.h>
7 #include <stddef.h>
8 #include <stdlib.h>
9 #ifdef __ANDROID__
10 #include <malloc.h>
11 #endif
12
13 #include <xnnpack/common.h>
14 #include <xnnpack/memory.h>
15
16
17 extern int posix_memalign(void **memptr, size_t alignment, size_t size);
18
19
xnn_allocate(void * context,size_t size)20 void* xnn_allocate(void* context, size_t size) {
21 return malloc(size);
22 }
23
xnn_reallocate(void * context,void * pointer,size_t size)24 void* xnn_reallocate(void* context, void* pointer, size_t size) {
25 return realloc(pointer, size);
26 }
27
xnn_deallocate(void * context,void * pointer)28 void xnn_deallocate(void* context, void* pointer) {
29 if XNN_LIKELY(pointer != NULL) {
30 free(pointer);
31 }
32 }
33
xnn_aligned_allocate(void * context,size_t alignment,size_t size)34 void* xnn_aligned_allocate(void* context, size_t alignment, size_t size) {
35 #if XNN_ARCH_ASMJS || XNN_ARCH_WASM
36 assert(alignment <= 2 * sizeof(void*));
37 return malloc(size);
38 #elif defined(__ANDROID__)
39 return memalign(alignment, size);
40 #else
41 void* memory_ptr = NULL;
42 if (posix_memalign(&memory_ptr, alignment, size) != 0) {
43 return NULL;
44 }
45 return memory_ptr;
46 #endif
47 }
48
xnn_aligned_deallocate(void * context,void * pointer)49 void xnn_aligned_deallocate(void* context, void* pointer) {
50 if XNN_LIKELY(pointer != NULL) {
51 free(pointer);
52 }
53 }
54