1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 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 "securec.h"
33 #include "los_config.h"
34 #include "los_memory.h"
35 #include <malloc.h>
36
zalloc(size_t size)37 void *zalloc(size_t size)
38 {
39 void *ptr = NULL;
40
41 if (size == 0) {
42 return NULL;
43 }
44
45 ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
46 if (ptr != NULL) {
47 (void)memset_s(ptr, size, 0, size);
48 }
49 return ptr;
50 }
51
__wrap__free_r(struct _reent * reent,void * aptr)52 void __wrap__free_r(struct _reent *reent, void *aptr)
53 {
54 if (aptr == NULL) {
55 return;
56 }
57
58 LOS_MemFree(OS_SYS_MEM_ADDR, aptr);
59 }
60
__wrap__malloc_usable_size_r(struct _reent * reent,void * aptr)61 size_t __wrap__malloc_usable_size_r(struct _reent *reent, void *aptr)
62 {
63 return 0;
64 }
65
__wrap__malloc_r(struct _reent * reent,size_t nbytes)66 void *__wrap__malloc_r(struct _reent *reent, size_t nbytes)
67 {
68 if (nbytes == 0) {
69 return NULL;
70 }
71
72 return LOS_MemAlloc(OS_SYS_MEM_ADDR, nbytes);
73 }
74
__wrap__memalign_r(struct _reent * reent,size_t align,size_t nbytes)75 void *__wrap__memalign_r(struct _reent *reent, size_t align, size_t nbytes)
76 {
77 if (nbytes == 0) {
78 return NULL;
79 }
80
81 return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, nbytes, align);
82 }
83
__wrap__realloc_r(struct _reent * reent,void * aptr,size_t nbytes)84 void *__wrap__realloc_r(struct _reent *reent, void *aptr, size_t nbytes)
85 {
86 if (aptr == NULL) {
87 return malloc(nbytes);
88 }
89
90 if (nbytes == 0) {
91 free(aptr);
92 return NULL;
93 }
94
95 return LOS_MemRealloc(OS_SYS_MEM_ADDR, aptr, nbytes);
96 }
97
__wrap__calloc_r(struct _reent * reent,size_t nitems,size_t size)98 void *__wrap__calloc_r(struct _reent *reent, size_t nitems, size_t size)
99 {
100 size_t real_size;
101 void *ptr = NULL;
102
103 if (nitems == 0 || size == 0) {
104 return NULL;
105 }
106
107 real_size = (size_t)(nitems * size);
108 ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, real_size);
109 if (ptr != NULL) {
110 (void)memset_s(ptr, real_size, 0, real_size);
111 }
112 return ptr;
113 }
114