Changeset 3740656 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:17Z (6 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.

Location:
uspace/lib/cpp
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/exception

    ra1aecb1 r3740656  
    3939                exception(const exception&) = default;
    4040                exception& operator=(const exception&) noexcept;
    41                 virtual const char *what() const;
     41                virtual const char* what() const;
    4242                virtual ~exception() = default;
    4343};
  • uspace/lib/cpp/include/new

    ra1aecb1 r3740656  
    4141                bad_alloc(const bad_alloc&);
    4242                bad_alloc& operator=(const bad_alloc&) = default;
    43                 virtual const char *what() const override;
     43                virtual const char* what() const override;
    4444                virtual ~bad_alloc() = default;
    4545};
    46 
    47 using new_handler = void (*)();
    4846
    4947struct nothrow_t {};
    5048extern const nothrow_t nothrow;
    5149
     50using new_handler = void (*)();
     51
     52new_handler set_new_handler(new_handler);
     53new_handler get_new_handler() noexcept;
     54
    5255}
    5356
    54 void *operator new(std::size_t);
     57void* operator new(std::size_t);
     58void* operator new(std::size_t, const std::nothrow_t&) noexcept;
     59void* operator new[](std::size_t);
     60void* operator new[](std::size_t, const std::nothrow_t&) noexcept;
    5561
    56 void operator delete(void *);
     62void operator delete(void* );
     63void operator delete[](void* );
    5764
    5865#endif
  • uspace/lib/cpp/src/exception.cpp

    ra1aecb1 r3740656  
    3030namespace std
    3131{
    32     const char *exception::what() const
     32    const char* exception::what() const
    3333    {
    3434        return "std::exception";
  • 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.