µOS++ IIIe / CMSIS++ / POSIX++ Reference  v6.3.11
“Perfekt ist nicht gut genug”
The third edition of µOS++ and CMSIS++, a proposal for the next generation CMSIS, written in C++.
initialise-free-store.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the µOS++ distribution.
3  * (https://github.com/micro-os-plus)
4  * Copyright (c) 2016 Liviu Ionescu.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or
11  * sell copies of the Software, and to permit persons to whom
12  * the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #if defined(__ARM_EABI__)
29 
30 // ----------------------------------------------------------------------------
31 
34 #include <cmsis-plus/memory/lifo.h>
37 
38 // ----------------------------------------------------------------------------
39 
40 using namespace os;
41 
42 // ----------------------------------------------------------------------------
43 
44 #if defined(OS_TYPE_APPLICATION_MEMORY_RESOURCE)
46 #else
47 //using free_store_memory_resource = os::memory::lifo;
49 #endif
50 
51 #if defined(OS_TYPE_RTOS_MEMORY_RESOURCE)
53 #else
55 #endif
56 
57 extern "C" void*
58 sbrk (ptrdiff_t incr);
59 
60 // ----------------------------------------------------------------------------
61 
62 #if !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
63 
64 // Reserve storage for the application memory resource.
65 static std::aligned_storage<sizeof(application_memory_resource),
67 
68 #endif /* !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS) */
69 
86 void __attribute__((weak))
88  std::size_t heap_size_bytes)
89 {
90  trace::printf ("%s(%p,%u)\n", __func__, heap_address, heap_size_bytes);
91 
92 #if !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
93 
94  // Construct the memory resource used for the application free store.
96  { "app", heap_address, heap_size_bytes };
97 
98  // Configure the memory manager to throw an exception when out of memory.
99  reinterpret_cast<rtos::memory::memory_resource*> (&application_free_store)->out_of_memory_handler (
101 
102  // Set the application free store memory manager.
104  reinterpret_cast<estd::pmr::memory_resource*> (&application_free_store));
105 
106  // Adjust sbrk() to prevent it overlapping the free store.
107  sbrk (
108  static_cast<char*> (static_cast<char*> (heap_address) + heap_size_bytes)
109  - static_cast<char*> (sbrk (0)));
110 
111 #if defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES)
112 
113  {
114  // Allocate the RTOS dynamic memory on the application free store.
115  void* rtos_arena =
116  reinterpret_cast<rtos::memory::memory_resource*> (&application_free_store)->allocate (
118 
119  // Allocate & construct the memory resource used for the RTOS.
121  { "sys", rtos_arena, OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES };
122 
123  // Configure the memory manager to throw an exception when out of memory.
125 
126  // Set RTOS system memory manager.
128  }
129 
130 #else
131 
132  // The RTOS system memory manager is identical with the application one.
134  reinterpret_cast<rtos::memory::memory_resource*> (&application_free_store));
135 
136 #endif /* defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES) */
137 
138 #if defined(OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE)
139 
140  {
141  static_assert(OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE > 1,
142  "Mutex pool size must be >1.");
146 
147  // Configure the memory manager to throw an exception when out of memory.
149 
150  rtos::memory::set_resource_typed<rtos::thread> (mr);
151  }
152 
153 #endif /* defined(OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE) */
154 
155 #if defined(OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE)
156 
157  {
159  "Mutex pool size must be >1.");
163 
164  // Configure the memory manager to throw an exception when out of memory.
166 
167  rtos::memory::set_resource_typed<rtos::condition_variable> (mr);
168  }
169 
170 #endif /* defined(OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE) */
171 
172 #if defined(OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE)
173 
174  {
176  "Mutex pool size must be >1.");
180 
181  // Configure the memory manager to throw an exception when out of memory.
183 
184  rtos::memory::set_resource_typed<rtos::event_flags> (mr);
185  }
186 
187 #endif /* defined(OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE) */
188 
189 #if defined(OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE)
190 
191  {
193  "Mutex pool size must be >1.");
197 
198  // Configure the memory manager to throw an exception when out of memory.
200 
201  rtos::memory::set_resource_typed<rtos::memory_pool> (mr);
202  }
203 
204 #endif /* defined(OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE) */
205 
206 #if defined(OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE)
207 
208  {
210  "Mutex pool size must be >1.");
214 
215  // Configure the memory manager to throw an exception when out of memory.
217 
218  rtos::memory::set_resource_typed<rtos::message_queue> (mr);
219  }
220 
221 #endif /* defined(OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE) */
222 
223 #if defined(OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE)
224 
225  {
226  static_assert(OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE > 1,
227  "Mutex pool size must be >1.");
231 
232  // Configure the memory manager to throw an exception when out of memory.
234 
235  rtos::memory::set_resource_typed<rtos::mutex> (mr);
236  }
237 
238 #endif /* defined(OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE) */
239 
240 #if defined(OS_INTEGER_RTOS_ALLOC_SEMAPHORE_POOL_SIZE)
241 
242  {
244  "Semaphore pool size must be >1.");
248 
249  // Configure the memory manager to throw an exception when out of memory.
251 
252  rtos::memory::set_resource_typed<rtos::semaphore> (mr);
253  }
254 
255 #endif /* defined(OS_INTEGER_RTOS_ALLOC_SEMAPHORE_POOL_SIZE) */
256 
257 #if defined(OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE)
258 
259  {
260  static_assert(OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE > 1,
261  "Mutex pool size must be >1.");
265 
266  // Configure the memory manager to throw an exception when out of memory.
268 
269  rtos::memory::set_resource_typed<rtos::timer> (mr);
270  }
271 
272 #endif /* defined(OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE) */
273 
274 #endif /* !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS) */
275 }
276 
292 void __attribute__((weak))
294 {
296 }
297 
298 #if defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES)
299 
315 void __attribute__((weak))
317 {
319 }
320 
321 #endif
322 
323 // ----------------------------------------------------------------------------
324 
325 #endif /* defined(__ARM_EABI__) */
Memory resource manager (abstract class).
Definition: os-memory.h:149
void * sbrk(ptrdiff_t incr)
void os_rtos_system_out_of_memory_hook(void)
Hook to handle out of memory in the RTOS dynamic memory.
#define OS_INTEGER_RTOS_ALLOC_SEMAPHORE_POOL_SIZE
Define a pool of semaphore objects.
Memory resource implementing the LIFO allocation/deallocation policies, using an existing arena...
Definition: lifo.h:78
Memory resource managing an internal pool. of same size blocks of type T.
Definition: block-pool.h:255
POSIX compliant semaphore.
Definition: os-semaphore.h:58
POSIX compliant mutex.
Definition: os-mutex.h:58
Definition: new.cpp:70
#define OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE
Define a pool of timer objects.
System namespace.
User single-shot or periodic timer.
Definition: os-timer.h:58
#define OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE
Define a pool of mutex objects.
#define OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE
Define a pool of condition variable objects.
memory_resource * set_default_resource(memory_resource *res) noexcept
Set the default RTOS system memory manager.
Definition: os-memory.cpp:192
POSIX compliant condition variable.
Definition: os-condvar.h:51
memory_resource * set_default_resource(memory_resource *res) noexcept
Set the default application memory manager.
#define OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE
Define a pool of event flags objects.
void os_rtos_application_out_of_memory_hook(void)
Hook to handle out of memory in the application free store.
#define OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE
Define a pool of message queue objects.
Synchronised memory pool, using the default RTOS allocator.
Definition: os-mempool.h:63
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition: trace.cpp:74
os::memory::first_fit_top application_memory_resource
#define OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE
Define a pool of thread objects.
void os_startup_initialize_free_store(void *heap_address, std::size_t heap_size_bytes)
Synchronised event flags.
Definition: os-evflags.h:55
Memory resource implementing the first fit, top-down allocation policies, using an existing arena...
Definition: first-fit-top.h:59
out_of_memory_handler_t out_of_memory_handler(out_of_memory_handler_t handler)
Set the out of memory handler.
Definition: os-memory.h:1348
thread & thread(void)
Get the current running thread.
Definition: os-thread.cpp:1481
#define OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES
Ask for separate RTOS dynamic memory and define its size.
#define OS_TYPE_APPLICATION_MEMORY_RESOURCE
The type of the memory manager to be used for the application free store.
static std::aligned_storage< sizeof(application_memory_resource), alignof(application_memory_resource)>::type application_free_store
#define OS_TYPE_RTOS_MEMORY_RESOURCE
The type of the memory manager to be used for the RTOS system area.
void __throw_bad_alloc(void)
#define OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE
Define a pool of memory pool objects.
POSIX compliant message queue, using the default RTOS allocator.
Definition: os-mqueue.h:63