1// 2// detail/thread_info_base.hpp 3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4// 5// Copyright (c) 2003-2015 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 ASIO_DETAIL_THREAD_INFO_BASE_HPP 12#define ASIO_DETAIL_THREAD_INFO_BASE_HPP 13 14 15#include <climits> 16#include <cstddef> 17#include "asio/detail/noncopyable.hpp" 18 19#include "asio/detail/push_options.hpp" 20 21namespace asio { 22namespace detail { 23 24class thread_info_base 25 : private noncopyable 26{ 27public: 28 thread_info_base() 29 : reusable_memory_(0) 30 { 31 } 32 33 ~thread_info_base() 34 { 35 if (reusable_memory_) 36 ::operator delete(reusable_memory_); 37 } 38 39 static void* allocate(thread_info_base* this_thread, std::size_t size) 40 { 41 if (this_thread && this_thread->reusable_memory_) 42 { 43 void* const pointer = this_thread->reusable_memory_; 44 this_thread->reusable_memory_ = 0; 45 46 unsigned char* const mem = static_cast<unsigned char*>(pointer); 47 if (static_cast<std::size_t>(mem[0]) >= size) 48 { 49 mem[size] = mem[0]; 50 return pointer; 51 } 52 53 ::operator delete(pointer); 54 } 55 56 void* const pointer = ::operator new(size + 1); 57 unsigned char* const mem = static_cast<unsigned char*>(pointer); 58 mem[size] = (size <= UCHAR_MAX) ? static_cast<unsigned char>(size) : 0; 59 return pointer; 60 } 61 62 static void deallocate(thread_info_base* this_thread, 63 void* pointer, std::size_t size) 64 { 65 if (size <= UCHAR_MAX) 66 { 67 if (this_thread && this_thread->reusable_memory_ == 0) 68 { 69 unsigned char* const mem = static_cast<unsigned char*>(pointer); 70 mem[0] = mem[size]; 71 this_thread->reusable_memory_ = pointer; 72 return; 73 } 74 } 75 76 ::operator delete(pointer); 77 } 78 79private: 80 void* reusable_memory_; 81}; 82 83} // namespace detail 84} // namespace asio 85 86#include "asio/detail/pop_options.hpp" 87 88#endif // ASIO_DETAIL_THREAD_INFO_BASE_HPP 89