1 /* ///////////////////////////////////////////////////////////////////////
2 * File: basic_pool_allocator.h
7 * Brief: basic_pool_allocator class
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
20 # error basic_pool_allocator.h need be supported by c++.
23 /* ///////////////////////////////////////////////////////////////////////
26 #include "singleton_pool.h"
27 #include "basic_pool.h"
29 /* ///////////////////////////////////////////////////////////////////////
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
49 , typename_param_k Tag
52 class basic_pool_allocator
53 : public allocator_base
<T
, basic_pool_allocator
<T
, ST
, Tag
> >
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
;
71 #ifdef EXTL_MEMORY_ALLOCATOR_REBIND_SUPPORT
72 template<typename_param_k T2
>
75 typedef basic_pool_allocator
<T2
, ST
, Tag
> other_type
;
83 typedef deleter class_type
;
86 deleter(void const* p
)
88 EXTL_SUPPRESS_UNUSED(p
);
90 deleter(class_type
const& rhs
)
92 EXTL_SUPPRESS_UNUSED(rhs
);
95 void operator()(pointer p
)
97 singleton_pool_type::deallocate(p
);
101 typedef deleter deleter_type
;
103 /// \name Constructors
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&){};
112 basic_pool_allocator(class_type
const&){};
117 friend class allocator_base
< T
, class_type
>;
119 /// \name Allocates and deallocates
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
);
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 &)
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 &)
167 /* ///////////////////////////////////////////////////////////////////////
170 #ifdef EXTL_MEMORY_ALLOCATOR_TEST_ENABLE
171 # include "unit_test/basic_pool_allocator_test.h"
174 /* ///////////////////////////////////////////////////////////////////////
179 /* //////////////////////////////////////////////////////////////////// */
180 #endif /* EXTL_MEMORY_BASIC_POOL_ALLOCATOR_H */
181 /* //////////////////////////////////////////////////////////////////// */