$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r68920 - in sandbox/guild/pool: boost/pool libs/pool/test
From: john_at_[hidden]
Date: 2011-02-15 13:39:08
Author: johnmaddock
Date: 2011-02-15 13:39:07 EST (Tue, 15 Feb 2011)
New Revision: 68920
URL: http://svn.boost.org/trac/boost/changeset/68920
Log:
Add test case for bug #4960.
Add conditional instrumentation code to headers so we can see what's being allocated when.
Fix issue #4960 by allowing pool to allocate 0 chunks.
Refs #4960.
Added:
   sandbox/guild/pool/libs/pool/test/test_bug_4960.cpp   (contents, props changed)
Text files modified: 
   sandbox/guild/pool/boost/pool/pool.hpp                      |    25 +++++++++++++++++++++++--               
   sandbox/guild/pool/boost/pool/pool_alloc.hpp                |    30 +++++++++++++++++++++++++++++-          
   sandbox/guild/pool/boost/pool/simple_segregated_storage.hpp |     9 +++++++++                               
   sandbox/guild/pool/libs/pool/test/Jamfile.v2                |     1 +                                       
   4 files changed, 62 insertions(+), 3 deletions(-)
Modified: sandbox/guild/pool/boost/pool/pool.hpp
==============================================================================
--- sandbox/guild/pool/boost/pool/pool.hpp	(original)
+++ sandbox/guild/pool/boost/pool/pool.hpp	2011-02-15 13:39:07 EST (Tue, 15 Feb 2011)
@@ -33,6 +33,11 @@
 // boost::simple_segregated_storage
 #include <boost/pool/simple_segregated_storage.hpp>
 
+#ifdef BOOST_POOL_INSTRUMENT
+#include <iostream>
+#include<iomanip>
+#endif
+
 #ifdef BOOST_NO_STDC_NAMESPACE
  namespace std { using ::malloc; using ::free; }
 #endif
@@ -67,7 +72,12 @@
 
 */
 
-namespace boost
+#ifdef BOOST_MSVC
+#pragma warning(push)
+#pragma warning(disable:4127)  // Conditional expression is constant
+#endif
+
+ namespace boost
 {
 
 //!  Default User allocator new used as default template parameter for UserAllocator.  Uese new and delete.
@@ -713,9 +723,16 @@
 
   void * ret = store().malloc_n(num_chunks, partition_size);
 
-  if (ret != 0)
+#ifdef BOOST_POOL_INSTRUMENT
+  std::cout << "Allocating " << n << " chunks from pool of size " << partition_size << std::endl;
+#endif
+  if ((ret != 0) || (n == 0))
     return ret;
 
+#ifdef BOOST_POOL_INSTRUMENT
+  std::cout << "Cache miss, allocating another chunk...\n";
+#endif
+
   // Not enough memory in our storages; make a new storage,
   BOOST_USING_STD_MAX();
   next_size = max BOOST_PREVENT_MACRO_SUBSTITUTION(next_size, num_chunks);
@@ -782,5 +799,9 @@
 
 } // namespace boost
 
+#ifdef BOOST_MSVC
+#pragma warning(pop)
+#endif
+
 #endif // #ifdef BOOST_POOL_HPP
 
Modified: sandbox/guild/pool/boost/pool/pool_alloc.hpp
==============================================================================
--- sandbox/guild/pool/boost/pool/pool_alloc.hpp	(original)
+++ sandbox/guild/pool/boost/pool/pool_alloc.hpp	2011-02-15 13:39:07 EST (Tue, 15 Feb 2011)
@@ -86,6 +86,11 @@
 
 #include <boost/detail/workaround.hpp>
 
+#ifdef BOOST_POOL_INSTRUMENT
+#include <iostream>
+#include <iomanip>
+#endif
+
 // The following code will be put into Boost.Config in a later revision
 #if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \
     BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
@@ -94,6 +99,19 @@
 
 namespace boost {
 
+#ifdef BOOST_POOL_INSTRUMENT
+
+template <bool b>
+struct debug_info
+{
+   static unsigned allocated;
+};
+
+template <bool b>
+unsigned debug_info<b>::allocated = 0;
+
+#endif
+
  //! Tag to identify pool_allocator when used as template parameter.
  struct pool_allocator_tag
 {
@@ -178,10 +196,15 @@
 
     static pointer allocate(const size_type n)
     {
+#ifdef BOOST_POOL_INSTRUMENT
+       debug_info<true>::allocated += n * sizeof(T);
+       std::cout << "Allocating " << n << " * " << sizeof(T) << " bytes...\n"
+          "Total allocated is now " << debug_info<true>::allocated << std::endl;
+#endif
       const pointer ret = static_cast<pointer>(
           singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
               NextSize, MaxSize>::ordered_malloc(n) );
-      if (ret == 0)
+      if ((ret == 0) && n)
         boost::throw_exception(std::bad_alloc());
       return ret;
     }
@@ -189,6 +212,11 @@
     { return allocate(n); }
     static void deallocate(const pointer ptr, const size_type n)
     {
+#ifdef BOOST_POOL_INSTRUMENT
+       debug_info<true>::allocated -= n * sizeof(T);
+       std::cout << "Deallocating " << n << " * " << sizeof(T) << " bytes...\n"
+          "Total allocated is now " << debug_info<true>::allocated << std::endl;
+#endif
 #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
       if (ptr == 0 || n == 0)
         return;
Modified: sandbox/guild/pool/boost/pool/simple_segregated_storage.hpp
==============================================================================
--- sandbox/guild/pool/boost/pool/simple_segregated_storage.hpp	(original)
+++ sandbox/guild/pool/boost/pool/simple_segregated_storage.hpp	2011-02-15 13:39:07 EST (Tue, 15 Feb 2011)
@@ -26,6 +26,11 @@
 
 #include <boost/pool/poolfwd.hpp>
 
+#ifdef BOOST_MSVC
+#pragma warning(push)
+#pragma warning(disable:4127)  // Conditional expression is constant
+#endif
+
 namespace boost {
 
 /*!
@@ -328,4 +333,8 @@
 
 } // namespace boost
 
+#ifdef BOOST_MSVC
+#pragma warning(pop)
+#endif
+
 #endif
Modified: sandbox/guild/pool/libs/pool/test/Jamfile.v2
==============================================================================
--- sandbox/guild/pool/libs/pool/test/Jamfile.v2	(original)
+++ sandbox/guild/pool/libs/pool/test/Jamfile.v2	2011-02-15 13:39:07 EST (Tue, 15 Feb 2011)
@@ -26,6 +26,7 @@
     [ run pool_msvc_compiler_bug_test.cpp ]
     [ run test_msvc_mem_leak_detect.cpp ]
     [ run test_bug_3349.cpp ]
+    [ run test_bug_4960.cpp ]
     ;
 
 
Added: sandbox/guild/pool/libs/pool/test/test_bug_4960.cpp
==============================================================================
--- (empty file)
+++ sandbox/guild/pool/libs/pool/test/test_bug_4960.cpp	2011-02-15 13:39:07 EST (Tue, 15 Feb 2011)
@@ -0,0 +1,45 @@
+/* Copyright (C) 2011 Kwan Ting Chan
+ * Based from bug report submitted by Xiaohan Wang
+ * 
+ * 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)
+ */
+
+// Test of bug #3349 (https://svn.boost.org/trac/boost/ticket/3349)
+
+#include <boost/pool/pool_alloc.hpp>
+#include <vector>
+#include <iostream>
+
+typedef std::vector<int, boost::pool_allocator<int> > EventVector;
+typedef std::vector<EventVector, boost::pool_allocator<EventVector> > IndexVector;
+
+int main() 
+{
+  IndexVector iv;
+  int limit = 100;
+  for (int i = 0; i < limit; ++i) 
+  {
+    iv.push_back(EventVector());
+    for(int j = 0; j < limit; ++j)
+      iv.back().push_back(j);
+  }
+
+   boost::pool<boost::default_user_allocator_new_delete> po(4);
+   for(int i = 0; i < limit; ++i)
+   {
+      void* p = po.ordered_malloc(0);
+      po.ordered_free(p, 0);
+   }
+
+  boost::pool_allocator<int> al;
+  for(int i = 0; i < limit; ++i)
+  {
+     int* p = al.allocate(0);
+     al.deallocate(p, 0);
+  }
+
+  std::cout << "it works\n";
+  return 0;
+}