1 // Copyright (C) 2000, 2001 Stephen Cleary
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org for updates, documentation, and revision history.
8 
9 #ifndef BOOST_SINGLETON_POOL_HPP
10 #define BOOST_SINGLETON_POOL_HPP
11 
12 #include <boost/pool/poolfwd.hpp>
13 
14 // boost::pool
15 #include <boost/pool/pool.hpp>
16 // boost::details::pool::singleton_default
17 #include <boost/pool/detail/singleton.hpp>
18 // boost::details::pool::guard
19 #include <boost/pool/detail/guard.hpp>
20 
21 namespace boost {
22 
23 //
24 // The singleton_pool class allows other pool interfaces for types of the same
25 //   size to share the same pool
26 //
27 template <typename Tag, unsigned RequestedSize,
28     typename UserAllocator,
29     typename Mutex,
30     unsigned NextSize,
31     unsigned MaxSize>
32 struct singleton_pool
33 {
34   public:
35     typedef Tag tag;
36     typedef Mutex mutex;
37     typedef UserAllocator user_allocator;
38     typedef typename pool<UserAllocator>::size_type size_type;
39     typedef typename pool<UserAllocator>::difference_type difference_type;
40 
41     BOOST_STATIC_CONSTANT(unsigned, requested_size = RequestedSize);
42     BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
43 
44   private:
45     struct pool_type: Mutex
46     {
47       pool<UserAllocator> p;
pool_typeboost::singleton_pool::pool_type48       pool_type():p(RequestedSize, NextSize) { }
49     };
50 
51     typedef details::pool::singleton_default<pool_type> singleton;
52 
53     singleton_pool();
54 
55   public:
BOOST_PREVENT_MACRO_SUBSTITUTIONboost::singleton_pool56     static void * malloc BOOST_PREVENT_MACRO_SUBSTITUTION()
57     {
58       pool_type & p = singleton::instance();
59       details::pool::guard<Mutex> g(p);
60       return (p.p.malloc)();
61     }
ordered_mallocboost::singleton_pool62     static void * ordered_malloc()
63     {
64       pool_type & p = singleton::instance();
65       details::pool::guard<Mutex> g(p);
66       return p.p.ordered_malloc();
67     }
ordered_mallocboost::singleton_pool68     static void * ordered_malloc(const size_type n)
69     {
70       pool_type & p = singleton::instance();
71       details::pool::guard<Mutex> g(p);
72       return p.p.ordered_malloc(n);
73     }
is_fromboost::singleton_pool74     static bool is_from(void * const ptr)
75     {
76       pool_type & p = singleton::instance();
77       details::pool::guard<Mutex> g(p);
78       return p.p.is_from(ptr);
79     }
BOOST_PREVENT_MACRO_SUBSTITUTIONboost::singleton_pool80     static void free BOOST_PREVENT_MACRO_SUBSTITUTION(void * const ptr)
81     {
82       pool_type & p = singleton::instance();
83       details::pool::guard<Mutex> g(p);
84       (p.p.free)(ptr);
85     }
ordered_freeboost::singleton_pool86     static void ordered_free(void * const ptr)
87     {
88       pool_type & p = singleton::instance();
89       details::pool::guard<Mutex> g(p);
90       p.p.ordered_free(ptr);
91     }
BOOST_PREVENT_MACRO_SUBSTITUTIONboost::singleton_pool92     static void free BOOST_PREVENT_MACRO_SUBSTITUTION(void * const ptr, const size_type n)
93     {
94       pool_type & p = singleton::instance();
95       details::pool::guard<Mutex> g(p);
96       (p.p.free)(ptr, n);
97     }
ordered_freeboost::singleton_pool98     static void ordered_free(void * const ptr, const size_type n)
99     {
100       pool_type & p = singleton::instance();
101       details::pool::guard<Mutex> g(p);
102       p.p.ordered_free(ptr, n);
103     }
release_memoryboost::singleton_pool104     static bool release_memory()
105     {
106       pool_type & p = singleton::instance();
107       details::pool::guard<Mutex> g(p);
108       return p.p.release_memory();
109     }
purge_memoryboost::singleton_pool110     static bool purge_memory()
111     {
112       pool_type & p = singleton::instance();
113       details::pool::guard<Mutex> g(p);
114       return p.p.purge_memory();
115     }
116 };
117 
118 } // namespace boost
119 
120 #endif
121