2 // detail/thread_info_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP
12 #define BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
20 #include <boost/asio/detail/noncopyable.hpp>
22 #include <boost/asio/detail/push_options.hpp>
28 class thread_info_base
40 ::operator delete(reusable_memory_);
43 static void* allocate(thread_info_base* this_thread, std::size_t size)
45 std::size_t chunks = (size + chunk_size - 1) / chunk_size;
47 if (this_thread && this_thread->reusable_memory_)
49 void* const pointer = this_thread->reusable_memory_;
50 this_thread->reusable_memory_ = 0;
52 unsigned char* const mem = static_cast<unsigned char*>(pointer);
53 if (static_cast<std::size_t>(mem[0]) >= chunks)
59 ::operator delete(pointer);
62 void* const pointer = ::operator new(chunks * chunk_size + 1);
63 unsigned char* const mem = static_cast<unsigned char*>(pointer);
64 mem[size] = (chunks <= UCHAR_MAX) ? static_cast<unsigned char>(chunks) : 0;
68 static void deallocate(thread_info_base* this_thread,
69 void* pointer, std::size_t size)
71 if (size <= chunk_size * UCHAR_MAX)
73 if (this_thread && this_thread->reusable_memory_ == 0)
75 unsigned char* const mem = static_cast<unsigned char*>(pointer);
77 this_thread->reusable_memory_ = pointer;
82 ::operator delete(pointer);
86 enum { chunk_size = 4 };
87 void* reusable_memory_;
94 #include <boost/asio/detail/pop_options.hpp>
96 #endif // BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP