Changeset 3740656 in mainline for uspace/lib/cpp/src/new.cpp


Ignore:
Timestamp:
2018-07-05T21:41:17Z (7 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c2c1966
Parents:
a1aecb1
git-author:
Jaroslav Jindrak <dzejrou@…> (2017-10-09 16:33:43)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:17)
Message:

cpp: added the rest of the C++11 memory allocation/deallocation operators.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/src/new.cpp

    ra1aecb1 r3740656  
    3131namespace std
    3232{
    33     const char *bad_alloc::what() const
     33    const char* bad_alloc::what() const
    3434    {
    3535        return "std::bad_alloc";
     
    3737
    3838    const nothrow_t nothrow{};
     39
     40    static new_handler handler = nullptr;
     41
     42    new_handler set_new_handler(new_handler h)
     43    {
     44        auto old = handler;
     45        handler = h;
     46
     47        return old;
     48    }
     49
     50    new_handler get_new_handler() noexcept
     51    {
     52        return handler;
     53    }
    3954}
    4055
    41 void *operator new(std::size_t size)
     56void* operator new(std::size_t size)
    4257{
    43     // TODO: Implement usage of std::new_handler.
    4458    if (size == 0)
    4559        size = 1;
    4660
    4761    void *ptr = std::malloc(size);
    48     // TODO: For this we need stack unwinding support.
    49     /* if (!ptr) */
    50     /*     throw std::bad_alloc{}; */
     62
     63    while (!ptr)
     64    {
     65        auto h = std::get_new_handler();
     66        if (h)
     67            h();
     68        else
     69        {
     70            // TODO: For this we need stack unwinding support.
     71            /*     throw std::bad_alloc{}; */
     72        }
     73    }
    5174
    5275    return ptr;
    5376}
    5477
    55 void operator delete(void *ptr)
     78void* operator new(std::size_t size, const std::nothrow_t& nt) noexcept
     79{
     80    void* ptr{nullptr};
     81
     82    try
     83    {
     84        ptr = ::operator new(size);
     85    }
     86    catch(...)
     87    { /* DUMMY BODY */ }
     88
     89    return ptr;
     90}
     91
     92void* operator new[](std::size_t size)
     93{
     94    return ::operator new(size);
     95}
     96
     97void* operator new[](std::size_t size, const std::nothrow_t& nt) noexcept
     98{
     99    return ::operator new(size, nt);
     100}
     101
     102void operator delete(void* ptr)
    56103{
    57104    if (ptr)
    58105        std::free(ptr);
    59106}
     107
     108void operator delete[](void* ptr)
     109{
     110    ::operator delete(ptr);
     111}
Note: See TracChangeset for help on using the changeset viewer.