µ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 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(__clang__)
29#pragma clang diagnostic ignored "-Wempty-translation-unit"
30#endif
31
32// ----------------------------------------------------------------------------
33
34#if defined(__ARM_EABI__)
35
36// ----------------------------------------------------------------------------
37
43
44// ----------------------------------------------------------------------------
45
46using namespace os;
47
48// ----------------------------------------------------------------------------
49
50#if defined(OS_TYPE_APPLICATION_MEMORY_RESOURCE)
52#else
53//using free_store_memory_resource = os::memory::lifo;
55#endif
56
57#if defined(OS_TYPE_RTOS_MEMORY_RESOURCE)
59#else
61#endif
62
63extern "C" void*
64sbrk (ptrdiff_t incr);
65
66// ----------------------------------------------------------------------------
67
68#if !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
69
70// Reserve storage for the application memory resource.
71static std::aligned_storage<sizeof(application_memory_resource),
73
74#endif /* !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS) */
75
92void __attribute__((weak))
94 std::size_t heap_size_bytes)
95{
96 trace::printf ("%s(%p,%u)\n", __func__, heap_address, heap_size_bytes);
97
98#if !defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
99
100 // Construct the memory resource used for the application free store.
102 { "app", heap_address, heap_size_bytes };
103
104 // Configure the memory manager to throw an exception when out of memory.
105 reinterpret_cast<rtos::memory::memory_resource*> (&application_free_store)->out_of_memory_handler (
107
108 // Set the application free store memory manager.
111
112 // Adjust sbrk() to prevent it overlapping the free store.
113 sbrk (
114 static_cast<char*> (static_cast<char*> (heap_address) + heap_size_bytes)
115 - static_cast<char*> (sbrk (0)));
116
117#if defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES)
118
119 {
120 // Allocate the RTOS dynamic memory on the application free store.
121 void* rtos_arena =
122 reinterpret_cast<rtos::memory::memory_resource*> (&application_free_store)->allocate (
124
125 // Allocate & construct the memory resource used for the RTOS.
127 { "sys", rtos_arena, OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES };
128
129 // Configure the memory manager to throw an exception when out of memory.
131
132 // Set RTOS system memory manager.
134 }
135
136#else
137
138 // The RTOS system memory manager is identical with the application one.
141
142#endif /* defined(OS_INTEGER_RTOS_DYNAMIC_MEMORY_SIZE_BYTES) */
143
144#if defined(OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE)
145
146 {
148 "Mutex pool size must be >1.");
152
153 // Configure the memory manager to throw an exception when out of memory.
155
156 rtos::memory::set_resource_typed<rtos::thread> (mr);
157 }
158
159#endif /* defined(OS_INTEGER_RTOS_ALLOC_THREAD_POOL_SIZE) */
160
161#if defined(OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE)
162
163 {
165 "Mutex pool size must be >1.");
169
170 // Configure the memory manager to throw an exception when out of memory.
172
173 rtos::memory::set_resource_typed<rtos::condition_variable> (mr);
174 }
175
176#endif /* defined(OS_INTEGER_RTOS_ALLOC_CONDITION_VARIABLE_POOL_SIZE) */
177
178#if defined(OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE)
179
180 {
182 "Mutex pool size must be >1.");
186
187 // Configure the memory manager to throw an exception when out of memory.
189
190 rtos::memory::set_resource_typed<rtos::event_flags> (mr);
191 }
192
193#endif /* defined(OS_INTEGER_RTOS_ALLOC_EVENT_FLAGS_POOL_SIZE) */
194
195#if defined(OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE)
196
197 {
199 "Mutex pool size must be >1.");
203
204 // Configure the memory manager to throw an exception when out of memory.
206
207 rtos::memory::set_resource_typed<rtos::memory_pool> (mr);
208 }
209
210#endif /* defined(OS_INTEGER_RTOS_ALLOC_MEMORY_POOL_POOL_SIZE) */
211
212#if defined(OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE)
213
214 {
216 "Mutex pool size must be >1.");
220
221 // Configure the memory manager to throw an exception when out of memory.
223
224 rtos::memory::set_resource_typed<rtos::message_queue> (mr);
225 }
226
227#endif /* defined(OS_INTEGER_RTOS_ALLOC_MESSAGE_QUEUE_POOL_SIZE) */
228
229#if defined(OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE)
230
231 {
233 "Mutex pool size must be >1.");
237
238 // Configure the memory manager to throw an exception when out of memory.
240
241 rtos::memory::set_resource_typed<rtos::mutex> (mr);
242 }
243
244#endif /* defined(OS_INTEGER_RTOS_ALLOC_MUTEX_POOL_SIZE) */
245
246#if defined(OS_INTEGER_RTOS_ALLOC_SEMAPHORE_POOL_SIZE)
247
248 {
250 "Semaphore pool size must be >1.");
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 {
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:266
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:89
POSIX compliant condition variable.
Definition os-condvar.h:62
Synchronised event flags.
Definition os-evflags.h:66
Memory resource manager (abstract class).
Definition os-memory.h:165
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:1379
Synchronised memory pool, using the default RTOS allocator.
Definition os-mempool.h:70
POSIX compliant message queue, using the default RTOS allocator.
Definition os-mqueue.h:70
POSIX compliant mutex.
Definition os-mutex.h:65
POSIX compliant semaphore.
POSIX compliant thread, using the default RTOS allocator.
Definition os-thread.h:247
User single-shot or periodic timer.
Definition os-timer.h:65
#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:74
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.