Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r78265 - in sandbox/pool: boost/pool libs/pool/test
From: svart.riddare_at_[hidden]
Date: 2012-04-30 07:59:54


Author: edupuis
Date: 2012-04-30 07:59:53 EDT (Mon, 30 Apr 2012)
New Revision: 78265
URL: http://svn.boost.org/trac/boost/changeset/78265

Log:
Added boost::static_pool class and related tests.
Added:
   sandbox/pool/boost/pool/static_pool.hpp (contents, props changed)
   sandbox/pool/libs/pool/test/test_static_pool.cpp (contents, props changed)
Text files modified:
   sandbox/pool/boost/pool/poolfwd.hpp | 6 ++++++
   sandbox/pool/libs/pool/test/Jamfile.v2 | 3 +++
   sandbox/pool/libs/pool/test/test_poisoned_macros.cpp | 3 +++
   3 files changed, 12 insertions(+), 0 deletions(-)

Modified: sandbox/pool/boost/pool/poolfwd.hpp
==============================================================================
--- sandbox/pool/boost/pool/poolfwd.hpp (original)
+++ sandbox/pool/boost/pool/poolfwd.hpp 2012-04-30 07:59:53 EDT (Mon, 30 Apr 2012)
@@ -47,6 +47,12 @@
 class object_pool;
 
 //
+// Location: <boost/pool/static_pool.hpp>
+//
+template<typename UserAllocator = default_user_allocator_new_delete>
+class static_pool;
+
+//
 // Location: <boost/pool/singleton_pool.hpp>
 //
 template <typename Tag, unsigned RequestedSize,

Added: sandbox/pool/boost/pool/static_pool.hpp
==============================================================================
--- (empty file)
+++ sandbox/pool/boost/pool/static_pool.hpp 2012-04-30 07:59:53 EDT (Mon, 30 Apr 2012)
@@ -0,0 +1,94 @@
+// Copyright (C) 2012 Étienne Dupuis
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org for updates, documentation, and revision history.
+
+#ifndef BOOST_STATIC_POOL_HPP
+#define BOOST_STATIC_POOL_HPP
+/*!
+\file
+\brief Provides a template type boost::static_pool<T, UserAllocator>
+that can be used for fast and efficient memory allocation of objects of type T.
+It also provides automatic destruction of non-deallocated objects.
+*/
+
+#include <boost/pool/poolfwd.hpp>
+
+// boost::pool
+#include <boost/pool/pool.hpp>
+
+namespace boost
+{
+
+
+// DOCUMENTATION TO BE INSERTED
+
+template<typename UserAllocator>
+class static_pool: protected pool<UserAllocator>
+{
+ public: // Overridden functions
+ explicit static_pool(const size_type nrequested_size, const size_t nrequested_items)
+ :
+ pool<UserAllocator>(nrequested_size, nrequested_items)
+ {
+ free(pool<UserAllocator>::malloc());
+ }
+
+ void * malloc BOOST_PREVENT_MACRO_SUBSTITUTION()
+ {
+ return store().empty() ? NULL : store().malloc();
+ }
+
+ void * ordered_malloc()
+ {
+ return store().empty() ? NULL : store().malloc();
+ }
+
+ void * ordered_malloc(size_type n)
+ {
+ const size_type partition_size = alloc_size();
+ const size_type total_req_size = n * requested_size;
+ const size_type num_chunks = total_req_size / partition_size +
+ ((total_req_size % partition_size) ? true : false);
+
+ return store().malloc_n(num_chunks, partition_size);
+ }
+
+ public: // Inherited functions
+ void free BOOST_PREVENT_MACRO_SUBSTITUTION(void * const chunk)
+ {
+ return pool<UserAllocator>::free(chunk);
+ }
+
+ void ordered_free(void * const chunk)
+ {
+ return pool<UserAllocator>::ordered_free(chunk);
+ }
+
+ void free BOOST_PREVENT_MACRO_SUBSTITUTION(void * const chunks, const size_type n)
+ {
+ return pool<UserAllocator>::free(chunks, n);
+ }
+
+ void ordered_free(void * const chunks, const size_type n)
+ {
+ return pool<UserAllocator>::ordered_free(chunks, n);
+ }
+
+ size_type get_requested_size() const
+ {
+ return pool<UserAllocator>::get_requestedSize();
+ }
+
+ bool is_from(void * const chunk) const
+ {
+ return pool<UserAllocator>::is_from(chunk);
+ }
+};
+
+} // namespace boost
+
+#endif

Modified: sandbox/pool/libs/pool/test/Jamfile.v2
==============================================================================
--- sandbox/pool/libs/pool/test/Jamfile.v2 (original)
+++ sandbox/pool/libs/pool/test/Jamfile.v2 2012-04-30 07:59:53 EDT (Mon, 30 Apr 2012)
@@ -26,6 +26,7 @@
 test-suite pool :
     [ run test_simple_seg_storage.cpp ]
     [ run test_pool_alloc.cpp ]
+ [ run test_static_pool.cpp ]
     [ run pool_msvc_compiler_bug_test.cpp ]
     [ run test_msvc_mem_leak_detect.cpp ]
     [ run test_bug_3349.cpp ]
@@ -42,6 +43,7 @@
 #
     [ run test_simple_seg_storage.cpp : : : [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_simple_seg_storage_valgrind ]
     [ run test_pool_alloc.cpp : : : [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_pool_alloc_valgrind ]
+ [ run test_static_pool.cpp : : : [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_static_pool_valgrind ]
     [ run pool_msvc_compiler_bug_test.cpp : : : [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : pool_msvc_compiler_bug_test_valgrind ]
     [ run test_msvc_mem_leak_detect.cpp : : : [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_msvc_mem_leak_detect_valgrind ]
     [ run test_bug_3349.cpp : : : [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_bug_3349_valgrind ]
@@ -57,6 +59,7 @@
 #
     [ run test_simple_seg_storage.cpp : : : <define>BOOST_POOL_VALGRIND=1 [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_simple_seg_storage_valgrind_2 ]
     [ run test_pool_alloc.cpp : : : <define>BOOST_POOL_VALGRIND=1 [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_pool_alloc_valgrind_2 ]
+ [ run test_static_pool.cpp : : : <define>BOOST_POOL_VALGRIND=1 [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_static_pool_valgrind_2 ]
     [ run pool_msvc_compiler_bug_test.cpp : : : <define>BOOST_POOL_VALGRIND=1 [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : pool_msvc_compiler_bug_test_valgrind_2 ]
     [ run test_msvc_mem_leak_detect.cpp : : : <define>BOOST_POOL_VALGRIND=1 [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_msvc_mem_leak_detect_valgrind_2 ]
     [ run test_bug_3349.cpp : : : <define>BOOST_POOL_VALGRIND=1 [ check-target-builds valgrind_config_check : <testing.launcher>"valgrind --error-exitcode=1" : <build>no ] : test_bug_3349_valgrind_2 ]

Modified: sandbox/pool/libs/pool/test/test_poisoned_macros.cpp
==============================================================================
--- sandbox/pool/libs/pool/test/test_poisoned_macros.cpp (original)
+++ sandbox/pool/libs/pool/test/test_poisoned_macros.cpp 2012-04-30 07:59:53 EDT (Mon, 30 Apr 2012)
@@ -33,6 +33,9 @@
 template class boost::pool<boost::default_user_allocator_new_delete>;
 template class boost::pool<boost::default_user_allocator_malloc_free>;
 
+template class boost::static_pool<boost::default_user_allocator_new_delete>;
+template class boost::static_pool<boost::default_user_allocator_malloc_free>;
+
 template class boost::pool_allocator<int, boost::default_user_allocator_new_delete>;
 template class boost::pool_allocator<int, boost::default_user_allocator_malloc_free>;
 template class boost::fast_pool_allocator<int, boost::default_user_allocator_new_delete>;

Added: sandbox/pool/libs/pool/test/test_static_pool.cpp
==============================================================================
--- (empty file)
+++ sandbox/pool/libs/pool/test/test_static_pool.cpp 2012-04-30 07:59:53 EDT (Mon, 30 Apr 2012)
@@ -0,0 +1,119 @@
+/* Copyright (C) 2011 Étienne Dupuis
+ *
+ * Use, modification and distribution is subject to the
+ * Boost Software License, Version 1.0. (See accompanying
+ * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include <M:/Boost/Sandbox/pool/boost/pool/static_pool.hpp>
+
+#include <boost/assert.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+// Allocator that checks whether malloc() is called only once
+
+struct user_allocator_once
+{
+ typedef std::size_t size_type;
+ typedef std::ptrdiff_t difference_type;
+
+ static char * malloc BOOST_PREVENT_MACRO_SUBSTITUTION(const size_type bytes)
+ {
+ BOOST_TEST(reset);
+ reset = false;
+
+ return new (std::nothrow) char[bytes];
+ }
+
+ static void free BOOST_PREVENT_MACRO_SUBSTITUTION(char * const block)
+ {
+ delete [] block;
+ }
+
+ static bool reset;
+};
+
+bool user_allocator_once::reset = false;
+
+// Main set of test
+
+static void test_static_pool()
+{
+ 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
+ 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(sizeof(int), pool_size);
+
+ for (int n = 0; n < 2; n++)
+ {
+ // TEST #1
+ for (int k = 0; k < num_iterations; k++)
+ {
+ p[k] = pool.malloc();
+ BOOST_TEST((p[k] != NULL) == (k < pool_size));
+ }
+
+ for (int k = 0; k < num_iterations; k++)
+ BOOST_ASSERT(!p[k] || pool.is_from(p[k]));
+
+ for (int k = 0; k < num_iterations; k++)
+ p[k] ? pool.ordered_free(p[k]) : (void)(0);
+
+ // TEST #2
+ 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));
+ }
+
+ for (int k = 0; k < num_iterations; k++)
+ p[k] ? pool.ordered_free(p[k], consecutive_elements) : (void)(0);
+
+ // TEST #3
+ for (int k = 0; k < num_iterations; k++)
+ {
+ p[k] = pool.malloc();
+ BOOST_TEST((p[k] != NULL) == (k < pool_size));
+ }
+
+ for (int k = 0; k < num_iterations; k++)
+ p[k] ? pool.free(p[k]) : (void)(0);
+
+ // TEST #4
+
+ srand(NULL);
+
+ int allocated = 0;
+ for (int k = 0; k < pool_size / 2; k++)
+ p[allocated++] = pool.malloc();
+
+ for (int k = 0; k < 100 * num_iterations; k++)
+ {
+ if ((rand() & 8) && (allocated < pool_size))
+ {
+ p[allocated++] = pool.malloc();
+ }
+ else if (allocated > 0)
+ {
+ void *_p = p[--allocated];
+ BOOST_TEST(_p && pool.is_from(_p));
+ pool.free(_p);
+ }
+ }
+
+ while (allocated > 0)
+ pool.free(p[--allocated]);
+ }
+}
+
+// Main
+
+int main()
+{
+ test_static_pool();
+ 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