VSF Documented
vsf_heap.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18#ifndef __VSF_HEAP_H__
19#define __VSF_HEAP_H__
20
21/*============================ INCLUDES ======================================*/
22
24#if VSF_USE_HEAP == ENABLED
25
26#include "hal/arch/vsf_arch.h"
28
29#if VSF_HEAP_CFG_TRACE == ENABLED
31#endif
32
38#if defined(__VSF_HEAP_CLASS_IMPLEMENT)
39# define __VSF_CLASS_IMPLEMENT__
40# undef __VSF_HEAP_CLASS_IMPLEMENT
41#elif defined(__VSF_HEAP_CLASS_INHERIT__)
42# define __VSF_CLASS_INHERIT__
43# undef __VSF_HEAP_CLASS_INHERIT__
44#endif
45
46#include "utilities/ooc_class.h"
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52/*============================ MACROS ========================================*/
53
54#ifndef VSF_HEAP_CFG_TRACE_LEAKAGE
55# define VSF_HEAP_CFG_TRACE_LEAKAGE DISABLED
56#endif
57
58#if VSF_ARCH_PROVIDE_HEAP == ENABLED
59# ifndef VSF_USE_ARCH_HEAP
60# define VSF_USE_ARCH_HEAP ENABLED
61# endif
62#else
63# if VSF_USE_ARCH_HEAP == ENABLED
64# warning VSF_USE_ARCH_HEAP is enabled, but arch does not provide heap
65# undef VSF_USE_ARCH_HEAP
66# endif
67# define VSF_USE_ARCH_HEAP DISABLED
68#endif
69
70#if !defined(VSF_HEAP_SIZE) && VSF_USE_ARCH_HEAP != ENABLED && !defined(__VSF_APPLET__)
71# warning VSF_HEAP_SIZE is not defined, use 128K as default
72# define VSF_HEAP_SIZE (128 * 1024)
73#endif
74
75#ifndef VSF_HEAP_ALIGN
76# define VSF_HEAP_ALIGN (2 * sizeof(uintalu_t))
77#endif
78
79#if VSF_HEAP_CFG_TRACE == ENABLED
80# define vsf_heap_malloc_aligned(__size, __alignment) \
81 ({ \
82 void * ptr = vsf_heap_malloc_aligned_imp(__size, __alignment); \
83 vsf_trace_debug("%s: malloc_align 0x%p %d" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr, __size);\
84 ptr; \
85 })
86# define vsf_heap_malloc(__size) \
87 ({ \
88 void * ptr = vsf_heap_malloc_imp(__size); \
89 vsf_trace_debug("%s: malloc 0x%p %d" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr, __size);\
90 ptr; \
91 })
92# define vsf_heap_realloc_aligned(__buffer, __size, __alignment) \
93 ({ \
94 void * ptr = vsf_heap_realloc_aligned_imp(__buffer, __size, __alignment);\
95 vsf_trace_debug("%s: realloc_align 0x%p --> 0x%p %d" VSF_TRACE_CFG_LINEEND, __FUNCTION__, __buffer, ptr, __size);\
96 ptr; \
97 })
98# define vsf_heap_realloc(__buffer, __size) \
99 ({ \
100 void * ptr = vsf_heap_realloc_imp(__buffer, __size); \
101 vsf_trace_debug("%s: realloc 0x%p --> 0x%p %d" VSF_TRACE_CFG_LINEEND, __FUNCTION__, __buffer, ptr, __size);\
102 ptr; \
103 })
104# define vsf_heap_free(__ptr) \
105 ({ \
106 vsf_trace_debug("%s: free 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, (__ptr));\
107 vsf_heap_free_imp(__ptr); \
108 })
109#else
110# define vsf_heap_malloc_aligned vsf_heap_malloc_aligned_imp
111# define vsf_heap_malloc vsf_heap_malloc_imp
112# define vsf_heap_realloc_aligned vsf_heap_realloc_aligned_imp
113# define vsf_heap_realloc vsf_heap_realloc_imp
114# define vsf_heap_free vsf_heap_free_imp
115#endif
116
117#ifndef VSF_HEAP_CFG_STATISTICS
118# define VSF_HEAP_CFG_STATISTICS ENABLED
119#endif
120
121/*============================ TYPES =========================================*/
122
123#if VSF_HEAP_CFG_STATISTICS == ENABLED
124typedef struct vsf_heap_statistics_t {
128#endif
129
131 protected_member(
132 // get_freelist MUST not return NULL
133 vsf_dlist_t * (*get_freelist)(vsf_heap_t *heap, uint_fast32_t size, bool is_alloc);
134
135 vsf_dlist_t *freelist;
136 uint8_t freelist_num;
139 private_member(
140 vsf_heap_statistics_t statistics;
141 )
142#endif
144 private_member(
145 bool locked;
146 uint32_t idx;
147 uint32_t locked_idx;
148 vsf_dlist_t freed_list;
149 )
150#endif
151};
152
153declare_interface(i_heap_t)
154
155
157def_interface(i_heap_t)
158#if VSF_USE_ARCH_HEAP != ENABLED
159 void (*Init) (void);
160 void (*AddBuffer) (uint8_t *buffer, uint_fast32_t size);
161 void (*AddMemory) (vsf_mem_t mem);
162#endif
163 void *(*MallocAligned) (uint_fast32_t size, uint_fast32_t alignment);
164 void *(*Malloc) (uint_fast32_t size);
165 void *(*ReallocAligned) (void *buffer, uint_fast32_t size, uint_fast32_t alignment);
166 void *(*Realloc) (void *buffer, uint_fast32_t size);
167 void (*Free) (void *buffer);
168#if (VSF_HEAP_CFG_STATISTICS == ENABLED) \
169 && ( (VSF_USE_ARCH_HEAP != ENABLED) \
170 || (VSF_ARCH_HEAP_HAS_STATISTICS == ENABLED))
171 void (*Statistics) (vsf_heap_statistics_t *statistics);
172#endif
173end_def_interface(i_heap_t)
175
176/*============================ GLOBAL VARIABLES ==============================*/
177
178extern const i_heap_t VSF_HEAP;
179
180/*============================ PROTOTYPES ====================================*/
181
182// heap class
183extern void __vsf_heap_init(vsf_heap_t *heap);
184extern void __vsf_heap_add_buffer(vsf_heap_t *heap, uint8_t *buffer, uint_fast32_t size);
187extern void __vsf_heap_free(vsf_heap_t *heap, void *buffer);
188extern uint_fast32_t __vsf_heap_size(vsf_heap_t *heap, void *buffer);
189#if VSF_HEAP_CFG_STATISTICS == ENABLED
190extern void __vsf_heap_statistics(vsf_heap_t *heap, vsf_heap_statistics_t *statistics);
191#endif
192#if VSF_HEAP_CFG_TRACE_LEAKAGE == ENABLED
193// if lock, heap will be locked(no previous allocated memory will be freed, and free operation will be saved in mcb)
194// if non-lock, heap will be unlocked, and memory allocated when heap locked will be traced
195extern void __vsf_heap_dump(vsf_heap_t *heap, bool lock);
196#endif
197
198#if VSF_USE_ARCH_HEAP != ENABLED
199// default heap
200extern void vsf_heap_init(void);
205extern void vsf_heap_add_buffer(uint8_t *buffer, uint_fast32_t size);
206extern void vsf_heap_add_memory(vsf_mem_t mem);
207#endif
208#if (VSF_HEAP_CFG_STATISTICS == ENABLED) \
209 && ( (VSF_USE_ARCH_HEAP != ENABLED) \
210 || (VSF_ARCH_HEAP_HAS_STATISTICS == ENABLED))
211extern void vsf_heap_statistics(vsf_heap_statistics_t *statistics);
212#endif
213extern uint_fast32_t vsf_heap_size(uint8_t *buffer);
217extern void * vsf_heap_realloc_imp(void *buffer, uint_fast32_t size);
218extern void vsf_heap_free_imp(void *buffer);
219
221extern char * vsf_heap_strdup(const char *str);
222
223#if VSF_HEAP_CFG_TRACE_LEAKAGE == ENABLED
224// if lock, heap will be locked(no previous allocated memory will be freed, and free operation will be saved in mcb)
225// if non-lock, heap will be unlocked, and memory allocated when heap locked will be traced
226extern void vsf_heap_dump(bool lock);
227#endif
228
229#endif
230
231#ifdef __cplusplus
232}
233#endif
234
235#endif
#define ENABLED
Definition __type.h:28
def_interface(i_adc_t) i_peripheral_t
vsf_err_t(* Init)(vsf_adc_cfg_t *pCfg)
Definition adc_interface.h:38
Definition vsf_heap.h:130
end_def_interface(i_pm_wakeup_t) struct vsf_pm_pclk_cfg_t
Definition device.h:249
#define vsf_class(__name)
Definition ooc_class.h:48
unsigned uint32_t
Definition stdint.h:9
unsigned int uint_fast32_t
Definition stdint.h:27
unsigned char uint8_t
Definition stdint.h:5
Definition vsf_list.h:883
Definition vsf_heap.h:124
uint32_t all_size
Definition vsf_heap.h:125
uint32_t used_size
Definition vsf_heap.h:126
Definition vsf_utilities.h:51
const i_heap_t VSF_HEAP
Definition vsf_heap.c:116
void __vsf_heap_init(vsf_heap_t *heap)
Definition vsf_heap.c:584
void * vsf_heap_malloc_aligned_imp(uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:813
uint_fast32_t vsf_heap_size(uint8_t *buffer)
Definition vsf_heap.c:982
#define VSF_HEAP_CFG_STATISTICS
Definition vsf_heap.h:118
void(* Statistics)(vsf_heap_statistics_t *statistics)
Definition vsf_heap.h:171
uint_fast32_t __vsf_heap_size(vsf_heap_t *heap, void *buffer)
Definition vsf_heap.c:533
void * vsf_heap_calloc(uint_fast32_t n, uint_fast32_t size)
Definition vsf_heap.c:1012
void * __vsf_heap_realloc_aligned(vsf_heap_t *heap, void *buffer, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:473
void vsf_heap_free_imp(void *buffer)
Definition vsf_heap.c:996
void __vsf_heap_dump(vsf_heap_t *heap, bool lock)
Definition vsf_heap.c:603
void vsf_heap_statistics(vsf_heap_statistics_t *statistics)
Definition vsf_heap.c:800
uint_fast32_t alignment
Definition vsf_heap.h:163
void(* Free)(void *buffer)
Definition vsf_heap.h:167
void * vsf_heap_realloc_imp(void *buffer, uint_fast32_t size)
Definition vsf_heap.c:924
#define VSF_HEAP_CFG_TRACE_LEAKAGE
Definition vsf_heap.h:55
void vsf_heap_dump(bool lock)
Definition vsf_heap.c:736
void __vsf_heap_statistics(vsf_heap_t *heap, vsf_heap_statistics_t *statistics)
Definition vsf_heap.c:592
void * vsf_heap_realloc_aligned_imp(void *buffer, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:873
void * vsf_heap_malloc_imp(uint_fast32_t size)
Definition vsf_heap.c:843
void * __vsf_heap_malloc_aligned(vsf_heap_t *heap, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:444
char * vsf_heap_strdup(const char *str)
Definition vsf_heap.c:1029
void __vsf_heap_free(vsf_heap_t *heap, void *buffer)
Definition vsf_heap.c:547
void __vsf_heap_add_buffer(vsf_heap_t *heap, uint8_t *buffer, uint_fast32_t size)
Definition vsf_heap.c:401
uint32_t size
Definition vsf_memfs.h:50
declare_interface(i_msg_tree_node_t) def_interface(i_msg_tree_node_t) vsf_msgt_handler_t msg_handler
Generated from commit: vsfteam/vsf@af87c60