PacketVector.cci
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief PacketVector inline non-template implementation */
16 
17 //#include "PacketVector.ih"
18 
19 // Custom includes
20 #include <string.h>
21 #include <senf/Utils/senfassert.hh>
22 
23 #define prefix_ inline
24 ///////////////////////////////cci.p///////////////////////////////////////
25 
26 #ifndef SENF_PACKET_STD_CONTAINER
27 
28 prefix_ senf::PacketVector::PacketVector()
29  : size_ (ChunkSize),
30  data_ (static_cast<value_type *>(Pool::ordered_malloc(1))),
31  b_ (data_ + HeadRoom),
32  e_ (b_),
33  owner_ (true)
34 {}
35 
36 prefix_ senf::PacketVector::PacketVector(size_type requestSize, value_type initValue)
37  : size_ (allocationSize(requestSize)),
38  data_ (static_cast<value_type *>(Pool::ordered_malloc(size_ >> ChunkSizeIndex))),
39  b_ (data_ + HeadRoom),
40  e_ (b_ + requestSize),
41  owner_ (true)
42 {
43  ::memset(b_, initValue, requestSize);
44 }
45 
46 prefix_ senf::PacketVector::PacketVector(value_type * data, size_type size,
47  size_type chunkSize, size_type offset)
48  : size_ (chunkSize ? chunkSize : size + offset),
49  data_ (data),
50  b_ (data_ + offset),
51  e_ (b_ + size),
52  owner_ (false)
53 {}
54 
55 prefix_ senf::PacketVector::~PacketVector()
56 {
57  if (owner_)
58  Pool::ordered_free(data_, size_ >> ChunkSizeIndex);
59 }
60 
61 prefix_ senf::PacketVector::iterator senf::PacketVector::begin()
62 {
63  return b_;
64 }
65 
66 prefix_ senf::PacketVector::const_iterator senf::PacketVector::begin()
67  const
68 {
69  return b_;
70 }
71 
72 prefix_ senf::PacketVector::iterator senf::PacketVector::end()
73 {
74  return e_;
75 }
76 
77 prefix_ senf::PacketVector::const_iterator senf::PacketVector::end()
78  const
79 {
80  return e_;
81 }
82 
83 prefix_ senf::PacketVector::size_type senf::PacketVector::size()
84  const
85 {
86  return e_ - b_;
87 }
88 
89 prefix_ void senf::PacketVector::clear()
90 {
91  e_ = b_;
92 }
93 
94 prefix_ bool senf::PacketVector::external()
95  const
96 {
97  return ! owner_;
98 }
99 
100 prefix_ void senf::PacketVector::releaseExternal()
101 {
102  if (! owner_)
103  grow(begin(), 0);
104 }
105 
106 prefix_ void senf::PacketVector::erase(iterator pos)
107 {
108  SENF_ASSERT( pos >= b_ && pos < e_, "invalid iterator passed to PacketVector::erase" );
109  ::memmove(pos, pos + 1, e_ - pos - 1);
110  -- e_;
111 }
112 
113 prefix_ void senf::PacketVector::erase(iterator first, iterator last)
114 {
115  SENF_ASSERT( first >= b_ && last <= e_ && last >= first,
116  "invalid iterator passed to PacketVector::erase" );
117  ::memmove(first, last, e_ - last);
118  e_ -= last - first;
119 }
120 
121 prefix_ void senf::PacketVector::truncateFront(iterator pos)
122 {
123  SENF_ASSERT( pos >= b_ && pos <= e_, "invalid iterator passed to PacketVector::truncateFront" );
124  b_ = pos;
125 }
126 
127 prefix_ void senf::PacketVector::truncateBack(iterator pos)
128 {
129  SENF_ASSERT( pos >= b_ && pos <= e_, "invalid iterator passed to PacketVector::truncateBack" );
130  e_ = pos;
131 }
132 
133 prefix_ void senf::PacketVector::insert(iterator pos, value_type v)
134 {
135  SENF_ASSERT( pos >= b_ && pos <= e_, "invalid iterator passed to PacketVector::insert" );
136  * move(pos, 1) = v;
137 }
138 
139 prefix_ void senf::PacketVector::insert(iterator pos, size_type n, value_type v)
140 {
141  SENF_ASSERT( pos >= b_ && pos <= e_, "invalid iterator passed to PacketVector::insert" );
142  if (n > 0)
143  ::memset(move(pos, n), v, n);
144 }
145 
146 prefix_ senf::PacketVector::size_type senf::PacketVector::allocationSize(size_type sz)
147 {
148  return ((sz + HeadRoom + TailRoom) | (ChunkSize - 1)) + 1;
149 }
150 
151 prefix_ senf::PacketVector::iterator senf::PacketVector::move(iterator pos, size_type n)
152 {
153  // we check the prevalent cases here and leave the exotic cases to moveGrow
154  if (pos - b_ < e_ - pos && b_ >= data_ + n) {
155  ::memmove(b_ - n, b_, pos - b_);
156  b_ -= n;
157  return pos - n;
158  }
159  else if (e_ + n <= data_ + size_) {
160  ::memmove(pos + n, pos, e_ - pos);
161  e_ += n;
162  return pos;
163  }
164  else
165  return moveGrow(pos, n);
166 }
167 
168 #endif
169 
170 ///////////////////////////////cci.e///////////////////////////////////////
171 #undef prefix_
172 
173 
174 // Local Variables:
175 // mode: c++
176 // fill-column: 100
177 // comment-column: 40
178 // c-file-style: "senf"
179 // indent-tabs-mode: nil
180 // ispell-local-dictionary: "american"
181 // compile-command: "scons -u test"
182 // End: