PacketImpl.hh
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 
17 #ifndef HH_SENF_Packets_PacketImpl_
18 #define HH_SENF_Packets_PacketImpl_ 1
19 
20 // Custom includes
21 #include <boost/noncopyable.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/ptr_container/ptr_vector.hpp>
24 #include <boost/type_traits/aligned_storage.hpp>
26 
27 //#include "PacketImpl.mpp"
28 #include "PacketImpl.ih"
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 // Here we only need the size of PacketInterpreter, no implementation is needed
32 #ifndef HH_SENF_Packets_Packets__decls_
33 #define HH_SENF_Packets_Packets__decls_
34 #include "PacketInterpreter.hh"
35 #include "PacketType.hh"
36 #undef HH_SENF_Packets_Packets__decls_
37 #else
38 #include "PacketInterpreter.hh"
39 #include "PacketType.hh"
40 #endif
41 
42 namespace senf {
43 
60  struct ComplexAnnotation {};
61 
97  void dumpPacketAnnotationRegistry(std::ostream & os);
98 
99  template <class PacketType> class PacketInterpreter;
100 
101 namespace detail {
102 
113  : boost::noncopyable,
114  public pool_alloc_mixin<PacketImpl>
115  {
116  public:
125 
126  // structors
127 
128  PacketImpl();
129  PacketImpl(size_type size, byte initValue);
130  template <class InputIterator>
131  PacketImpl(InputIterator b, InputIterator e);
132  PacketImpl(byte * data, size_type size, size_type chunkSize = 0u,
133  size_type offset = 0u);
134  ~PacketImpl();
135 
136  // reference/memory management
137 
138  void add_ref();
139  bool release();
140  refcount_t refcount() const;
141 
142  // Interpreter chain
143 
144  PacketInterpreterBase * first();
145  PacketInterpreterBase * last();
146 
149 
150  void * allocateInterpreter();
151  void deallocateInterpreter(void * address);
152 
153  bool usingExternalMemory() const;
154  void releaseExternalMemory();
155 
156  void memDebug(std::ostream & os);
157 
158  void appendInterpreter (PacketInterpreterBase * p);
159  void prependInterpreter (PacketInterpreterBase * p);
160  void prependInterpreter (PacketInterpreterBase * p, PacketInterpreterBase * before);
161  void truncateInterpreters (PacketInterpreterBase * p);
162  void truncateInterpretersBackwards (PacketInterpreterBase * p);
163 
164  // This one is DANGEROUS:
165  // a) if called without a Guard in place, this will destroy the PacketImpl
166  // b) this will invalidate ALL (!!) packet's referencing this data
167  void clearInterpreters();
168 
169  // Data container
170 
171  iterator begin();
172  iterator end();
173  size_type size();
174 
175  void insert(PacketData * self, iterator pos, byte v);
176  void insert(PacketData * self, iterator pos, size_type n, byte v);
177  template <class ForwardIterator>
178  void insert(PacketData * self, iterator pos, ForwardIterator f, ForwardIterator l);
179 
180  void erase(PacketData * self, iterator pos);
181  void erase(PacketData * self, iterator first, iterator last);
182  void truncateDataBackwards(PacketData * self, iterator pos);
183  iterator truncateRawDataBackwards(iterator pos);
184  void truncateRawDataForward(iterator pos);
185  void clear(PacketData * self);
186 
187  // Annotations
188  template <class Annotation>
189  Annotation & annotation();
190 
191  void clearAnnotations();
192  void assignAnnotations(PacketImpl const & other);
193  void dumpAnnotations(std::ostream & os);
194 
203  struct Guard {
204  Guard(PacketImpl * impl);
205  ~Guard();
207  };
208 
209  static size_type maxPreallocHigh();
210  static size_type maxPreallocHeapcount();
211 
212  private:
213  void eraseInterpreters(interpreter_list::iterator b, interpreter_list::iterator e);
214  void updateIterators(PacketData * self, difference_type pos, difference_type n);
215 
216  void * annotation(AnnotationRegistry::key_type key); // may return 0 !
217 #ifndef SENF_PACKET_NO_COMPLEX_ANNOTATIONS
218  void * complexAnnotation(AnnotationRegistry::key_type key); // may return 0 !
219  template <class Annotation>
220  void * complexAnnotation();
221 #endif
222 
223 #ifndef SENF_PACKET_NO_HEAP_INTERPRETERS
224  void destroySelf();
225 #endif
226 
227  refcount_t refcount_;
228  raw_container data_;
229  interpreter_list interpreters_;
230 
231  // Annotations
232 
233  typedef boost::aligned_storage<SENF_PACKET_ANNOTATION_SLOTSIZE>::type SimpleAnnotationSlot;
234 
235 #ifndef SENF_PACKET_NO_COMPLEX_ANNOTATIONS
236  typedef boost::ptr_vector< boost::nullable<AnnotationRegistry::EntryBase> >
237  ComplexAnnotations;
238  ComplexAnnotations complexAnnotations_;
239 #endif
240 
241  SimpleAnnotationSlot simpleAnnotations_[SENF_PACKET_ANNOTATION_SLOTS];
242 
243  // Preallocated interpreters
244 
245  union PreallocSlot {
246  boost::aligned_storage<
248  boost::alignment_of< PacketInterpreter<PacketTypeBase> >::value
249  >::type storage_;
250 #ifndef SENF_PACKET_ALTERNATIVE_PREALLOC
251  int nextOffset_;
252 #else
253  PreallocSlot * nextFree_;
254 #endif
255  };
256 
257  PreallocSlot prealloc_[SENF_PACKET_PREALLOC_INTERPRETERS];
258 #ifdef SENF_PACKET_ALTERNATIVE_PREALLOC
259  size_type preallocHigh_;
260 #endif
261  PreallocSlot * preallocFree_;
262 #ifndef SENF_PACKET_NO_HEAP_INTERPRETERS
263  size_type preallocHeapcount_;
264 #endif
265 
266 #ifdef SENF_DEBUG
267  static size_type maxPreallocHigh_;
268 #ifndef SENF_PACKET_NO_HEAP_INTERPRETERS
269  static size_type maxPreallocHeapcount_;
270 #endif
271 #endif
272  };
273 
276 
277 }}
278 
279 //-/////////////////////////////////////////////////////////////////////////////////////////////////
280 #endif
281 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_PacketImpl_i_)
282 #define HH_SENF_Packets_PacketImpl_i_
283 #include "PacketImpl.cci"
284 #include "PacketImpl.ct"
285 #include "PacketImpl.cti"
286 #endif
287 
288 
289 // Local Variables:
290 // mode: c++
291 // fill-column: 100
292 // c-file-style: "senf"
293 // indent-tabs-mode: nil
294 // ispell-local-dictionary: "american"
295 // compile-command: "scons -u test"
296 // comment-column: 40
297 // End:
Internal: Keep PacketImpl instance alive.
Definition: PacketImpl.hh:203
senf::detail::packet::byte byte
Definition: PacketImpl.hh:117
senf::detail::packet::difference_type difference_type
Definition: PacketImpl.hh:120
void intrusive_ptr_add_ref(PacketInterpreterBase const *p)
Internal: Concrete packet interpreter.
Definition: PacketImpl.hh:99
Internal: Base packet interpreter class.
senf::detail::packet::refcount_t refcount_t
Definition: PacketImpl.hh:124
senf::detail::packet::iterator iterator
Definition: PacketImpl.hh:122
boost::intrusive::list< PacketInterpreterBase, boost::intrusive::constant_time_size< false >, boost::intrusive::base_hook< interpreter_list_base > > interpreter_list
Definition: PacketTypes.hh:58
Marker base-class for complex annotations.
Definition: PacketImpl.hh:60
raw_container::const_iterator const_iterator
Definition: PacketTypes.hh:70
Packet data STL-sequence view.
Definition: PacketData.hh:61
raw_container::size_type size_type
Definition: PacketTypes.hh:66
PacketInterpreter public header.
Internal: Packet data storage.
Definition: PacketImpl.hh:112
raw_container::iterator iterator
Definition: PacketTypes.hh:69
senf::detail::packet::size_type size_type
Definition: PacketImpl.hh:119
senf::detail::packet::raw_container raw_container
Definition: PacketImpl.hh:118
senf::detail::packet::interpreter_list interpreter_list
Definition: PacketImpl.hh:121
raw_container::difference_type difference_type
Definition: PacketTypes.hh:67
void intrusive_ptr_release(PacketInterpreterBase const *p)
boost::uint8_t byte
Definition: PacketTypes.hh:60
void dumpPacketAnnotationRegistry(std::ostream &os)
Dump annotation registry debug information.
Definition: PacketImpl.cc:348
senf::detail::packet::const_iterator const_iterator
Definition: PacketImpl.hh:123
::phoenix::function< detail::clear > const clear
PacketType public header.