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