µ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++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#if defined(__clang__)
14#pragma clang diagnostic ignored "-Wempty-translation-unit"
15#endif
16
17// ----------------------------------------------------------------------------
18
19#if defined(__ARM_EABI__)
20
21// ----------------------------------------------------------------------------
22
23#if defined(OS_USE_OS_APP_CONFIG_H)
24#include <cmsis-plus/os-app-config.h>
25#endif
26
32
33// ----------------------------------------------------------------------------
34
35using namespace os;
36
37// ----------------------------------------------------------------------------
38
39#if defined(OS_TYPE_APPLICATION_MEMORY_RESOURCE)
41#else
42//using free_store_memory_resource = os::memory::lifo;
44#endif
45
46#if defined(OS_TYPE_RTOS_MEMORY_RESOURCE)
48#else
50#endif
51
52extern "C" void*
53sbrk (ptrdiff_t incr);
54
55// ----------------------------------------------------------------------------
56
57#if !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
58
59// Reserve storage for the application memory resource.
60static std::aligned_storage<sizeof(application_memory_resource),
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 { "app", heap_address, heap_size_bytes };
92
93 // Configure the memory manager to throw an exception when out of memory.
94 reinterpret_cast<rtos::memory::memory_resource*> (&application_free_store)->out_of_memory_handler (
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*> (&application_free_store)->allocate (
116
117 // Allocate & construct the memory resource used for the RTOS.
119 { "sys", rtos_arena, OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES };
120
121 // Configure the memory manager to throw an exception when out of memory.
123
124 // Set RTOS system memory manager.
126 }
127
128#else
129
130 // The RTOS system memory manager is identical with the application one.
133
134#endif /* defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES) */
135
136#if defined(OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE)
137
138 {
140 "Mutex pool size must be >1.");
144
145 // Configure the memory manager to throw an exception when out of memory.
147
148 rtos::memory::set_resource_typed<rtos::thread> (mr);
149 }
150
151#endif /* defined(OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE) */
152
153#if defined(OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE)
154
155 {
157 "Mutex pool size must be >1.");
161
162 // Configure the memory manager to throw an exception when out of memory.
164
165 rtos::memory::set_resource_typed<rtos::condition_variable> (mr);
166 }
167
168#endif /* defined(OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE) */
169
170#if defined(OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE)
171
172 {
174 "Mutex pool size must be >1.");
178
179 // Configure the memory manager to throw an exception when out of memory.
181
182 rtos::memory::set_resource_typed<rtos::event_flags> (mr);
183 }
184
185#endif /* defined(OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE) */
186
187#if defined(OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE)
188
189 {
191 "Mutex pool size must be >1.");
195
196 // Configure the memory manager to throw an exception when out of memory.
198
199 rtos::memory::set_resource_typed<rtos::memory_pool> (mr);
200 }
201
202#endif /* defined(OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE) */
203
204#if defined(OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE)
205
206 {
208 "Mutex pool size must be >1.");
212
213 // Configure the memory manager to throw an exception when out of memory.
215
216 rtos::memory::set_resource_typed<rtos::message_queue> (mr);
217 }
218
219#endif /* defined(OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE) */
220
221#if defined(OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE)
222
223 {
225 "Mutex pool size must be >1.");
229
230 // Configure the memory manager to throw an exception when out of memory.
232
233 rtos::memory::set_resource_typed<rtos::mutex> (mr);
234 }
235
236#endif /* defined(OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE) */
237
238#if defined(OS_INTEGER_RTOS_ALLOC_SEMAPHORE_POOL_SIZE)
239
240 {
242 "Semaphore pool size must be >1.");
246
247 // Configure the memory manager to throw an exception when out of memory.
249
250 rtos::memory::set_resource_typed<rtos::semaphore> (mr);
251 }
252
253#endif /* defined(OS_INTEGER_RTOS_ALLOC_SEMAPHORE_POOL_SIZE) */
254
255#if defined(OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE)
256
257 {
259 "Mutex pool size must be >1.");
263
264 // Configure the memory manager to throw an exception when out of memory.
266
267 rtos::memory::set_resource_typed<rtos::timer> (mr);
268 }
269
270#endif /* defined(OS_INTEGER_RTOS_ALLOC_TIMER_POOL_SIZE) */
271
272#endif /* !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS) */
273}
274
290void __attribute__((weak))
292{
294}
295
296#if defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES)
297
313void __attribute__((weak))
315{
317}
318
319#endif
320
321// ----------------------------------------------------------------------------
322
323#endif /* defined(__ARM_EABI__) */
Memory resource managing an internal pool. of same size blocks of type T.
Definition block-pool.h:250
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:73
POSIX compliant condition variable.
Definition os-condvar.h:46
Synchronised event flags.
Definition os-evflags.h:58
Memory resource manager (abstract class).
Definition os-memory.h:160
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:1374
Synchronised memory pool, using the default RTOS allocator.
Definition os-mempool.h:68
POSIX compliant message queue, using the default RTOS allocator.
Definition os-mqueue.h:68
POSIX compliant mutex.
Definition os-mutex.h:53
POSIX compliant semaphore.
POSIX compliant thread, using the default RTOS allocator.
Definition os-thread.h:250
User single-shot or periodic timer.
Definition os-timer.h:57
#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:60
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.