1 /* ///////////////////////////////////////////////////////////////////////
2 * File: singleton_pool.h
7 * Brief: singleton_pool class
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_MEMORY_SINGLETON_POOL_H
14 #define EXTL_MEMORY_SINGLETON_POOL_H
16 /*!\file singleton_pool.h
17 * \brief singleton_pool class
20 # error singleton_pool.h need be supported by c++.
23 /* ///////////////////////////////////////////////////////////////////////
26 #include "../utility/singleton.h"
27 #include "../utility/uncopyable.h"
28 #include "../synch/synch_traits_selector.h"
29 //#include "../synch/scoped_lock.h"
31 /* ///////////////////////////////////////////////////////////////////////
36 /*!\brief singleton_pool class
38 * \param Tag The tag of the different singleton pool class
39 * \param P The pool type
40 * \param ST The synchronous traits type
42 * \ingroup extl_group_memory
44 template< typename_param_k Tag
46 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
47 , typename_param_k ST
= typename_type_def_k
synch_traits_selector::synch_traits_type
53 : private uncopyable
< singleton_pool
<Tag
, P
, ST
> > /* Prohibit copying constructor and copy assignment */
58 typedef singleton_pool
<Tag
, P
, ST
> class_type
;
59 typedef ST synch_traits_type
;
60 typedef typename_type_k
synch_traits_type::lock_type lock_type
;
61 typedef typename_type_k
P::pointer pointer
;
62 typedef typename_type_k
P::const_pointer const_pointer
;
63 typedef typename_type_k
P::reference reference
;
64 typedef typename_type_k
P::const_reference const_reference
;
65 typedef typename_type_k
P::value_type value_type
;
66 typedef typename_type_k
P::size_type size_type
;
71 /* Prohibit create an instance */
76 /// Bind pool and lock as a singleton
80 pool_type():P(), lock_type(){}
82 /// The singleton type
83 typedef singleton<pool_type> singleton_type;
89 /// The singleton type
90 typedef singleton
<pool_type
, lock_type
, tag_type
> singleton_type
;
94 static pointer
allocate(size_type n
)
96 pool_type
& pool
= singleton_type::instance();
97 //scoped_lock<lock_type> guard(pool);
98 return pool
.allocate(n
);
100 /// Deallocates memory
101 static void deallocate(pointer p
)
103 pool_type
& pool
= singleton_type::instance();
104 //scoped_lock<lock_type> guard(pool);
107 /// Reallocates memory
108 static void* reallocate(pointer p
, size_type n
)
110 pool_type
& pool
= singleton_type::instance();
111 //scoped_lock<lock_type> guard(pool);
112 return pool
.reallocate(p
, n
);
115 static pool_type
& get_pool()
117 return singleton_type::instance();
121 /* ///////////////////////////////////////////////////////////////////////
125 /* //////////////////////////////////////////////////////////////////// */
126 #endif /* EXTL_MEMORY_SINGLETON_POOL_H */
127 /* //////////////////////////////////////////////////////////////////// */