]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
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) | |
6 | // | |
7 | // See http://www.boost.org/libs/container for documentation. | |
8 | // | |
9 | ////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #define BOOST_CONTAINER_SOURCE | |
12 | #include <boost/container/pmr/memory_resource.hpp> | |
92f5a8d4 | 13 | #include <boost/container/pmr/global_resource.hpp> |
7c673cae FG |
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 | |
20effc67 | 17 | #include <boost/container/detail/singleton.hpp> |
7c673cae FG |
18 | |
19 | #include <cstddef> | |
20 | #include <new> | |
21 | ||
22 | namespace boost { | |
23 | namespace container { | |
24 | namespace pmr { | |
25 | ||
26 | class new_delete_resource_imp | |
27 | : public memory_resource | |
28 | { | |
29 | public: | |
30 | ||
20effc67 | 31 | ~new_delete_resource_imp() BOOST_OVERRIDE |
7c673cae FG |
32 | {} |
33 | ||
20effc67 | 34 | void* do_allocate(std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE |
b32b8144 | 35 | { (void)bytes; (void)alignment; return new char[bytes]; } |
7c673cae | 36 | |
20effc67 | 37 | void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE |
b32b8144 | 38 | { (void)bytes; (void)alignment; delete[]((char*)p); } |
7c673cae | 39 | |
20effc67 | 40 | bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT BOOST_OVERRIDE |
7c673cae | 41 | { return &other == this; } |
20effc67 | 42 | }; |
7c673cae FG |
43 | |
44 | struct null_memory_resource_imp | |
45 | : public memory_resource | |
46 | { | |
47 | public: | |
48 | ||
20effc67 | 49 | ~null_memory_resource_imp() BOOST_OVERRIDE |
7c673cae FG |
50 | {} |
51 | ||
20effc67 | 52 | void* do_allocate(std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE |
7c673cae FG |
53 | { |
54 | (void)bytes; (void)alignment; | |
1e59de90 | 55 | #if defined(BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS) || defined(BOOST_NO_EXCEPTIONS) |
7c673cae FG |
56 | throw_bad_alloc(); |
57 | return 0; | |
1e59de90 TL |
58 | #else |
59 | throw std::bad_alloc(); | |
60 | #endif | |
7c673cae FG |
61 | } |
62 | ||
20effc67 | 63 | void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE |
7c673cae FG |
64 | { (void)p; (void)bytes; (void)alignment; } |
65 | ||
20effc67 | 66 | bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT BOOST_OVERRIDE |
7c673cae | 67 | { return &other == this; } |
20effc67 | 68 | }; |
7c673cae FG |
69 | |
70 | BOOST_CONTAINER_DECL memory_resource* new_delete_resource() BOOST_NOEXCEPT | |
71 | { | |
20effc67 | 72 | return &boost::container::dtl::singleton_default<new_delete_resource_imp>::instance(); |
7c673cae FG |
73 | } |
74 | ||
75 | BOOST_CONTAINER_DECL memory_resource* null_memory_resource() BOOST_NOEXCEPT | |
76 | { | |
20effc67 | 77 | return &boost::container::dtl::singleton_default<null_memory_resource_imp>::instance(); |
7c673cae FG |
78 | } |
79 | ||
20effc67 TL |
80 | static memory_resource *default_memory_resource = |
81 | &boost::container::dtl::singleton_default<new_delete_resource_imp>::instance(); | |
7c673cae FG |
82 | |
83 | BOOST_CONTAINER_DECL memory_resource* set_default_resource(memory_resource* r) BOOST_NOEXCEPT | |
84 | { | |
85 | //TO-DO: synchronizes-with part using atomics | |
86 | if(dlmalloc_global_sync_lock()){ | |
87 | memory_resource *previous = default_memory_resource; | |
20effc67 TL |
88 | if(!previous){ |
89 | //function called before main, default_memory_resource is not initialized yet | |
90 | previous = new_delete_resource(); | |
91 | } | |
7c673cae FG |
92 | default_memory_resource = r ? r : new_delete_resource(); |
93 | dlmalloc_global_sync_unlock(); | |
94 | return previous; | |
95 | } | |
96 | else{ | |
97 | return new_delete_resource(); | |
98 | } | |
99 | } | |
100 | ||
101 | BOOST_CONTAINER_DECL memory_resource* get_default_resource() BOOST_NOEXCEPT | |
102 | { | |
103 | //TO-DO: synchronizes-with part using atomics | |
104 | if(dlmalloc_global_sync_lock()){ | |
105 | memory_resource *current = default_memory_resource; | |
20effc67 TL |
106 | if(!current){ |
107 | //function called before main, default_memory_resource is not initialized yet | |
108 | current = new_delete_resource(); | |
109 | } | |
7c673cae FG |
110 | dlmalloc_global_sync_unlock(); |
111 | return current; | |
112 | } | |
113 | else{ | |
114 | return new_delete_resource(); | |
115 | } | |
116 | } | |
117 | ||
118 | } //namespace pmr { | |
119 | } //namespace container { | |
120 | } //namespace boost { |