Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r83155 - sandbox/pool2/boost/pool
From: svart.riddare_at_[hidden]
Date: 2013-02-25 14:59:43


Author: edupuis
Date: 2013-02-25 14:59:42 EST (Mon, 25 Feb 2013)
New Revision: 83155
URL: http://svn.boost.org/trac/boost/changeset/83155

Log:
Pool policy is now a full class rather than a simple function pointer.
Added:
   sandbox/pool2/boost/pool/policies.hpp (contents, props changed)
Text files modified:
   sandbox/pool2/boost/pool/pool.hpp | 117 ++-------------------------------------
   1 files changed, 8 insertions(+), 109 deletions(-)

Added: sandbox/pool2/boost/pool/policies.hpp
==============================================================================
--- (empty file)
+++ sandbox/pool2/boost/pool/policies.hpp 2013-02-25 14:59:42 EST (Mon, 25 Feb 2013)
@@ -0,0 +1,152 @@
+#ifndef __BOOST_POOL_POLICIES_HPP
+#define __BOOST_POOL_POLICIES_HPP
+
+#ifndef __BOOST_POOL_HPP
+#error Please include <boost/pool.hpp>
+#endif
+
+/* -------------------------------------------------------------------------- */
+/* -- typedef boost::pools::growth_policy -- */
+/* -------------------------------------------------------------------------- */
+
+namespace pools
+{
+ /** --------------------------------------------------------------------------
+ * Pool growth policy function.
+ * The pool growth policy function controls how a pool grows if it runs out
+ * of buffers.
+ * \param poolSize
+ * Current number of buffers allocated by the pool.
+ * \return
+ * Number of new buffers the pool should allocate. The returned value
+ * can be zero, in which case the pool will no longer grow.
+ * ------------------------------------------------------------------------ */
+ typedef size_t (*growth_policy)(size_t poolSize);
+
+ /** --------------------------------------------------------------------------
+ * No growth policy function.
+ * This pools::policy function always return 0, regardless of the pool size.
+ * Hence it describes a pool which never grows.
+ * ----------------------------------------------------------------------- */
+ /*static*/ size_t policy_no_growth(size_t)
+ {
+ return 0;
+ }
+
+ /** --------------------------------------------------------------------------
+ * Constant growth policy function.
+ * This pools::policy function returns a constant, regardless of the pool size.
+ * Hence it describes a pool which grows linearly.
+ * \tparam N
+ * Value returned by the policy function. There is no default value
+ * for this parameter.
+ * ------------------------------------------------------------------------- */
+ template<size_t N>
+ static size_t policy_constant_growth(size_t)
+ {
+ return N;
+ }
+
+ /** --------------------------------------------------------------------------
+ * Exponential growth policy function.
+ * This pools::policy function returns a multiple of the current pool size.
+ * Hence it describes a pool which grows exponentially. An example of such
+ * an exponential growth is a pool which doubles its size when it runs out
+ * of buffers.
+ * \tparam M
+ * Numerator of the multiplicative factor applied to the pool size.
+ * Given template parameter M, the pool size after
+ * growing will be M times the actual pool size. Use M = 2 for
+ * a pool that should double its size whenever it runs out of buffers.
+ * \tparam N
+ * Denominator of the multiplicative factor applied to the pool size.
+ * Given template parameters M and N, the pool size after
+ * growing will be M/N times the actual pool size. It is required
+ * that N is greater than zero and M greater or equal than N.
+ * N has a default value, one. If M equals N, then the pool can
+ * not grow. Otherwise, this function ensures that the pool will grow by
+ * at least one buffer.
+ * ------------------------------------------------------------------------- */
+ template<size_t M, size_t N>
+ static size_t policy_exponential_growth(size_t s)
+ {
+ /* -- Invalid parameters -- */
+
+ BOOST_STATIC_ASSERT(M >= N);
+ BOOST_STATIC_ASSERT(N > 0);
+
+ if (!N || M <= N)
+ return 0;
+
+ /* -- Compute M/N -- */
+
+ size_t Q = M / N;
+ size_t R = M % N;
+
+ /* -- Perform multiplication, taking care of overflows -- */
+
+ if (!Q || (std::numeric_limits<size_t>::max() / Q < s))
+ return 0;
+
+ size_t size = (R && (std::numeric_limits<size_t>::max() / R < s)) ? (Q * s + R * (s / N)) : (Q * s + R * s / N);
+
+ if (size < Q * s)
+ return 0;
+
+ /* -- Return result -- */
+
+ return size ? size : 1;
+ }
+
+ /** --------------------------------------------------------------------------
+ * Exponential growth policy function with N = 1
+ * ------------------------------------------------------------------------- */
+ template<size_t M>
+ static size_t policy_exponential_growth(size_t s)
+ {
+ return policy_exponential_growth<M, 1>(s);
+ }
+}
+
+/* -------------------------------------------------------------------------- */
+/* -- Class boost::pools::policy -- */
+/* -------------------------------------------------------------------------- */
+
+namespace pools
+{
+ class policy
+ {
+ public :
+ policy(size_t initialPoolSize) : _initialPoolSize(initialPoolSize), _maximalPoolSize(initialPoolSize), _growth(policy_no_growth) {}
+ policy(size_t initialPoolSize, growth_policy growth) : _initialPoolSize(initialPoolSize), _maximalPoolSize(std::numeric_limits<size_t>::max()), _growth(growth) {}
+ policy(size_t initialPoolSize, size_t maximalPoolSize, growth_policy growth) : _initialPoolSize(initialPoolSize), _maximalPoolSize(maximalPoolSize), _growth(growth) {}
+
+ public :
+ size_t growth(size_t poolSize) const;
+
+ private :
+ size_t _initialPoolSize;
+ size_t _maximalPoolSize;
+ growth_policy _growth;
+ };
+
+ /* -------------------------------------------------------------------------- */
+
+ inline
+ size_t policy::growth(size_t poolSize) const
+ {
+ /* -- Coherency checks -- */
+
+ assert(poolSize <= _maximalPoolSize);
+
+ /* -- Return pool growth -- */
+
+ return std::min(_maximalPoolSize - poolSize, poolSize ? (*_growth)(poolSize) : _initialPoolSize);
+ }
+
+ /* -------------------------------------------------------------------------- */
+}
+
+/* -------------------------------------------------------------------------- */
+
+#endif

Modified: sandbox/pool2/boost/pool/pool.hpp
==============================================================================
--- sandbox/pool2/boost/pool/pool.hpp (original)
+++ sandbox/pool2/boost/pool/pool.hpp 2013-02-25 14:59:42 EST (Mon, 25 Feb 2013)
@@ -20,6 +20,8 @@
 namespace boost
 {
 
+#include "policies.hpp"
+
 /** ----------------------------------------------------------------------------
  * A pool holds memory buffers that are allocated only once.
  *
@@ -87,113 +89,10 @@
 class pool;
 
 /* -------------------------------------------------------------------------- */
-/* -- typedef boost::pools::policy -- */
-/* -------------------------------------------------------------------------- */
-
-namespace pools
-{
- /** --------------------------------------------------------------------------
- * Pool growth policy function.
- * The pool growth policy function controls how a pool grows if it runs out
- * of buffers.
- * \param poolSize
- * Current number of buffers allocated by the pool.
- * \return
- * Number of new buffers the pool should allocate. The returned value
- * can be zero, in which case the pool will no longer grow.
- * ------------------------------------------------------------------------ */
- typedef size_t (*policy)(size_t poolSize);
-
- /** --------------------------------------------------------------------------
- * No growth policy function.
- * This pools::policy function always return 0, regardless of the pool size.
- * Hence it describes a pool which never grows.
- * ----------------------------------------------------------------------- */
- static size_t policy_no_growth(size_t)
- {
- return 0;
- }
-
- /** --------------------------------------------------------------------------
- * Constant growth policy function.
- * This pools::policy function returns a constant, regardless of the pool size.
- * Hence it describes a pool which grows linearly.
- * \tparam N
- * Value returned by the policy function. There is no default value
- * for this parameter.
- * ------------------------------------------------------------------------- */
- template<size_t N>
- static size_t policy_constant_growth(size_t)
- {
- return N;
- }
-
- /** --------------------------------------------------------------------------
- * Exponential growth policy function.
- * This pools::policy function returns a multiple of the current pool size.
- * Hence it describes a pool which grows exponentially. An example of such
- * an exponential growth is a pool which doubles its size when it runs out
- * of buffers.
- * \tparam M
- * Numerator of the multiplicative factor applied to the pool size.
- * Given template parameter M, the pool size after
- * growing will be M times the actual pool size. Use M = 2 for
- * a pool that should double its size whenever it runs out of buffers.
- * \tparam N
- * Denominator of the multiplicative factor applied to the pool size.
- * Given template parameters M and N, the pool size after
- * growing will be M/N times the actual pool size. It is required
- * that N is greater than zero and M greater or equal than N.
- * N has a default value, one. If M equals N, then the pool can
- * not grow. Otherwise, this function ensures that the pool will grow by
- * at least one buffer.
- * ------------------------------------------------------------------------- */
- template<size_t M, size_t N>
- static size_t policy_exponential_growth(size_t s)
- {
- /* -- Invalid parameters -- */
-
- BOOST_STATIC_ASSERT(M >= N);
- BOOST_STATIC_ASSERT(N > 0);
-
- if (!N || M <= N)
- return 0;
-
- /* -- Compute M/N -- */
-
- size_t Q = M / N;
- size_t R = M % N;
-
- /* -- Perform multiplication, taking care of overflows -- */
-
- if (!Q || (std::numeric_limits<size_t>::max() / Q < s))
- return 0;
-
- size_t size = (R && (std::numeric_limits<size_t>::max() / R < s)) ? (Q * s + R * (s / N)) : (Q * s + R * s / N);
-
- if (size < Q * s)
- return 0;
-
- /* -- Return result -- */
-
- return size ? size : 1;
- }
-
- /** --------------------------------------------------------------------------
- * Exponential growth policy function with N = 1
- * ------------------------------------------------------------------------- */
- template<size_t M>
- static size_t policy_exponential_growth(size_t s)
- {
- return policy_exponential_growth<M, 1>(s);
- }
-}
-
-/* -------------------------------------------------------------------------- */
 /* -- class boost::pool -- */
 /* -------------------------------------------------------------------------- */
 
-template<typename T, bool ThreadSafe = false, class Allocator = std::allocator<char>>
+template<typename T, bool ThreadSafe, class Allocator>
 class pool
 {
         private :
@@ -218,7 +117,7 @@
                  * \param allocator
                  * Custom allocator instance.
                  * ---------------------------------------------------------------------- */
- pool(size_t initialPoolSize, size_t bufferSize = 1, pools::policy policy = pools::policy_no_growth, const Allocator& allocator = Allocator());
+ pool(const pools::policy& policy, size_t bufferSize = 1, const Allocator& allocator = Allocator());
 
                 /** ------------------------------------------------------------------------
                  * Destroys a pool.
@@ -236,7 +135,7 @@
 
         private :
                 /** ------------------------------------------------------------------------
- * Requests a pool buffer, \b without initialing it's content.
+ * Requests a pool buffer, \b without initializing it's content.
                  * This private function is used by the various flavors of pool::request().
                  * ----------------------------------------------------------------------- */
                 T *request_core(void);
@@ -357,10 +256,10 @@
 /* -------------------------------------------------------------------------- */
 
 template<typename T, bool ThreadSafe, class Allocator>
-pool<T, ThreadSafe, Allocator>::pool(size_t initialPoolSize, size_t bufferSize, pools::policy policy, const Allocator& allocator)
+pool<T, ThreadSafe, Allocator>::pool(const pools::policy& policy, size_t bufferSize, const Allocator& allocator)
         : _allocator(allocator), _policy(policy), _allocated(0), _requested(0), _size(bufferSize)
 {
- grow(initialPoolSize);
+ grow(_policy.growth(0));
 }
 
 /* -------------------------------------------------------------------------- */
@@ -459,7 +358,7 @@
         /* -- Try growing the pool if there is no free buffer availables -- */
 
         if (_list.empty())
- grow((*_policy)(_allocated));
+ grow(_policy.growth(_allocated));
 
         /* -- Request a pointer from the free list -- */
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk