µ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++ 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/*
13 * [Partly inspired from the LLVM libcxx sources].
14 * Copyright (c) 2009-2013 by the contributors listed in
15 * 'LLVM libcxx Credits.txt'. See 'LLVM libcxx License.txt' for details.
16 *
17 * References are to ISO/IEC 14882:2011(E) Third edition (2011-09-01)
18 */
19
25#if defined(OS_USE_OS_APP_CONFIG_H)
26#include <cmsis-plus/os-app-config.h>
27#endif
28
29#include <cmsis-plus/rtos/os.h>
31
32// ----------------------------------------------------------------------------
33
34#if defined(__clang__)
35#pragma clang diagnostic ignored "-Wc++98-compat"
36#endif
37
38// ----------------------------------------------------------------------------
39
40using namespace os;
41
42// ----------------------------------------------------------------------------
43
44namespace
45{
53 std::new_handler new_handler_;
54} // namespace
55
56namespace std
57{
58 // Constant to be used as parameter to differentiate
59 // the `noexcept` functions.
60 const nothrow_t nothrow = nothrow_t{};
61
78 new_handler
79 set_new_handler (new_handler handler) noexcept
80 {
81 trace::printf ("std::%s(%p) \n", __func__, handler);
82
83 new_handler prev_handler;
84
85 prev_handler = new_handler_;
86 new_handler_ = handler;
87
88 return prev_handler;
89 }
90
100 new_handler
101 get_new_handler () noexcept
102 {
103 return new_handler_;
104 }
105
106} /* namespace std */
107
108// ----------------------------------------------------------------------------
109
110// error: end of file with unbalanced grouping commands
111/*
112 * @name Standard operators
113 * @{
114 */
115
136void* __attribute__ ((weak)) operator new (std::size_t bytes)
137{
139 if (bytes == 0)
140 {
141 bytes = 1;
142 }
143
144 // ----- Begin of critical section ------------------------------------------
146
147 while (true)
148 {
149 void* mem = estd::pmr::get_default_resource ()->allocate (bytes);
150
151 if (mem != nullptr)
152 {
153#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
154 trace::printf ("::%s(%d)=%p\n", __func__, bytes, mem);
155#endif
156 return mem;
157 }
158
159 // If allocate() fails and there is a new_handler,
160 // call it to try free up memory.
161 if (new_handler_)
162 {
163 new_handler_ ();
164 }
165 else
166 {
168 }
169 }
170
171 // ----- End of critical section --------------------------------------------
172}
173
197void* __attribute__ ((weak)) operator new (std::size_t bytes,
198 const std::nothrow_t
199 & nothrow
200 __attribute__ ((unused))) noexcept
201{
203
204 if (bytes == 0)
205 {
206 bytes = 1;
207 }
208
209 // ----- Begin of critical section ------------------------------------------
211
212 while (true)
213 {
214 void* mem = estd::pmr::get_default_resource ()->allocate (bytes);
215
216 if (mem != nullptr)
217 {
218#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
219 trace::printf ("::%s(%d)=%p\n", __func__, bytes, mem);
220#endif
221 return mem;
222 }
223
224 // If allocate() fails and there is a new_handler,
225 // call it to try free up memory.
226 if (new_handler_)
227 {
228 new_handler_ ();
229 }
230 else
231 {
232 break; // return nullptr
233 }
234 }
235
236 // ----- End of critical section --------------------------------------------
237
238 return nullptr;
239}
240
241// error: Ignoring @brief command inside argument documentation
242/*
243 * @ingroup cmsis-plus-rtos-memres
244 * @brief Allocate space for an array of new object instances.
245 * @param bytes Number of bytes to allocate.
246 * @return Pointer to allocated object.
247 *
248 * @details
249 * The allocation function (3.7.4.1) called by the array form of a
250 * new-expression (5.3.4) to allocate size bytes of storage suitably
251 * aligned to represent any array object of that size or smaller.
252 *
253 * @note A C++ program may define a function with this function signature
254 * that displaces the default version defined by the C++ standard library.
255 *
256 * @warning Cannot be invoked from Interrupt Service Routines.
257 */
258void* __attribute__ ((weak)) operator new[] (std::size_t bytes)
259{
260 return ::operator new (bytes);
261}
262
263// error: Ignoring @brief command inside argument documentation
264/*
265 * @ingroup cmsis-plus-rtos-memres
266 * @brief Allocate space for an array of new object instances (nothrow).
267 * @param bytes Number of bytes to allocate.
268 * @param nothrow (unused)
269 * @return Pointer to allocated object.
270 *
271 * @details
272 * Same as new[](size), except that it is called by
273 * a C++ program that prefers a null
274 * pointer result as an error indication, instead of a `bad_alloc` exception.
275 *
276 * @note A C++ program may define a function with this function signature
277 * that displaces the default version defined by the C++ standard library.
278 *
279 * @warning Cannot be invoked from Interrupt Service Routines.
280 */
281void* __attribute__ ((weak)) operator new[] (std::size_t bytes,
282 const std::nothrow_t
283 & nothrow
284 __attribute__ ((unused))) noexcept
285{
286 return ::operator new (bytes, std::nothrow);
287}
288
289// ----------------------------------------------------------------------------
290
315void __attribute__ ((weak)) operator delete (void* ptr) noexcept
316{
317#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
318 trace::printf ("::%s(%p)\n", __func__, ptr);
319#endif
320
322
323 if (ptr)
324 {
325 // ----- Begin of critical section --------------------------------------
327
328 // The unknown size is passed as 0.
330 // ----- End of critical section ----------------------------------------
331 }
332}
333
334#pragma GCC diagnostic push
335#if defined(__clang__)
336#elif defined(__GNUC__)
337#pragma GCC diagnostic ignored "-Wc++14-compat"
338#pragma GCC diagnostic ignored "-Wredundant-decls"
339#endif
340
341void
342operator delete (void* ptr, std::size_t bytes) noexcept;
343
369void __attribute__ ((weak)) operator delete (void* ptr,
370 std::size_t bytes) noexcept
371{
372#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
373 trace::printf ("::%s(%p,%u)\n", __func__, ptr, bytes);
374#endif
375
377
378 if (ptr)
379 {
380 // ----- Begin of critical section --------------------------------------
382
384 // ----- End of critical section ----------------------------------------
385 }
386}
387
388#pragma GCC diagnostic pop
389
409void __attribute__ ((weak)) operator delete (void* ptr, const std::nothrow_t
410 & nothrow
411 __attribute__ ((unused))) noexcept
412{
413#if defined(OS_TRACE_LIBCPP_OPERATOR_NEW)
414 trace::printf ("::%s(%p)\n", __func__, ptr);
415#endif
416
418
419 if (ptr)
420 {
421 // ----- Begin of critical section --------------------------------------
423
425 // ----- End of critical section ----------------------------------------
426 }
427}
428
448void __attribute__ ((weak)) operator delete[] (void* ptr) noexcept
449{
450 ::operator delete (ptr);
451}
452
453#pragma GCC diagnostic push
454#if defined(__clang__)
455#elif defined(__GNUC__)
456#pragma GCC diagnostic ignored "-Wc++14-compat"
457#pragma GCC diagnostic ignored "-Wredundant-decls"
458#endif
459
460void
461operator delete[] (void* ptr, std::size_t bytes) noexcept;
462
483void __attribute__ ((weak)) operator delete[] (void* ptr,
484 std::size_t bytes) noexcept
485{
486 ::operator delete (ptr, bytes);
487}
488
489#pragma GCC diagnostic pop
490
513void __attribute__ ((weak)) operator delete[] (void* ptr, const std::nothrow_t
514 & nothrow) noexcept
515{
516 ::operator delete (ptr, nothrow);
517}
518
519// error: end of file with unbalanced grouping commands
520/*
521 * @}
522 */
523
524// ----------------------------------------------------------------------------
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:1312
void * allocate(std::size_t bytes, std::size_t alignment=max_align)
Allocate a memory block.
Definition os-memory.h:1290
Scheduler critical section RAII helper.
Definition os-sched.h:171
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
memory_resource * get_default_resource(void) noexcept
Get the default application memory manager.
std::new_handler new_handler_
The current new handler.
Definition new.cpp:53
void __throw_bad_alloc(void)
bool in_handler_mode(void)
Check if the CPU is in handler mode.
Definition os-sched.h:1101
System namespace.
Standard std namespace.
new_handler get_new_handler() noexcept
Get the current handler.
Definition new.cpp:101
new_handler set_new_handler(new_handler handler) noexcept
Establishes the function designated by handler as the current new_handler.
Definition new.cpp:79
const nothrow_t nothrow
Definition new.cpp:60
Single file µOS++ RTOS definitions.