From ad3f75dc81efc8818786c32d6190e3ed069b9fc6 Mon Sep 17 00:00:00 2001 From: Andreas Hansson Date: Thu, 16 Oct 2014 05:49:46 -0400 Subject: 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). --- src/base/inet.hh | 10 +++++----- src/dev/etherlink.cc | 6 +++--- src/dev/etherpkt.hh | 5 ++--- src/dev/ethertap.cc | 2 +- src/dev/i8254xGBe.cc | 7 ++++--- src/dev/ns_gige.cc | 12 +++++++----- src/dev/pktfifo.cc | 2 +- src/dev/sinic.cc | 6 +++--- 8 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/base/inet.hh b/src/base/inet.hh index 5130a072c..07bbdc3b4 100644 --- a/src/base/inet.hh +++ b/src/base/inet.hh @@ -182,7 +182,7 @@ class EthPtr const EthPacketPtr packet() const { return p; } EthPacketPtr packet() { return p; } bool operator!() const { return !p; } - operator bool() const { return p; } + operator bool() const { return (p != nullptr); } int off() const { return 0; } int pstart() const { return off() + ((const EthHdr*)p->data)->size(); } }; @@ -324,7 +324,7 @@ class IpPtr const EthPacketPtr packet() const { return p; } EthPacketPtr packet() { 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 pstart() const { return (off() + get()->size()); } }; @@ -440,7 +440,7 @@ class Ip6Ptr const EthPacketPtr packet() const { return p; } EthPacketPtr packet() { 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 pstart() const { return off() + get()->size(); } }; @@ -576,7 +576,7 @@ class TcpPtr const EthPacketPtr packet() const { return p; } EthPacketPtr packet() { return p; } bool operator!() const { return !p; } - operator bool() const { return p; } + operator bool() const { return (p != nullptr); } int off() const { return _off; } int pstart() const { return off() + get()->size(); } }; @@ -671,7 +671,7 @@ class UdpPtr const EthPacketPtr packet() const { return p; } EthPacketPtr packet() { return p; } bool operator!() const { return !p; } - operator bool() const { return p; } + operator bool() const { return (p != nullptr); } int off() const { return _off; } int pstart() const { return off() + get()->size(); } }; diff --git a/src/dev/etherlink.cc b/src/dev/etherlink.cc index 0117bb7c2..c5ef66d99 100644 --- a/src/dev/etherlink.cc +++ b/src/dev/etherlink.cc @@ -195,7 +195,7 @@ EtherLink::Link::transmit(EthPacketPtr pkt) void EtherLink::Link::serialize(const string &base, ostream &os) { - bool packet_exists = packet; + bool packet_exists = packet != nullptr; paramOut(os, base + ".packet_exists", packet_exists); if (packet_exists) packet->serialize(base + ".packet", os); @@ -216,7 +216,7 @@ EtherLink::Link::unserialize(const string &base, Checkpoint *cp, bool packet_exists; paramIn(cp, section, base + ".packet_exists", packet_exists); if (packet_exists) { - packet = new EthPacketData(16384); + packet = make_shared(16384); packet->unserialize(base + ".packet", cp, section); } @@ -273,7 +273,7 @@ LinkDelayEvent::unserialize(Checkpoint *cp, const string §ion, link = parent->link[number]; - packet = new EthPacketData(16384); + packet = make_shared(16384); packet->unserialize("packet", cp, section); } diff --git a/src/dev/etherpkt.hh b/src/dev/etherpkt.hh index c71d9cc30..16576d329 100644 --- a/src/dev/etherpkt.hh +++ b/src/dev/etherpkt.hh @@ -40,14 +40,13 @@ #include #include -#include "base/refcnt.hh" #include "base/types.hh" /* * Reference counted class containing ethernet packet data */ class Checkpoint; -class EthPacketData : public RefCounted +class EthPacketData { public: /* @@ -81,6 +80,6 @@ class EthPacketData : public RefCounted const std::string §ion); }; -typedef RefCountingPtr EthPacketPtr; +typedef std::shared_ptr EthPacketPtr; #endif // __ETHERPKT_HH__ diff --git a/src/dev/ethertap.cc b/src/dev/ethertap.cc index e14fd90c2..38ce4a2b3 100644 --- a/src/dev/ethertap.cc +++ b/src/dev/ethertap.cc @@ -235,7 +235,7 @@ EtherTap::process(int revent) while (data_len != 0 && buffer_offset >= data_len + sizeof(uint32_t)) { EthPacketPtr packet; - packet = new EthPacketData(data_len); + packet = make_shared(data_len); packet->length = data_len; memcpy(packet->data, data, data_len); diff --git a/src/dev/i8254xGBe.cc b/src/dev/i8254xGBe.cc index d86d7b486..af9734761 100644 --- a/src/dev/i8254xGBe.cc +++ b/src/dev/i8254xGBe.cc @@ -41,6 +41,7 @@ */ #include +#include #include "base/inet.hh" #include "base/trace.hh" @@ -2147,7 +2148,7 @@ IGbE::txStateMachine() } if (!txPacket) { - txPacket = new EthPacketData(16384); + txPacket = std::make_shared(16384); } if (!txDescCache.packetWaiting()) { @@ -2469,7 +2470,7 @@ IGbE::serialize(std::ostream &os) rxFifo.serialize("rxfifo", os); txFifo.serialize("txfifo", os); - bool txPktExists = txPacket; + bool txPktExists = txPacket != nullptr; SERIALIZE_SCALAR(txPktExists); if (txPktExists) txPacket->serialize("txpacket", os); @@ -2526,7 +2527,7 @@ IGbE::unserialize(Checkpoint *cp, const std::string §ion) bool txPktExists; UNSERIALIZE_SCALAR(txPktExists); if (txPktExists) { - txPacket = new EthPacketData(16384); + txPacket = std::make_shared(16384); txPacket->unserialize("txpacket", cp, section); } diff --git a/src/dev/ns_gige.cc b/src/dev/ns_gige.cc index f180b0ecd..239babe70 100644 --- a/src/dev/ns_gige.cc +++ b/src/dev/ns_gige.cc @@ -34,6 +34,7 @@ * DP83820 ethernet controller. Does not support priority queueing */ #include +#include #include #include "base/debug.hh" @@ -51,6 +52,7 @@ // clang complains about std::set being overloaded with Packet::set if // we open up the entire namespace std +using std::make_shared; using std::min; using std::ostream; using std::string; @@ -1676,7 +1678,7 @@ NSGigE::txKick() case txFifoBlock: if (!txPacket) { DPRINTF(EthernetSM, "****starting the tx of a new packet****\n"); - txPacket = new EthPacketData(16384); + txPacket = make_shared(16384); txPacketBufPtr = txPacket->data; } @@ -2185,7 +2187,7 @@ NSGigE::serialize(ostream &os) /* * Serialize the various helper variables */ - bool txPacketExists = txPacket; + bool txPacketExists = txPacket != nullptr; SERIALIZE_SCALAR(txPacketExists); if (txPacketExists) { txPacket->length = txPacketBufPtr - txPacket->data; @@ -2194,7 +2196,7 @@ NSGigE::serialize(ostream &os) SERIALIZE_SCALAR(txPktBufPtr); } - bool rxPacketExists = rxPacket; + bool rxPacketExists = rxPacket != nullptr; SERIALIZE_SCALAR(rxPacketExists); if (rxPacketExists) { rxPacket->serialize("rxPacket", os); @@ -2352,7 +2354,7 @@ NSGigE::unserialize(Checkpoint *cp, const std::string §ion) bool txPacketExists; UNSERIALIZE_SCALAR(txPacketExists); if (txPacketExists) { - txPacket = new EthPacketData(16384); + txPacket = make_shared(16384); txPacket->unserialize("txPacket", cp, section); uint32_t txPktBufPtr; UNSERIALIZE_SCALAR(txPktBufPtr); @@ -2364,7 +2366,7 @@ NSGigE::unserialize(Checkpoint *cp, const std::string §ion) UNSERIALIZE_SCALAR(rxPacketExists); rxPacket = 0; if (rxPacketExists) { - rxPacket = new EthPacketData(16384); + rxPacket = make_shared(16384); rxPacket->unserialize("rxPacket", cp, section); uint32_t rxPktBufPtr; UNSERIALIZE_SCALAR(rxPktBufPtr); diff --git a/src/dev/pktfifo.cc b/src/dev/pktfifo.cc index e5d67d5ec..efd8b083d 100644 --- a/src/dev/pktfifo.cc +++ b/src/dev/pktfifo.cc @@ -77,7 +77,7 @@ void PacketFifoEntry::unserialize(const string &base, Checkpoint *cp, const string §ion) { - packet = new EthPacketData(16384); + packet = make_shared(16384); packet->unserialize(base + ".packet", cp, section); paramIn(cp, section, base + ".slack", slack); paramIn(cp, section, base + ".number", number); diff --git a/src/dev/sinic.cc b/src/dev/sinic.cc index 7da70c482..798fb17ef 100644 --- a/src/dev/sinic.cc +++ b/src/dev/sinic.cc @@ -1056,7 +1056,7 @@ Device::txKick() assert(Regs::get_TxDone_Busy(vnic->TxDone)); if (!txPacket) { // Grab a new packet from the fifo. - txPacket = new EthPacketData(16384); + txPacket = make_shared(16384); txPacketOffset = 0; } @@ -1403,7 +1403,7 @@ Device::serialize(std::ostream &os) SERIALIZE_SCALAR(txState); SERIALIZE_SCALAR(txFull); txFifo.serialize("txFifo", os); - bool txPacketExists = txPacket; + bool txPacketExists = txPacket != nullptr; SERIALIZE_SCALAR(txPacketExists); if (txPacketExists) { txPacket->serialize("txPacket", os); @@ -1498,7 +1498,7 @@ Device::unserialize(Checkpoint *cp, const std::string §ion) UNSERIALIZE_SCALAR(txPacketExists); txPacket = 0; if (txPacketExists) { - txPacket = new EthPacketData(16384); + txPacket = make_shared(16384); txPacket->unserialize("txPacket", cp, section); UNSERIALIZE_SCALAR(txPacketOffset); UNSERIALIZE_SCALAR(txPacketBytes); -- cgit v1.2.3