1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #define BOOST_CONTAINER_SOURCE
12 #include <boost/container/pmr/memory_resource.hpp>
14 #include <boost/core/no_exceptions_support.hpp>
15 #include <boost/container/throw_exception.hpp>
16 #include <boost/container/detail/dlmalloc.hpp> //For global lock
25 class new_delete_resource_imp
26 : public memory_resource
30 virtual ~new_delete_resource_imp()
33 virtual void* do_allocate(std::size_t bytes
, std::size_t alignment
)
34 { return new char[bytes
]; (void)bytes
; (void)alignment
; }
36 virtual void do_deallocate(void* p
, std::size_t bytes
, std::size_t alignment
)
37 { delete[]((char*)p
); (void)bytes
; (void)alignment
; }
39 virtual bool do_is_equal(const memory_resource
& other
) const BOOST_NOEXCEPT
40 { return &other
== this; }
41 } new_delete_resource_instance
;
43 struct null_memory_resource_imp
44 : public memory_resource
48 virtual ~null_memory_resource_imp()
51 virtual void* do_allocate(std::size_t bytes
, std::size_t alignment
)
53 (void)bytes
; (void)alignment
;
58 virtual void do_deallocate(void* p
, std::size_t bytes
, std::size_t alignment
)
59 { (void)p
; (void)bytes
; (void)alignment
; }
61 virtual bool do_is_equal(const memory_resource
& other
) const BOOST_NOEXCEPT
62 { return &other
== this; }
63 } null_memory_resource_instance
;
65 BOOST_CONTAINER_DECL memory_resource
* new_delete_resource() BOOST_NOEXCEPT
67 return &new_delete_resource_instance
;
70 BOOST_CONTAINER_DECL memory_resource
* null_memory_resource() BOOST_NOEXCEPT
72 return &null_memory_resource_instance
;
75 static memory_resource
*default_memory_resource
= &new_delete_resource_instance
;
77 BOOST_CONTAINER_DECL memory_resource
* set_default_resource(memory_resource
* r
) BOOST_NOEXCEPT
79 //TO-DO: synchronizes-with part using atomics
80 if(dlmalloc_global_sync_lock()){
81 memory_resource
*previous
= default_memory_resource
;
82 default_memory_resource
= r
? r
: new_delete_resource();
83 dlmalloc_global_sync_unlock();
87 return new_delete_resource();
91 BOOST_CONTAINER_DECL memory_resource
* get_default_resource() BOOST_NOEXCEPT
93 //TO-DO: synchronizes-with part using atomics
94 if(dlmalloc_global_sync_lock()){
95 memory_resource
*current
= default_memory_resource
;
96 dlmalloc_global_sync_unlock();
100 return new_delete_resource();
105 } //namespace container {
106 } //namespace boost {