Ticket #5568: singleton_pool.hpp

File singleton_pool.hpp, 3.4 KB (added by alvaro.castro.s@…, 11 years ago)

singleton_pool MaxSize propagation

Line 
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
21namespace 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//
27template <typename Tag, unsigned RequestedSize,
28 typename UserAllocator,
29 typename Mutex,
30 unsigned NextSize,
31 unsigned MaxSize>
32struct 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;
48 pool_type():p(RequestedSize, NextSize, MaxSize) { }
49 };
50
51 typedef details::pool::singleton_default<pool_type> singleton;
52
53 singleton_pool();
54
55 public:
56 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 }
62 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 }
68 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 }
74 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 }
80 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 }
86 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 }
92 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 }
98 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 }
104 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 }
110 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