From ad01cda727d63e3755f14bedd7be6c097e4585fa Mon Sep 17 00:00:00 2001 From: Ivan Morozko Date: Wed, 25 Dec 2024 00:33:02 +0400 Subject: [PATCH] Replace variable length arrays with std::vector (#1662) Variable Length Arrays, while widely supported as compiler extensions are not part of the C++ standard. Replaced them with std::vector. --- Examples/DpdkExample-FilterTraffic/main.cpp | 4 ++-- Examples/PfRingExample-FilterTraffic/main.cpp | 8 ++++---- Pcap++/src/DpdkDevice.cpp | 20 +++++++++---------- Pcap++/src/DpdkDeviceList.cpp | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Examples/DpdkExample-FilterTraffic/main.cpp b/Examples/DpdkExample-FilterTraffic/main.cpp index c48e865677..be72a1ed81 100644 --- a/Examples/DpdkExample-FilterTraffic/main.cpp +++ b/Examples/DpdkExample-FilterTraffic/main.cpp @@ -162,7 +162,7 @@ void listDpdkPorts() void prepareCoreConfiguration(std::vector& dpdkDevicesToUse, std::vector& coresToUse, bool writePacketsToDisk, const std::string& packetFilePath, pcpp::DpdkDevice* sendPacketsTo, - AppWorkerConfig workerConfigArr[], int workerConfigArrLen, uint16_t rxQueues) + std::vector& workerConfigArr, int workerConfigArrLen, uint16_t rxQueues) { // create a list of pairs of DpdkDevice and RX queues for all RX queues in all requested devices int totalNumOfRxQueues = 0; @@ -588,7 +588,7 @@ int main(int argc, char* argv[]) } // prepare configuration for every core - AppWorkerConfig workerConfigArr[coresToUse.size()]; + std::vector workerConfigArr(coresToUse.size()); prepareCoreConfiguration(dpdkDevicesToUse, coresToUse, writePacketsToDisk, packetFilePath, sendPacketsTo, workerConfigArr, coresToUse.size(), rxQueues); diff --git a/Examples/PfRingExample-FilterTraffic/main.cpp b/Examples/PfRingExample-FilterTraffic/main.cpp index e6c6a38942..f070fceb67 100644 --- a/Examples/PfRingExample-FilterTraffic/main.cpp +++ b/Examples/PfRingExample-FilterTraffic/main.cpp @@ -406,7 +406,7 @@ int main(int argc, char* argv[]) int threadCount = 0; // create an array of packet stats with the size of all machine cores - PacketStats packetStatsArr[totalNumOfCores]; + std::vector packetStatsArr(totalNumOfCores); // init each packet stats instance with an illegal core ID for (int coreId = 0; coreId < totalNumOfCores; coreId++) @@ -430,7 +430,7 @@ int main(int argc, char* argv[]) PacketMatchingEngine matchingEngine(srcIPToMatch, dstIPToMatch, srcPortToMatch, dstPortToMatch, protocolToMatch); // create a flow table for each core - std::unordered_map flowTables[totalNumOfCores]; + std::vector> flowTables(totalNumOfCores); pcpp::PcapFileWriterDevice** pcapWriters = nullptr; @@ -463,9 +463,9 @@ int main(int argc, char* argv[]) // prepare packet capture configuration CaptureThreadArgs args; - args.packetStatArr = packetStatsArr; + args.packetStatArr = packetStatsArr.data(); args.matchingEngine = &matchingEngine; - args.flowTables = flowTables; + args.flowTables = flowTables.data(); args.sendPacketsTo = sendPacketsToIface; args.pcapWriters = pcapWriters; diff --git a/Pcap++/src/DpdkDevice.cpp b/Pcap++/src/DpdkDevice.cpp index bb4b025733..7aa7b7e171 100644 --- a/Pcap++/src/DpdkDevice.cpp +++ b/Pcap++/src/DpdkDevice.cpp @@ -929,8 +929,8 @@ namespace pcpp return 0; } - struct rte_mbuf* mBufArray[rawPacketArrLength]; - uint16_t packetsReceived = rte_eth_rx_burst(m_Id, rxQueueId, mBufArray, rawPacketArrLength); + std::vector mBufArray(rawPacketArrLength); + uint16_t packetsReceived = rte_eth_rx_burst(m_Id, rxQueueId, mBufArray.data(), rawPacketArrLength); if (unlikely(!packetsReceived)) { @@ -972,8 +972,8 @@ namespace pcpp return 0; } - struct rte_mbuf* mBufArray[packetsArrLength]; - uint16_t packetsReceived = rte_eth_rx_burst(m_Id, rxQueueId, mBufArray, packetsArrLength); + std::vector mBufArray(packetsArrLength); + uint16_t packetsReceived = rte_eth_rx_burst(m_Id, rxQueueId, mBufArray.data(), packetsArrLength); if (unlikely(!packetsReceived)) { @@ -1133,9 +1133,9 @@ namespace pcpp uint16_t DpdkDevice::sendPackets(Packet** packetsArr, uint16_t arrLength, uint16_t txQueueId, bool useTxBuffer) { - rte_mbuf* mBufArr[arrLength]; + std::vector mBufArr(arrLength); MBufRawPacketVector mBufVec; - MBufRawPacket* mBufRawPacketArr[arrLength]; + std::vector mBufRawPacketArr(arrLength); for (size_t i = 0; i < arrLength; i++) { @@ -1162,7 +1162,7 @@ namespace pcpp } uint16_t packetsSent = - sendPacketsInner(txQueueId, (void*)mBufArr, getNextPacketFromMBufArray, arrLength, useTxBuffer); + sendPacketsInner(txQueueId, (void*)mBufArr.data(), getNextPacketFromMBufArray, arrLength, useTxBuffer); bool needToFreeMbuf = (!useTxBuffer && (packetsSent != arrLength)); for (int index = 0; index < arrLength; index++) @@ -1174,8 +1174,8 @@ namespace pcpp uint16_t DpdkDevice::sendPackets(RawPacketVector& rawPacketsVec, uint16_t txQueueId, bool useTxBuffer) { size_t vecSize = rawPacketsVec.size(); - rte_mbuf* mBufArr[vecSize]; - MBufRawPacket* mBufRawPacketArr[vecSize]; + std::vector mBufArr(vecSize); + std::vector mBufRawPacketArr(vecSize); MBufRawPacketVector mBufVec; int mBufIndex = 0; @@ -1204,7 +1204,7 @@ namespace pcpp } uint16_t packetsSent = - sendPacketsInner(txQueueId, (void*)mBufArr, getNextPacketFromMBufArray, vecSize, useTxBuffer); + sendPacketsInner(txQueueId, (void*)mBufArr.data(), getNextPacketFromMBufArray, vecSize, useTxBuffer); bool needToFreeMbuf = (!useTxBuffer && (packetsSent != vecSize)); for (size_t index = 0; index < rawPacketsVec.size(); index++) diff --git a/Pcap++/src/DpdkDeviceList.cpp b/Pcap++/src/DpdkDeviceList.cpp index f41d1bba9e..9331734707 100644 --- a/Pcap++/src/DpdkDeviceList.cpp +++ b/Pcap++/src/DpdkDeviceList.cpp @@ -121,7 +121,7 @@ namespace pcpp // Should be equal to the number of static params initDpdkArgc += 7; - std::string dpdkParamsArray[initDpdkArgc]; + std::vector dpdkParamsArray(initDpdkArgc); initDpdkArgvBuffer = new char*[initDpdkArgc]; i = 0; while (dpdkParamsStream.good() && i < initDpdkArgc)