ChibiOS/RT 7.0.6
chmemheaps.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 Giovanni Di Sirio.
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file oslib/include/chmemheaps.h
21 * @brief Memory heaps macros and structures.
22 *
23 * @addtogroup oslib_memheaps
24 * @{
25 */
26
27#ifndef CHMEMHEAPS_H
28#define CHMEMHEAPS_H
29
30#if (CH_CFG_USE_HEAP == TRUE) || defined(__DOXYGEN__)
31
32/*===========================================================================*/
33/* Module constants. */
34/*===========================================================================*/
35
36/**
37 * @brief Minimum alignment used for heap.
38 * @note Cannot use the sizeof operator in this macro.
39 */
40#if (SIZEOF_PTR == 4) || defined(__DOXYGEN__)
41#define CH_HEAP_ALIGNMENT 8U
42#elif (SIZEOF_PTR == 2)
43#define CH_HEAP_ALIGNMENT 4U
44#else
45#error "unsupported pointer size"
46#endif
47
48/*===========================================================================*/
49/* Module pre-compile time settings. */
50/*===========================================================================*/
51
52/*===========================================================================*/
53/* Derived constants and error checks. */
54/*===========================================================================*/
55
56#if CH_CFG_USE_MEMCORE == FALSE
57#error "CH_CFG_USE_HEAP requires CH_CFG_USE_MEMCORE"
58#endif
59
60#if (CH_CFG_USE_MUTEXES == FALSE) && (CH_CFG_USE_SEMAPHORES == FALSE)
61#error "CH_CFG_USE_HEAP requires CH_CFG_USE_MUTEXES and/or CH_CFG_USE_SEMAPHORES"
62#endif
63
64/*===========================================================================*/
65/* Module data structures and types. */
66/*===========================================================================*/
67
68/**
69 * @brief Type of a memory heap.
70 */
72
73/**
74 * @brief Type of a memory heap header.
75 */
77
78/**
79 * @brief Memory heap block header.
80 */
82 struct {
83 heap_header_t *next; /**< @brief Next block in free list. */
84 size_t pages; /**< @brief Size of the area in pages. */
86 struct {
87 memory_heap_t *heap; /**< @brief Block owner heap. */
88 size_t size; /**< @brief Size of the area in bytes. */
90};
91
92/**
93 * @brief Structure describing a memory heap.
94 */
96 memgetfunc2_t provider; /**< @brief Memory blocks provider for
97 this heap. */
98 heap_header_t header; /**< @brief Free blocks list header. */
99#if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
100 mutex_t mtx; /**< @brief Heap access mutex. */
101#else
102 semaphore_t sem; /**< @brief Heap access semaphore. */
103#endif
104};
105
106/*===========================================================================*/
107/* Module macros. */
108/*===========================================================================*/
109
110/**
111 * @brief Allocation of an aligned static heap buffer.
112 */
113#define CH_HEAP_AREA(name, size) \
114 ALIGNED_VAR(CH_HEAP_ALIGNMENT) \
115 uint8_t name[MEM_ALIGN_NEXT((size), CH_HEAP_ALIGNMENT)]
116
117/*===========================================================================*/
118/* External declarations. */
119/*===========================================================================*/
120
121#ifdef __cplusplus
122extern "C" {
123#endif
124 void __heap_init(void);
125 void chHeapObjectInit(memory_heap_t *heapp, void *buf, size_t size);
126 void *chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align);
127 void chHeapFree(void *p);
128 size_t chHeapStatus(memory_heap_t *heapp, size_t *totalp, size_t *largestp);
129#ifdef __cplusplus
130}
131#endif
132
133/*===========================================================================*/
134/* Module inline functions. */
135/*===========================================================================*/
136
137/**
138 * @brief Allocates a block of memory from the heap by using the first-fit
139 * algorithm.
140 * @details The allocated block is guaranteed to be properly aligned for a
141 * pointer data type.
142 *
143 * @param[in] heapp pointer to a heap descriptor or @p NULL in order to
144 * access the default heap.
145 * @param[in] size the size of the block to be allocated. Note that the
146 * allocated block may be a bit bigger than the requested
147 * size for alignment and fragmentation reasons.
148 * @return A pointer to the allocated block.
149 * @retval NULL if the block cannot be allocated.
150 *
151 * @api
152 */
153static inline void *chHeapAlloc(memory_heap_t *heapp, size_t size) {
154
155 return chHeapAllocAligned(heapp, size, CH_HEAP_ALIGNMENT);
156}
157
158/**
159 * @brief Returns the size of an allocated block.
160 * @note The returned value is the requested size, the real size is the
161 * same value aligned to the next @p CH_HEAP_ALIGNMENT multiple.
162 *
163 * @param[in] p pointer to the memory block
164 * @return Size of the block.
165 *
166 * @api
167 */
168static inline size_t chHeapGetSize(const void *p) {
169
170 return ((heap_header_t *)p - 1U)->used.size;
171}
172
173#endif /* CH_CFG_USE_HEAP == TRUE */
174
175#endif /* CHMEMHEAPS_H */
176
177/** @} */
struct ch_mutex mutex_t
Type of a mutex structure.
Definition chmtx.h:51
void *(* memgetfunc2_t)(size_t size, unsigned align, size_t offset)
Enhanced memory get function.
Definition chmemcore.h:75
static size_t chHeapGetSize(const void *p)
Returns the size of an allocated block.
Definition chmemheaps.h:168
void chHeapFree(void *p)
Frees a previously allocated memory block.
Definition chmemheaps.c:299
void __heap_init(void)
Initializes the default heap.
Definition chmemheaps.c:106
void chHeapObjectInit(memory_heap_t *heapp, void *buf, size_t size)
Initializes a memory heap from a static memory area.
Definition chmemheaps.c:130
struct memory_heap memory_heap_t
Type of a memory heap.
Definition chmemheaps.h:71
static void * chHeapAlloc(memory_heap_t *heapp, size_t size)
Allocates a block of memory from the heap by using the first-fit algorithm.
Definition chmemheaps.h:153
size_t chHeapStatus(memory_heap_t *heapp, size_t *totalp, size_t *largestp)
Reports the heap status.
Definition chmemheaps.c:361
void * chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align)
Allocates a block of memory from the heap by using the first-fit algorithm.
Definition chmemheaps.c:171
union heap_header heap_header_t
Type of a memory heap header.
Definition chmemheaps.h:76
#define CH_HEAP_ALIGNMENT
Minimum alignment used for heap.
Definition chmemheaps.h:41
struct ch_semaphore semaphore_t
Semaphore structure.
Structure describing a memory heap.
Definition chmemheaps.h:95
memgetfunc2_t provider
Memory blocks provider for this heap.
Definition chmemheaps.h:96
mutex_t mtx
Heap access mutex.
Definition chmemheaps.h:100
heap_header_t header
Free blocks list header.
Definition chmemheaps.h:98
Memory heap block header.
Definition chmemheaps.h:81
struct heap_header::@243146030147220160171120151301327232220324214160 used
size_t size
Size of the area in bytes.
Definition chmemheaps.h:88
struct heap_header::@120065012040315142046275253116336007146263343274 free
size_t pages
Size of the area in pages.
Definition chmemheaps.h:84
heap_header_t * next
Next block in free list.
Definition chmemheaps.h:83
memory_heap_t * heap
Block owner heap.
Definition chmemheaps.h:87