µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
initialise-free-store.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2016-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#if defined(__clang__)
13#pragma clang diagnostic ignored "-Wempty-translation-unit"
14#endif
15
16// ----------------------------------------------------------------------------
17
18#if defined(__ARM_EABI__)
19
20// ----------------------------------------------------------------------------
21
22#if defined(OS_USE_OS_APP_CONFIG_H)
23#include <cmsis-plus/os-app-config.h>
24#endif
25
31
32// ----------------------------------------------------------------------------
33
34using namespace os;
35
36// ----------------------------------------------------------------------------
37
38#if defined(OS_TYPE_APPLICATION_MEMORY_RESOURCE)
40#else
41// using free_store_memory_resource = os::memory::lifo;
43#endif
44
45#if defined(OS_TYPE_RTOS_MEMORY_RESOURCE)
47#else
49#endif
50
51extern "C" void*
52sbrk (ptrdiff_t incr);
53
54// ----------------------------------------------------------------------------
55
56#if !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
57
58// Reserve storage for the application memory resource.
59static std::aligned_storage<sizeof (application_memory_resource),
60 alignof (application_memory_resource)>::type
62
63#endif /* !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS) */
64
81void __attribute__ ((weak))
83 std::size_t heap_size_bytes)
84{
85 trace::printf ("%s(%p,%u)\n", __func__, heap_address, heap_size_bytes);
86
87#if !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
88
89 // Construct the memory resource used for the application free store.
91 application_memory_resource{ "app", heap_address, heap_size_bytes };
92
93 // Configure the memory manager to throw an exception when out of memory.
95 ->out_of_memory_handler (os_rtos_application_out_of_memory_hook);
96
97 // Set the application free store memory manager.
100
101 // Adjust sbrk() to prevent it overlapping the free store.
102#pragma GCC diagnostic push
103#pragma GCC diagnostic ignored "-Wuseless-cast"
104 sbrk (
105 static_cast<char*> (static_cast<char*> (heap_address) + heap_size_bytes)
106 - static_cast<char*> (sbrk (0)));
107#pragma GCC diagnostic pop
108
109#if defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES)
110
111 {
112 // Allocate the RTOS dynamic memory on the application free store.
113 void* rtos_arena
114 = reinterpret_cast<rtos::memory::memory_resource*> (
117
118 // Allocate & construct the memory resource used for the RTOS.
121 };
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*> (
136
137#endif /* defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES) */
138
139#if defined(OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE)
140
141 {
143 "Mutex pool size must be >1.");
147
148 // Configure the memory manager to throw an exception when out of memory.
150
151 rtos::memory::set_resource_typed<rtos::thread> (mr);
152 }
153
154#endif /* defined(OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE) */
155
156#if defined(OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE)
157
158 {
160 "Mutex pool size must be >1.");
165
166 // Configure the memory manager to throw an exception when out of memory.
168
169 rtos::memory::set_resource_typed<rtos::condition_variable> (mr);
170 }
171
172#endif /* defined(OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE) */
173
174#if defined(OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE)
175
176 {
178 "Mutex pool size must be >1.");
182 "pool-ef");
183
184 // Configure the memory manager to throw an exception when out of memory.
186
187 rtos::memory::set_resource_typed<rtos::event_flags> (mr);
188 }
189
190#endif /* defined(OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE) */
191
192#if defined(OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE)
193
194 {
196 "Mutex pool size must be >1.");
200 "pool-mp");
201
202 // Configure the memory manager to throw an exception when out of memory.
204
205 rtos::memory::set_resource_typed<rtos::memory_pool> (mr);
206 }
207
208#endif /* defined(OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE) */
209
210#if defined(OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE)
211
212 {
214 "Mutex pool size must be >1.");
219
220 // Configure the memory manager to throw an exception when out of memory.
222
223 rtos::memory::set_resource_typed<rtos::message_queue> (mr);
224 }
225
226#endif /* defined(OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE) */
227
228#if defined(OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE)
229
230 {
231 static_assert (OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE > 1,
232 "Mutex pool size must be >1.");
236
237 // Configure the memory manager to throw an exception when out of memory.
239
240 rtos::memory::set_resource_typed<rtos::mutex> (mr);
241 }
242
243#endif /* defined(OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE) */
244
245#if defined(OS_INTEGER_RTOS_ALLOC_SEMAPHORE_POOL_SIZE)
246
247 {
249 "Semaphore pool size must be >1.");
253 "pool-sp");
254
255 // Configure the memory manager to throw an exception when out of memory.
257
258 rtos::memory::set_resource_typed<rtos::semaphore> (mr);
259 }
260
261#endif /* defined(OS_INTEGER_RTOS_ALLOC_SEMAPHORE_POOL_SIZE) */
262
263#if defined(OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE)
264
265 {
266 static_assert (OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE > 1,
267 "Mutex pool size must be >1.");
271
272 // Configure the memory manager to throw an exception when out of memory.
274
275 rtos::memory::set_resource_typed<rtos::timer> (mr);
276 }
277
278#endif /* defined(OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE) */
279
280#endif /* !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS) */
281}
282
298void __attribute__ ((weak))
300{
302}
303
304#if defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES)
305
321void __attribute__ ((weak))
323{
325}
326
327#endif
328
329// ----------------------------------------------------------------------------
330
331#endif /* defined(__ARM_EABI__) */
Memory resource managing an internal pool. of same size blocks of type T.
Definition block-pool.h:245
Memory resource implementing the first fit, top-down allocation policies, using an existing arena.
Memory resource implementing the LIFO allocation/deallocation policies, using an existing arena.
Definition lifo.h:72
POSIX compliant condition variable.
Definition os-condvar.h:45
Synchronised event flags.
Definition os-evflags.h:57
Memory resource manager (abstract class).
Definition os-memory.h:159
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:1375
Synchronised memory pool, using the default RTOS allocator.
Definition os-mempool.h:67
POSIX compliant message queue, using the default RTOS allocator.
Definition os-mqueue.h:67
POSIX compliant mutex.
Definition os-mutex.h:52
POSIX compliant semaphore.
POSIX compliant thread, using the default RTOS allocator.
Definition os-thread.h:251
User single-shot or periodic timer.
Definition os-timer.h:56
#define OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE
Define a pool of thread objects.
#define OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE
Define a pool of event flags objects.
#define OS_INTEGER_RTOS_ALLOC_SEMAPHORE_POOL_SIZE
Define a pool of semaphore objects.
#define OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE
Define a pool of condition variable objects.
#define OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE
Define a pool of mutex objects.
#define OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE
Define a pool of memory pool objects.
#define OS_TYPE_APPLICATION_MEMORY_RESOURCE
The type of the memory manager to be used for the application free store.
#define OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE
Define a pool of timer objects.
#define OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE
Define a pool of message queue objects.
#define OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES
Ask for separate RTOS dynamic memory and define its size.
#define OS_TYPE_RTOS_MEMORY_RESOURCE
The type of the memory manager to be used for the RTOS system area.
void os_rtos_application_out_of_memory_hook(void)
Hook to handle out of memory in the application free store.
void os_rtos_system_out_of_memory_hook(void)
Hook to handle out of memory in the RTOS dynamic memory.
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
memory_resource * set_default_resource(memory_resource *res) noexcept
Set the default RTOS system memory manager.
memory_resource * set_default_resource(memory_resource *res) noexcept
Set the default application memory manager.
static std::aligned_storage< sizeof(application_memory_resource), alignof(application_memory_resource)>::type application_free_store
os::memory::first_fit_top application_memory_resource
void os_startup_initialize_free_store(void *heap_address, std::size_t heap_size_bytes)
void * sbrk(ptrdiff_t incr)
void __throw_bad_alloc(void)
System namespace.
Standard std namespace.