Skip to content

Commit

Permalink
Removed VLA usage as it is not part of the C++ standard. (#1661)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimi1010 authored Dec 25, 2024
1 parent ad01cda commit 8b20f90
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Pcap++/src/XdpDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <net/if.h>
#include <sys/mman.h>
#include <unistd.h>
#include <vector>
#include <functional>
#include <algorithm>
#include <poll.h>
Expand Down Expand Up @@ -144,6 +145,7 @@ namespace pcpp
pollfd pollFds[1];
pollFds[0] = { .fd = xsk_socket__fd(socketInfo->xsk), .events = POLLIN };

std::vector<RawPacket> receiveBuffer;
while (m_ReceivingPackets)
{
checkCompletionRing();
Expand All @@ -169,14 +171,16 @@ namespace pcpp

uint32_t receivedPacketsCount = xsk_ring_cons__peek(&socketInfo->rx, m_Config->rxTxBatchSize, &rxId);

if (!receivedPacketsCount)
if (receivedPacketsCount == 0)
{
continue;
}

m_Stats.rxPackets += receivedPacketsCount;

RawPacket rawPacketsArr[receivedPacketsCount];
// Reserves at least enough memory to hold all the received packets. No-op if capacity is enough.
// May hold more memory than needed if a previous cycle has reserved more already.
receiveBuffer.reserve(receivedPacketsCount);

for (uint32_t i = 0; i < receivedPacketsCount; i++)
{
Expand All @@ -186,14 +190,15 @@ namespace pcpp
auto data = m_Umem->getDataPtr(addr);
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
rawPacketsArr[i].initWithRawData(data, static_cast<int>(len), ts);
// Initializes the RawPacket directly into the buffer.
receiveBuffer.emplace_back(data, static_cast<int>(len), ts, false);

m_Stats.rxBytes += len;

m_Umem->freeFrame(addr);
}

onPacketsArrive(rawPacketsArr, receivedPacketsCount, this, onPacketsArriveUserCookie);
onPacketsArrive(receiveBuffer.data(), receiveBuffer.size(), this, onPacketsArriveUserCookie);

xsk_ring_cons__release(&socketInfo->rx, receivedPacketsCount);
m_Stats.rxRingId = rxId + receivedPacketsCount;
Expand All @@ -202,6 +207,9 @@ namespace pcpp
{
m_ReceivingPackets = false;
}

// Clears the receive buffer.
receiveBuffer.clear();
}

return true;
Expand Down

0 comments on commit 8b20f90

Please sign in to comment.