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