1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <time.h>
7 
8 #include <k_api.h>
9 #include <aos/errno.h>
10 #include <aos/kernel.h>
11 
12 #include "rhino_p.h"
13 
14 #if (RHINO_CONFIG_KOBJ_DYN_ALLOC == 0)
15 #warning "RHINO_CONFIG_KOBJ_DYN_ALLOC is disabled!"
16 #endif
17 
aos_zalloc(size_t size)18 void *aos_zalloc(size_t size)
19 {
20     void *tmp = NULL;
21 
22     if (size == 0) {
23         return NULL;
24     }
25 
26 #if (RHINO_CONFIG_MM_DEBUG > 0u)
27     tmp = krhino_mm_alloc(size | AOS_UNSIGNED_INT_MSB);
28     krhino_owner_return_addr(tmp);
29 #else
30     tmp = krhino_mm_alloc(size);
31 #endif
32 
33     if (tmp) {
34         memset(tmp, 0, size);
35     }
36 
37     return tmp;
38 }
39 
aos_malloc(size_t size)40 void *aos_malloc(size_t size)
41 {
42     void *tmp = NULL;
43 
44     if (size == 0) {
45         return NULL;
46     }
47 
48 #if (RHINO_CONFIG_MM_DEBUG > 0u)
49     tmp = krhino_mm_alloc(size | AOS_UNSIGNED_INT_MSB);
50     krhino_owner_return_addr(tmp);
51 #else
52     tmp = krhino_mm_alloc(size);
53 #endif
54 
55     return tmp;
56 }
57 
aos_calloc(size_t nitems,size_t size)58 void *aos_calloc(size_t nitems, size_t size)
59 {
60     void *tmp = NULL;
61     size_t len = (size_t)nitems * size;
62 
63     if (len == 0) {
64         return NULL;
65     }
66 
67 #if (RHINO_CONFIG_MM_DEBUG > 0u)
68     tmp = krhino_mm_alloc(len | AOS_UNSIGNED_INT_MSB);
69     krhino_owner_return_addr(tmp);
70 #else
71     tmp = krhino_mm_alloc(len);
72 #endif
73 
74     if (tmp) {
75         memset(tmp, 0, len);
76     }
77 
78     return tmp;
79 }
80 
aos_realloc(void * mem,size_t size)81 void *aos_realloc(void *mem, size_t size)
82 {
83     void *tmp = NULL;
84 
85 #if (RHINO_CONFIG_MM_DEBUG > 0u)
86     tmp = krhino_mm_realloc(mem, size | AOS_UNSIGNED_INT_MSB);
87     krhino_owner_return_addr(tmp);
88 #else
89     tmp = krhino_mm_realloc(mem, size);
90 #endif
91 
92     return tmp;
93 }
94 
aos_alloc_trace(void * addr,uintptr_t allocator)95 void aos_alloc_trace(void *addr, uintptr_t allocator)
96 {
97 #if (RHINO_CONFIG_MM_DEBUG > 0u)
98     krhino_owner_attach(addr, allocator);
99 #endif
100 }
101 
aos_free(void * mem)102 void aos_free(void *mem)
103 {
104     if (mem == NULL) {
105         return;
106     }
107 
108     krhino_mm_free(mem);
109 }
110