• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "stdlib.h"
33 #include "string.h"
34 #include "los_vm_map.h"
35 
36 /*
37  * Allocates the requested memory and returns a pointer to it. The requested
38  * size is nitems each size bytes long (total memory requested is nitems*size).
39  * The space is initialized to all zero bits.
40  */
41 
calloc(size_t nitems,size_t size)42 void *calloc(size_t nitems, size_t size)
43 {
44     size_t real_size;
45     void *ptr = NULL;
46 
47     if (nitems == 0 || size == 0) {
48         return NULL;
49     }
50 
51     real_size = (size_t)(nitems * size);
52     ptr = LOS_KernelMalloc((UINT32) real_size);
53     if (ptr != NULL) {
54         (void) memset_s((void *) ptr, real_size, 0, real_size);
55     }
56     return ptr;
57 }
58 
59 /*
60  * Deallocates the memory previously allocated by a call to calloc, malloc, or
61  * realloc. The argument ptr points to the space that was previously allocated.
62  * If ptr points to a memory block that was not allocated with calloc, malloc,
63  * or realloc, or is a space that has been deallocated, then the result is undefined.
64  */
65 
free(void * ptr)66 void free(void *ptr)
67 {
68     if (ptr == NULL) {
69         return;
70     }
71 
72     LOS_KernelFree(ptr);
73 }
74 
75 /*
76  * Allocates the requested memory and returns a pointer to it. The requested
77  * size is size bytes. The value of the space is indeterminate.
78  */
79 
malloc(size_t size)80 void *malloc(size_t size)
81 {
82     if (size == 0) {
83         return NULL;
84     }
85 
86     return LOS_KernelMalloc((UINT32) size);
87 }
88 
zalloc(size_t size)89 void *zalloc(size_t size)
90 {
91     void *ptr = NULL;
92 
93     if (size == 0) {
94         return NULL;
95     }
96 
97     ptr = LOS_KernelMalloc((UINT32) size);
98     if (ptr != NULL) {
99         (void) memset_s(ptr, size, 0, size);
100     }
101     return ptr;
102 }
103 
104 /*
105  * allocates a block of size bytes whose address is a multiple of boundary.
106  * The boundary must be a power of two!
107  */
108 
memalign(size_t boundary,size_t size)109 void *memalign(size_t boundary, size_t size)
110 {
111     if (size == 0) {
112         return NULL;
113     }
114 
115     return LOS_KernelMallocAlign((UINT32) size, (UINT32) boundary);
116 }
117 
118 /*
119  * Attempts to resize the memory block pointed to by ptr that was previously
120  * allocated with a call to malloc or calloc. The contents pointed to by ptr are
121  * unchanged. If the value of size is greater than the previous size of the
122  * block, then the additional bytes have an undeterminate value. If the value
123  * of size is less than the previous size of the block, then the difference of
124  * bytes at the end of the block are freed. If ptr is null, then it behaves like
125  * malloc. If ptr points to a memory block that was not allocated with calloc
126  * or malloc, or is a space that has been deallocated, then the result is
127  * undefined. If the new space cannot be allocated, then the contents pointed
128  * to by ptr are unchanged. If size is zero, then the memory block is completely
129  * freed.
130  */
131 
realloc(void * ptr,size_t size)132 void *realloc(void *ptr, size_t size)
133 {
134     if (ptr == NULL) {
135         ptr = malloc(size);
136         return ptr;
137     }
138 
139     if (size == 0) {
140         free(ptr);
141         return NULL;
142     }
143 
144     return LOS_KernelRealloc(ptr, (UINT32) size);
145 }
146