Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use smart pointers for PcapLiveDeviceList's internal memory management. #1440

Conversation

Dimi1010
Copy link
Collaborator

@Dimi1010 Dimi1010 commented Jun 6, 2024

Part of #1431. Partially implements point 1.

This PR implements usage of smart pointers for managing memory of member variables.
Dynamic memory management of translation unit local variables will be handled in a different PR.

This implementation uses shared pointers to store the live devices in preparation for point 4 of #1431.

@Dimi1010 Dimi1010 requested a review from seladb as a code owner June 6, 2024 19:51
@@ -23,20 +24,26 @@ namespace pcpp
class PcapLiveDeviceList
{
private:
std::vector<PcapLiveDevice*> m_LiveDeviceList;
std::vector<std::shared_ptr<PcapLiveDevice>> m_LiveDeviceList;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a vector of shared_ptr and not unique_ptr? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation uses shared pointers to store the live devices in preparation for point 4 of #1431.

From the PR description. I plan on having the API return shared pointers.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I fully understand point 4, but I'm not sure there's a good reason to unify the APIs of PcapLiveDeviceList and PcapRemoteDeviceList, from multiple reasons:

  • PcapLiveDeviceList is a singleton because it finds all the interfaces on the machine. However PcapRemoteDeviceList is not a singleton because it encapsulates remote devices on other machines so a user can have multiple instances of it. The API is quite different so not sure it's a good idea to merge it
  • Remote devices are a niche that isn't used a lot, whereas pcap devices is probably the most popular feature of PcapPlusPlus. I wouldn't mix between them

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, one of the reasons I started thinking about it is that both PcapLiveDeviceList and PcapRemoteDeviceList share a couple similarities and have some code duplication.

  • Both store their devices in vector<DevicePtr>
  • The methods getPcapLiveDeviceByIp and getPcapRemoteDeviceByIP have practically duplicate implementation.

One of the reasons for unifying the API is having an internal base class template<DeviceType> PcapLiveDeviceListBase (name subject to change) that implements the getPcapDeviceByIP methods that currently have duplicate implementation.

The current methods could still be kept and just call the new functions too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, even if the API is not unified, I still have the proposal to get the API to return shared_ptr instead of raw pointers so you don't suddenly get invalid pointers. (refresh, or in case of RemoteDeviceList just the list going out of scope).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be honest, I wouldn't spend time on it because although there is some code duplication, it's not a lot and we can keep the code simple. If you really insist we can discuss it more...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, ok then. Gonna shelve point 4 for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PcapLiveDeviceList is a singleton

@seladb Btw, if the class is a singleton, why does it have a clone method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to unique_ptr. ad77379

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PcapLiveDeviceList is a singleton

@seladb Btw, if the class is a singleton, why does it have a clone method?

I agree this API is a bit weird 🙈
The reason is that users wanted to capture traffic from the same interface more than once, here is the GitHub issue: #766

The idea was to allow cloning a live device, or to allow cloning all devices...

Pcap++/src/PcapLiveDeviceList.cpp Outdated Show resolved Hide resolved
@Dimi1010 Dimi1010 requested a review from seladb June 7, 2024 10:23
Copy link
Collaborator

@tigercosmos tigercosmos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tigercosmos
Copy link
Collaborator

@Dimi1010 please resolve the conflict.

@tigercosmos tigercosmos merged commit 2f49cf8 into seladb:dev Jun 8, 2024
39 checks passed
@Dimi1010 Dimi1010 deleted the feature/pcap-live-dev-list-internal-smart-pointers branch June 8, 2024 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants