]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/thread_info_base.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2016 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: | |
32 | thread_info_base() | |
33 | : reusable_memory_(0) | |
34 | { | |
35 | } | |
36 | ||
37 | ~thread_info_base() | |
38 | { | |
39 | if (reusable_memory_) | |
40 | ::operator delete(reusable_memory_); | |
41 | } | |
42 | ||
43 | static void* allocate(thread_info_base* this_thread, std::size_t size) | |
44 | { | |
45 | if (this_thread && this_thread->reusable_memory_) | |
46 | { | |
47 | void* const pointer = this_thread->reusable_memory_; | |
48 | this_thread->reusable_memory_ = 0; | |
49 | ||
50 | unsigned char* const mem = static_cast<unsigned char*>(pointer); | |
51 | if (static_cast<std::size_t>(mem[0]) >= size) | |
52 | { | |
53 | mem[size] = mem[0]; | |
54 | return pointer; | |
55 | } | |
56 | ||
57 | ::operator delete(pointer); | |
58 | } | |
59 | ||
60 | void* const pointer = ::operator new(size + 1); | |
61 | unsigned char* const mem = static_cast<unsigned char*>(pointer); | |
62 | mem[size] = (size <= UCHAR_MAX) ? static_cast<unsigned char>(size) : 0; | |
63 | return pointer; | |
64 | } | |
65 | ||
66 | static void deallocate(thread_info_base* this_thread, | |
67 | void* pointer, std::size_t size) | |
68 | { | |
69 | if (size <= UCHAR_MAX) | |
70 | { | |
71 | if (this_thread && this_thread->reusable_memory_ == 0) | |
72 | { | |
73 | unsigned char* const mem = static_cast<unsigned char*>(pointer); | |
74 | mem[0] = mem[size]; | |
75 | this_thread->reusable_memory_ = pointer; | |
76 | return; | |
77 | } | |
78 | } | |
79 | ||
80 | ::operator delete(pointer); | |
81 | } | |
82 | ||
83 | private: | |
84 | void* reusable_memory_; | |
85 | }; | |
86 | ||
87 | } // namespace detail | |
88 | } // namespace asio | |
89 | } // namespace boost | |
90 | ||
91 | #include <boost/asio/detail/pop_options.hpp> | |
92 | ||
93 | #endif // BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP |