• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <boost/container/pmr/global_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
17 #include <boost/container/detail/singleton.hpp>
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 
~new_delete_resource_imp()31    ~new_delete_resource_imp() BOOST_OVERRIDE
32    {}
33 
do_allocate(std::size_t bytes,std::size_t alignment)34    void* do_allocate(std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
35    {  (void)bytes; (void)alignment; return new char[bytes];  }
36 
do_deallocate(void * p,std::size_t bytes,std::size_t alignment)37    void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
38    {  (void)bytes; (void)alignment; delete[]((char*)p);  }
39 
do_is_equal(const memory_resource & other) const40    bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT BOOST_OVERRIDE
41    {  return &other == this;   }
42 };
43 
44 struct null_memory_resource_imp
45    : public memory_resource
46 {
47    public:
48 
~null_memory_resource_impboost::container::pmr::null_memory_resource_imp49    ~null_memory_resource_imp() BOOST_OVERRIDE
50    {}
51 
do_allocateboost::container::pmr::null_memory_resource_imp52    void* do_allocate(std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
53    {
54       (void)bytes; (void)alignment;
55       throw_bad_alloc();
56       return 0;
57    }
58 
do_deallocateboost::container::pmr::null_memory_resource_imp59    void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
60    {  (void)p;  (void)bytes; (void)alignment;  }
61 
do_is_equalboost::container::pmr::null_memory_resource_imp62    bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT BOOST_OVERRIDE
63    {  return &other == this;   }
64 };
65 
new_delete_resource()66 BOOST_CONTAINER_DECL memory_resource* new_delete_resource() BOOST_NOEXCEPT
67 {
68    return &boost::container::dtl::singleton_default<new_delete_resource_imp>::instance();
69 }
70 
null_memory_resource()71 BOOST_CONTAINER_DECL memory_resource* null_memory_resource() BOOST_NOEXCEPT
72 {
73    return &boost::container::dtl::singleton_default<null_memory_resource_imp>::instance();
74 }
75 
76 static memory_resource *default_memory_resource =
77    &boost::container::dtl::singleton_default<new_delete_resource_imp>::instance();
78 
set_default_resource(memory_resource * r)79 BOOST_CONTAINER_DECL memory_resource* set_default_resource(memory_resource* r) BOOST_NOEXCEPT
80 {
81    //TO-DO: synchronizes-with part using atomics
82    if(dlmalloc_global_sync_lock()){
83       memory_resource *previous = default_memory_resource;
84       if(!previous){
85          //function called before main, default_memory_resource is not initialized yet
86          previous = new_delete_resource();
87       }
88       default_memory_resource = r ? r : new_delete_resource();
89       dlmalloc_global_sync_unlock();
90       return previous;
91    }
92    else{
93       return new_delete_resource();
94    }
95 }
96 
get_default_resource()97 BOOST_CONTAINER_DECL memory_resource* get_default_resource() BOOST_NOEXCEPT
98 {
99    //TO-DO: synchronizes-with part using atomics
100    if(dlmalloc_global_sync_lock()){
101       memory_resource *current = default_memory_resource;
102       if(!current){
103          //function called before main, default_memory_resource is not initialized yet
104          current = new_delete_resource();
105       }
106       dlmalloc_global_sync_unlock();
107       return current;
108    }
109    else{
110       return new_delete_resource();
111    }
112 }
113 
114 }  //namespace pmr {
115 }  //namespace container {
116 }  //namespace boost {
117