• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <stdint.h>
33 #include "securec.h"
34 #include "los_config.h"
35 #include "los_memory.h"
36 
37 #if (LOSCFG_LIBC_NEWLIB == 1)
__wrap__calloc_r(struct _reent * reent,size_t nitems,size_t size)38 void *__wrap__calloc_r(struct _reent *reent, size_t nitems, size_t size)
39 #else
40 void *calloc(size_t nitems, size_t size)
41 #endif
42 {
43 #if (LOSCFG_LIBC_NEWLIB == 1)
44     (void)reent;
45 #endif
46     size_t real_size;
47     void *ptr = NULL;
48 
49     if ((nitems == 0) || (size == 0) || (nitems > (UINT32_MAX / size))) {
50         return NULL;
51     }
52 
53     real_size = (size_t)(nitems * size);
54     ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, real_size);
55     if (ptr != NULL) {
56         (void)memset_s(ptr, real_size, 0, real_size);
57     }
58     return ptr;
59 }
60 
61 #if (LOSCFG_LIBC_NEWLIB == 1)
__wrap__free_r(struct _reent * reent,void * ptr)62 void __wrap__free_r(struct _reent *reent, void *ptr)
63 #else
64 void free(void *ptr)
65 #endif
66 {
67 #if (LOSCFG_LIBC_NEWLIB == 1)
68     (void)reent;
69 #endif
70     if (ptr == NULL) {
71         return;
72     }
73 
74     LOS_MemFree(OS_SYS_MEM_ADDR, ptr);
75 }
76 
77 #if (LOSCFG_LIBC_NEWLIB == 1)
__wrap__malloc_usable_size_r(struct _reent * reent,void * aptr)78 size_t __wrap__malloc_usable_size_r(struct _reent *reent, void *aptr)
79 {
80     (void)reent;
81     (void)aptr;
82     return 0;
83 }
84 #endif
85 
86 #if (LOSCFG_LIBC_NEWLIB == 1)
__wrap__malloc_r(struct _reent * reent,size_t size)87 void *__wrap__malloc_r(struct _reent *reent, size_t size)
88 #else
89 void *malloc(size_t size)
90 #endif
91 {
92 #if (LOSCFG_LIBC_NEWLIB == 1)
93     (void)reent;
94 #endif
95     if (size == 0) {
96         return NULL;
97     }
98 
99     return LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
100 }
101 
zalloc(size_t size)102 void *zalloc(size_t size)
103 {
104     void *ptr = NULL;
105 
106     if (size == 0) {
107         return NULL;
108     }
109 
110     ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
111     if (ptr != NULL) {
112         (void)memset_s(ptr, size, 0, size);
113     }
114     return ptr;
115 }
116 
117 #if (LOSCFG_LIBC_NEWLIB == 1)
__wrap__memalign_r(struct _reent * reent,size_t boundary,size_t size)118 void *__wrap__memalign_r(struct _reent *reent, size_t boundary, size_t size)
119 #else
120 void *memalign(size_t boundary, size_t size)
121 #endif
122 {
123 #if (LOSCFG_LIBC_NEWLIB == 1)
124     (void)reent;
125 #endif
126     if (size == 0) {
127         return NULL;
128     }
129 
130     return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, size, boundary);
131 }
132 
133 #if (LOSCFG_LIBC_NEWLIB == 1)
__wrap__realloc_r(struct _reent * reent,void * ptr,size_t size)134 void *__wrap__realloc_r(struct _reent *reent, void *ptr, size_t size)
135 #else
136 void *realloc(void *ptr, size_t size)
137 #endif
138 {
139 #if (LOSCFG_LIBC_NEWLIB == 1)
140     (void)reent;
141 #endif
142     if (ptr == NULL) {
143         return malloc(size);
144     }
145 
146     if (size == 0) {
147         free(ptr);
148         return NULL;
149     }
150 
151     return LOS_MemRealloc(OS_SYS_MEM_ADDR, ptr, size);
152 }
153