更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_memory.h
浏览该文件的文档.
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/**
33 * @defgroup los_memory Dynamic memory
34 * @ingroup kernel
35 */
36
37#ifndef _LOS_MEMORY_H
38#define _LOS_MEMORY_H
39
40#include "los_config.h"
41#include "los_base.h"
42#include "los_toolchain.h"
43#include "los_membox.h"
44
45#ifdef __cplusplus
46#if __cplusplus
47extern "C" {
48#endif /* __cplusplus */
49#endif /* __cplusplus */
50
51#ifdef LOSCFG_MEM_LEAKCHECK
52
53/**
54 * @ingroup los_memory
55 * The omit layers of function call from call kernel memory interfaces
56 * such as LOS_MemAlloc/LOS_MemAllocAlign/LOS_MemRealloc/LOS_MemFree.
57 */
58#define LOS_OMIT_LR_CNT 2
59
60/**
61 * @ingroup los_memory
62 * The recored layes of function call.
63 */
64#define LOS_RECORD_LR_CNT 3
65#endif
66
67/**
68 * @ingroup los_memory
69 * The start address of exc interaction dynamic memory pool address, when the exc
70 * interaction feature not support, m_aucSysMem0 equals to m_aucSysMem1.
71 */
72extern UINT8 *m_aucSysMem0;//异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
73
74/**
75 * @ingroup los_memory
76 * The start address of system dynamic memory pool address.
77 */
78extern UINT8 *m_aucSysMem1;//系统动态内存池地址的起始地址
79
80#ifdef LOSCFG_MEM_MUL_POOL
81/**
82 * @ingroup los_memory
83 * @brief Deinitialize dynamic memory.
84 *
85 * @par Description:
86 * <ul>
87 * <li>This API is used to deinitialize the dynamic memory of a doubly linked list.</li>
88 * </ul>
89 *
90 * @param pool [IN] Starting address of memory.
91 *
92 * @retval #OS_ERROR The dynamic memory fails to be deinitialized.
93 * @retval #LOS_OK The dynamic memory is successfully deinitialized.
94 * @par Dependency:
95 * <ul>
96 * <li>los_memory.h: the header file that contains the API declaration.</li>
97 * </ul>
98 * @see None.
99 */
100extern UINT32 LOS_MemDeInit(VOID *pool);
101
102/**
103 * @ingroup los_memory
104 * @brief Print infomation about all pools.
105 *
106 * @par Description:
107 * <ul>
108 * <li>This API is used to print infomation about all pools.</li>
109 * </ul>
110 *
111 * @retval #UINT32 The pool number.
112 * @par Dependency:
113 * <ul>
114 * <li>los_memory.h: the header file that contains the API declaration.</li>
115 * </ul>
116 * @see None.
117 */
118extern UINT32 LOS_MemPoolList(VOID);
119#endif
120
121/**
122 * @ingroup los_memory
123 * Memory pool extern information structure
124 */
125typedef struct {//内存池外部信息结构
126 UINT32 totalUsedSize; // 内存池的内存使用量
127 UINT32 totalFreeSize; // 内存池的剩余内存大小
128 UINT32 maxFreeNodeSize; // 内存池的最大空闲内存块大小
129 UINT32 usedNodeNum; // 内存池的非空闲内存块个数
130 UINT32 freeNodeNum; // 内存池的空闲内存块个数
131#ifdef LOSCFG_MEM_WATERLINE
132 UINT32 usageWaterLine; // 内存池的水线值
133#endif
135
136/**
137 * @ingroup los_memory
138 * @brief Initialize dynamic memory.
139 *
140 * @par Description:
141 * <ul>
142 * <li>This API is used to initialize the dynamic memory of a doubly linked list.</li>
143 * </ul>
144 * @attention
145 * <ul>
146 * <li>The size parameter value should match the following two conditions :
147 * 1) Be less than or equal to the Memory pool size;
148 * 2) Be greater than the size of OS_MEM_MIN_POOL_SIZE.</li>
149 * <li>Call this API when dynamic memory needs to be initialized during the startup of Huawei LiteOS.</li>
150 * <li>The parameter input must be four byte-aligned.</li>
151 * <li>The init area [pool, pool + size] should not conflict with other pools.</li>
152 * </ul>
153 *
154 * @param pool [IN] Starting address of memory.
155 * @param size [IN] Memory size.
156 *
157 * @retval #OS_ERROR The dynamic memory fails to be initialized.
158 * @retval #LOS_OK The dynamic memory is successfully initialized.
159 * @par Dependency:
160 * <ul>
161 * <li>los_memory.h: the header file that contains the API declaration.</li>
162 * </ul>
163 * @see None.
164 */
165extern UINT32 LOS_MemInit(VOID *pool, UINT32 size);
166
167/**
168 * @ingroup los_memory
169 * @brief Enable memory pool to support dynamic expansion.
170 *
171 * @par Description:
172 * <ul>
173 * <li>This API is used to enable the dynamic memory to expand size dynamically.</li>
174 * </ul>
175 * @attention
176 * <ul>
177 * <li>The memory pool is default diabled dynamic expansion.
178 * </ul>
179 *
180 * @param pool [IN] Starting address of memory.
181 *
182 * @retval node.
183 * @par Dependency:
184 * <ul>
185 * <li>los_memory.h: the header file that contains the API declaration.</li>
186 * </ul>
187 * @see None.
188 */
189extern VOID LOS_MemExpandEnable(VOID *pool);
190
191/**
192 * @ingroup los_memory
193 * @brief Allocate dynamic memory.
194 *
195 * @par Description:
196 * <ul>
197 * <li>This API is used to allocate a memory block of which the size is specified.</li>
198 * </ul>
199 * @attention
200 * <ul>
201 * <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
202 * <li>The size of the input parameter size can not be greater than the memory pool size that specified at the second
203 * input parameter of LOS_MemInit.</li>
204 * <li>The size of the input parameter size must be four byte-aligned.</li>
205 * </ul>
206 *
207 * @param pool [IN] Pointer to the memory pool that contains the memory block to be allocated.
208 * @param size [IN] Size of the memory block to be allocated (unit: byte).
209 *
210 * @retval #NULL The memory fails to be allocated.
211 * @retval #VOID* The memory is successfully allocated with the starting address of the allocated memory block
212 * returned.
213 * @par Dependency:
214 * <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
215 * @see LOS_MemRealloc | LOS_MemAllocAlign | LOS_MemFree
216 */
217extern VOID *LOS_MemAlloc(VOID *pool, UINT32 size);
218
219/**
220 * @ingroup los_memory
221 * @brief Free dynamic memory.
222 *
223 * @par Description:
224 * <li>This API is used to free specified dynamic memory that has been allocated.</li>
225 * @attention
226 * <ul>
227 * <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
228 * <li>The input ptr parameter must be allocated by LOS_MemAlloc or LOS_MemAllocAlign or LOS_MemRealloc.</li>
229 * </ul>
230 *
231 * @param pool [IN] Pointer to the memory pool that contains the dynamic memory block to be freed.
232 * @param ptr [IN] Starting address of the memory block to be freed.
233 *
234 * @retval #LOS_NOK The memory block fails to be freed because the starting address of the memory block is
235 * invalid, or the memory overwriting occurs.
236 * @retval #LOS_OK The memory block is successfully freed.
237 * @par Dependency:
238 * <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
239 * @see LOS_MemAlloc | LOS_MemRealloc | LOS_MemAllocAlign
240 */
241extern UINT32 LOS_MemFree(VOID *pool, VOID *ptr);
242
243/**
244 * @ingroup los_memory
245 * @brief Re-allocate a memory block.
246 *
247 * @par Description:
248 * <ul>
249 * <li>This API is used to allocate a new memory block of which the size is specified by size if the original memory
250 * block size is insufficient. The new memory block will copy the data in the original memory block of which the
251 * address is specified by ptr. The size of the new memory block determines the maximum size of data to be copied.
252 * After the new memory block is created, the original one is freed.</li>
253 * </ul>
254 * @attention
255 * <ul>
256 * <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
257 * <li>The input ptr parameter must be allocated by LOS_MemAlloc or LOS_MemAllocAlign.</li>
258 * <li>The size of the input parameter size can not be greater than the memory pool size that specified at the second
259 * input parameter of LOS_MemInit.</li>
260 * <li>The size of the input parameter size must be aligned as follows: 1) if the ptr is allocated by LOS_MemAlloc,
261 * it must be four byte-aligned; 2) if the ptr is allocated by LOS_MemAllocAlign, it must be aligned with the size of
262 * the input parameter boundary of LOS_MemAllocAlign.</li>
263 * </ul>
264 *
265 * @param pool [IN] Pointer to the memory pool that contains the original and new memory blocks.
266 * @param ptr [IN] Address of the original memory block.
267 * @param size [IN] Size of the new memory block.
268 *
269 * @retval #NULL The memory fails to be re-allocated.
270 * @retval #VOID* The memory is successfully re-allocated with the starting address of the new memory block returned.
271 * @par Dependency:
272 * <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
273 * @see LOS_MemAlloc | LOS_MemAllocAlign | LOS_MemFree
274 */
275extern VOID *LOS_MemRealloc(VOID *pool, VOID *ptr, UINT32 size);
276
277/**
278 * @ingroup los_memory
279 * @brief Allocate aligned memory.
280 *
281 * @par Description:
282 * <ul>
283 * <li>This API is used to allocate memory blocks of specified size and of which the starting addresses are aligned on
284 * a specified boundary.</li>
285 * </ul>
286 * @attention
287 * <ul>
288 * <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
289 * <li>The size of the input parameter size can not be greater than the memory pool size that specified at the second
290 * input parameter of LOS_MemInit.</li>
291 * <li>The alignment parameter value must be a power of 2 with the minimum value being 4.</li>
292 * </ul>
293 *
294 * @param pool [IN] Pointer to the memory pool that contains the memory blocks to be allocated.
295 * @param size [IN] Size of the memory to be allocated.
296 * @param boundary [IN] Boundary on which the memory is aligned.
297 *
298 * @retval #NULL The memory fails to be allocated.
299 * @retval #VOID* The memory is successfully allocated with the starting address of the allocated memory returned.
300 * @par Dependency:
301 * <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
302 * @see LOS_MemAlloc | LOS_MemRealloc | LOS_MemFree
303 */
304extern VOID *LOS_MemAllocAlign(VOID *pool, UINT32 size, UINT32 boundary);
305
306/**
307 * @ingroup los_memory
308 * @brief Get the size of memory pool's size.
309 *
310 * @par Description:
311 * <ul>
312 * <li>This API is used to get the size of memory pool' total size.</li>
313 * </ul>
314 * @attention
315 * <ul>
316 * <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
317 * </ul>
318 *
319 * @param pool [IN] A pointer pointed to the memory pool.
320 *
321 * @retval #LOS_NOK The incoming parameter pool is NULL.
322 * @retval #UINT32 The size of the memory pool.
323 * @par Dependency:
324 * <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
325 * @see None.
326 */
327extern UINT32 LOS_MemPoolSizeGet(const VOID *pool);
328
329/**
330 * @ingroup los_memory
331 * @brief Get the size of memory totally used.
332 *
333 * @par Description:
334 * <ul>
335 * <li>This API is used to get the size of memory totally used in memory pool.</li>
336 * </ul>
337 * @attention
338 * <ul>
339 * <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
340 * </ul>
341 *
342 * @param pool [IN] A pointer pointed to the memory pool.
343 *
344 * @retval #LOS_NOK The incoming parameter pool is NULL.
345 * @retval #UINT32 The size of the memory pool used.
346 * @par Dependency:
347 * <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
348 * @see None.
349 */
350extern UINT32 LOS_MemTotalUsedGet(VOID *pool);
351
352/**
353 * @ingroup los_memory
354 * @brief Get the infomation of memory pool.
355 *
356 * @par Description:
357 * <ul>
358 * <li>This API is used to get the infomation of memory pool.</li>
359 * </ul>
360 * @attention
361 * <ul>
362 * <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
363 * </ul>
364 *
365 * @param pool [IN] A pointer pointed to the memory pool.
366 * @param poolStatus [IN] A pointer for storage the pool status
367 *
368 * @retval #LOS_NOK The incoming parameter pool is NULL or invalid.
369 * @retval #LOS_OK Success to get memory infomation.
370 * @par Dependency:
371 * <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
372 * @see None.
373 */
374extern UINT32 LOS_MemInfoGet(VOID *pool, LOS_MEM_POOL_STATUS *poolStatus);
375
376/**
377 * @ingroup los_memory
378 * @brief Get the number of free node in every size.
379 *
380 * @par Description:
381 * <ul>
382 * <li>This API is used to get the number of free node in every size.</li>
383 * </ul>
384 * @attention
385 * <ul>
386 * <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
387 * </ul>
388 *
389 * @param pool [IN] A pointer pointed to the memory pool.
390 *
391 * @retval #LOS_NOK The incoming parameter pool is NULL.
392 * @retval #UINT32 The address of the last used node that casts to UINT32.
393 * @par Dependency:
394 * <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
395 * @see None.
396 */
397extern UINT32 LOS_MemFreeNodeShow(VOID *pool);
398
399/**
400 * @ingroup los_memory
401 * @brief Check the memory pool integrity.
402 *
403 * @par Description:
404 * <ul>
405 * <li>This API is used to check the memory pool integrity.</li>
406 * </ul>
407 * @attention
408 * <ul>
409 * <li>The input pool parameter must be initialized via func LOS_MemInit.</li>
410 * <li>LOS_MemIntegrityCheck will be called by malloc function when the macro of LOSCFG_BASE_MEM_NODE_INTEGRITY_CHECK
411 * is defined in LiteOS.</li>
412 * <li>LOS_MemIntegrityCheck function can be called by user anytime.</li>
413 * </ul>
414 *
415 * @param pool [IN] A pointer pointed to the memory pool.
416 *
417 * @retval #LOS_NOK The memory pool (pool) is impaired.
418 * @retval #LOS_OK The memory pool (pool) is integrated.
419 * @par Dependency:
420 * <ul><li>los_memory.h: the header file that contains the API declaration.</li></ul>
421 * @see None.
422 */
423extern UINT32 LOS_MemIntegrityCheck(const VOID *pool);
424
425extern void *boot_alloc_mem(size_t len);
426
427#ifdef __cplusplus
428#if __cplusplus
429}
430#endif /* __cplusplus */
431#endif /* __cplusplus */
432
433#endif /* _LOS_MEMORY_H */
UINT32 LOS_MemPoolList(VOID)
Print infomation about all pools.
Definition: los_memory.c:1061
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
Allocate dynamic memory.
Definition: los_memory.c:1123
UINT8 * m_aucSysMem1
系统动态内存池地址的起始地址 @note_thinking 能否不要用 0,1来命名核心变量 ???
Definition: los_memory.c:108
VOID * LOS_MemRealloc(VOID *pool, VOID *ptr, UINT32 size)
Re-allocate a memory block.
Definition: los_memory.c:1510
UINT32 LOS_MemInfoGet(VOID *pool, LOS_MEM_POOL_STATUS *poolStatus)
Get the infomation of memory pool.
Definition: los_memory.c:1965
VOID * LOS_MemAllocAlign(VOID *pool, UINT32 size, UINT32 boundary)
Allocate aligned memory.
Definition: los_memory.c:1150
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
Free dynamic memory.
Definition: los_memory.c:1369
UINT32 LOS_MemFreeNodeShow(VOID *pool)
Get the number of free node in every size.
Definition: los_memory.c:2048
UINT32 LOS_MemPoolSizeGet(const VOID *pool)
Get the size of memory pool's size.
Definition: los_memory.c:1603
VOID LOS_MemExpandEnable(VOID *pool)
Enable memory pool to support dynamic expansion.
Definition: los_memory.c:500
UINT32 LOS_MemTotalUsedGet(VOID *pool)
Get the size of memory totally used.
Definition: los_memory.c:1628
UINT8 * m_aucSysMem0
异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
Definition: los_memory.c:107
UINT32 LOS_MemDeInit(VOID *pool)
Deinitialize dynamic memory.
Definition: los_memory.c:1045
UINT32 LOS_MemInit(VOID *pool, UINT32 size)
Initialize dynamic memory.
Definition: los_memory.c:1021
UINT32 LOS_MemIntegrityCheck(const VOID *pool)
Check the memory pool integrity.
Definition: los_memory.c:1903
void * boot_alloc_mem(size_t len)
unsigned char UINT8
Definition: los_typedef.h:55
unsigned int UINT32
Definition: los_typedef.h:57