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