2015-09-07 19:16:46 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
|
|
|
|
|
// Software License, Version 1.0. (See accompanying file
|
|
|
|
|
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
//
|
|
|
|
|
// See http://www.boost.org/libs/container for documentation.
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#define BOOST_CONTAINER_SOURCE
|
|
|
|
|
#include <boost/container/pmr/memory_resource.hpp>
|
2019-01-03 03:32:03 +01:00
|
|
|
#include <boost/container/pmr/global_resource.hpp>
|
2015-09-07 19:16:46 +02:00
|
|
|
#include <boost/core/no_exceptions_support.hpp>
|
|
|
|
|
#include <boost/container/throw_exception.hpp>
|
|
|
|
|
#include <boost/container/detail/dlmalloc.hpp> //For global lock
|
2020-05-23 23:38:58 +02:00
|
|
|
#include <boost/container/detail/singleton.hpp>
|
2015-09-07 19:16:46 +02:00
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
|
namespace container {
|
|
|
|
|
namespace pmr {
|
|
|
|
|
|
|
|
|
|
class new_delete_resource_imp
|
|
|
|
|
: public memory_resource
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2020-05-08 09:28:49 -07:00
|
|
|
~new_delete_resource_imp() BOOST_OVERRIDE
|
2015-09-07 19:16:46 +02:00
|
|
|
{}
|
|
|
|
|
|
2020-05-08 09:28:49 -07:00
|
|
|
void* do_allocate(std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
|
2016-11-02 13:35:48 +01:00
|
|
|
{ (void)bytes; (void)alignment; return new char[bytes]; }
|
2015-09-07 19:16:46 +02:00
|
|
|
|
2020-05-08 09:28:49 -07:00
|
|
|
void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
|
2016-11-02 13:35:48 +01:00
|
|
|
{ (void)bytes; (void)alignment; delete[]((char*)p); }
|
2015-09-07 19:16:46 +02:00
|
|
|
|
2020-05-08 09:28:49 -07:00
|
|
|
bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT BOOST_OVERRIDE
|
2015-09-07 19:16:46 +02:00
|
|
|
{ return &other == this; }
|
2020-05-23 23:38:58 +02:00
|
|
|
};
|
2015-09-07 19:16:46 +02:00
|
|
|
|
|
|
|
|
struct null_memory_resource_imp
|
|
|
|
|
: public memory_resource
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2020-05-08 09:28:49 -07:00
|
|
|
~null_memory_resource_imp() BOOST_OVERRIDE
|
2015-09-07 19:16:46 +02:00
|
|
|
{}
|
|
|
|
|
|
2020-05-08 09:28:49 -07:00
|
|
|
void* do_allocate(std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
|
2015-09-07 19:16:46 +02:00
|
|
|
{
|
|
|
|
|
(void)bytes; (void)alignment;
|
|
|
|
|
throw_bad_alloc();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-08 09:28:49 -07:00
|
|
|
void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
|
2015-09-07 19:16:46 +02:00
|
|
|
{ (void)p; (void)bytes; (void)alignment; }
|
|
|
|
|
|
2020-05-08 09:28:49 -07:00
|
|
|
bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT BOOST_OVERRIDE
|
2015-09-07 19:16:46 +02:00
|
|
|
{ return &other == this; }
|
2020-05-23 23:38:58 +02:00
|
|
|
};
|
2015-09-07 19:16:46 +02:00
|
|
|
|
|
|
|
|
BOOST_CONTAINER_DECL memory_resource* new_delete_resource() BOOST_NOEXCEPT
|
|
|
|
|
{
|
2020-05-23 23:38:58 +02:00
|
|
|
return &boost::container::dtl::singleton_default<new_delete_resource_imp>::instance();
|
2015-09-07 19:16:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_CONTAINER_DECL memory_resource* null_memory_resource() BOOST_NOEXCEPT
|
|
|
|
|
{
|
2020-05-23 23:38:58 +02:00
|
|
|
return &boost::container::dtl::singleton_default<null_memory_resource_imp>::instance();
|
2015-09-07 19:16:46 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-23 23:38:58 +02:00
|
|
|
static memory_resource *default_memory_resource =
|
|
|
|
|
&boost::container::dtl::singleton_default<new_delete_resource_imp>::instance();
|
2015-09-07 19:16:46 +02:00
|
|
|
|
|
|
|
|
BOOST_CONTAINER_DECL memory_resource* set_default_resource(memory_resource* r) BOOST_NOEXCEPT
|
|
|
|
|
{
|
|
|
|
|
//TO-DO: synchronizes-with part using atomics
|
|
|
|
|
if(dlmalloc_global_sync_lock()){
|
|
|
|
|
memory_resource *previous = default_memory_resource;
|
|
|
|
|
default_memory_resource = r ? r : new_delete_resource();
|
|
|
|
|
dlmalloc_global_sync_unlock();
|
|
|
|
|
return previous;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
return new_delete_resource();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_CONTAINER_DECL memory_resource* get_default_resource() BOOST_NOEXCEPT
|
|
|
|
|
{
|
|
|
|
|
//TO-DO: synchronizes-with part using atomics
|
|
|
|
|
if(dlmalloc_global_sync_lock()){
|
|
|
|
|
memory_resource *current = default_memory_resource;
|
2020-05-24 00:37:36 +02:00
|
|
|
if(!current){
|
|
|
|
|
//function called before main, default_memory resource was not initialied yet
|
|
|
|
|
current = new_delete_resource();
|
|
|
|
|
}
|
2015-09-07 19:16:46 +02:00
|
|
|
dlmalloc_global_sync_unlock();
|
|
|
|
|
return current;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
return new_delete_resource();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} //namespace pmr {
|
|
|
|
|
} //namespace container {
|
|
|
|
|
} //namespace boost {
|