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#if VSF_ARCH_PROVIDE_HEAP == ENABLED
55# ifndef VSF_USE_ARCH_HEAP
56# define VSF_USE_ARCH_HEAP ENABLED
57# endif
58#else
59# if VSF_USE_ARCH_HEAP == ENABLED
60# warning VSF_USE_ARCH_HEAP is enabled, but arch does not provide heap
61# undef VSF_USE_ARCH_HEAP
62# endif
63# define VSF_USE_ARCH_HEAP DISABLED
64#endif
65
66#if !defined(VSF_HEAP_SIZE) && VSF_USE_ARCH_HEAP != ENABLED && !defined(__VSF_APPLET__)
67# warning VSF_HEAP_SIZE is not defined, use 128K as default
68# define VSF_HEAP_SIZE (128 * 1024)
69#endif
70
71#ifndef VSF_HEAP_ALIGN
72# define VSF_HEAP_ALIGN (2 * sizeof(uintalu_t))
73#endif
74
75#if VSF_HEAP_CFG_TRACE == ENABLED
76# define vsf_heap_malloc_aligned(...) \
77 ({ \
78 void * ptr = vsf_heap_malloc_aligned_imp(__VA_ARGS__); \
79 vsf_trace_debug("%s: malloc_align 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr);\
80 ptr; \
81 })
82# define vsf_heap_malloc(...) \
83 ({ \
84 void * ptr = vsf_heap_malloc_imp(__VA_ARGS__); \
85 vsf_trace_debug("%s: malloc 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr);\
86 ptr; \
87 })
88# define vsf_heap_realloc_aligned(...) \
89 ({ \
90 void * ptr = vsf_heap_realloc_aligned_imp(__VA_ARGS__); \
91 vsf_trace_debug("%s: realloc_align 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr);\
92 ptr; \
93 })
94# define vsf_heap_realloc(...) \
95 ({ \
96 void * ptr = vsf_heap_realloc_imp(__VA_ARGS__); \
97 vsf_trace_debug("%s: realloc 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr);\
98 ptr; \
99 })
100# define vsf_heap_free(__ptr) \
101 ({ \
102 vsf_trace_debug("%s: free 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, (__ptr));\
103 vsf_heap_free_imp(__ptr); \
104 })
105#else
106# define vsf_heap_malloc_aligned vsf_heap_malloc_aligned_imp
107# define vsf_heap_malloc vsf_heap_malloc_imp
108# define vsf_heap_realloc_aligned vsf_heap_realloc_aligned_imp
109# define vsf_heap_realloc vsf_heap_realloc_imp
110# define vsf_heap_free vsf_heap_free_imp
111#endif
112
113#ifndef VSF_HEAP_CFG_STATISTICS
114# define VSF_HEAP_CFG_STATISTICS ENABLED
115#endif
116
117/*============================ TYPES =========================================*/
118
119#if VSF_HEAP_CFG_STATISTICS == ENABLED
120typedef struct vsf_heap_statistics_t {
124#endif
125
127 protected_member(
128 vsf_dlist_t * (*get_freelist)(vsf_heap_t *heap, uint_fast32_t size);
131 private_member(
132 vsf_heap_statistics_t statistics;
133 )
134#endif
135};
136
137declare_interface(i_heap_t)
138
139
141def_interface(i_heap_t)
142#if VSF_USE_ARCH_HEAP != ENABLED
143 void (*Init) (void);
144 void (*AddBuffer) (uint8_t *buffer, uint_fast32_t size);
145 void (*AddMemory) (vsf_mem_t mem);
146#endif
147 void *(*MallocAligned) (uint_fast32_t size, uint_fast32_t alignment);
148 void *(*Malloc) (uint_fast32_t size);
149 void *(*ReallocAligned) (void *buffer, uint_fast32_t size, uint_fast32_t alignment);
150 void *(*Realloc) (void *buffer, uint_fast32_t size);
151 void (*Free) (void *buffer);
152#if (VSF_HEAP_CFG_STATISTICS == ENABLED) \
153 && ( (VSF_USE_ARCH_HEAP != ENABLED) \
154 || (VSF_ARCH_HEAP_HAS_STATISTICS == ENABLED))
155 void (*Statistics) (vsf_heap_statistics_t *statistics);
156#endif
157end_def_interface(i_heap_t)
159
160/*============================ GLOBAL VARIABLES ==============================*/
161
162extern const i_heap_t VSF_HEAP;
163
164/*============================ PROTOTYPES ====================================*/
165
166// heap class
167extern void __vsf_heap_add_buffer(vsf_heap_t *heap, uint8_t *buffer, uint_fast32_t size);
170extern void __vsf_heap_free(vsf_heap_t *heap, void *buffer);
171extern uint_fast32_t __vsf_heap_size(vsf_heap_t *heap, void *buffer);
172#if VSF_HEAP_CFG_STATISTICS == ENABLED
173extern void __vsf_heap_statistics(vsf_heap_t *heap, vsf_heap_statistics_t *statistics);
174#endif
175
176#if VSF_USE_ARCH_HEAP != ENABLED
177// default heap
178extern void vsf_heap_init(void);
183extern void vsf_heap_add_buffer(uint8_t *buffer, uint_fast32_t size);
184extern void vsf_heap_add_memory(vsf_mem_t mem);
185#endif
186#if (VSF_HEAP_CFG_STATISTICS == ENABLED) \
187 && ( (VSF_USE_ARCH_HEAP != ENABLED) \
188 || (VSF_ARCH_HEAP_HAS_STATISTICS == ENABLED))
189extern void vsf_heap_statistics(vsf_heap_statistics_t *statistics);
190#endif
191extern uint_fast32_t vsf_heap_size(uint8_t *buffer);
195extern void * vsf_heap_realloc_imp(void *buffer, uint_fast32_t size);
196extern void vsf_heap_free_imp(void *buffer);
197
199extern char * vsf_heap_strdup(const char *str);
200
201#endif
202
203#ifdef __cplusplus
204}
205#endif
206
207#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:126
end_def_interface(i_pm_wakeup_t) struct vsf_pm_pclk_cfg_t
Definition device.h:249
unsigned int uint32_t
Definition lvgl.h:43
unsigned char uint8_t
Definition lvgl.h:40
#define vsf_class(__name)
Definition ooc_class.h:48
unsigned int uint_fast32_t
Definition stdint.h:27
Definition vsf_list.h:883
Definition vsf_heap.h:120
uint32_t all_size
Definition vsf_heap.h:121
uint32_t used_size
Definition vsf_heap.h:122
Definition vsf_utilities.h:51
const i_heap_t VSF_HEAP
Definition vsf_heap.c:110
void * vsf_heap_malloc_aligned_imp(uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:600
uint_fast32_t vsf_heap_size(uint8_t *buffer)
Definition vsf_heap.c:712
#define VSF_HEAP_CFG_STATISTICS
Definition vsf_heap.h:114
void(* Statistics)(vsf_heap_statistics_t *statistics)
Definition vsf_heap.h:155
uint_fast32_t __vsf_heap_size(vsf_heap_t *heap, void *buffer)
Definition vsf_heap.c:503
void * vsf_heap_calloc(uint_fast32_t n, uint_fast32_t size)
Definition vsf_heap.c:742
void * __vsf_heap_realloc_aligned(vsf_heap_t *heap, void *buffer, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:443
void vsf_heap_free_imp(void *buffer)
Definition vsf_heap.c:726
void vsf_heap_statistics(vsf_heap_statistics_t *statistics)
Definition vsf_heap.c:587
uint_fast32_t alignment
Definition vsf_heap.h:147
void(* Free)(void *buffer)
Definition vsf_heap.h:151
void * vsf_heap_realloc_imp(void *buffer, uint_fast32_t size)
Definition vsf_heap.c:666
void __vsf_heap_statistics(vsf_heap_t *heap, vsf_heap_statistics_t *statistics)
Definition vsf_heap.c:532
void * vsf_heap_realloc_aligned_imp(void *buffer, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:636
void * vsf_heap_malloc_imp(uint_fast32_t size)
Definition vsf_heap.c:618
void * __vsf_heap_malloc_aligned(vsf_heap_t *heap, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:422
char * vsf_heap_strdup(const char *str)
Definition vsf_heap.c:752
void __vsf_heap_free(vsf_heap_t *heap, void *buffer)
Definition vsf_heap.c:517
void __vsf_heap_add_buffer(vsf_heap_t *heap, uint8_t *buffer, uint_fast32_t size)
Definition vsf_heap.c:379
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