Add exception handling for new operator (#791)
The new operator in the snmalloc did not throw exceptions in the case of allocation failure. Moreover, it was not possible to override the behaviour of the failure of the new operator using the std::set_new_handler function. This PR adds the necessary code to the snmalloc new operator to throw std::bad_alloc when the allocation fails. It also allows the std::set_new_handler function to be used to set a custom handler for allocation failures.
This commit is contained in:
committed by
GitHub
parent
b49a42d9be
commit
452fcc44b0
@@ -18,9 +18,27 @@ int main()
|
||||
# include <atomic>
|
||||
# include <iostream>
|
||||
# include <memory>
|
||||
# include <new>
|
||||
# include <snmalloc/backend/globalconfig.h>
|
||||
# include <snmalloc/snmalloc_core.h>
|
||||
|
||||
// This code makes the delete overloads build with the correct noexcept spec.
|
||||
# ifdef _WIN32
|
||||
# ifdef __clang__
|
||||
# define EXCEPTSPEC noexcept
|
||||
# else
|
||||
# define EXCEPTSPEC
|
||||
# endif
|
||||
# else
|
||||
# ifdef _GLIBCXX_USE_NOEXCEPT
|
||||
# define EXCEPTSPEC _GLIBCXX_USE_NOEXCEPT
|
||||
# elif defined(_NOEXCEPT)
|
||||
# define EXCEPTSPEC _NOEXCEPT
|
||||
# else
|
||||
# define EXCEPTSPEC
|
||||
# endif
|
||||
# endif
|
||||
|
||||
namespace snmalloc
|
||||
{
|
||||
// Instantiate the allocator with a client meta data provider that uses an
|
||||
@@ -170,12 +188,12 @@ void* operator new(size_t size)
|
||||
return snmalloc::miracle::malloc(size);
|
||||
}
|
||||
|
||||
void operator delete(void* p)
|
||||
void operator delete(void* p) EXCEPTSPEC
|
||||
{
|
||||
snmalloc::miracle::free(p);
|
||||
}
|
||||
|
||||
void operator delete(void* p, size_t)
|
||||
void operator delete(void* p, size_t) EXCEPTSPEC
|
||||
{
|
||||
snmalloc::miracle::free(p);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user