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