µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
new.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/*
14 * [Partly inspired from the LLVM libcxx sources].
15 * Copyright (c) 2009-2013 by the contributors listed in
16 * 'LLVM libcxx Credits.txt'. See 'LLVM libcxx License.txt' for details.
17 *
18 * References are to ISO/IEC 14882:2011(E) Third edition (2011-09-01)
19 */
20
26#if defined(OS_USE_OS_APP_CONFIG_H)
27#include <cmsis-plus/os-app-config.h>
28#endif
29
30#include <cmsis-plus/rtos/os.h>
32
33// ----------------------------------------------------------------------------
34
35#if defined(__clang__)
36#pragma clang diagnostic ignored "-Wc++98-compat"
37#endif
38
39// ----------------------------------------------------------------------------
40
41using namespace os;
42
43// ----------------------------------------------------------------------------
44
45namespace
46{
54 std::new_handler new_handler_;
55}
56
62namespace std
63{
64 // Constant to be used as parameter to differentiate
65 // the `noexcept` functions.
66 const nothrow_t nothrow = nothrow_t
67 { };
68
85 new_handler
86 set_new_handler (new_handler handler) noexcept
87 {
88 trace::printf ("std::%s(%p) \n", __func__, handler);
89
90 new_handler prev_handler;
91
92 prev_handler = new_handler_;
93 new_handler_ = handler;
94
95 return prev_handler;
96 }
97
107 new_handler
108 get_new_handler () noexcept
109 {
110 return new_handler_;
111 }
112
113} /* namespace std */
114
115// ----------------------------------------------------------------------------
116
141void *
142__attribute__((weak))
143operator new (std::size_t bytes)
144{
146 if (bytes == 0)
147 {
148 bytes = 1;
149 }
150
151 // ----- Begin of critical section ------------------------------------------
153
154 while (true)
155 {
156 void* mem = estd::pmr::get_default_resource ()->allocate (bytes);
157
158 if (mem != nullptr)
159 {
160#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
161 trace::printf ("::%s(%d)=%p\n", __func__, bytes, mem);
162#endif
163 return mem;
164 }
165
166 // If allocate() fails and there is a new_handler,
167 // call it to try free up memory.
168 if (new_handler_)
169 {
170 new_handler_ ();
171 }
172 else
173 {
175 }
176 }
177
178 // ----- End of critical section --------------------------------------------
179}
180
203void*
204__attribute__((weak))
205operator new (std::size_t bytes,
206 const std::nothrow_t& nothrow __attribute__((unused))) noexcept
207{
209
210 if (bytes == 0)
211 {
212 bytes = 1;
213 }
214
215 // ----- Begin of critical section ------------------------------------------
217
218 while (true)
219 {
220 void* mem = estd::pmr::get_default_resource ()->allocate (bytes);
221
222 if (mem != nullptr)
223 {
224#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
225 trace::printf ("::%s(%d)=%p\n", __func__, bytes, mem);
226#endif
227 return mem;
228 }
229
230 // If allocate() fails and there is a new_handler,
231 // call it to try free up memory.
232 if (new_handler_)
233 {
234 new_handler_ ();
235 }
236 else
237 {
238 break; // return nullptr
239 }
240 }
241
242 // ----- End of critical section --------------------------------------------
243
244 return nullptr;
245}
246
262void*
263__attribute__((weak))
264operator new[] (std::size_t bytes)
265{
266 return ::operator new (bytes);
267}
268
285void*
286__attribute__((weak))
287operator new[] (std::size_t bytes,
288 const std::nothrow_t& nothrow __attribute__((unused))) noexcept
289{
290 return ::operator new (bytes, std::nothrow);
291}
292
293// ----------------------------------------------------------------------------
294
318void
319__attribute__((weak))
320operator delete (void* ptr) noexcept
321{
322#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
323 trace::printf ("::%s(%p)\n", __func__, ptr);
324#endif
325
327
328 if (ptr)
329 {
330 // ----- Begin of critical section --------------------------------------
332
333 // The unknown size is passed as 0.
335 // ----- End of critical section ----------------------------------------
336 }
337}
338
339#pragma GCC diagnostic push
340#if defined(__clang__)
341#elif defined(__GNUC__)
342#pragma GCC diagnostic ignored "-Wc++14-compat"
343#pragma GCC diagnostic ignored "-Wredundant-decls"
344#endif
345
346void
347operator delete (void* ptr, std::size_t bytes) noexcept;
348
373void
374__attribute__((weak))
375operator delete (void* ptr, std::size_t bytes) noexcept
376{
377#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
378 trace::printf ("::%s(%p,%u)\n", __func__, ptr, bytes);
379#endif
380
382
383 if (ptr)
384 {
385 // ----- Begin of critical section --------------------------------------
387
389 // ----- End of critical section ----------------------------------------
390 }
391}
392
393#pragma GCC diagnostic pop
394
413void
414__attribute__((weak))
415operator delete (void* ptr,
416 const std::nothrow_t& nothrow __attribute__((unused))) noexcept
417{
418#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
419 trace::printf ("::%s(%p)\n", __func__, ptr);
420#endif
421
423
424 if (ptr)
425 {
426 // ----- Begin of critical section --------------------------------------
428
430 // ----- End of critical section ----------------------------------------
431 }
432}
433
452void
453__attribute__((weak))
454operator delete[] (void* ptr) noexcept
455{
456 ::operator delete (ptr);
457}
458
459#pragma GCC diagnostic push
460#if defined(__clang__)
461#elif defined(__GNUC__)
462#pragma GCC diagnostic ignored "-Wc++14-compat"
463#pragma GCC diagnostic ignored "-Wredundant-decls"
464#endif
465
466void
467operator delete[] (void* ptr, std::size_t bytes) noexcept;
468
488void
489__attribute__((weak))
490operator delete[] (void* ptr, std::size_t bytes) noexcept
491{
492 ::operator delete (ptr, bytes);
493}
494
495#pragma GCC diagnostic pop
496
518void
519__attribute__((weak))
520operator delete[] (void* ptr, const std::nothrow_t& nothrow) noexcept
521{
522 ::operator delete (ptr, nothrow);
523}
524
533// ----------------------------------------------------------------------------
void deallocate(void *addr, std::size_t bytes, std::size_t alignment=max_align) noexcept
Deallocate the previously allocated memory block.
Definition os-memory.h:1311
void * allocate(std::size_t bytes, std::size_t alignment=max_align)
Allocate a memory block.
Definition os-memory.h:1289
Scheduler critical section RAII helper.
Definition os-sched.h:170
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
std::new_handler new_handler_
The current new handler.
Definition new.cpp:54
new_handler get_new_handler() noexcept
Get the current handler.
Definition new.cpp:108
new_handler set_new_handler(new_handler handler) noexcept
Establishes the function designated by handler as the current new_handler.
Definition new.cpp:86
memory_resource * get_default_resource(void) noexcept
Get the default application memory manager.
const nothrow_t nothrow
Definition new.cpp:66
void __throw_bad_alloc(void)
bool in_handler_mode(void)
Check if the CPU is in handler mode.
Definition os-sched.h:1108
System namespace.
Standard std namespace.
Single file µOS++ RTOS definitions.