source:
mainline/uspace/lib/cpp/src/new.cpp@
8fd0675f
| Last change on this file since 8fd0675f was b57ba05, checked in by , 3 years ago | |
|---|---|
|
|
| File size: 1.7 KB | |
| Rev | Line | |
|---|---|---|
| [a1aecb1] | 1 | /* |
| [b57ba05] | 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak |
| [a1aecb1] | 3 | * |
| [b57ba05] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
| [a1aecb1] | 5 | */ |
| 6 | #include <cstdlib> | |
| 7 | #include <new> | |
| 8 | ||
| 9 | namespace std | |
| 10 | { | |
| [7bbf91e] | 11 | const char* bad_alloc::what() const noexcept |
| [a1aecb1] | 12 | { |
| 13 | return "std::bad_alloc"; | |
| 14 | } | |
| 15 | ||
| 16 | const nothrow_t nothrow{}; | |
| [3740656] | 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 | } | |
| [a1aecb1] | 32 | } |
| 33 | ||
| [3740656] | 34 | void* operator new(std::size_t size) |
| [a1aecb1] | 35 | { |
| 36 | if (size == 0) | |
| 37 | size = 1; | |
| 38 | ||
| 39 | void *ptr = std::malloc(size); | |
| [3740656] | 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 | ||
| [2841b4f] | 56 | void* operator new(std::size_t ignored, void* ptr) |
| 57 | { // Placement new. | |
| 58 | return ptr; | |
| 59 | } | |
| 60 | ||
| [3740656] | 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 */ } | |
| [a1aecb1] | 71 | |
| 72 | return ptr; | |
| 73 | } | |
| 74 | ||
| [3740656] | 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 | ||
| [bc7ec7c] | 85 | void operator delete(void* ptr) noexcept |
| [a1aecb1] | 86 | { |
| 87 | if (ptr) | |
| 88 | std::free(ptr); | |
| 89 | } | |
| [3740656] | 90 | |
| [bc7ec7c] | 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 | |
| [3740656] | 102 | { |
| 103 | ::operator delete(ptr); | |
| 104 | } |
Note:
See TracBrowser
for help on using the repository browser.
