更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
malloc.c
浏览该文件的文档.
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
42void *calloc(size_t nitems, size_t size)
43{ /*lint !e578*/
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/// 释放ptr所指向的内存空间
66void 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 * 分配请求的内存并返回指向它的指针。 请求的大小是 size 字节。 内存值是不确定的。
79 */
80/// 动态分配内存块大小
81void *malloc(size_t size)
82{ /*lint !e31 !e10*/
83 if (size == 0) {
84 return NULL;
85 }
86
87 return LOS_KernelMalloc((UINT32) size);
88}
89/// 分配请求的内存并返回指向它的指针。 请求的大小是 size 字节。 内存值是0。
90/// 原来 zalloc的含义是 zero malloc
91void *zalloc(size_t size)
92{ /*lint !e10*/
93 void *ptr = NULL;
94
95 if (size == 0) {
96 return NULL;
97 }
98
99 ptr = LOS_KernelMalloc((UINT32) size);
100 if (ptr != NULL) {
101 (void) memset_s(ptr, size, 0, size);
102 }
103 return ptr;
104}
105
106/*
107 * allocates a block of size bytes whose address is a multiple of boundary.
108 * The boundary must be a power of two!
109 * 分配一个大小字节的块,其地址是边界的倍数。边界必须是 2 的幂!
110 */
111
112void *memalign(size_t boundary, size_t size)
113{
114 if (size == 0) {
115 return NULL;
116 }
117
118 return LOS_KernelMallocAlign((UINT32) size, (UINT32) boundary);
119}
120
121/*
122 * Attempts to resize the memory block pointed to by ptr that was previously
123 * allocated with a call to malloc or calloc. The contents pointed to by ptr are
124 * unchanged. If the value of size is greater than the previous size of the
125 * block, then the additional bytes have an undeterminate value. If the value
126 * of size is less than the previous size of the block, then the difference of
127 * bytes at the end of the block are freed. If ptr is null, then it behaves like
128 * malloc. If ptr points to a memory block that was not allocated with calloc
129 * or malloc, or is a space that has been deallocated, then the result is
130 * undefined. If the new space cannot be allocated, then the contents pointed
131 * to by ptr are unchanged. If size is zero, then the memory block is completely
132 * freed.
133 * 尝试调整先前通过调用 malloc 或 calloc 分配的 ptr 指向的内存块的大小。
134 * ptr 指向的内容不变。 如果 size 的值大于块的先前大小,则附加字节具有不确定的值。
135 * 如果 size 的值小于块的先前大小,则释放块末尾的字节差。 如果 ptr 为 null,
136 * 则它的行为类似于 malloc。 如果 ptr 指向未使用 calloc 或 malloc 分配的内存块,
137 * 或者是已释放的空间,则结果未定义。 如果无法分配新空间,则 ptr 指向的内容不变。
138 * 如果大小为零,则内存块被完全释放。
139 */
140/// 重分配内存
141void *realloc(void *ptr, size_t size)
142{
143 if (ptr == NULL) {
144 ptr = malloc(size);
145 return ptr;
146 }
147
148 if (size == 0) {
149 free(ptr);
150 return NULL;
151 }
152
153 return LOS_KernelRealloc(ptr, (UINT32) size);
154}
UINT64 size_t
Definition: los_typedef.h:80
unsigned int UINT32
Definition: los_typedef.h:57
VOID * LOS_KernelMalloc(UINT32 size)
VOID * LOS_KernelRealloc(VOID *ptr, UINT32 size)
VOID LOS_KernelFree(VOID *ptr)
VOID * LOS_KernelMallocAlign(UINT32 size, UINT32 boundary)
void * zalloc(size_t size)
Definition: malloc.c:91
void * memalign(size_t boundary, size_t size)
Definition: malloc.c:112
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void * realloc(void *ptr, size_t size)
重分配内存
Definition: malloc.c:141
void * calloc(size_t nitems, size_t size)
Definition: malloc.c:42
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void