dev: Use shared_ptr for EthPacketData

This patch transitions the EthPacketData from the ad-hoc
RefCountingPtr to the c++11 shared_ptr. There are no changes in
behaviour, and the code modifications are mainly replacing "new" with
"make_shared".

The bool casting operator for the shared_ptr is explicit, and we must
therefore either cast it, compare it to NULL (p != nullptr), double
negate it (!!p) or do a (p ? true : false).
This commit is contained in:
Andreas Hansson 2014-10-16 05:49:46 -04:00
parent 4e67ab6663
commit ad3f75dc81
8 changed files with 26 additions and 24 deletions

View file

@ -182,7 +182,7 @@ class EthPtr
const EthPacketPtr packet() const { return p; } const EthPacketPtr packet() const { return p; }
EthPacketPtr packet() { return p; } EthPacketPtr packet() { return p; }
bool operator!() const { return !p; } bool operator!() const { return !p; }
operator bool() const { return p; } operator bool() const { return (p != nullptr); }
int off() const { return 0; } int off() const { return 0; }
int pstart() const { return off() + ((const EthHdr*)p->data)->size(); } int pstart() const { return off() + ((const EthHdr*)p->data)->size(); }
}; };
@ -324,7 +324,7 @@ class IpPtr
const EthPacketPtr packet() const { return p; } const EthPacketPtr packet() const { return p; }
EthPacketPtr packet() { return p; } EthPacketPtr packet() { return p; }
bool operator!() const { return !p; } bool operator!() const { return !p; }
operator bool() const { return p; } operator bool() const { return (p != nullptr); }
int off() const { return (sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); } int off() const { return (sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); }
int pstart() const { return (off() + get()->size()); } int pstart() const { return (off() + get()->size()); }
}; };
@ -440,7 +440,7 @@ class Ip6Ptr
const EthPacketPtr packet() const { return p; } const EthPacketPtr packet() const { return p; }
EthPacketPtr packet() { return p; } EthPacketPtr packet() { return p; }
bool operator!() const { return !p; } bool operator!() const { return !p; }
operator bool() const { return p; } operator bool() const { return (p != nullptr); }
int off() const { return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); } int off() const { return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); }
int pstart() const { return off() + get()->size(); } int pstart() const { return off() + get()->size(); }
}; };
@ -576,7 +576,7 @@ class TcpPtr
const EthPacketPtr packet() const { return p; } const EthPacketPtr packet() const { return p; }
EthPacketPtr packet() { return p; } EthPacketPtr packet() { return p; }
bool operator!() const { return !p; } bool operator!() const { return !p; }
operator bool() const { return p; } operator bool() const { return (p != nullptr); }
int off() const { return _off; } int off() const { return _off; }
int pstart() const { return off() + get()->size(); } int pstart() const { return off() + get()->size(); }
}; };
@ -671,7 +671,7 @@ class UdpPtr
const EthPacketPtr packet() const { return p; } const EthPacketPtr packet() const { return p; }
EthPacketPtr packet() { return p; } EthPacketPtr packet() { return p; }
bool operator!() const { return !p; } bool operator!() const { return !p; }
operator bool() const { return p; } operator bool() const { return (p != nullptr); }
int off() const { return _off; } int off() const { return _off; }
int pstart() const { return off() + get()->size(); } int pstart() const { return off() + get()->size(); }
}; };

View file

@ -195,7 +195,7 @@ EtherLink::Link::transmit(EthPacketPtr pkt)
void void
EtherLink::Link::serialize(const string &base, ostream &os) EtherLink::Link::serialize(const string &base, ostream &os)
{ {
bool packet_exists = packet; bool packet_exists = packet != nullptr;
paramOut(os, base + ".packet_exists", packet_exists); paramOut(os, base + ".packet_exists", packet_exists);
if (packet_exists) if (packet_exists)
packet->serialize(base + ".packet", os); packet->serialize(base + ".packet", os);
@ -216,7 +216,7 @@ EtherLink::Link::unserialize(const string &base, Checkpoint *cp,
bool packet_exists; bool packet_exists;
paramIn(cp, section, base + ".packet_exists", packet_exists); paramIn(cp, section, base + ".packet_exists", packet_exists);
if (packet_exists) { if (packet_exists) {
packet = new EthPacketData(16384); packet = make_shared<EthPacketData>(16384);
packet->unserialize(base + ".packet", cp, section); packet->unserialize(base + ".packet", cp, section);
} }
@ -273,7 +273,7 @@ LinkDelayEvent::unserialize(Checkpoint *cp, const string &section,
link = parent->link[number]; link = parent->link[number];
packet = new EthPacketData(16384); packet = make_shared<EthPacketData>(16384);
packet->unserialize("packet", cp, section); packet->unserialize("packet", cp, section);
} }

View file

@ -40,14 +40,13 @@
#include <iosfwd> #include <iosfwd>
#include <memory> #include <memory>
#include "base/refcnt.hh"
#include "base/types.hh" #include "base/types.hh"
/* /*
* Reference counted class containing ethernet packet data * Reference counted class containing ethernet packet data
*/ */
class Checkpoint; class Checkpoint;
class EthPacketData : public RefCounted class EthPacketData
{ {
public: public:
/* /*
@ -81,6 +80,6 @@ class EthPacketData : public RefCounted
const std::string &section); const std::string &section);
}; };
typedef RefCountingPtr<EthPacketData> EthPacketPtr; typedef std::shared_ptr<EthPacketData> EthPacketPtr;
#endif // __ETHERPKT_HH__ #endif // __ETHERPKT_HH__

View file

@ -235,7 +235,7 @@ EtherTap::process(int revent)
while (data_len != 0 && buffer_offset >= data_len + sizeof(uint32_t)) { while (data_len != 0 && buffer_offset >= data_len + sizeof(uint32_t)) {
EthPacketPtr packet; EthPacketPtr packet;
packet = new EthPacketData(data_len); packet = make_shared<EthPacketData>(data_len);
packet->length = data_len; packet->length = data_len;
memcpy(packet->data, data, data_len); memcpy(packet->data, data, data_len);

View file

@ -41,6 +41,7 @@
*/ */
#include <algorithm> #include <algorithm>
#include <memory>
#include "base/inet.hh" #include "base/inet.hh"
#include "base/trace.hh" #include "base/trace.hh"
@ -2147,7 +2148,7 @@ IGbE::txStateMachine()
} }
if (!txPacket) { if (!txPacket) {
txPacket = new EthPacketData(16384); txPacket = std::make_shared<EthPacketData>(16384);
} }
if (!txDescCache.packetWaiting()) { if (!txDescCache.packetWaiting()) {
@ -2469,7 +2470,7 @@ IGbE::serialize(std::ostream &os)
rxFifo.serialize("rxfifo", os); rxFifo.serialize("rxfifo", os);
txFifo.serialize("txfifo", os); txFifo.serialize("txfifo", os);
bool txPktExists = txPacket; bool txPktExists = txPacket != nullptr;
SERIALIZE_SCALAR(txPktExists); SERIALIZE_SCALAR(txPktExists);
if (txPktExists) if (txPktExists)
txPacket->serialize("txpacket", os); txPacket->serialize("txpacket", os);
@ -2526,7 +2527,7 @@ IGbE::unserialize(Checkpoint *cp, const std::string &section)
bool txPktExists; bool txPktExists;
UNSERIALIZE_SCALAR(txPktExists); UNSERIALIZE_SCALAR(txPktExists);
if (txPktExists) { if (txPktExists) {
txPacket = new EthPacketData(16384); txPacket = std::make_shared<EthPacketData>(16384);
txPacket->unserialize("txpacket", cp, section); txPacket->unserialize("txpacket", cp, section);
} }

View file

@ -34,6 +34,7 @@
* DP83820 ethernet controller. Does not support priority queueing * DP83820 ethernet controller. Does not support priority queueing
*/ */
#include <deque> #include <deque>
#include <memory>
#include <string> #include <string>
#include "base/debug.hh" #include "base/debug.hh"
@ -51,6 +52,7 @@
// clang complains about std::set being overloaded with Packet::set if // clang complains about std::set being overloaded with Packet::set if
// we open up the entire namespace std // we open up the entire namespace std
using std::make_shared;
using std::min; using std::min;
using std::ostream; using std::ostream;
using std::string; using std::string;
@ -1676,7 +1678,7 @@ NSGigE::txKick()
case txFifoBlock: case txFifoBlock:
if (!txPacket) { if (!txPacket) {
DPRINTF(EthernetSM, "****starting the tx of a new packet****\n"); DPRINTF(EthernetSM, "****starting the tx of a new packet****\n");
txPacket = new EthPacketData(16384); txPacket = make_shared<EthPacketData>(16384);
txPacketBufPtr = txPacket->data; txPacketBufPtr = txPacket->data;
} }
@ -2185,7 +2187,7 @@ NSGigE::serialize(ostream &os)
/* /*
* Serialize the various helper variables * Serialize the various helper variables
*/ */
bool txPacketExists = txPacket; bool txPacketExists = txPacket != nullptr;
SERIALIZE_SCALAR(txPacketExists); SERIALIZE_SCALAR(txPacketExists);
if (txPacketExists) { if (txPacketExists) {
txPacket->length = txPacketBufPtr - txPacket->data; txPacket->length = txPacketBufPtr - txPacket->data;
@ -2194,7 +2196,7 @@ NSGigE::serialize(ostream &os)
SERIALIZE_SCALAR(txPktBufPtr); SERIALIZE_SCALAR(txPktBufPtr);
} }
bool rxPacketExists = rxPacket; bool rxPacketExists = rxPacket != nullptr;
SERIALIZE_SCALAR(rxPacketExists); SERIALIZE_SCALAR(rxPacketExists);
if (rxPacketExists) { if (rxPacketExists) {
rxPacket->serialize("rxPacket", os); rxPacket->serialize("rxPacket", os);
@ -2352,7 +2354,7 @@ NSGigE::unserialize(Checkpoint *cp, const std::string &section)
bool txPacketExists; bool txPacketExists;
UNSERIALIZE_SCALAR(txPacketExists); UNSERIALIZE_SCALAR(txPacketExists);
if (txPacketExists) { if (txPacketExists) {
txPacket = new EthPacketData(16384); txPacket = make_shared<EthPacketData>(16384);
txPacket->unserialize("txPacket", cp, section); txPacket->unserialize("txPacket", cp, section);
uint32_t txPktBufPtr; uint32_t txPktBufPtr;
UNSERIALIZE_SCALAR(txPktBufPtr); UNSERIALIZE_SCALAR(txPktBufPtr);
@ -2364,7 +2366,7 @@ NSGigE::unserialize(Checkpoint *cp, const std::string &section)
UNSERIALIZE_SCALAR(rxPacketExists); UNSERIALIZE_SCALAR(rxPacketExists);
rxPacket = 0; rxPacket = 0;
if (rxPacketExists) { if (rxPacketExists) {
rxPacket = new EthPacketData(16384); rxPacket = make_shared<EthPacketData>(16384);
rxPacket->unserialize("rxPacket", cp, section); rxPacket->unserialize("rxPacket", cp, section);
uint32_t rxPktBufPtr; uint32_t rxPktBufPtr;
UNSERIALIZE_SCALAR(rxPktBufPtr); UNSERIALIZE_SCALAR(rxPktBufPtr);

View file

@ -77,7 +77,7 @@ void
PacketFifoEntry::unserialize(const string &base, Checkpoint *cp, PacketFifoEntry::unserialize(const string &base, Checkpoint *cp,
const string &section) const string &section)
{ {
packet = new EthPacketData(16384); packet = make_shared<EthPacketData>(16384);
packet->unserialize(base + ".packet", cp, section); packet->unserialize(base + ".packet", cp, section);
paramIn(cp, section, base + ".slack", slack); paramIn(cp, section, base + ".slack", slack);
paramIn(cp, section, base + ".number", number); paramIn(cp, section, base + ".number", number);

View file

@ -1056,7 +1056,7 @@ Device::txKick()
assert(Regs::get_TxDone_Busy(vnic->TxDone)); assert(Regs::get_TxDone_Busy(vnic->TxDone));
if (!txPacket) { if (!txPacket) {
// Grab a new packet from the fifo. // Grab a new packet from the fifo.
txPacket = new EthPacketData(16384); txPacket = make_shared<EthPacketData>(16384);
txPacketOffset = 0; txPacketOffset = 0;
} }
@ -1403,7 +1403,7 @@ Device::serialize(std::ostream &os)
SERIALIZE_SCALAR(txState); SERIALIZE_SCALAR(txState);
SERIALIZE_SCALAR(txFull); SERIALIZE_SCALAR(txFull);
txFifo.serialize("txFifo", os); txFifo.serialize("txFifo", os);
bool txPacketExists = txPacket; bool txPacketExists = txPacket != nullptr;
SERIALIZE_SCALAR(txPacketExists); SERIALIZE_SCALAR(txPacketExists);
if (txPacketExists) { if (txPacketExists) {
txPacket->serialize("txPacket", os); txPacket->serialize("txPacket", os);
@ -1498,7 +1498,7 @@ Device::unserialize(Checkpoint *cp, const std::string &section)
UNSERIALIZE_SCALAR(txPacketExists); UNSERIALIZE_SCALAR(txPacketExists);
txPacket = 0; txPacket = 0;
if (txPacketExists) { if (txPacketExists) {
txPacket = new EthPacketData(16384); txPacket = make_shared<EthPacketData>(16384);
txPacket->unserialize("txPacket", cp, section); txPacket->unserialize("txPacket", cp, section);
UNSERIALIZE_SCALAR(txPacketOffset); UNSERIALIZE_SCALAR(txPacketOffset);
UNSERIALIZE_SCALAR(txPacketBytes); UNSERIALIZE_SCALAR(txPacketBytes);