2 // detail/thread_info_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 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
34 enum { mem_index = 0 };
39 enum { mem_index = 1 };
44 for (int i = 0; i < max_mem_index; ++i)
45 reusable_memory_[i] = 0;
50 for (int i = 0; i < max_mem_index; ++i)
51 if (reusable_memory_[i])
52 ::operator delete(reusable_memory_[i]);
55 static void* allocate(thread_info_base* this_thread, std::size_t size)
57 return allocate(default_tag(), this_thread, size);
60 static void deallocate(thread_info_base* this_thread,
61 void* pointer, std::size_t size)
63 deallocate(default_tag(), this_thread, pointer, size);
66 template <typename Purpose>
67 static void* allocate(Purpose, thread_info_base* this_thread,
70 std::size_t chunks = (size + chunk_size - 1) / chunk_size;
72 if (this_thread && this_thread->reusable_memory_[Purpose::mem_index])
74 void* const pointer = this_thread->reusable_memory_[Purpose::mem_index];
75 this_thread->reusable_memory_[Purpose::mem_index] = 0;
77 unsigned char* const mem = static_cast<unsigned char*>(pointer);
78 if (static_cast<std::size_t>(mem[0]) >= chunks)
84 ::operator delete(pointer);
87 void* const pointer = ::operator new(chunks * chunk_size + 1);
88 unsigned char* const mem = static_cast<unsigned char*>(pointer);
89 mem[size] = (chunks <= UCHAR_MAX) ? static_cast<unsigned char>(chunks) : 0;
93 template <typename Purpose>
94 static void deallocate(Purpose, thread_info_base* this_thread,
95 void* pointer, std::size_t size)
97 if (size <= chunk_size * UCHAR_MAX)
99 if (this_thread && this_thread->reusable_memory_[Purpose::mem_index] == 0)
101 unsigned char* const mem = static_cast<unsigned char*>(pointer);
103 this_thread->reusable_memory_[Purpose::mem_index] = pointer;
108 ::operator delete(pointer);
112 enum { chunk_size = 4 };
113 enum { max_mem_index = 2 };
114 void* reusable_memory_[max_mem_index];
117 } // namespace detail
121 #include <boost/asio/detail/pop_options.hpp>
123 #endif // BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP