Debian Codegraph @

contrib/epee/src/buffer.cpp

raw symbols SCIP
1 // Copyright (c) 2018-2022, The Monero Project
2
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include <limits>
31 #include <string.h>
32 #include "net/buffer.h"
33
34 #undef MONERO_DEFAULT_LOG_CATEGORY
35 #define MONERO_DEFAULT_LOG_CATEGORY "net.buffer"
36
37 namespace epee
38 {
39 namespace net_utils
40 {
41
42 void buffer::append(const void *data, size_t sz)
43 {
44 const size_t capacity = storage.capacity();
45 const size_t avail = capacity - storage.size();
46
47 CHECK_AND_ASSERT_THROW_MES(storage.size() < std::numeric_limits<size_t>::max() - sz, "Too much data to append");
48
49 // decide when to move
50 if (sz > avail)
51 {
52 // we have to reallocate or move
53 const bool move = size() + sz <= capacity;
54 if (move)
55 {
56 const size_t bytes = storage.size() - offset;
57 NET_BUFFER_LOG("appending " << sz << " from " << size() << " by moving " << bytes << " from offset " << offset << " first (forced)");
58 memmove(storage.data(), storage.data() + offset, bytes);
59 storage.resize(bytes);
60 offset = 0;
61 }
62 else
63 {
64 NET_BUFFER_LOG("appending " << sz << " from " << size() << " by reallocating");
65 std::vector<uint8_t> new_storage;
66 size_t reserve = (((size() + sz) * 3 / 2) + 4095) & ~4095;
67 new_storage.reserve(reserve);
68 new_storage.resize(size());
69 if (size() > 0)
70 memcpy(new_storage.data(), storage.data() + offset, storage.size() - offset);
71 offset = 0;
72 std::swap(storage, new_storage);
73 }
74 }
75 else
76 {
77 // we have space already
78 if (size() <= 4096 && offset > 4096 * 16 && offset >= capacity / 2)
79 {
80 // we have little to move, and we're far enough into the buffer that it's probably a win to move anyway
81 const size_t pos = storage.size() - offset;
82 NET_BUFFER_LOG("appending " << sz << " from " << size() << " by moving " << pos << " from offset " << offset << " first (unforced)");
83 memmove(storage.data(), storage.data() + offset, storage.size() - offset);
84 storage.resize(pos);
85 offset = 0;
86 }
87 else
88 {
89 NET_BUFFER_LOG("appending " << sz << " from " << size() << " by writing to existing capacity");
90 }
91 }
92
93 // add the new data
94 storage.insert(storage.end(), (const uint8_t*)data, (const uint8_t*)data + sz);
95
96 NET_BUFFER_LOG("storage now " << offset << "/" << storage.size() << "/" << storage.capacity());
97 }
98
99 }
100 }