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 | struct singleton_pool |
---|
32 | { |
---|
33 | public: |
---|
34 | typedef Tag tag; |
---|
35 | typedef Mutex mutex; |
---|
36 | typedef UserAllocator user_allocator; |
---|
37 | typedef typename pool<UserAllocator>::size_type size_type; |
---|
38 | typedef typename pool<UserAllocator>::difference_type difference_type; |
---|
39 | |
---|
40 | BOOST_STATIC_CONSTANT(unsigned, requested_size = RequestedSize); |
---|
41 | BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize); |
---|
42 | |
---|
43 | private: |
---|
44 | struct pool_type: Mutex |
---|
45 | { |
---|
46 | pool<UserAllocator> p; |
---|
47 | pool_type():p(RequestedSize, NextSize) { } |
---|
48 | }; |
---|
49 | |
---|
50 | typedef details::pool::singleton_default<pool_type> singleton; |
---|
51 | |
---|
52 | singleton_pool(); |
---|
53 | |
---|
54 | public: |
---|
55 | static void * malloc() |
---|
56 | { |
---|
57 | pool_type & p = singleton::instance(); |
---|
58 | details::pool::guard<Mutex> g(p); |
---|
59 | return p.p.malloc(); |
---|
60 | } |
---|
61 | static void * ordered_malloc() |
---|
62 | { |
---|
63 | pool_type & p = singleton::instance(); |
---|
64 | details::pool::guard<Mutex> g(p); |
---|
65 | return p.p.ordered_malloc(); |
---|
66 | } |
---|
67 | static void * ordered_malloc(const size_type n) |
---|
68 | { |
---|
69 | pool_type & p = singleton::instance(); |
---|
70 | details::pool::guard<Mutex> g(p); |
---|
71 | return p.p.ordered_malloc(n); |
---|
72 | } |
---|
73 | static bool is_from(void * const ptr) |
---|
74 | { |
---|
75 | pool_type & p = singleton::instance(); |
---|
76 | details::pool::guard<Mutex> g(p); |
---|
77 | return p.p.is_from(ptr); |
---|
78 | } |
---|
79 | static void free(void * const ptr) |
---|
80 | { |
---|
81 | pool_type & p = singleton::instance(); |
---|
82 | details::pool::guard<Mutex> g(p); |
---|
83 | p.p.free(ptr); |
---|
84 | } |
---|
85 | static void ordered_free(void * const ptr) |
---|
86 | { |
---|
87 | pool_type & p = singleton::instance(); |
---|
88 | details::pool::guard<Mutex> g(p); |
---|
89 | p.p.ordered_free(ptr); |
---|
90 | } |
---|
91 | static void free(void * const ptr, const size_type n) |
---|
92 | { |
---|
93 | pool_type & p = singleton::instance(); |
---|
94 | details::pool::guard<Mutex> g(p); |
---|
95 | p.p.free(ptr, n); |
---|
96 | } |
---|
97 | static void ordered_free(void * const ptr, const size_type n) |
---|
98 | { |
---|
99 | pool_type & p = singleton::instance(); |
---|
100 | details::pool::guard<Mutex> g(p); |
---|
101 | p.p.ordered_free(ptr, n); |
---|
102 | } |
---|
103 | static bool release_memory() |
---|
104 | { |
---|
105 | pool_type & p = singleton::instance(); |
---|
106 | details::pool::guard<Mutex> g(p); |
---|
107 | return p.p.release_memory(); |
---|
108 | } |
---|
109 | static bool purge_memory() |
---|
110 | { |
---|
111 | pool_type & p = singleton::instance(); |
---|
112 | details::pool::guard<Mutex> g(p); |
---|
113 | return p.p.purge_memory(); |
---|
114 | } |
---|
115 | }; |
---|
116 | |
---|
117 | } // namespace boost |
---|
118 | |
---|
119 | #endif |
---|