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 !defined(VSF_HEAP_SIZE) && VSF_ARCH_PROVIDE_HEAP != ENABLED && !defined(__VSF_APPLET__)
55# warning VSF_HEAP_SIZE is not defined, use 128K as default
56# define VSF_HEAP_SIZE (128 * 1024)
57#endif
58
59#ifndef VSF_HEAP_ALIGN
60# define VSF_HEAP_ALIGN (2 * sizeof(uintalu_t))
61#endif
62
63#if VSF_HEAP_CFG_TRACE == ENABLED
64# define vsf_heap_malloc_aligned(...) \
65 ({ \
66 void * ptr = vsf_heap_malloc_aligned_imp(__VA_ARGS__); \
67 vsf_trace_debug("%s: malloc_align 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr);\
68 ptr; \
69 })
70# define vsf_heap_malloc(...) \
71 ({ \
72 void * ptr = vsf_heap_malloc_imp(__VA_ARGS__); \
73 vsf_trace_debug("%s: malloc 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr);\
74 ptr; \
75 })
76# define vsf_heap_realloc_aligned(...) \
77 ({ \
78 void * ptr = vsf_heap_realloc_aligned_imp(__VA_ARGS__); \
79 vsf_trace_debug("%s: realloc_align 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr);\
80 ptr; \
81 })
82# define vsf_heap_realloc(...) \
83 ({ \
84 void * ptr = vsf_heap_realloc_imp(__VA_ARGS__); \
85 vsf_trace_debug("%s: realloc 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, ptr);\
86 ptr; \
87 })
88# define vsf_heap_free(__ptr) \
89 ({ \
90 vsf_trace_debug("%s: free 0x%p" VSF_TRACE_CFG_LINEEND, __FUNCTION__, (__ptr));\
91 vsf_heap_free_imp(__ptr); \
92 })
93#else
94# define vsf_heap_malloc_aligned vsf_heap_malloc_aligned_imp
95# define vsf_heap_malloc vsf_heap_malloc_imp
96# define vsf_heap_realloc_aligned vsf_heap_realloc_aligned_imp
97# define vsf_heap_realloc vsf_heap_realloc_imp
98# define vsf_heap_free vsf_heap_free_imp
99#endif
100
101#ifndef VSF_HEAP_CFG_STATISTICS
102# define VSF_HEAP_CFG_STATISTICS ENABLED
103#endif
104
105/*============================ TYPES =========================================*/
106
107#if VSF_HEAP_CFG_STATISTICS == ENABLED
108typedef struct vsf_heap_statistics_t {
112#endif
113
115 protected_member(
116 vsf_dlist_t * (*get_freelist)(vsf_heap_t *heap, uint_fast32_t size);
119 private_member(
120 vsf_heap_statistics_t statistics;
121 )
122#endif
123};
124
125declare_interface(i_heap_t)
126
127
129def_interface(i_heap_t)
130#if VSF_ARCH_PROVIDE_HEAP != ENABLED
131 void (*Init) (void);
132 void (*AddBuffer) (uint8_t *buffer, uint_fast32_t size);
133 void (*AddMemory) (vsf_mem_t mem);
134#endif
135 void *(*MallocAligned) (uint_fast32_t size, uint_fast32_t alignment);
136 void *(*Malloc) (uint_fast32_t size);
137 void *(*ReallocAligned) (void *buffer, uint_fast32_t size, uint_fast32_t alignment);
138 void *(*Realloc) (void *buffer, uint_fast32_t size);
139 void (*Free) (void *buffer);
140#if (VSF_HEAP_CFG_STATISTICS == ENABLED) \
141 && ( (VSF_ARCH_PROVIDE_HEAP != ENABLED) \
142 || (VSF_ARCH_HEAP_HAS_STATISTICS == ENABLED))
143 void (*Statistics) (vsf_heap_statistics_t *statistics);
144#endif
145end_def_interface(i_heap_t)
147
148/*============================ GLOBAL VARIABLES ==============================*/
149
150extern const i_heap_t VSF_HEAP;
151
152/*============================ PROTOTYPES ====================================*/
153
154// heap class
155extern void __vsf_heap_add_buffer(vsf_heap_t *heap, uint8_t *buffer, uint_fast32_t size);
158extern void __vsf_heap_free(vsf_heap_t *heap, void *buffer);
159extern uint_fast32_t __vsf_heap_size(vsf_heap_t *heap, void *buffer);
160#if VSF_HEAP_CFG_STATISTICS == ENABLED
161extern void __vsf_heap_statistics(vsf_heap_t *heap, vsf_heap_statistics_t *statistics);
162#endif
163
164#if VSF_ARCH_PROVIDE_HEAP != ENABLED
165// default heap
166extern void vsf_heap_init(void);
171extern void vsf_heap_add_buffer(uint8_t *buffer, uint_fast32_t size);
172extern void vsf_heap_add_memory(vsf_mem_t mem);
173#endif
174#if (VSF_HEAP_CFG_STATISTICS == ENABLED) \
175 && ( (VSF_ARCH_PROVIDE_HEAP != ENABLED) \
176 || (VSF_ARCH_HEAP_HAS_STATISTICS == ENABLED))
177extern void vsf_heap_statistics(vsf_heap_statistics_t *statistics);
178#endif
179extern uint_fast32_t vsf_heap_size(uint8_t *buffer);
183extern void * vsf_heap_realloc_imp(void *buffer, uint_fast32_t size);
184extern void vsf_heap_free_imp(void *buffer);
185
187extern char * vsf_heap_strdup(const char *str);
188
189#endif
190
191#ifdef __cplusplus
192}
193#endif
194
195#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:114
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:108
uint32_t all_size
Definition vsf_heap.h:109
uint32_t used_size
Definition vsf_heap.h:110
Definition vsf_utilities.h:51
const i_heap_t VSF_HEAP
Definition vsf_heap.c:109
void * vsf_heap_malloc_aligned_imp(uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:593
uint_fast32_t vsf_heap_size(uint8_t *buffer)
Definition vsf_heap.c:667
#define VSF_HEAP_CFG_STATISTICS
Definition vsf_heap.h:102
void(* Statistics)(vsf_heap_statistics_t *statistics)
Definition vsf_heap.h:143
uint_fast32_t __vsf_heap_size(vsf_heap_t *heap, void *buffer)
Definition vsf_heap.c:499
void * vsf_heap_calloc(uint_fast32_t n, uint_fast32_t size)
Definition vsf_heap.c:687
void * __vsf_heap_realloc_aligned(vsf_heap_t *heap, void *buffer, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:439
void vsf_heap_free_imp(void *buffer)
Definition vsf_heap.c:676
void vsf_heap_statistics(vsf_heap_statistics_t *statistics)
Definition vsf_heap.c:580
uint_fast32_t alignment
Definition vsf_heap.h:135
void(* Free)(void *buffer)
Definition vsf_heap.h:139
void * vsf_heap_realloc_imp(void *buffer, uint_fast32_t size)
Definition vsf_heap.c:642
void __vsf_heap_statistics(vsf_heap_t *heap, vsf_heap_statistics_t *statistics)
Definition vsf_heap.c:528
void * vsf_heap_realloc_aligned_imp(void *buffer, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:619
void * vsf_heap_malloc_imp(uint_fast32_t size)
Definition vsf_heap.c:606
void * __vsf_heap_malloc_aligned(vsf_heap_t *heap, uint_fast32_t size, uint_fast32_t alignment)
Definition vsf_heap.c:418
char * vsf_heap_strdup(const char *str)
Definition vsf_heap.c:697
void __vsf_heap_free(vsf_heap_t *heap, void *buffer)
Definition vsf_heap.c:513
void __vsf_heap_add_buffer(vsf_heap_t *heap, uint8_t *buffer, uint_fast32_t size)
Definition vsf_heap.c:375
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