blob: a9683bd1603a0d84b590d27a195fdc2ea461efce [file] [log] [blame]
Marat Dukhan04f03be2019-11-19 12:36:47 -08001// 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
17extern int posix_memalign(void **memptr, size_t alignment, size_t size);
18
19
20void* xnn_allocate(void* context, size_t size) {
21 return malloc(size);
22}
23
24void* xnn_reallocate(void* context, void* pointer, size_t size) {
25 return realloc(pointer, size);
26}
27
28void xnn_deallocate(void* context, void* pointer) {
29 if XNN_LIKELY(pointer != NULL) {
30 free(pointer);
31 }
32}
33
34void* 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
49void xnn_aligned_deallocate(void* context, void* pointer) {
50 if XNN_LIKELY(pointer != NULL) {
51 free(pointer);
52 }
53}