Brian Silverman | fad8f55 | 2018-08-04 23:36:19 -0700 | [diff] [blame^] | 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> |
| 13 | |
| 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 |
| 17 | |
| 18 | #include <cstddef> |
| 19 | #include <new> |
| 20 | |
| 21 | namespace boost { |
| 22 | namespace container { |
| 23 | namespace pmr { |
| 24 | |
| 25 | class new_delete_resource_imp |
| 26 | : public memory_resource |
| 27 | { |
| 28 | public: |
| 29 | |
| 30 | virtual ~new_delete_resource_imp() |
| 31 | {} |
| 32 | |
| 33 | virtual void* do_allocate(std::size_t bytes, std::size_t alignment) |
| 34 | { (void)bytes; (void)alignment; return new char[bytes]; } |
| 35 | |
| 36 | virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) |
| 37 | { (void)bytes; (void)alignment; delete[]((char*)p); } |
| 38 | |
| 39 | virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT |
| 40 | { return &other == this; } |
| 41 | } new_delete_resource_instance; |
| 42 | |
| 43 | struct null_memory_resource_imp |
| 44 | : public memory_resource |
| 45 | { |
| 46 | public: |
| 47 | |
| 48 | virtual ~null_memory_resource_imp() |
| 49 | {} |
| 50 | |
| 51 | virtual void* do_allocate(std::size_t bytes, std::size_t alignment) |
| 52 | { |
| 53 | (void)bytes; (void)alignment; |
| 54 | throw_bad_alloc(); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) |
| 59 | { (void)p; (void)bytes; (void)alignment; } |
| 60 | |
| 61 | virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT |
| 62 | { return &other == this; } |
| 63 | } null_memory_resource_instance; |
| 64 | |
| 65 | BOOST_CONTAINER_DECL memory_resource* new_delete_resource() BOOST_NOEXCEPT |
| 66 | { |
| 67 | return &new_delete_resource_instance; |
| 68 | } |
| 69 | |
| 70 | BOOST_CONTAINER_DECL memory_resource* null_memory_resource() BOOST_NOEXCEPT |
| 71 | { |
| 72 | return &null_memory_resource_instance; |
| 73 | } |
| 74 | |
| 75 | static memory_resource *default_memory_resource = &new_delete_resource_instance; |
| 76 | |
| 77 | BOOST_CONTAINER_DECL memory_resource* set_default_resource(memory_resource* r) BOOST_NOEXCEPT |
| 78 | { |
| 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(); |
| 84 | return previous; |
| 85 | } |
| 86 | else{ |
| 87 | return new_delete_resource(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | BOOST_CONTAINER_DECL memory_resource* get_default_resource() BOOST_NOEXCEPT |
| 92 | { |
| 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(); |
| 97 | return current; |
| 98 | } |
| 99 | else{ |
| 100 | return new_delete_resource(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | } //namespace pmr { |
| 105 | } //namespace container { |
| 106 | } //namespace boost { |