1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "platform/include/allocator.h"
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #define MEM_ALIGN 16
21 
22 static inline void *AllocatorMalloc(size_t size);
23 static inline void *AllocatorCalloc(size_t size);
24 static inline void AllocatorFree(void *ptr);
25 
26 const Allocator MEM_MALLOC = {
27     .alloc = AllocatorMalloc,
28     .free = AllocatorFree,
29 };
30 const Allocator MEM_CALLOC = {
31     .alloc = AllocatorCalloc,
32     .free = AllocatorFree,
33 };
34 
SysMalloc(size_t size)35 void *SysMalloc(size_t size)
36 {
37     return malloc(size);
38 }
39 
SysCalloc(size_t size)40 void *SysCalloc(size_t size)
41 {
42     return calloc(1, size);
43 }
44 
SysFree(void * ptr)45 void SysFree(void *ptr)
46 {
47     if (ptr != NULL) {
48         free(ptr);
49     }
50 }
51 
AllocatorMalloc(size_t size)52 static void *AllocatorMalloc(size_t size)
53 {
54     return malloc(size);
55 }
56 
AllocatorCalloc(size_t size)57 static void *AllocatorCalloc(size_t size)
58 {
59     return calloc(1, size);
60 }
61 
AllocatorFree(void * ptr)62 static void AllocatorFree(void *ptr)
63 {
64     if (ptr != NULL) {
65         free(ptr);
66     }
67 }