XFusion API v1.3.0
载入中...
搜索中...
未找到
xf_heap.c
浏览该文件的文档.
1
13/* ==================== [Includes] ========================================== */
14
15#include "xf_heap.h"
16#include "xf_alloc.h"
17
18/* ==================== [Defines] =========================================== */
19
20/* ==================== [Typedefs] ========================================== */
21
22typedef struct _heap_t {
24 void *lock;
25 unsigned int init;
26 unsigned int free_bytes;
29
30/* ==================== [Static Prototypes] ================================= */
31
32static void *_xf_malloc(unsigned int size);
33static void _xf_free(void *pv);
34static unsigned int _xf_heap_get_free_size(void);
35static unsigned int _xf_heap_get_min_ever_free_size(void);
36
37/* ==================== [Static Variables] ================================== */
38
39/*初始化默认参数*/
40static heap_t s_heap = {
42 .init = 0,
43 .free_bytes = 0,
44 .min_ever_free_bytes_remaining = 0,
45 .func = {
46 .malloc = xf_heap_malloc,
47 .free = xf_heap_free,
48 .init = xf_heap_region,
49 .get_block_size = xf_heap_get_block_size,
50 }
51};
52
53const static xf_heap_api_t *s_heap_api = {0};
54const static xf_heap_api_t _heap_api = {
56 .free = _xf_free,
57 .get_free_size = _xf_heap_get_free_size,
58 .get_min_ever_free_size = _xf_heap_get_min_ever_free_size
59};
60
61/* ==================== [Macros] ============================================ */
62
63/* ==================== [Global Functions] ================================== */
64
66{
67 if (api == (xf_heap_api_t *)0) {
68 return XF_HEAP_FAIL;
69 }
70
71 s_heap_api = api;
72 return XF_HEAP_OK;
73}
74
75
76int xf_heap_init(const xf_heap_region_t *const regions)
77{
78 unsigned int total_size = 0;
79
81 return XF_HEAP_INITED;
82 }
84 total_size = s_heap.func.init(regions);
85 s_heap.free_bytes = total_size;
87
89
90 return XF_HEAP_OK;
91}
92
94{
96 return XF_HEAP_UNINIT;
97 }
98
99 s_heap.init = 0;
100 s_heap.free_bytes = 0;
102
104
105 return XF_HEAP_OK;
106}
107
108void *xf_malloc(unsigned int size)
109{
110 if (s_heap_api == (xf_heap_api_t *)0) {
111 return (void *) 0;
112 }
113
114 return s_heap_api->malloc(size);
115}
116
117void xf_free(void *pv)
118{
119 if (s_heap_api == (xf_heap_api_t *)0) {
120 return;
121 }
122 s_heap_api->free(pv);
123}
124
125unsigned int xf_heap_get_free_size(void)
126{
127 if (s_heap_api == (xf_heap_api_t *)0) {
128 return (unsigned int) -1;
129 }
130 return s_heap_api->get_free_size();
131}
132
134{
135 if (s_heap_api == (xf_heap_api_t *)0) {
136 return (unsigned int) -1;
137 }
139}
140
141/* ==================== [Static Functions] ================================== */
142
143static void *_xf_malloc(unsigned int size)
144{
145 void *res = (void *) 0;
147 {
149 return (void *) 0;
150 }
151 res = s_heap.func.malloc(size);
152 if (res != (void *) 0) {
156 }
157 }
158 }
160 return res;
161}
162
163static void _xf_free(void *pv)
164{
166 {
168 return;
169 }
170 if (pv != (void *) 0) {
172 }
173 s_heap.func.free(pv);
174 }
176}
177
178static unsigned int _xf_heap_get_free_size(void)
179{
180 unsigned int res = 0;
182 {
184 return 0;
185 }
186
187 res = s_heap.free_bytes;
188 }
190
191 return res;
192}
193
194static unsigned int _xf_heap_get_min_ever_free_size(void)
195{
196 unsigned int res = 0;
198 {
200 return 0;
201 }
202
204 }
206
207 return res;
208}
unsigned int xf_heap_get_min_ever_free_size(void)
获取曾经最小的空闲内存块。
Definition xf_heap.c:133
unsigned int xf_heap_get_free_size(void)
获取内存总空闲大小。
Definition xf_heap.c:125
#define xf_malloc(x)
Definition xf_stdlib.h:38
#define xf_free(x)
Definition xf_stdlib.h:39
xf_alloc_func_t func
Definition xf_heap.c:23
void * lock
Definition xf_heap.c:24
unsigned int min_ever_free_bytes_remaining
Definition xf_heap.c:27
unsigned int free_bytes
Definition xf_heap.c:26
unsigned int init
Definition xf_heap.c:25
内存分配操作集。
Definition xf_heap.h:75
unsigned int(* init)(const xf_heap_region_t *const regions)
Definition xf_heap.h:78
void *(* malloc)(unsigned int size)
Definition xf_heap.h:76
void(* free)(void *pv)
Definition xf_heap.h:77
unsigned int(* get_block_size)(void *pv)
Definition xf_heap.h:79
xf_heap_get_min_ever_free_size_t get_min_ever_free_size
Definition xf_heap.h:91
xf_heap_get_free_size_t get_free_size
Definition xf_heap.h:90
xf_malloc_t malloc
Definition xf_heap.h:88
xf_free_t free
Definition xf_heap.h:89
堆内存块结构体。
Definition xf_heap.h:67
void xf_heap_free(void *pv)
带内存管理的内存释放函数。
Definition xf_alloc.c:119
unsigned int xf_heap_get_block_size(void *pv)
获取内存块的实际大小。
Definition xf_alloc.c:204
unsigned int xf_heap_region(const xf_heap_region_t *const heap_regions)
内存注册,需要在使用 xf_heap_malloc 之前注册。
Definition xf_alloc.c:141
void * xf_heap_malloc(unsigned int size)
带内存管理的内存申请函数。
Definition xf_alloc.c:60
基于 heap5 的内存分配实现。
堆内存接口。
int xf_heap_uninit(void)
内存反初始化。
Definition xf_heap.c:93
static heap_t s_heap
Definition xf_heap.c:40
static const xf_heap_api_t * s_heap_api
Definition xf_heap.c:53
struct _heap_t heap_t
int xf_heap_api_redirect(const xf_heap_api_t *api)
重新定义内存管理接口
Definition xf_heap.c:65
static void _xf_free(void *pv)
Definition xf_heap.c:163
static unsigned int _xf_heap_get_min_ever_free_size(void)
Definition xf_heap.c:194
static const xf_heap_api_t _heap_api
Definition xf_heap.c:54
static unsigned int _xf_heap_get_free_size(void)
Definition xf_heap.c:178
static void * _xf_malloc(unsigned int size)
Definition xf_heap.c:143
int xf_heap_init(const xf_heap_region_t *const regions)
内存初始化。
Definition xf_heap.c:76
#define XF_HEAP_INITED
#define XF_HEAP_LOCK(PLOCK)
#define XF_HEAP_UNINIT
#define XF_HEAP_MAGIC_NUM
#define XF_HEAP_OK
#define XF_HEAP_UNLOCK(PLOCK)
#define XF_HEAP_LOCK_PTR
#define XF_HEAP_FAIL
heap的错误码