2020
2121#include " buffer_store.hpp"
2222#include " ip4/addr.hpp"
23+ #include < delegate>
2324#include < cassert>
2425
25- namespace net {
26- void default_packet_deleter (void * p);
26+ namespace net
27+ {
28+ class Packet ;
29+ using Packet_ptr = std::unique_ptr<Packet>;
2730
28- class Packet : public std ::enable_shared_from_this<Packet>
31+ class Packet
2932 {
3033 public:
31- using deleter_t = delegate<void (void *)>;
32-
3334 /* *
3435 * Construct, using existing buffer.
3536 *
@@ -41,25 +42,30 @@ namespace net {
4142 Packet (
4243 uint16_t cap,
4344 uint16_t len,
44- deleter_t del = default_packet_deleter ) noexcept
45+ BufferStore* bs ) noexcept
4546 : capacity_ (cap),
4647 size_ (len),
47- deleter_ {del}
48+ bufstore (bs)
4849 {}
49- ~Packet () {
50- deleter_ (this );
50+
51+ virtual ~Packet ()
52+ {
53+ if (bufstore)
54+ bufstore->release (this );
55+ else
56+ delete[] (uint8_t *) this ;
5157 }
5258
5359 /* * Get the buffer */
5460 BufferStore::buffer_t buffer () const noexcept
5561 { return (BufferStore::buffer_t ) buf_; }
5662
5763 /* * Get the network packet length - i.e. the number of populated bytes */
58- inline uint16_t size () const noexcept
64+ uint16_t size () const noexcept
5965 { return size_; }
6066
6167 /* * Get the size of the buffer. This is >= len(), usually MTU-size */
62- inline uint16_t capacity () const noexcept
68+ uint16_t capacity () const noexcept
6369 { return capacity_; }
6470
6571 void set_size (uint16_t new_size) noexcept {
@@ -77,27 +83,28 @@ namespace net {
7783 /* Add a packet to this packet chain. */
7884 void chain (Packet_ptr p) noexcept {
7985 if (!chain_) {
80- chain_ = p ;
81- last_ = p ;
86+ chain_ = std::move (p) ;
87+ last_ = chain_. get () ;
8288 } else {
83- last_->chain (p);
84- last_ = p->last_in_chain () ? p->last_in_chain () : p;
89+ auto * ptr = p.get ();
90+ last_->chain (std::move (p));
91+ last_ = ptr->last_in_chain () ? ptr->last_in_chain () : ptr;
8592 assert (last_);
8693 }
8794 }
8895
8996 /* Get the last packet in the chain */
90- Packet_ptr last_in_chain () noexcept
97+ Packet* last_in_chain () noexcept
9198 { return last_; }
9299
93100 /* Get the tail, i.e. chain minus the first element */
94- Packet_ptr tail () noexcept
95- { return chain_; }
101+ Packet* tail () noexcept
102+ { return chain_. get () ; }
96103
97104 /* Get the tail, and detach it from the head (for FIFO) */
98- Packet_ptr detach_tail () noexcept
105+ auto detach_tail () noexcept
99106 {
100- auto tail = chain_;
107+ auto tail = std::move ( chain_) ;
101108 chain_ = 0 ;
102109 return tail;
103110 }
@@ -107,11 +114,11 @@ namespace net {
107114 * For a UDPv6 packet, the payload location is the start of
108115 * the UDPv6 header, and so on
109116 */
110- inline void set_payload (BufferStore::buffer_t location) noexcept
117+ void set_payload (BufferStore::buffer_t location) noexcept
111118 { payload_ = location; }
112119
113120 /* * Get the payload of the packet */
114- inline BufferStore::buffer_t payload () const noexcept
121+ BufferStore::buffer_t payload () const noexcept
115122 { return payload_; }
116123
117124 /* *
@@ -120,21 +127,15 @@ namespace net {
120127 * Unfortunately, we can't upcast with std::static_pointer_cast
121128 * however, all classes derived from Packet should be good to use
122129 */
123- static Packet_ptr packet (Packet_ptr pckt) noexcept
124- { return *static_cast <Packet_ptr*>(&pckt); }
125-
126- // custom deleter for Packet used by network stack
127- void set_deleter (deleter_t cb) {
128- this ->deleter_ = cb;
129- }
130+ // static Packet_ptr packet(Packet_ptr pckt) noexcept
131+ // { return *static_cast<Packet_ptr*>(&pckt); }
130132
131133 // override delete to do nothing
132134 static void operator delete (void *) {}
133135
134136 private:
135- /* * Let's chain packets */
136- Packet_ptr chain_ {0 };
137- Packet_ptr last_ {0 };
137+ Packet_ptr chain_ {nullptr };
138+ Packet* last_ {nullptr };
138139
139140 /* * Default constructor Deleted. See Packet(Packet&). */
140141 Packet () = delete ;
@@ -158,16 +159,12 @@ namespace net {
158159
159160 uint16_t capacity_;
160161 uint16_t size_;
162+ BufferStore* bufstore;
161163 ip4::Addr next_hop4_;
162- deleter_t deleter_;
163164 BufferStore::buffer_t payload_ {nullptr };
164165 BufferStore::buffer_t buf_[0 ];
165166 }; // < class Packet
166167
167- inline void default_packet_deleter (void * p) {
168- delete (Packet*) p;
169- }
170-
171168} // < namespace net
172169
173170#endif
0 commit comments