Skip to content

Commit 28dc27d

Browse files
authored
Updated DpdkDeviceList to use internal::DeviceListBase. (#1796)
* Changed PointerVector API to return `T*` even when the vector itself is `const` as modifications to the pointee object would not modify the vector itself. * Updated DpdkDeviceList to use DeviceListBase.
1 parent 27b5779 commit 28dc27d

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed

Common++/header/PointerVector.h

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -218,25 +218,13 @@ namespace pcpp
218218
}
219219

220220
/// @return A pointer of the first element in the vector
221-
T* front()
222-
{
223-
return m_Vector.front();
224-
}
225-
226-
/// @return A pointer to the first element in the vector
227-
T const* front() const
221+
T* front() const
228222
{
229223
return m_Vector.front();
230224
}
231225

232226
/// @return A pointer to the last element in the vector
233-
T* back()
234-
{
235-
return m_Vector.back();
236-
}
237-
238-
/// @return A pointer to the last element in the vector.
239-
T const* back() const
227+
T* back() const
240228
{
241229
return m_Vector.back();
242230
}
@@ -297,15 +285,7 @@ namespace pcpp
297285
/// Return a pointer to the element in a certain index
298286
/// @param[in] index The index to retrieve the element from
299287
/// @return The element at the specified position in the vector
300-
T* at(int index)
301-
{
302-
return m_Vector.at(index);
303-
}
304-
305-
/// Return a const pointer to the element in a certain index
306-
/// @param[in] index The index to retrieve the element from
307-
/// @return The element at the specified position in the vector
308-
const T* at(int index) const
288+
T* at(int index) const
309289
{
310290
return m_Vector.at(index);
311291
}

Pcap++/header/DpdkDeviceList.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "SystemUtils.h"
66
#include "DpdkDevice.h"
7+
#include "DeviceListBase.h"
78
#include "Logger.h"
89
#include <vector>
910

@@ -54,17 +55,19 @@ namespace pcpp
5455
/// once in every application at its startup process
5556
/// - it contains the list of DpdkDevice instances and enables access to them
5657
/// - it has methods to start and stop worker threads. See more details in startDpdkWorkerThreads()
57-
class DpdkDeviceList
58+
class DpdkDeviceList : internal::DeviceListBase<DpdkDevice>
5859
{
5960
friend class KniDeviceList;
6061

6162
private:
63+
using Base = internal::DeviceListBase<DpdkDevice>;
64+
6265
bool m_IsInitialized;
6366
static bool m_IsDpdkInitialized;
6467
static uint32_t m_MBufPoolSizePerDevice;
6568
static uint16_t m_MBufDataSize;
6669
static CoreMask m_CoreMask;
67-
std::vector<DpdkDevice*> m_DpdkDeviceList;
70+
std::vector<DpdkDevice*> m_DpdkDeviceListView;
6871
std::vector<DpdkWorkerThread*> m_WorkerThreads;
6972

7073
DpdkDeviceList();
@@ -139,7 +142,7 @@ namespace pcpp
139142
/// @return A vector of all DpdkDevice instances
140143
const std::vector<DpdkDevice*>& getDpdkDeviceList() const
141144
{
142-
return m_DpdkDeviceList;
145+
return m_DpdkDeviceListView;
143146
}
144147

145148
/// @return DPDK master core which is the core that initializes the application

Pcap++/src/DpdkDeviceList.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <sstream>
3838
#include <iomanip>
3939
#include <string>
40+
#include <memory>
4041
#include <algorithm>
4142
#include <unistd.h>
4243

@@ -63,12 +64,7 @@ namespace pcpp
6364

6465
DpdkDeviceList::~DpdkDeviceList()
6566
{
66-
for (auto dev : m_DpdkDeviceList)
67-
{
68-
delete dev;
69-
}
70-
71-
m_DpdkDeviceList.clear();
67+
m_DpdkDeviceListView.clear();
7268
}
7369

7470
bool DpdkDeviceList::initDpdk(CoreMask coreMask, uint32_t mBufPoolSizePerDevice, uint16_t mBufDataSize,
@@ -194,13 +190,17 @@ namespace pcpp
194190
// Initialize a DpdkDevice per port
195191
for (int i = 0; i < numOfPorts; i++)
196192
{
197-
DpdkDevice* newDevice = new DpdkDevice(i, mBufPoolSizePerDevice, mBufDataSize);
193+
auto newDevice = std::unique_ptr<DpdkDevice>(new DpdkDevice(i, mBufPoolSizePerDevice, mBufDataSize));
198194
PCPP_LOG_DEBUG("DpdkDevice #" << i << ": Name='" << newDevice->getDeviceName() << "', PCI-slot='"
199195
<< newDevice->getPciAddress() << "', PMD='" << newDevice->getPMDName()
200196
<< "', MAC Addr='" << newDevice->getMacAddress() << "'");
201-
m_DpdkDeviceList.push_back(newDevice);
197+
m_DeviceList.pushBack(std::move(newDevice));
202198
}
203199

200+
// Copy the devices into the view vector.
201+
m_DpdkDeviceListView.resize(m_DeviceList.size());
202+
std::copy(m_DeviceList.begin(), m_DeviceList.end(), m_DpdkDeviceListView.begin());
203+
204204
m_IsInitialized = true;
205205
return true;
206206
}
@@ -213,12 +213,12 @@ namespace pcpp
213213
return nullptr;
214214
}
215215

216-
if ((uint32_t)portId >= m_DpdkDeviceList.size())
216+
if ((uint32_t)portId >= m_DeviceList.size())
217217
{
218218
return nullptr;
219219
}
220220

221-
return m_DpdkDeviceList.at(portId);
221+
return m_DeviceList.at(portId);
222222
}
223223

224224
DpdkDevice* DpdkDeviceList::getDeviceByPciAddress(const std::string& pciAddr) const
@@ -229,10 +229,10 @@ namespace pcpp
229229
return nullptr;
230230
}
231231

232-
auto devIter = std::find_if(m_DpdkDeviceList.begin(), m_DpdkDeviceList.end(),
232+
auto devIter = std::find_if(m_DeviceList.begin(), m_DeviceList.end(),
233233
[&pciAddr](const DpdkDevice* dev) { return dev->getPciAddress() == pciAddr; });
234234

235-
if (devIter == m_DpdkDeviceList.end())
235+
if (devIter == m_DeviceList.end())
236236
{
237237
PCPP_LOG_DEBUG("Found no DPDK devices with PCI address '" << pciAddr << "'");
238238
return nullptr;

0 commit comments

Comments
 (0)