Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54392 - in trunk: boost/asio/detail libs/asio/test
From: chris_at_[hidden]
Date: 2009-06-27 01:24:19


Author: chris_kohlhoff
Date: 2009-06-27 01:24:16 EDT (Sat, 27 Jun 2009)
New Revision: 54392
URL: http://svn.boost.org/trac/boost/changeset/54392

Log:
Fix custom memory allocation for timers. Ref #3107.

Text files modified:
   trunk/boost/asio/detail/timer_queue.hpp | 10 ++++---
   trunk/libs/asio/test/deadline_timer.cpp | 53 ++++++++++++++++++++++++++++++++++++++++
   2 files changed, 59 insertions(+), 4 deletions(-)

Modified: trunk/boost/asio/detail/timer_queue.hpp
==============================================================================
--- trunk/boost/asio/detail/timer_queue.hpp (original)
+++ trunk/boost/asio/detail/timer_queue.hpp 2009-06-27 01:24:16 EDT (Sat, 27 Jun 2009)
@@ -67,8 +67,10 @@
     heap_.reserve(heap_.size() + 1);
 
     // Create a new timer object.
- std::auto_ptr<timer<Handler> > new_timer(
- new timer<Handler>(time, handler, token));
+ typedef timer<Handler> timer_type;
+ typedef handler_alloc_traits<Handler, timer_type> alloc_traits;
+ raw_handler_ptr<alloc_traits> raw_ptr(handler);
+ handler_ptr<alloc_traits> new_timer(raw_ptr, time, handler, token);
 
     // Insert the new timer into the hash.
     typedef typename hash_map<void*, timer_base*>::iterator iterator;
@@ -78,12 +80,12 @@
     if (!result.second)
     {
       result.first->second->prev_ = new_timer.get();
- new_timer->next_ = result.first->second;
+ new_timer.get()->next_ = result.first->second;
       result.first->second = new_timer.get();
     }
 
     // Put the timer at the correct position in the heap.
- new_timer->heap_index_ = heap_.size();
+ new_timer.get()->heap_index_ = heap_.size();
     heap_.push_back(new_timer.get());
     up_heap(heap_.size() - 1);
     bool is_first = (heap_[0] == new_timer.get());

Modified: trunk/libs/asio/test/deadline_timer.cpp
==============================================================================
--- trunk/libs/asio/test/deadline_timer.cpp (original)
+++ trunk/libs/asio/test/deadline_timer.cpp 2009-06-27 01:24:16 EDT (Sat, 27 Jun 2009)
@@ -203,10 +203,63 @@
     BOOST_CHECK(timers[i].t.cancel() == 1);
 }
 
+struct custom_allocation_timer_handler
+{
+ custom_allocation_timer_handler(int* count) : count_(count) {}
+ void operator()(const boost::system::error_code&) {}
+ int* count_;
+};
+
+void* asio_handler_allocate(std::size_t size,
+ custom_allocation_timer_handler* handler)
+{
+ ++(*handler->count_);
+ return ::operator new(size);
+}
+
+void asio_handler_deallocate(void* pointer, std::size_t,
+ custom_allocation_timer_handler* handler)
+{
+ --(*handler->count_);
+ ::operator delete(pointer);
+}
+
+void deadline_timer_custom_allocation_test()
+{
+ static boost::asio::io_service io_service;
+ struct timer
+ {
+ boost::asio::deadline_timer t;
+ timer() : t(io_service) {}
+ } timers[100];
+
+ int allocation_count = 0;
+
+ for (int i = 0; i < 50; ++i)
+ {
+ timers[i].t.expires_at(boost::posix_time::pos_infin);
+ timers[i].t.async_wait(custom_allocation_timer_handler(&allocation_count));
+ }
+
+ for (int i = 50; i < 100; ++i)
+ {
+ timers[i].t.expires_at(boost::posix_time::neg_infin);
+ timers[i].t.async_wait(custom_allocation_timer_handler(&allocation_count));
+ }
+
+ for (int i = 0; i < 50; ++i)
+ timers[i].t.cancel();
+
+ io_service.run();
+
+ BOOST_CHECK(allocation_count == 0);
+}
+
 test_suite* init_unit_test_suite(int, char*[])
 {
   test_suite* test = BOOST_TEST_SUITE("deadline_timer");
   test->add(BOOST_TEST_CASE(&deadline_timer_test));
   test->add(BOOST_TEST_CASE(&deadline_timer_cancel_test));
+ test->add(BOOST_TEST_CASE(&deadline_timer_custom_allocation_test));
   return test;
 }


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