ChibiOS/NIL  3.2.2
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 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  */
71 typedef void *(*memgetfunc_t)(size_t size, unsigned align);
72 
73 /**
74  * @brief Enhanced memory get function.
75  */
76 typedef void *(*memgetfunc2_t)(size_t size, unsigned align, size_t offset);
77 
78 /**
79  * @brief Type of memory core object.
80  */
81 typedef struct {
82  /**
83  * @brief Next free address.
84  */
85  uint8_t *nextmem;
86  /**
87  * @brief Final address.
88  */
89  uint8_t *endmem;
90 } memcore_t;
91 
92 /*===========================================================================*/
93 /* Module macros. */
94 /*===========================================================================*/
95 
96 /*===========================================================================*/
97 /* External declarations. */
98 /*===========================================================================*/
99 
100 #if !defined(__DOXYGEN__)
101 extern memcore_t ch_memcore;
102 #endif
103 
104 #ifdef __cplusplus
105 extern "C" {
106 #endif
107  void _core_init(void);
108  void *chCoreAllocAlignedWithOffsetI(size_t size,
109  unsigned align,
110  size_t offset);
111  void *chCoreAllocAlignedWithOffset(size_t size,
112  unsigned align,
113  size_t offset);
114  size_t chCoreGetStatusX(void);
115 #ifdef __cplusplus
116 }
117 #endif
118 
119 /*===========================================================================*/
120 /* Module inline functions. */
121 /*===========================================================================*/
122 
123 /**
124  * @brief Allocates a memory block.
125  * @details The allocated block is guaranteed to be properly aligned to the
126  * specified alignment.
127  *
128  * @param[in] size the size of the block to be allocated.
129  * @param[in] align desired memory alignment
130  * @return A pointer to the allocated memory block.
131  * @retval NULL allocation failed, core memory exhausted.
132  *
133  * @iclass
134  */
135 static inline void *chCoreAllocAlignedI(size_t size, unsigned align) {
136 
137  return chCoreAllocAlignedWithOffsetI(size, align, 0U);
138 }
139 
140 /**
141  * @brief Allocates a memory block.
142  * @details The allocated block is guaranteed to be properly aligned to the
143  * specified alignment.
144  *
145  * @param[in] size the size of the block to be allocated
146  * @param[in] align desired memory alignment
147  * @return A pointer to the allocated memory block.
148  * @retval NULL allocation failed, core memory exhausted.
149  *
150  * @api
151  */
152 static inline void *chCoreAllocAligned(size_t size, unsigned align) {
153  void *p;
154 
155  chSysLock();
156  p = chCoreAllocAlignedWithOffsetI(size, align, 0U);
157  chSysUnlock();
158 
159  return p;
160 }
161 
162 /**
163  * @brief Allocates a memory block.
164  * @details The allocated block is guaranteed to be properly aligned for a
165  * pointer data type.
166  *
167  * @param[in] size the size of the block to be allocated.
168  * @return A pointer to the allocated memory block.
169  * @retval NULL allocation failed, core memory exhausted.
170  *
171  * @iclass
172  */
173 static inline void *chCoreAllocI(size_t size) {
174 
175  return chCoreAllocAlignedWithOffsetI(size, PORT_NATURAL_ALIGN, 0U);
176 }
177 
178 /**
179  * @brief Allocates a memory block.
180  * @details The allocated block is guaranteed to be properly aligned for a
181  * pointer data type.
182  *
183  * @param[in] size the size of the block to be allocated.
184  * @return A pointer to the allocated memory block.
185  * @retval NULL allocation failed, core memory exhausted.
186  *
187  * @api
188  */
189 static inline void *chCoreAlloc(size_t size) {
190 
191  return chCoreAllocAlignedWithOffset(size, PORT_NATURAL_ALIGN, 0U);
192 }
193 
194 #endif /* CH_CFG_USE_MEMCORE == TRUE */
195 
196 #endif /* CHMEMCORE_H */
197 
198 /** @} */
void * chCoreAllocAlignedWithOffset(size_t size, unsigned align, size_t offset)
Allocates a memory block.
Definition: chmemcore.c:148
#define chSysLock()
Enters the kernel lock state.
Definition: ch.h:1151
void * chCoreAllocAlignedWithOffsetI(size_t size, unsigned align, size_t offset)
Allocates a memory block.
Definition: chmemcore.c:112
void _core_init(void)
Low level memory manager initialization.
Definition: chmemcore.c:81
Type of memory core object.
Definition: chmemcore.h:81
memcore_t ch_memcore
Memory core descriptor.
Definition: chmemcore.c:58
static void * chCoreAlloc(size_t size)
Allocates a memory block.
Definition: chmemcore.h:189
#define chSysUnlock()
Leaves the kernel lock state.
Definition: ch.h:1161
static void * chCoreAllocI(size_t size)
Allocates a memory block.
Definition: chmemcore.h:173
size_t chCoreGetStatusX(void)
Core memory status.
Definition: chmemcore.c:167
static void * chCoreAllocAligned(size_t size, unsigned align)
Allocates a memory block.
Definition: chmemcore.h:152
uint8_t * nextmem
Next free address.
Definition: chmemcore.h:85
uint8_t * endmem
Final address.
Definition: chmemcore.h:89
static void * chCoreAllocAlignedI(size_t size, unsigned align)
Allocates a memory block.
Definition: chmemcore.h:135