/src/libtorrent/src/stack_allocator.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | |
3 | | Copyright (c) 2017-2019, Arvid Norberg |
4 | | All rights reserved. |
5 | | |
6 | | Redistribution and use in source and binary forms, with or without |
7 | | modification, are permitted provided that the following conditions |
8 | | are met: |
9 | | |
10 | | * Redistributions of source code must retain the above copyright |
11 | | notice, this list of conditions and the following disclaimer. |
12 | | * Redistributions in binary form must reproduce the above copyright |
13 | | notice, this list of conditions and the following disclaimer in |
14 | | the documentation and/or other materials provided with the distribution. |
15 | | * Neither the name of the author nor the names of its |
16 | | contributors may be used to endorse or promote products derived |
17 | | from this software without specific prior written permission. |
18 | | |
19 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
23 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | | POSSIBILITY OF SUCH DAMAGE. |
30 | | |
31 | | */ |
32 | | |
33 | | #include "libtorrent/stack_allocator.hpp" |
34 | | #include <cstdarg> // for va_list, va_copy, va_end |
35 | | |
36 | | namespace libtorrent { |
37 | | namespace aux { |
38 | | |
39 | | allocation_slot stack_allocator::copy_string(string_view str) |
40 | 1.86k | { |
41 | 1.86k | auto const ret = m_storage.size(); |
42 | 1.86k | if (std::numeric_limits<int>::max() - str.size() <= ret) |
43 | 0 | return {}; |
44 | 1.86k | m_storage.resize(ret + str.size() + 1); |
45 | 1.86k | std::memcpy(&m_storage[ret], str.data(), str.size()); |
46 | 1.86k | m_storage[ret + str.length()] = '\0'; |
47 | 1.86k | return allocation_slot(ret); |
48 | 1.86k | } |
49 | | |
50 | | allocation_slot stack_allocator::copy_string(char const* str) |
51 | 0 | { |
52 | 0 | auto const ret = m_storage.size(); |
53 | 0 | auto const len = std::strlen(str); |
54 | 0 | if (std::numeric_limits<int>::max() - len <= ret) |
55 | 0 | return {}; |
56 | 0 | m_storage.resize(ret + len + 1); |
57 | 0 | std::memcpy(&m_storage[ret], str, len); |
58 | 0 | m_storage[ret + len] = '\0'; |
59 | 0 | return allocation_slot(ret); |
60 | 0 | } |
61 | | |
62 | | allocation_slot stack_allocator::format_string(char const* fmt, va_list v) |
63 | 0 | { |
64 | 0 | auto const pos = m_storage.size(); |
65 | 0 | std::size_t len = 512; |
66 | |
|
67 | 0 | for(;;) |
68 | 0 | { |
69 | 0 | if (std::numeric_limits<int>::max() - len <= pos) |
70 | 0 | return {}; |
71 | | |
72 | 0 | m_storage.resize(pos + len + 1); |
73 | |
|
74 | 0 | va_list args; |
75 | 0 | va_copy(args, v); |
76 | |
|
77 | 0 | #ifdef __clang__ |
78 | 0 | #pragma clang diagnostic push |
79 | 0 | #pragma clang diagnostic ignored "-Wformat-nonliteral" |
80 | 0 | #endif |
81 | 0 | int const ret = std::vsnprintf(m_storage.data() + pos, len + 1, fmt, args); |
82 | 0 | #ifdef __clang__ |
83 | 0 | #pragma clang diagnostic pop |
84 | 0 | #endif |
85 | |
|
86 | 0 | va_end(args); |
87 | |
|
88 | 0 | if (ret < 0) |
89 | 0 | { |
90 | 0 | m_storage.resize(pos); |
91 | 0 | return copy_string("(format error)"); |
92 | 0 | } |
93 | 0 | if (static_cast<std::size_t>(ret) > len) |
94 | 0 | { |
95 | | // try again |
96 | 0 | len = numeric_cast<std::size_t>(ret); |
97 | 0 | continue; |
98 | 0 | } |
99 | 0 | break; |
100 | 0 | } |
101 | | |
102 | | // +1 is to include the 0-terminator |
103 | 0 | m_storage.resize(pos + len + 1); |
104 | 0 | return allocation_slot(pos); |
105 | 0 | } |
106 | | |
107 | | allocation_slot stack_allocator::copy_buffer(span<char const> buf) |
108 | 0 | { |
109 | 0 | auto const ret = m_storage.size(); |
110 | 0 | auto const size = numeric_cast<std::size_t>(buf.size()); |
111 | 0 | if (size < 1) return {}; |
112 | | |
113 | 0 | if (std::numeric_limits<int>::max() - size <= ret) |
114 | 0 | return {}; |
115 | 0 | m_storage.resize(ret + size); |
116 | 0 | std::memcpy(&m_storage[ret], buf.data(), numeric_cast<std::size_t>(size)); |
117 | 0 | return allocation_slot(ret); |
118 | 0 | } |
119 | | |
120 | | allocation_slot stack_allocator::allocate(int const bytes) |
121 | 0 | { |
122 | 0 | if (bytes < 1) return {}; |
123 | 0 | auto const ret = m_storage.size(); |
124 | 0 | if (std::numeric_limits<int>::max() - static_cast<std::size_t>(bytes) <= ret) |
125 | 0 | return {}; |
126 | 0 | m_storage.resize(ret + numeric_cast<std::size_t>(bytes)); |
127 | 0 | return allocation_slot(ret); |
128 | 0 | } |
129 | | |
130 | | char* stack_allocator::ptr(allocation_slot const idx) |
131 | 0 | { |
132 | 0 | static char tmp = 0; |
133 | 0 | if(!idx.is_valid()) return &tmp; |
134 | 0 | TORRENT_ASSERT(idx.val() < m_storage.size()); |
135 | 0 | return &m_storage[idx.val()]; |
136 | 0 | } |
137 | | |
138 | | char const* stack_allocator::ptr(allocation_slot const idx) const |
139 | 3.72k | { |
140 | 3.72k | if(!idx.is_valid()) return ""; |
141 | 3.72k | TORRENT_ASSERT(idx.val() < m_storage.size()); |
142 | 3.72k | return &m_storage[idx.val()]; |
143 | 3.72k | } |
144 | | |
145 | | void stack_allocator::swap(stack_allocator& rhs) |
146 | 0 | { |
147 | 0 | m_storage.swap(rhs.m_storage); |
148 | 0 | std::swap(m_generation, rhs.m_generation); |
149 | 0 | } |
150 | | |
151 | | void stack_allocator::reset(std::uint32_t const generation) |
152 | 3 | { |
153 | 3 | m_storage.clear(); |
154 | 3 | m_generation = generation; |
155 | 3 | } |
156 | | } |
157 | | } |
158 | | |