• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * Description: Malloc function Header. \n
16  *
17  * History: \n
18  * 2023-2-14, Create file. \n
19  */
20 
21 #ifndef __MALLOC_ROM_H__
22 #define __MALLOC_ROM_H__
23 
24 #include <stdint.h>
25 #include <stddef.h>
26 #include "common_def.h"
27 
28 typedef struct malloc_dfx {
29     uint32_t heap_malloc_size;    /* 已经申请的堆空间总大小 */
30     uint32_t heap_free_size;      /* 剩余堆空间总大小 */
31     uint32_t heap_size;           /* 堆空间总大小 */
32     uintptr_t heap_start_addr;  /* 堆空间起始地址 */
33     uintptr_t heap_end_addr;    /* 堆空间结束地址 */
34     uint32_t start_status;        /* 启动状态维测,结合set_start_status函数使用 */
35 } malloc_dfx_t;
36 
37 typedef void (*free_func)(void *addr);
38 typedef void *(*malloc_func)(uint32_t size, uint32_t caller);
39 typedef void (*malloc_init_func)(uint32_t heap_start_addr, uint32_t heap_end_addr);
40 
41 typedef struct {
42     malloc_init_func init;   /* 初始化堆内存管理接口 */
43     malloc_func malloc; /* 申请堆内存接口 */
44     free_func free;     /* 释放堆内存接口 */
45 } malloc_funcs;
46 
47 malloc_funcs *malloc_get_funcs(void);
48 uint32_t malloc_register_funcs(const malloc_funcs *funcs);
49 /**
50 * @ingroup
51 * @brief  动态内存管理模块初始化接口,需要调用uapi_register_malloc接口先注册后才能使用。
52 *
53 * @par 描述:
54 *          使用动态内存前,需要调用1次本接口进行初始化
55 * @attention   无。
56 *
57 * @param  heap_start_addr [IN] 类型 #uintptr_t  堆管理起始地址。
58 * @param  heap_end_addr [IN] 类型 #uintptr_t  堆管理结束地址。
59 * @param  check_sum [IN] 类型 #uint32_t 校验值,前两个参数的异或结果。
60 *
61 * @retval 无
62 *
63 * <ul><li>malloc.h: 该接口声明所在的头文件.</li></ul>
64 * @see uapi_register_malloc
65  */
66 void malloc_init(uintptr_t heap_start_addr, uintptr_t heap_end_addr);
67 
68 /**
69 * @ingroup
70 * @brief  申请动态内存,需要调用uapi_register_malloc接口先注册后才能使用。
71 *
72 * @par 描述:
73 *        申请动态内存
74 * @attention   无。
75 *
76 * @param  size [IN] 类型 #uint32_t 需要申请的内存大小
77 *
78 * @retval void*  申请到的内存指针,如果申请失败则返回HI_NULL
79 *
80 * <ul><li>malloc.h: 该接口声明所在的头文件.</li></ul>
81 * @see uapi_register_malloc
82  */
83 void *malloc(size_t size);
84 
85 /**
86 * @ingroup
87 * @brief  释放动态内存,需要调用uapi_register_malloc接口先注册后才能使用。
88 *
89 * @par 描述:
90 *       释放动态内存
91 * @attention   无。
92 *
93 * @param  addr [IN] 类型 #void*  需要释放的内存地址指针。注意不要多次释放同一个内存地址。
94 *
95 * @retval EXT_ERR_SUCCESS      释放内存成功。
96 * @retval EXT_ERR_FAILURE     释放内存错误。
97 *
98 * <ul><li>malloc.h: 该接口声明所在的头文件.</li></ul>
99 * @see uapi_register_malloc
100  */
101 void free(void *addr);
102 
103 void rom_malloc_init(uint32_t heap_start_addr, uint32_t heap_end_addr);
104 void *rom_malloc(uint32_t size, uint32_t caller);
105 uint32_t  rom_free(void *addr);
106 void print_mem_info(void);
107 #endif
108