bufArray.hpp
Go to the documentation of this file.
1 #ifndef BUFARRAY_HPP
2 #define BUFARRAY_HPP
3 
4 #include <cassert>
5 #include <cstdint>
6 #include <stdexcept>
7 #include <utility>
8 #include <vector>
9 
14 /*
15 template <typename Packet> struct BufArray : public std::pair<Packet **, unsigned int> {
16 
17  BufArray(Packet **pkts, unsigned int numPkts)
18  : std::pair<Packet **, unsigned int>(pkts, numPkts){};
19 
20  Packet **getArray() { return this->first; }
21  unsigned int getNum() { return this->second; }
22  void setNum(unsigned int num) { this->second = num; }
23 
24  void addPkt(Packet *pkt) {
25  this->getArray()[this->getNum()] = pkt;
26  this->second += 1;
27  }
28 
29  Packet *operator[](unsigned int idx) { return this->getArray()[idx]; }
30 
31  Packet **begin() { return &this->getArray()[0]; }
32  Packet **end() { return &this->getArray()[this->getNum()]; }
33 };
34 */
35 
42 template <typename Packet> class BufArray {
43 private:
44  Packet **pkts;
45  std::vector<bool> sendMask;
46  uint32_t numBufs;
47  uint32_t numSlots;
48  bool fromLua;
49 
50 public:
57  BufArray(Packet **pkts, uint32_t numPkts, bool fromLua = false) {
58  this->pkts = pkts;
59  this->fromLua = fromLua;
60 
61  if (numPkts == 0) {
62  std::abort();
63  throw std::runtime_error(
64  "BufArray::BufArray() Please use an array with at least one slot");
65  }
66 
67  // In the beginning, the pkts** fits exactly
68  numBufs = numPkts;
69  numSlots = numPkts;
70 
71  sendMask.resize(numPkts);
72  for (uint32_t i = 0; i < numPkts; i++) {
73  sendMask[i] = true;
74  }
75  };
76 
78  if (!fromLua) {
79  free(pkts);
80  }
81  }
82 
87  void markDropPkt(uint32_t pktIdx) {
88  assert(pktIdx < numBufs);
89  sendMask[pktIdx] = false;
90  };
91 
96  void markDropPkt(Packet *pkt) {
97  assert(pkt != nullptr);
98  for (uint32_t pktIdx = 0; pktIdx < numBufs; pktIdx++) {
99  if (pkt == pkts[pktIdx]) {
100  sendMask[pktIdx] = false;
101  }
102  }
103  };
104 
109  void markSendPkt(uint32_t pktIdx) {
110  assert(pktIdx < numBufs);
111  sendMask[pktIdx] = true;
112  };
113 
120  void addPkt(Packet *pkt) {
121  // Do we need to grow?
122  if (numBufs == numSlots) {
123  // The +1 is for empty BufArrays
124  Packet **newPkts =
125  reinterpret_cast<Packet **>(malloc((sizeof(Packet *) * numSlots * 2)));
126  for (uint32_t i = 0; i < numSlots; i++) {
127  newPkts[i] = pkts[i];
128  }
129 
130  // If the old memory is not from Lua, free it
131  if (!fromLua) {
132  free(pkts);
133  }
134  numSlots *= 2;
135 
136  sendMask.resize(numSlots);
137 
138  pkts = newPkts;
139 
140  // Now we allocated our own memory
141  fromLua = false;
142  }
143  sendMask[numBufs] = true;
144  pkts[numBufs++] = pkt;
145  };
146 
151  uint32_t getSendCount() const {
152  uint32_t count = 0;
153  for (auto i : sendMask) {
154  if (i) {
155  count++;
156  }
157  }
158 
159  return count;
160  };
161 
166  uint32_t getFreeCount() const {
167  uint32_t sendCount = getSendCount();
168  return numBufs - sendCount;
169  }
170 
175  void getSendBufs(Packet **sendBufs) const {
176  uint32_t curSendBufs = 0;
177  uint32_t curPkts = 0;
178  uint32_t sendCount = getSendCount();
179 
180  while (curSendBufs < sendCount) {
181  if (sendMask[curPkts]) {
182  sendBufs[curSendBufs++] = pkts[curPkts++];
183  } else {
184  curPkts++;
185  }
186  }
187  }
188 
193  void getFreeBufs(Packet **freeBufs) const {
194  uint32_t curFreeBufs = 0;
195  uint32_t curPkts = 0;
196  uint32_t freeCount = getFreeCount();
197 
198  while (curFreeBufs < freeCount) {
199  if (!sendMask[curPkts]) {
200  freeBufs[curFreeBufs++] = pkts[curPkts++];
201  } else {
202  curPkts++;
203  }
204  }
205  }
206 
211  uint32_t getTotalCount() const { return numBufs; }
212 
213  Packet *operator[](unsigned int idx) const { return pkts[idx]; }
214 
215  class iterator {
216  private:
217  friend BufArray;
218  BufArray<Packet> *ba;
219  uint32_t idx;
220  iterator(BufArray<Packet> *ba, uint32_t idx) : ba(ba), idx(idx) {}
221 
222  public:
223  iterator(const iterator &it) : ba(it.ba), idx(it.idx){};
224 
225  iterator operator++() { idx++; };
226 
227  bool operator==(const iterator &it) const {
228  if (this->idx == it.idx) {
229  return true;
230  } else {
231  return false;
232  }
233  }
234 
235  bool operator!=(const iterator &it) const { return !((*this) == it); }
236 
237  Packet *operator->() { return ba->pkts[idx]; }
238  Packet *operator*() { return ba->pkts[idx]; }
239  };
240 
241  iterator begin() { return iterator(this, 0); }
242  iterator end() { return iterator(this, numBufs); }
243 };
244 
245 #endif /* BUFARRAY_HPP */
iterator operator++()
Definition: bufArray.hpp:225
Packet * operator->()
Definition: bufArray.hpp:237
void markDropPkt(uint32_t pktIdx)
Mark one packet as drop.
Definition: bufArray.hpp:87
void markSendPkt(uint32_t pktIdx)
Mark one packet as send.
Definition: bufArray.hpp:109
uint32_t getFreeCount() const
Get the number of packets currently marked as free.
Definition: bufArray.hpp:166
iterator end()
Definition: bufArray.hpp:242
iterator(const iterator &it)
Definition: bufArray.hpp:223
BufArray(Packet **pkts, uint32_t numPkts, bool fromLua=false)
Constructor.
Definition: bufArray.hpp:57
Packet * operator*()
Definition: bufArray.hpp:238
uint32_t getTotalCount() const
Get the number of all packets in the BufArray.
Definition: bufArray.hpp:211
void markDropPkt(Packet *pkt)
Mark one packet as drop.
Definition: bufArray.hpp:96
Packet * operator[](unsigned int idx) const
Definition: bufArray.hpp:213
uint32_t getSendCount() const
Get the number of packets currently marked as send.
Definition: bufArray.hpp:151
void addPkt(Packet *pkt)
Add one packet to the BufArray.
Definition: bufArray.hpp:120
void getSendBufs(Packet **sendBufs) const
Get all the packets which are to be sent.
Definition: bufArray.hpp:175
~BufArray()
Definition: bufArray.hpp:77
iterator begin()
Definition: bufArray.hpp:241
void getFreeBufs(Packet **freeBufs) const
Get all the packets which are to be freed.
Definition: bufArray.hpp:193
Wrapper around MoonGen bufarrays.
Definition: bufArray.hpp:42
bool operator==(const iterator &it) const
Definition: bufArray.hpp:227
bool operator!=(const iterator &it) const
Definition: bufArray.hpp:235