ChibiOS/RT  6.0.3
chmemcore.c
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.c
22  * @brief Core memory manager code.
23  *
24  * @addtogroup oslib_memcore
25  * @details Core Memory Manager related APIs and services.
26  * <h2>Operation mode</h2>
27  * The core memory manager is a simplified allocator that only
28  * allows to allocate memory blocks without the possibility to
29  * free them.<br>
30  * This allocator is meant as a memory blocks provider for the
31  * other allocators such as:
32  * - C-Runtime allocator (through a compiler specific adapter module).
33  * - Heap allocator (see @ref oslib_memheaps).
34  * - Memory pools allocator (see @ref oslib_mempools).
35  * .
36  * By having a centralized memory provider the various allocators
37  * can coexist and share the main memory.<br>
38  * This allocator, alone, is also useful for very simple
39  * applications that just require a simple way to get memory
40  * blocks.
41  * @pre In order to use the core memory manager APIs the @p CH_CFG_USE_MEMCORE
42  * option must be enabled in @p chconf.h.
43  * @note Compatible with RT and NIL.
44  * @{
45  */
46 
47 #include "ch.h"
48 
49 #if (CH_CFG_USE_MEMCORE == TRUE) || defined(__DOXYGEN__)
50 
51 /*===========================================================================*/
52 /* Module exported variables. */
53 /*===========================================================================*/
54 
55 /**
56  * @brief Memory core descriptor.
57  */
59 
60 /*===========================================================================*/
61 /* Module local types. */
62 /*===========================================================================*/
63 
64 /*===========================================================================*/
65 /* Module local variables. */
66 /*===========================================================================*/
67 
68 /*===========================================================================*/
69 /* Module local functions. */
70 /*===========================================================================*/
71 
72 /*===========================================================================*/
73 /* Module exported functions. */
74 /*===========================================================================*/
75 
76 /**
77  * @brief Low level memory manager initialization.
78  *
79  * @notapi
80  */
81 void _core_init(void) {
82 #if CH_CFG_MEMCORE_SIZE == 0
83  extern uint8_t __heap_base__[];
84  extern uint8_t __heap_end__[];
85 
86  /*lint -save -e9033 [10.8] Required cast operations.*/
87  ch_memcore.nextmem = __heap_base__;
88  ch_memcore.endmem = __heap_end__;
89  /*lint restore*/
90 #else
91  static uint8_t static_heap[CH_CFG_MEMCORE_SIZE];
92 
93  ch_memcore.nextmem = &static_heap[0];
94  ch_memcore.endmem = &static_heap[CH_CFG_MEMCORE_SIZE];
95 #endif
96 }
97 
98 /**
99  * @brief Allocates a memory block.
100  * @details This function allocates a block of @p offset + @p size bytes. The
101  * returned pointer has @p offset bytes before its address and
102  * @p size bytes after.
103  *
104  * @param[in] size the size of the block to be allocated.
105  * @param[in] align desired memory alignment
106  * @param[in] offset aligned pointer offset
107  * @return A pointer to the allocated memory block.
108  * @retval NULL allocation failed, core memory exhausted.
109  *
110  * @iclass
111  */
113  unsigned align,
114  size_t offset) {
115  uint8_t *p, *next;
116 
119 
120  size = MEM_ALIGN_NEXT(size, align);
121  p = (uint8_t *)MEM_ALIGN_NEXT(ch_memcore.nextmem + offset, align);
122  next = p + size;
123 
124  /* Considering also the case where there is numeric overflow.*/
125  if ((next > ch_memcore.endmem) || (next < ch_memcore.nextmem)) {
126  return NULL;
127  }
128 
129  ch_memcore.nextmem = next;
130 
131  return p;
132 }
133 
134 /**
135  * @brief Allocates a memory block.
136  * @details This function allocates a block of @p offset + @p size bytes. The
137  * returned pointer has @p offset bytes before its address and
138  * @p size bytes after.
139  *
140  * @param[in] size the size of the block to be allocated.
141  * @param[in] align desired memory alignment
142  * @param[in] offset aligned pointer offset
143  * @return A pointer to the allocated memory block.
144  * @retval NULL allocation failed, core memory exhausted.
145  *
146  * @api
147  */
149  unsigned align,
150  size_t offset) {
151  void *p;
152 
153  chSysLock();
154  p = chCoreAllocAlignedWithOffsetI(size, align, offset);
155  chSysUnlock();
156 
157  return p;
158 }
159 
160 /**
161  * @brief Core memory status.
162  *
163  * @return The size, in bytes, of the free core memory.
164  *
165  * @xclass
166  */
167 size_t chCoreGetStatusX(void) {
168 
169  /*lint -save -e9033 [10.8] The cast is safe.*/
170  return (size_t)(ch_memcore.endmem - ch_memcore.nextmem);
171  /*lint -restore*/
172 }
173 #endif /* CH_CFG_USE_MEMCORE == TRUE */
174 
175 /** @} */
void * chCoreAllocAlignedWithOffset(size_t size, unsigned align, size_t offset)
Allocates a memory block.
Definition: chmemcore.c:148
static void chSysLock(void)
Enters the kernel lock state.
Definition: chsys.h:353
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
static void chSysUnlock(void)
Leaves the kernel lock state.
Definition: chsys.h:365
#define MEM_IS_VALID_ALIGNMENT(a)
Returns whatever a constant is a valid alignment.
Definition: chalign.h:98
memcore_t ch_memcore
Memory core descriptor.
Definition: chmemcore.c:58
#define chDbgCheck(c)
Function parameters check.
Definition: chdebug.h:101
#define CH_CFG_MEMCORE_SIZE
Managed RAM size.
Definition: chconf.h:123
size_t chCoreGetStatusX(void)
Core memory status.
Definition: chmemcore.c:167
void chDbgCheckClassI(void)
I-class functions context check.
Definition: chdebug.c:233
uint8_t * nextmem
Next free address.
Definition: chmemcore.h:85
ChibiOS/RT main include file.
#define MEM_ALIGN_NEXT(p, a)
Aligns to the next aligned memory address.
Definition: chalign.h:79
uint8_t * endmem
Final address.
Definition: chmemcore.h:89