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 {
127 // Historical peak of used_size since heap init. Lets callers derive
128 // the minimum-ever free size as (all_size - max_used_size), which is
129 // useful for low-water / worst-case headroom diagnostics.
131 // Size (in bytes, MCB header included) of the largest contiguous
132 // free block at the moment of the statistics call. Computed lazily
133 // by scanning the freelist -- intended for diagnostic paths only,
134 // e.g. fragmentation reporting or pre-allocation feasibility checks.
137#endif
138
140 protected_member(
141 // get_freelist MUST not return NULL
142 vsf_dlist_t * (*get_freelist)(vsf_heap_t *heap, uint_fast32_t size, bool is_alloc);
143
144 vsf_dlist_t *freelist;
145 uint8_t freelist_num;
148 private_member(
149 vsf_heap_statistics_t statistics;
150 )
151#endif
153 private_member(
154 bool locked;
155 uint32_t idx;
156 uint32_t locked_idx;
157 vsf_dlist_t freed_list;
158 )
159#endif
160};
161
162declare_interface(i_heap_t)
163
164
166def_interface(i_heap_t)
167#if VSF_USE_ARCH_HEAP != ENABLED
168 void (*Init) (void);
169 void (*AddBuffer) (uint8_t *buffer, uint_fast32_t size);
170 void (*AddMemory) (vsf_mem_t mem);
171#endif
172 void *(*MallocAligned) (uint_fast32_t size, uint_fast32_t alignment);
173 void *(*Malloc) (uint_fast32_t size);
174 void *(*ReallocAligned) (void *buffer, uint_fast32_t size, uint_fast32_t alignment);
175 void *(*Realloc) (void *buffer, uint_fast32_t size);
176 void (*Free) (void *buffer);
177#if (VSF_HEAP_CFG_STATISTICS == ENABLED) \
178 && ( (VSF_USE_ARCH_HEAP != ENABLED) \
179 || (VSF_ARCH_HEAP_HAS_STATISTICS == ENABLED))
180 void (*Statistics) (vsf_heap_statistics_t *statistics);
181#endif
182end_def_interface(i_heap_t)
184
185/*============================ GLOBAL VARIABLES ==============================*/
186
187extern const i_heap_t VSF_HEAP;
188
189/*============================ PROTOTYPES ====================================*/
190
191// heap class
192extern void __vsf_heap_init(vsf_heap_t *heap);
193extern void __vsf_heap_add_buffer(vsf_heap_t *heap, uint8_t *buffer, uint_fast32_t size);
196extern void __vsf_heap_free(vsf_heap_t *heap, void *buffer);
197extern bool __vsf_heap_is_free(vsf_heap_t *heap, void *buffer);
198extern uint_fast32_t __vsf_heap_size(vsf_heap_t *heap, void *buffer);
199#if VSF_HEAP_CFG_STATISTICS == ENABLED
200extern void __vsf_heap_statistics(vsf_heap_t *heap, vsf_heap_statistics_t *statistics);
201#endif
202#if VSF_HEAP_CFG_TRACE_LEAKAGE == ENABLED
203// if lock, heap will be locked(no previous allocated memory will be freed, and free operation will be saved in mcb)
204// if non-lock, heap will be unlocked, and memory allocated when heap locked will be traced
205extern void __vsf_heap_dump(vsf_heap_t *heap, bool lock);
206#endif
207
208#if VSF_USE_ARCH_HEAP != ENABLED
209// default heap
210extern void vsf_heap_init(void);
215extern void vsf_heap_add_buffer(uint8_t *buffer, uint_fast32_t size);
216extern void vsf_heap_add_memory(vsf_mem_t mem);
217#endif
218#if (VSF_HEAP_CFG_STATISTICS == ENABLED) \
219 && ( (VSF_USE_ARCH_HEAP != ENABLED) \
220 || (VSF_ARCH_HEAP_HAS_STATISTICS == ENABLED))
221extern void vsf_heap_statistics(vsf_heap_statistics_t *statistics);
222#endif
223extern uint_fast32_t vsf_heap_size(uint8_t *buffer);
227extern void * vsf_heap_realloc_imp(void *buffer, uint_fast32_t size);
228extern void vsf_heap_free_imp(void *buffer);
229extern bool vsf_heap_is_free(void *buffer);
230
232extern char * vsf_heap_strdup(const char *str);
233
234#if VSF_HEAP_CFG_TRACE_LEAKAGE == ENABLED
235// if lock, heap will be locked(no previous allocated memory will be freed, and free operation will be saved in mcb)
236// if non-lock, heap will be unlocked, and memory allocated when heap locked will be traced
237extern void vsf_heap_dump(bool lock);
238#endif
239
240#endif
241
242#ifdef __cplusplus
243}
244#endif
245
246#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:139
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:52
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 max_used_size
Definition vsf_heap.h:130
uint32_t all_size
Definition vsf_heap.h:125
uint32_t largest_free_block
Definition vsf_heap.h:135
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:623
void * vsf_heap_malloc_aligned_imp(uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:886
uint_fast32_t vsf_heap_size(uint8_t *buffer)
Definition vsf_heap.c:1055
#define VSF_HEAP_CFG_STATISTICS
Definition vsf_heap.h:118
void(* Statistics)(vsf_heap_statistics_t *statistics)
Definition vsf_heap.h:180
uint_fast32_t __vsf_heap_size(vsf_heap_t *heap, void *buffer)
Definition vsf_heap.c:549
void * vsf_heap_calloc(uint_fast32_t n, uint_fast32_t size)
Definition vsf_heap.c:1085
void * __vsf_heap_realloc_aligned(vsf_heap_t *heap, void *buffer, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:483
void vsf_heap_free_imp(void *buffer)
Definition vsf_heap.c:1069
void __vsf_heap_dump(vsf_heap_t *heap, bool lock)
Definition vsf_heap.c:664
bool vsf_heap_is_free(void *buffer)
Definition vsf_heap.c:1123
void vsf_heap_statistics(vsf_heap_statistics_t *statistics)
Definition vsf_heap.c:861
uint_fast32_t alignment
Definition vsf_heap.h:172
void(* Free)(void *buffer)
Definition vsf_heap.h:176
bool __vsf_heap_is_free(vsf_heap_t *heap, void *buffer)
Definition vsf_heap.c:600
void * vsf_heap_realloc_imp(void *buffer, uint_fast32_t size)
Definition vsf_heap.c:997
#define VSF_HEAP_CFG_TRACE_LEAKAGE
Definition vsf_heap.h:55
void vsf_heap_dump(bool lock)
Definition vsf_heap.c:797
void __vsf_heap_statistics(vsf_heap_t *heap, vsf_heap_statistics_t *statistics)
Definition vsf_heap.c:631
void * vsf_heap_realloc_aligned_imp(void *buffer, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:946
void * vsf_heap_malloc_imp(uint_fast32_t size)
Definition vsf_heap.c:916
void * __vsf_heap_malloc_aligned(vsf_heap_t *heap, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:454
char * vsf_heap_strdup(const char *str)
Definition vsf_heap.c:1102
void __vsf_heap_free(vsf_heap_t *heap, void *buffer)
Definition vsf_heap.c:563
void __vsf_heap_add_buffer(vsf_heap_t *heap, uint8_t *buffer, uint_fast32_t size)
Definition vsf_heap.c:411
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@bf47061