1 // 2 // detail/thread_info_base.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) 6 // 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) 9 // 10 11 #ifndef BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP 12 #define BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP 13 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 15 # pragma once 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18 #include <climits> 19 #include <cstddef> 20 #include <boost/asio/detail/noncopyable.hpp> 21 22 #include <boost/asio/detail/push_options.hpp> 23 24 namespace boost { 25 namespace asio { 26 namespace detail { 27 28 class thread_info_base 29 : private noncopyable 30 { 31 public: thread_info_base()32 thread_info_base() 33 : reusable_memory_(0) 34 { 35 } 36 ~thread_info_base()37 ~thread_info_base() 38 { 39 if (reusable_memory_) 40 ::operator delete(reusable_memory_); 41 } 42 allocate(thread_info_base * this_thread,std::size_t size)43 static void* allocate(thread_info_base* this_thread, std::size_t size) 44 { 45 std::size_t chunks = (size + chunk_size - 1) / chunk_size; 46 47 if (this_thread && this_thread->reusable_memory_) 48 { 49 void* const pointer = this_thread->reusable_memory_; 50 this_thread->reusable_memory_ = 0; 51 52 unsigned char* const mem = static_cast<unsigned char*>(pointer); 53 if (static_cast<std::size_t>(mem[0]) >= chunks) 54 { 55 mem[size] = mem[0]; 56 return pointer; 57 } 58 59 ::operator delete(pointer); 60 } 61 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; 65 return pointer; 66 } 67 deallocate(thread_info_base * this_thread,void * pointer,std::size_t size)68 static void deallocate(thread_info_base* this_thread, 69 void* pointer, std::size_t size) 70 { 71 if (size <= chunk_size * UCHAR_MAX) 72 { 73 if (this_thread && this_thread->reusable_memory_ == 0) 74 { 75 unsigned char* const mem = static_cast<unsigned char*>(pointer); 76 mem[0] = mem[size]; 77 this_thread->reusable_memory_ = pointer; 78 return; 79 } 80 } 81 82 ::operator delete(pointer); 83 } 84 85 private: 86 enum { chunk_size = 4 }; 87 void* reusable_memory_; 88 }; 89 90 } // namespace detail 91 } // namespace asio 92 } // namespace boost 93 94 #include <boost/asio/detail/pop_options.hpp> 95 96 #endif // BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP 97