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