Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r78295 - in sandbox/pool: boost/pool libs/pool/test
From: svart.riddare_at_[hidden]
Date: 2012-05-01 09:36:02


Author: edupuis
Date: 2012-05-01 09:36:01 EDT (Tue, 01 May 2012)
New Revision: 78295
URL: http://svn.boost.org/trac/boost/changeset/78295

Log:
Added array_pool, array_object_pool and updated related test program. These pools are static_pools whose maximum size is known at compile time.
Text files modified:
   sandbox/pool/boost/pool/object_pool.hpp | 40 +++++++++++++++++++++++
   sandbox/pool/boost/pool/pool.hpp | 2 +
   sandbox/pool/boost/pool/poolfwd.hpp | 9 +++++
   sandbox/pool/boost/pool/static_pool.hpp | 5 ++
   sandbox/pool/libs/pool/test/test_static_pool.cpp | 68 +++++++++++++++++++++++++--------------
   5 files changed, 100 insertions(+), 24 deletions(-)

Modified: sandbox/pool/boost/pool/object_pool.hpp
==============================================================================
--- sandbox/pool/boost/pool/object_pool.hpp (original)
+++ sandbox/pool/boost/pool/object_pool.hpp 2012-05-01 09:36:01 EDT (Tue, 01 May 2012)
@@ -369,6 +369,46 @@
     }
 };
 
+/*! \brief A template class
+that can be used for fast and efficient memory allocation of objects.
+It also provides automatic destruction of non-deallocated objects.
+
+\details
+
+<b>T</b> The type of object to allocate/deallocate.
+T must have a non-throwing destructor.
+
+<b>UserAllocator</b>
+Defines the allocator that the underlying StaticPool will use to allocate memory from the system.
+See User Allocators for details.
+
+Class static_object_pool is a template class
+that can be used for fast and efficient memory allocation of objects.
+It also provides automatic destruction of non-deallocated objects.
+
+When the object pool is destroyed, then the destructor for type T
+is called for each allocated T that has not yet been deallocated. O(N).
+
+The ArrayObjectPool does not allocate any memory as it's size is known at compile time.
+*/
+
+template<typename T, size_t PoolSize>
+class array_object_pool : public object_pool_base<T, array_pool<sizeof(T), PoolSize> >
+{
+ public:
+ typedef typename object_pool_base<T, array_pool<sizeof(T), PoolSize> >::element_type element_type; //!< ElementType
+ typedef typename object_pool_base<T, array_pool<sizeof(T), PoolSize> >::user_allocator user_allocator; //!<
+ typedef typename object_pool_base<T, array_pool<sizeof(T), PoolSize> >::size_type size_type; //!< pool<UserAllocator>::size_type
+ typedef typename object_pool_base<T, array_pool<sizeof(T), PoolSize> >::difference_type difference_type; //!< pool<UserAllocator>::difference_type
+
+ public:
+ explicit array_object_pool()
+ :
+ object_pool_base<T, array_pool<sizeof(T), PoolSize> >(PoolSize)
+ { //! Constructs a new (empty by default) ArrayObjectPool.
+ }
+};
+
 } // namespace boost
 
 // The following code might be put into some Boost.Config header in a later revision

Modified: sandbox/pool/boost/pool/pool.hpp
==============================================================================
--- sandbox/pool/boost/pool/pool.hpp (original)
+++ sandbox/pool/boost/pool/pool.hpp 2012-05-01 09:36:01 EDT (Tue, 01 May 2012)
@@ -26,6 +26,8 @@
 
 #include <boost/pool/poolfwd.hpp>
 
+// boost::static_unsigned_max
+#include <boost/integer/static_min_max.hpp>
 // boost::math::static_lcm
 #include <boost/math/common_factor_ct.hpp>
 // boost::simple_segregated_storage

Modified: sandbox/pool/boost/pool/poolfwd.hpp
==============================================================================
--- sandbox/pool/boost/pool/poolfwd.hpp (original)
+++ sandbox/pool/boost/pool/poolfwd.hpp 2012-05-01 09:36:01 EDT (Tue, 01 May 2012)
@@ -49,6 +49,9 @@
 template <typename T, typename UserAllocator = default_user_allocator_new_delete>
 class static_object_pool;
 
+template <typename T, size_t PoolSize>
+class array_object_pool;
+
 //
 // Location: <boost/pool/static_pool.hpp>
 //
@@ -56,6 +59,12 @@
 class static_pool;
 
 //
+// Location: <boost/pool/array_pool.hpp>
+//
+template<size_t RequestedSize, size_t PoolSize>
+class array_pool;
+
+//
 // Location: <boost/pool/singleton_pool.hpp>
 //
 template <typename Tag, unsigned RequestedSize,

Modified: sandbox/pool/boost/pool/static_pool.hpp
==============================================================================
--- sandbox/pool/boost/pool/static_pool.hpp (original)
+++ sandbox/pool/boost/pool/static_pool.hpp 2012-05-01 09:36:01 EDT (Tue, 01 May 2012)
@@ -39,7 +39,12 @@
     :
     pool<UserAllocator>(nrequested_size, nrequested_items)
     {
+#if 1
+ void *p = pool<UserAllocator>::malloc BOOST_PREVENT_MACRO_SUBSTITUTION();
+ p ? free BOOST_PREVENT_MACRO_SUBSTITUTION(p) : (void)(0);
+#else
       free BOOST_PREVENT_MACRO_SUBSTITUTION(pool<UserAllocator>::malloc BOOST_PREVENT_MACRO_SUBSTITUTION());
+#endif
     }
 
     void * malloc BOOST_PREVENT_MACRO_SUBSTITUTION()

Modified: sandbox/pool/libs/pool/test/test_static_pool.cpp
==============================================================================
--- sandbox/pool/libs/pool/test/test_static_pool.cpp (original)
+++ sandbox/pool/libs/pool/test/test_static_pool.cpp 2012-05-01 09:36:01 EDT (Tue, 01 May 2012)
@@ -6,10 +6,10 @@
  */
 
 #include <boost/pool/static_pool.hpp>
+#include <boost/pool/array_pool.hpp>
 #include <boost/pool/object_pool.hpp>
 
 #include <boost/assert.hpp>
-#include <boost/detail/lightweight_test.hpp>
 
 // Allocator that checks whether malloc() is called only once
 
@@ -20,7 +20,7 @@
 
   static char * malloc BOOST_PREVENT_MACRO_SUBSTITUTION(const size_type bytes)
   {
- BOOST_TEST(reset);
+ BOOST_ASSERT(reset);
     reset = false;
 
     return new (std::nothrow) char[bytes];
@@ -50,18 +50,14 @@
 
 // Testing static_pool
 
-template<int SIZE>
-void test_static_pool()
+template<typename Pool>
+void test_static_pool(Pool& pool, int pool_size)
 {
- static const int pool_size = 24; // Initial size of pool
   static const int num_iterations = pool_size + 8; // Number of mallocs we will try on pool in following tests
   static const int num_random_iterations = 2000; // Number of iterations for test #4 that follows
   static const int consecutive_elements = 5; // Number of consecutive elements we will request
 
- void *p[num_iterations];
-
- user_allocator_once::reset = true;
- boost::static_pool<user_allocator_once> pool(SIZE, pool_size);
+ void **p = new void *[num_iterations];
 
   for (int n = 0; n < 2; n++)
   {
@@ -69,7 +65,7 @@
     for (int k = 0; k < num_iterations; k++)
     {
       p[k] = pool.malloc();
- BOOST_TEST((p[k] != NULL) == (k < pool_size));
+ BOOST_ASSERT((p[k] != NULL) == (k < pool_size));
     }
 
     for (int k = 0; k < num_iterations; k++)
@@ -82,7 +78,7 @@
     for (int k = 0; k < num_iterations; k++)
     {
       p[k] = pool.ordered_malloc(consecutive_elements);
- BOOST_TEST((p[k] != NULL) == (k < pool_size / consecutive_elements));
+ BOOST_ASSERT((p[k] != NULL) == (k < pool_size / consecutive_elements));
     }
 
     for (int k = 0; k < num_iterations; k++)
@@ -92,7 +88,7 @@
     for (int k = 0; k < num_iterations; k++)
     {
       p[k] = pool.malloc();
- BOOST_TEST((p[k] != NULL) == (k < pool_size));
+ BOOST_ASSERT((p[k] != NULL) == (k < pool_size));
     }
 
     for (int k = 0; k < num_iterations; k++)
@@ -111,11 +107,12 @@
       if ((rand() & 8) && (allocated < pool_size))
       {
         p[allocated++] = pool.malloc();
+ BOOST_ASSERT(p[allocated - 1]);
       }
       else if (allocated > 0)
       {
         void *_p = p[--allocated];
- BOOST_TEST(_p && pool.is_from(_p));
+ BOOST_ASSERT(_p && pool.is_from(_p));
         pool.free(_p);
       }
     }
@@ -123,20 +120,18 @@
     while (allocated > 0)
       pool.free(p[--allocated]);
   }
+
+ delete[] p;
 }
 
 // Testing static_object_pool
 
-template<typename T>
-void test_static_object_pool()
+template<typename Pool>
+void test_static_object_pool(Pool& pool, int pool_size)
 {
- static const int pool_size = 24; // Initial size of pool
   static const int num_random_iterations = 200; // Number of iterations f
 
- user_allocator_once::reset = true;
- boost::static_object_pool<T, user_allocator_once> pool(pool_size);
-
- T *p[pool_size];
+ Pool::element_type **p = new Pool::element_type *[pool_size];
 
   // TEST #1
 
@@ -151,26 +146,51 @@
     if ((rand() & 8) && (allocated < pool_size))
     {
       p[allocated++] = pool.construct();
+ BOOST_ASSERT(p[allocated - 1]);
     }
     else if (allocated > 0)
     {
       pool.destroy(p[--allocated]);
     }
 
- BOOST_TEST(Count::count == allocated);
+ BOOST_ASSERT(Count::count == allocated);
   }
 
   while (allocated < pool_size / 4)
     p[allocated++] = pool.construct();
+
+ delete[] p;
 }
 
 // Main
 
 int main()
 {
- test_static_pool<8>();
- test_static_object_pool<Count>();
- BOOST_TEST(Count::count == 0);
+ static const int pool_size = 24;
+
+ { // Testing static_pool
+ user_allocator_once::reset = true;
+ boost::static_pool<user_allocator_once> pool(8, pool_size);
+ test_static_pool(pool, pool_size);
+ }
+
+ { // Testing array_pool
+ boost::array_pool<8, pool_size> pool;
+ test_static_pool(pool, pool_size);
+ }
+
+ { // Testing static_object_pool
+ user_allocator_once::reset = true;
+ boost::static_object_pool<Count, user_allocator_once> pool(pool_size);
+ test_static_object_pool(pool, pool_size);
+ }
+ BOOST_ASSERT(Count::count == 0);
+
+ { // Testing array_object_pool
+ boost::array_object_pool<Count, pool_size> pool;
+ test_static_object_pool(pool, pool_size);
+ }
+ BOOST_ASSERT(Count::count == 0);
 
   return 0;
 }


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