ChibiOS 21.11.5
chmemcore.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/chmemcore.h
21 * @brief Core memory manager macros and structures.
22 *
23 * @addtogroup oslib_memcore
24 * @{
25 */
26
27#ifndef CHMEMCORE_H
28#define CHMEMCORE_H
29
30#if (CH_CFG_USE_MEMCORE == TRUE) || defined(__DOXYGEN__)
31
32/*===========================================================================*/
33/* Module constants. */
34/*===========================================================================*/
35
36/*===========================================================================*/
37/* Module pre-compile time settings. */
38/*===========================================================================*/
39
40/**
41 * @brief Managed RAM size.
42 * @details Size of the RAM area to be managed by the OS. If set to zero
43 * then the whole available RAM is used. The core memory is made
44 * available to the heap allocator and/or can be used directly through
45 * the simplified core memory allocator.
46 *
47 * @note In order to let the OS manage the whole RAM the linker script must
48 * provide the @p __heap_base__ and @p __heap_end__ symbols.
49 * @note Requires @p CH_CFG_USE_MEMCORE.
50 */
51#if !defined(CH_CFG_MEMCORE_SIZE) || defined(__DOXYGEN__)
52#define CH_CFG_MEMCORE_SIZE 0
53#endif
54
55/*===========================================================================*/
56/* Derived constants and error checks. */
57/*===========================================================================*/
58
59#if CH_CFG_MEMCORE_SIZE < 0
60#error "invalid CH_CFG_MEMCORE_SIZE value specified"
61#endif
62
63/*===========================================================================*/
64/* Module data structures and types. */
65/*===========================================================================*/
66
67/**
68 * @brief Memory get function.
69 */
70typedef void *(*memgetfunc_t)(size_t size, unsigned align);
71
72/**
73 * @brief Enhanced memory get function.
74 */
75typedef void *(*memgetfunc2_t)(size_t size, unsigned align, size_t offset);
76
77/**
78 * @brief Type of memory core object.
79 */
80typedef struct {
81 /**
82 * @brief Next free address.
83 */
84 uint8_t *basemem;
85 /**
86 * @brief Final address.
87 */
88 uint8_t *topmem;
89} memcore_t;
90
91/*===========================================================================*/
92/* Module macros. */
93/*===========================================================================*/
94
95/**
96 * @brief Allocates a memory block.
97 * @note This is a generic form with unspecified allocation position.
98 *
99 * @iclass
100 */
101#define chCoreAllocAlignedWithOffsetI chCoreAllocFromTopI
102
103/**
104 * @brief Allocates a memory block.
105 * @note This is a generic form with unspecified allocation position.
106 *
107 * @api
108 */
109#define chCoreAllocAlignedWithOffset chCoreAllocFromTop
110
111/*===========================================================================*/
112/* External declarations. */
113/*===========================================================================*/
114
115#if !defined(__DOXYGEN__)
116extern memcore_t ch_memcore;
117#endif
118
119#ifdef __cplusplus
120extern "C" {
121#endif
122 void __core_init(void);
123 void *chCoreAllocFromBaseI(size_t size, unsigned align, size_t offset);
124 void *chCoreAllocFromTopI(size_t size, unsigned align, size_t offset);
125 void *chCoreAllocFromBase(size_t size, unsigned align, size_t offset);
126 void *chCoreAllocFromTop(size_t size, unsigned align, size_t offset);
127 size_t chCoreGetStatusX(void);
128#ifdef __cplusplus
129}
130#endif
131
132/*===========================================================================*/
133/* Module inline functions. */
134/*===========================================================================*/
135
136/**
137 * @brief Allocates a memory block.
138 * @details The allocated block is guaranteed to be properly aligned to the
139 * specified alignment.
140 * @note This is a generic form with unspecified allocation position.
141 *
142 * @param[in] size the size of the block to be allocated.
143 * @param[in] align desired memory alignment
144 * @return A pointer to the allocated memory block.
145 * @retval NULL allocation failed, core memory exhausted.
146 *
147 * @iclass
148 */
149static inline void *chCoreAllocAlignedI(size_t size, unsigned align) {
150
151 return chCoreAllocAlignedWithOffsetI(size, align, 0U);
152}
153
154/**
155 * @brief Allocates a memory block.
156 * @details The allocated block is guaranteed to be properly aligned to the
157 * specified alignment.
158 * @note This is a generic form with unspecified allocation position.
159 *
160 * @param[in] size the size of the block to be allocated
161 * @param[in] align desired memory alignment
162 * @return A pointer to the allocated memory block.
163 * @retval NULL allocation failed, core memory exhausted.
164 *
165 * @api
166 */
167static inline void *chCoreAllocAligned(size_t size, unsigned align) {
168
169 return chCoreAllocAlignedWithOffset(size, align, 0U);
170}
171
172/**
173 * @brief Allocates a memory block.
174 * @details The allocated block is guaranteed to be properly aligned for a
175 * pointer data type.
176 * @note This is a generic form with unspecified allocation position.
177 *
178 * @param[in] size the size of the block to be allocated.
179 * @return A pointer to the allocated memory block.
180 * @retval NULL allocation failed, core memory exhausted.
181 *
182 * @iclass
183 */
184static inline void *chCoreAllocI(size_t size) {
185
187}
188
189/**
190 * @brief Allocates a memory block.
191 * @details The allocated block is guaranteed to be properly aligned for a
192 * pointer data type.
193 * @note This is a generic form with unspecified allocation position.
194 *
195 * @param[in] size the size of the block to be allocated.
196 * @return A pointer to the allocated memory block.
197 * @retval NULL allocation failed, core memory exhausted.
198 *
199 * @api
200 */
201static inline void *chCoreAlloc(size_t size) {
202
204}
205
206#endif /* CH_CFG_USE_MEMCORE == TRUE */
207
208#endif /* CHMEMCORE_H */
209
210/** @} */
static void * chCoreAllocAlignedI(size_t size, unsigned align)
Allocates a memory block.
Definition chmemcore.h:149
void * chCoreAllocFromTop(size_t size, unsigned align, size_t offset)
Allocates a memory block starting from the top address downward.
Definition chmemcore.c:231
#define chCoreAllocAlignedWithOffset
Allocates a memory block.
Definition chmemcore.h:109
void * chCoreAllocFromBase(size_t size, unsigned align, size_t offset)
Allocates a memory block starting from the lowest address upward.
Definition chmemcore.c:207
size_t chCoreGetStatusX(void)
Core memory status.
Definition chmemcore.c:248
memcore_t ch_memcore
Memory core descriptor.
Definition chmemcore.c:57
void * chCoreAllocFromTopI(size_t size, unsigned align, size_t offset)
Allocates a memory block starting from the top address downward.
Definition chmemcore.c:159
static void * chCoreAlloc(size_t size)
Allocates a memory block.
Definition chmemcore.h:201
void * chCoreAllocFromBaseI(size_t size, unsigned align, size_t offset)
Allocates a memory block starting from the lowest address upward.
Definition chmemcore.c:111
#define chCoreAllocAlignedWithOffsetI
Allocates a memory block.
Definition chmemcore.h:101
static void * chCoreAllocAligned(size_t size, unsigned align)
Allocates a memory block.
Definition chmemcore.h:167
void __core_init(void)
Low level memory manager initialization.
Definition chmemcore.c:80
static void * chCoreAllocI(size_t size)
Allocates a memory block.
Definition chmemcore.h:184
#define PORT_NATURAL_ALIGN
Natural alignment constant.
Definition chcore.h:49
Type of memory core object.
Definition chmemcore.h:80
uint8_t * basemem
Next free address.
Definition chmemcore.h:84
uint8_t * topmem
Final address.
Definition chmemcore.h:88