remove \r
[extl.git] / extl / memory / basic_pool_allocator.h
blobbadbb90b21a5f16283678ee324fff8cbc607199a
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: basic_pool_allocator.h
4 * Created: 08.04.28
5 * Updated: 08.04.28
7 * Brief: basic_pool_allocator class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_MEMORY_BASIC_POOL_ALLOCATOR_H
14 #define EXTL_MEMORY_BASIC_POOL_ALLOCATOR_H
16 /*!\file basic_pool_allocator.h
17 * \brief basic_pool_allocator class
19 #ifndef __cplusplus
20 # error basic_pool_allocator.h need be supported by c++.
21 #endif
23 /* ///////////////////////////////////////////////////////////////////////
24 * Includes
26 #include "singleton_pool.h"
27 #include "basic_pool.h"
29 /* ///////////////////////////////////////////////////////////////////////
30 * ::extl namespace
32 EXTL_BEGIN_NAMESPACE
34 struct default_basic_pool_allocator_tag{};
35 /*\brief basic_pool_allocator class
37 * \param T The element type
38 * \param ST The synchronous traits type
39 * \param Tag The allocator tag type
41 * \ingroup extl_group_memory
43 template < typename_param_k T
44 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
45 , typename_param_k ST = typename_type_def_k synch_traits_selector::synch_traits_type
46 , typename_param_k Tag = default_basic_pool_allocator_tag
47 #else
48 , typename_param_k ST
49 , typename_param_k Tag
50 #endif
52 class basic_pool_allocator
53 : public allocator_base<T, basic_pool_allocator<T, ST, Tag> >
55 /// \name Types
56 /// @{
57 public:
58 typedef ST synch_traits_type;
59 typedef typename_type_k synch_traits_type::lock_type lock_type;
60 typedef basic_pool_allocator< T, ST, Tag > class_type;
61 typedef allocator_base< T, class_type > base_type;
62 typedef default_basic_pool pool_type;
63 typedef typename_type_k base_type::value_type value_type;
64 typedef typename_type_k base_type::pointer pointer;
65 typedef typename_type_k base_type::const_pointer const_pointer;
66 typedef typename_type_k base_type::size_type size_type;
67 typedef singleton_pool<Tag, pool_type, ST> singleton_pool_type;
68 /// @}
70 /// Rebinds allocator
71 #ifdef EXTL_MEMORY_ALLOCATOR_REBIND_SUPPORT
72 template<typename_param_k T2>
73 struct rebind
75 typedef basic_pool_allocator<T2, ST, Tag> other_type;
77 #endif
78 public:
79 /// The deleter
80 struct deleter
82 public:
83 typedef deleter class_type;
84 public:
85 deleter(){}
86 deleter(void const* p)
88 EXTL_SUPPRESS_UNUSED(p);
90 deleter(class_type const& rhs)
92 EXTL_SUPPRESS_UNUSED(rhs);
94 public:
95 void operator()(pointer p)
97 singleton_pool_type::deallocate(p);
100 /// The deleter type
101 typedef deleter deleter_type;
103 /// \name Constructors
104 /// @{
105 public:
106 basic_pool_allocator() EXTL_THROW_0() {};
108 #ifdef EXTL_MEMORY_ALLOCATOR_REBIND_SUPPORT
109 template <typename_param_k T1, typename_param_k ST1, typename_param_k Tag1>
110 basic_pool_allocator(basic_pool_allocator< T1, ST1, Tag1 > const&){};
111 #else
112 basic_pool_allocator(class_type const&){};
113 #endif
114 /// @}
116 private:
117 friend class allocator_base< T, class_type >;
119 /// \name Allocates and deallocates
120 /// @{
121 private:
122 void* do_allocate(size_type n, void const* hint)
124 EXTL_SUPPRESS_UNUSED(hint);
125 EXTL_SUPPRESS_UNUSED(n);
126 return static_cast<void*>(singleton_pool_type::allocate(n * sizeof(value_type)));
128 void* do_reallocate(void* p, size_type n, void const* hint)
130 EXTL_SUPPRESS_UNUSED(hint);
131 EXTL_SUPPRESS_UNUSED(n);
132 return static_cast<void*>(singleton_pool_type::reallocate(p, n * sizeof(value_type)));
134 void do_deallocate(void* p, size_type n)
136 EXTL_SUPPRESS_UNUSED(n);
137 singleton_pool_type::deallocate(p);
139 void do_deallocate(void* p)
141 singleton_pool_type::deallocate(p);
143 /// @}
145 public:
146 /// Gets the pool
147 static pool_type& get_pool()
149 return singleton_pool_type::get_pool();
153 /* //////////////////////////////////////////////////////////////////// */
154 #ifdef EXTL_FUNCTION_TEMPLATE_NON_TYPE_PARAM_SUPPORT
155 template < typename_param_k T, typename_param_k ST, typename_param_k Tag >
156 inline e_bool_t operator ==(basic_pool_allocator<T, ST, Tag> const&, basic_pool_allocator<T, ST, Tag> const &)
158 return e_true_v;
161 template < typename_param_k T, typename_param_k ST, typename_param_k Tag >
162 inline e_bool_t operator !=(basic_pool_allocator<T, ST, Tag> const &, basic_pool_allocator<T, ST, Tag> const &)
164 return e_false_v;
166 #endif
167 /* ///////////////////////////////////////////////////////////////////////
168 * Unit-testing
170 #ifdef EXTL_MEMORY_ALLOCATOR_TEST_ENABLE
171 # include "unit_test/basic_pool_allocator_test.h"
172 #endif
174 /* ///////////////////////////////////////////////////////////////////////
175 * ::extl namespace
177 EXTL_END_NAMESPACE
179 /* //////////////////////////////////////////////////////////////////// */
180 #endif /* EXTL_MEMORY_BASIC_POOL_ALLOCATOR_H */
181 /* //////////////////////////////////////////////////////////////////// */