|
Last change
on this file since 8624d1f was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago |
|
Update headers in C++ files
|
-
Property mode
set to
100644
|
|
File size:
1.7 KB
|
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 | #include <cstdlib>
|
|---|
| 7 | #include <new>
|
|---|
| 8 |
|
|---|
| 9 | namespace std
|
|---|
| 10 | {
|
|---|
| 11 | const char* bad_alloc::what() const noexcept
|
|---|
| 12 | {
|
|---|
| 13 | return "std::bad_alloc";
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | const nothrow_t nothrow{};
|
|---|
| 17 |
|
|---|
| 18 | static new_handler handler = nullptr;
|
|---|
| 19 |
|
|---|
| 20 | new_handler set_new_handler(new_handler h)
|
|---|
| 21 | {
|
|---|
| 22 | auto old = handler;
|
|---|
| 23 | handler = h;
|
|---|
| 24 |
|
|---|
| 25 | return old;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | new_handler get_new_handler() noexcept
|
|---|
| 29 | {
|
|---|
| 30 | return handler;
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | void* operator new(std::size_t size)
|
|---|
| 35 | {
|
|---|
| 36 | if (size == 0)
|
|---|
| 37 | size = 1;
|
|---|
| 38 |
|
|---|
| 39 | void *ptr = std::malloc(size);
|
|---|
| 40 |
|
|---|
| 41 | while (!ptr)
|
|---|
| 42 | {
|
|---|
| 43 | auto h = std::get_new_handler();
|
|---|
| 44 | if (h)
|
|---|
| 45 | h();
|
|---|
| 46 | else
|
|---|
| 47 | {
|
|---|
| 48 | // TODO: For this we need stack unwinding support.
|
|---|
| 49 | /* throw std::bad_alloc{}; */
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | return ptr;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | void* operator new(std::size_t ignored, void* ptr)
|
|---|
| 57 | { // Placement new.
|
|---|
| 58 | return ptr;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | void* operator new(std::size_t size, const std::nothrow_t& nt) noexcept
|
|---|
| 62 | {
|
|---|
| 63 | void* ptr{nullptr};
|
|---|
| 64 |
|
|---|
| 65 | try
|
|---|
| 66 | {
|
|---|
| 67 | ptr = ::operator new(size);
|
|---|
| 68 | }
|
|---|
| 69 | catch(...)
|
|---|
| 70 | { /* DUMMY BODY */ }
|
|---|
| 71 |
|
|---|
| 72 | return ptr;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | void* operator new[](std::size_t size)
|
|---|
| 76 | {
|
|---|
| 77 | return ::operator new(size);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | void* operator new[](std::size_t size, const std::nothrow_t& nt) noexcept
|
|---|
| 81 | {
|
|---|
| 82 | return ::operator new(size, nt);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void operator delete(void* ptr) noexcept
|
|---|
| 86 | {
|
|---|
| 87 | if (ptr)
|
|---|
| 88 | std::free(ptr);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void operator delete(void* ptr, std::size_t ignored) noexcept
|
|---|
| 92 | {
|
|---|
| 93 | ::operator delete(ptr);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void operator delete[](void* ptr) noexcept
|
|---|
| 97 | {
|
|---|
| 98 | ::operator delete(ptr);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | void operator delete[](void* ptr, std::size_t ignored) noexcept
|
|---|
| 102 | {
|
|---|
| 103 | ::operator delete(ptr);
|
|---|
| 104 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.