table: don't use shared_ptr in FIB
refs #3164
Change-Id: I5b5eb47d60f6bf5b6389c32ac840f793767e4334
diff --git a/daemon/table/fib-entry.cpp b/daemon/table/fib-entry.cpp
index b2ea76c..cb37b0f 100644
--- a/daemon/table/fib-entry.cpp
+++ b/daemon/table/fib-entry.cpp
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology,
- * The University of Memphis
+ * Copyright (c) 2014-2016, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -34,40 +34,37 @@
}
NextHopList::iterator
-Entry::findNextHop(Face& face)
+Entry::findNextHop(const Face& face)
{
return std::find_if(m_nextHops.begin(), m_nextHops.end(),
[&face] (const NextHop& nexthop) {
- return nexthop.getFace().get() == &face;
+ return &nexthop.getFace() == &face;
});
}
bool
-Entry::hasNextHop(shared_ptr<Face> face) const
+Entry::hasNextHop(const Face& face) const
{
- return const_cast<Entry*>(this)->findNextHop(*face) != m_nextHops.end();
+ return const_cast<Entry*>(this)->findNextHop(face) != m_nextHops.end();
}
void
-Entry::addNextHop(shared_ptr<Face> face, uint64_t cost)
+Entry::addNextHop(Face& face, uint64_t cost)
{
- auto it = this->findNextHop(*face);
+ auto it = this->findNextHop(face);
if (it == m_nextHops.end()) {
- m_nextHops.push_back(fib::NextHop(face));
- it = m_nextHops.end();
- --it;
+ m_nextHops.emplace_back(face);
+ it = std::prev(m_nextHops.end());
}
- // now it refers to the NextHop for face
it->setCost(cost);
-
this->sortNextHops();
}
void
-Entry::removeNextHop(shared_ptr<Face> face)
+Entry::removeNextHop(const Face& face)
{
- auto it = this->findNextHop(*face);
+ auto it = this->findNextHop(face);
if (it != m_nextHops.end()) {
m_nextHops.erase(it);
}
diff --git a/daemon/table/fib-entry.hpp b/daemon/table/fib-entry.hpp
index 9106d9f..8f83266 100644
--- a/daemon/table/fib-entry.hpp
+++ b/daemon/table/fib-entry.hpp
@@ -67,37 +67,33 @@
bool
hasNextHops() const;
- /** \return whether there is a NextHop record for face
- *
- * \todo change parameter type to Face&
+ /** \return whether there is a NextHop record for \p face
*/
bool
- hasNextHop(shared_ptr<Face> face) const;
+ hasNextHop(const Face& face) const;
/** \brief adds a NextHop record
*
- * If a NextHop record for face already exists, its cost is updated.
- * \note shared_ptr is passed by value because this function will take shared ownership
+ * If a NextHop record for \p face already exists, its cost is updated.
*/
void
- addNextHop(shared_ptr<Face> face, uint64_t cost);
+ addNextHop(Face& face, uint64_t cost);
/** \brief removes a NextHop record
*
* If no NextHop record for face exists, do nothing.
- *
- * \todo change parameter type to Face&
*/
void
- removeNextHop(shared_ptr<Face> face);
+ removeNextHop(const Face& face);
private:
- /** @note This method is non-const because normal iterator is needed by callers.
+ /** \note This method is non-const because mutable iterators are needed by callers.
*/
NextHopList::iterator
- findNextHop(Face& face);
+ findNextHop(const Face& face);
- /// sorts the nexthop list
+ /** \brief sorts the nexthop list
+ */
void
sortNextHops();
diff --git a/daemon/table/fib-nexthop.cpp b/daemon/table/fib-nexthop.cpp
index 235fc79..e554362 100644
--- a/daemon/table/fib-nexthop.cpp
+++ b/daemon/table/fib-nexthop.cpp
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology,
- * The University of Memphis
+ * Copyright (c) 2014-2016, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -28,29 +28,11 @@
namespace nfd {
namespace fib {
-NextHop::NextHop(shared_ptr<Face> face)
- : m_face(face)
+NextHop::NextHop(Face& face)
+ : m_face(&face)
, m_cost(0)
{
}
-const shared_ptr<Face>&
-NextHop::getFace() const
-{
- return m_face;
-}
-
-void
-NextHop::setCost(uint64_t cost)
-{
- m_cost = cost;
-}
-
-uint64_t
-NextHop::getCost() const
-{
- return m_cost;
-}
-
} // namespace fib
} // namespace nfd
diff --git a/daemon/table/fib-nexthop.hpp b/daemon/table/fib-nexthop.hpp
index 9f7ed93..c8c006e 100644
--- a/daemon/table/fib-nexthop.hpp
+++ b/daemon/table/fib-nexthop.hpp
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology,
- * The University of Memphis
+ * Copyright (c) 2014-2016, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -39,19 +39,28 @@
{
public:
explicit
- NextHop(shared_ptr<Face> face);
+ NextHop(Face& face);
- const shared_ptr<Face>&
- getFace() const;
-
- void
- setCost(uint64_t cost);
+ Face&
+ getFace() const
+ {
+ return *m_face;
+ }
uint64_t
- getCost() const;
+ getCost() const
+ {
+ return m_cost;
+ }
+
+ void
+ setCost(uint64_t cost)
+ {
+ m_cost = cost;
+ }
private:
- shared_ptr<Face> m_face;
+ Face* m_face;
uint64_t m_cost;
};
diff --git a/daemon/table/fib.cpp b/daemon/table/fib.cpp
index 946652e..2df3122 100644
--- a/daemon/table/fib.cpp
+++ b/daemon/table/fib.cpp
@@ -32,8 +32,9 @@
#include <type_traits>
namespace nfd {
+namespace fib {
-const shared_ptr<fib::Entry> Fib::s_emptyEntry = make_shared<fib::Entry>(Name());
+const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name());
// http://en.cppreference.com/w/cpp/concept/ForwardIterator
BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Fib::const_iterator>));
@@ -57,39 +58,38 @@
}
static inline bool
-predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& entry)
+predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& nte)
{
- return entry.getFibEntry() != nullptr;
+ return nte.getFibEntry() != nullptr;
}
-shared_ptr<fib::Entry>
+const Entry&
Fib::findLongestPrefixMatch(const Name& prefix) const
{
- shared_ptr<name_tree::Entry> nameTreeEntry =
+ shared_ptr<name_tree::Entry> nte =
m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry);
- if (nameTreeEntry != nullptr) {
- return nameTreeEntry->getFibEntry();
+ if (nte != nullptr) {
+ return *nte->getFibEntry();
}
- return s_emptyEntry;
+ return *s_emptyEntry;
}
-shared_ptr<fib::Entry>
-Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const
+const Entry&
+Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nte) const
{
- shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
+ Entry* entry = nte->getFibEntry();
if (entry != nullptr)
- return entry;
+ return *entry;
- nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry,
- &predicate_NameTreeEntry_hasFibEntry);
- if (nameTreeEntry != nullptr) {
- return nameTreeEntry->getFibEntry();
+ nte = m_nameTree.findLongestPrefixMatch(nte, &predicate_NameTreeEntry_hasFibEntry);
+ if (nte != nullptr) {
+ return *nte->getFibEntry();
}
- return s_emptyEntry;
+ return *s_emptyEntry;
}
-shared_ptr<fib::Entry>
+const Entry&
Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
{
shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(pitEntry);
@@ -97,82 +97,81 @@
return findLongestPrefixMatch(nte);
}
-shared_ptr<fib::Entry>
+const Entry&
Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
{
- shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(measurementsEntry);
- BOOST_ASSERT(nameTreeEntry != nullptr);
-
- return findLongestPrefixMatch(nameTreeEntry);
+ shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(measurementsEntry);
+ BOOST_ASSERT(nte != nullptr);
+ return findLongestPrefixMatch(nte);
}
-shared_ptr<fib::Entry>
-Fib::findExactMatch(const Name& prefix) const
+Entry*
+Fib::findExactMatch(const Name& prefix)
{
- shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
- if (nameTreeEntry != nullptr)
- return nameTreeEntry->getFibEntry();
+ shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix);
+ if (nte != nullptr)
+ return nte->getFibEntry();
return nullptr;
}
-std::pair<shared_ptr<fib::Entry>, bool>
+std::pair<Entry*, bool>
Fib::insert(const Name& prefix)
{
- shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
- shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
- if (entry != nullptr)
+ shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(prefix);
+ Entry* entry = nte->getFibEntry();
+ if (entry != nullptr) {
return std::make_pair(entry, false);
+ }
- entry = make_shared<fib::Entry>(prefix);
- nameTreeEntry->setFibEntry(entry);
+ nte->setFibEntry(make_unique<Entry>(prefix));
++m_nItems;
- return std::make_pair(entry, true);
+ return std::make_pair(nte->getFibEntry(), true);
}
void
-Fib::erase(shared_ptr<name_tree::Entry> nameTreeEntry)
+Fib::erase(shared_ptr<name_tree::Entry> nte)
{
- nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
- m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
+ nte->setFibEntry(nullptr);
+ m_nameTree.eraseEntryIfEmpty(nte);
--m_nItems;
}
void
Fib::erase(const Name& prefix)
{
- shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
- if (nameTreeEntry != nullptr) {
- this->erase(nameTreeEntry);
+ shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix);
+ if (nte != nullptr) {
+ this->erase(nte);
}
}
void
-Fib::erase(const fib::Entry& entry)
+Fib::erase(const Entry& entry)
{
- shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(entry);
- if (nameTreeEntry != nullptr) {
- this->erase(nameTreeEntry);
+ shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
+ if (nte != nullptr) {
+ this->erase(nte);
}
}
void
-Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
+Fib::removeNextHopFromAllEntries(const Face& face)
{
- std::list<fib::Entry*> toErase;
+ std::list<Entry*> toErase;
auto&& enumerable = m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry);
for (const name_tree::Entry& nte : enumerable) {
- shared_ptr<fib::Entry> entry = nte.getFibEntry();
+ Entry* entry = nte.getFibEntry();
entry->removeNextHop(face);
if (!entry->hasNextHops()) {
- toErase.push_back(entry.get());
+ toErase.push_back(entry);
// entry needs to be erased, but we must wait until the enumeration ends,
// because otherwise NameTree iterator would be invalidated
}
}
- for (fib::Entry* entry : toErase) {
+ for (Entry* entry : toErase) {
this->erase(*entry);
}
}
@@ -183,4 +182,5 @@
return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry).begin());
}
+} // namespace fib
} // namespace nfd
diff --git a/daemon/table/fib.hpp b/daemon/table/fib.hpp
index bde7428..bfe6684 100644
--- a/daemon/table/fib.hpp
+++ b/daemon/table/fib.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2015, Regents of the University of California,
+ * Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -33,13 +33,14 @@
namespace measurements {
class Entry;
-}
+} // namespace measurements
namespace pit {
class Entry;
-}
+} // namespace pit
-/** \class Fib
- * \brief represents the FIB
+namespace fib {
+
+/** \brief represents the Forwarding Information Base (FIB)
*/
class Fib : noncopyable
{
@@ -53,44 +54,54 @@
size() const;
public: // lookup
- /// performs a longest prefix match
- shared_ptr<fib::Entry>
+ /** \brief performs a longest prefix match
+ */
+ const Entry&
findLongestPrefixMatch(const Name& prefix) const;
- /// performs a longest prefix match
- shared_ptr<fib::Entry>
+ /** \brief performs a longest prefix match
+ *
+ * This is equivalent to .findLongestPrefixMatch(pitEntry.getName())
+ */
+ const Entry&
findLongestPrefixMatch(const pit::Entry& pitEntry) const;
- /// performs a longest prefix match
- shared_ptr<fib::Entry>
+ /** \brief performs a longest prefix match
+ *
+ * This is equivalent to .findLongestPrefixMatch(measurementsEntry.getName())
+ */
+ const Entry&
findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
- shared_ptr<fib::Entry>
- findExactMatch(const Name& prefix) const;
+ /** \brief performs an exact match lookup
+ */
+ Entry*
+ findExactMatch(const Name& prefix);
public: // mutation
/** \brief inserts a FIB entry for prefix
+ *
* If an entry for exact same prefix exists, that entry is returned.
- * \return{ the entry, and true for new entry, false for existing entry }
+ * \return the entry, and true for new entry or false for existing entry
*/
- std::pair<shared_ptr<fib::Entry>, bool>
+ std::pair<Entry*, bool>
insert(const Name& prefix);
void
erase(const Name& prefix);
void
- erase(const fib::Entry& entry);
+ erase(const Entry& entry);
- /** \brief removes the NextHop record for face in all entrites
+ /** \brief removes the NextHop record for face in all entries
*
- * This is usually invoked when face goes away.
+ * This is usually invoked when face is destroyed.
* Removing the last NextHop in a FIB entry will erase the FIB entry.
*
* \todo change parameter type to Face&
*/
void
- removeNextHopFromAllEntries(shared_ptr<Face> face);
+ removeNextHopFromAllEntries(const Face& face);
public: // enumeration
class const_iterator;
@@ -110,7 +121,7 @@
const_iterator
end() const;
- class const_iterator : public std::iterator<std::forward_iterator_tag, const fib::Entry>
+ class const_iterator : public std::iterator<std::forward_iterator_tag, const Entry>
{
public:
const_iterator() = default;
@@ -120,10 +131,10 @@
~const_iterator();
- const fib::Entry&
+ const Entry&
operator*() const;
- shared_ptr<fib::Entry>
+ const Entry*
operator->() const;
const_iterator&
@@ -143,8 +154,8 @@
};
private:
- shared_ptr<fib::Entry>
- findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const;
+ const Entry&
+ findLongestPrefixMatch(shared_ptr<name_tree::Entry> nte) const;
void
erase(shared_ptr<name_tree::Entry> nameTreeEntry);
@@ -153,13 +164,12 @@
NameTree& m_nameTree;
size_t m_nItems;
- /** \brief The empty FIB entry.
+ /** \brief the empty FIB entry.
*
* This entry has no nexthops.
* It is returned by findLongestPrefixMatch if nothing is matched.
- * Returning empty entry instead of nullptr makes forwarding and strategy implementation easier.
*/
- static const shared_ptr<fib::Entry> s_emptyEntry;
+ static const unique_ptr<Entry> s_emptyEntry;
};
inline size_t
@@ -189,7 +199,7 @@
Fib::const_iterator
Fib::const_iterator::operator++(int)
{
- Fib::const_iterator temp(*this);
+ const_iterator temp(*this);
++(*this);
return temp;
}
@@ -201,30 +211,34 @@
return *this;
}
-inline const fib::Entry&
+inline const Entry&
Fib::const_iterator::operator*() const
{
- return *this->operator->();
+ return *m_nameTreeIterator->getFibEntry();
}
-inline shared_ptr<fib::Entry>
+inline const Entry*
Fib::const_iterator::operator->() const
{
return m_nameTreeIterator->getFibEntry();
}
inline bool
-Fib::const_iterator::operator==(const Fib::const_iterator& other) const
+Fib::const_iterator::operator==(const const_iterator& other) const
{
return m_nameTreeIterator == other.m_nameTreeIterator;
}
inline bool
-Fib::const_iterator::operator!=(const Fib::const_iterator& other) const
+Fib::const_iterator::operator!=(const const_iterator& other) const
{
return m_nameTreeIterator != other.m_nameTreeIterator;
}
+} // namespace fib
+
+using fib::Fib;
+
} // namespace nfd
#endif // NFD_DAEMON_TABLE_FIB_HPP
diff --git a/daemon/table/name-tree-entry.cpp b/daemon/table/name-tree-entry.cpp
index 889ed97..87853f2 100644
--- a/daemon/table/name-tree-entry.cpp
+++ b/daemon/table/name-tree-entry.cpp
@@ -65,14 +65,14 @@
}
void
-Entry::setFibEntry(shared_ptr<fib::Entry> fibEntry)
+Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry)
{
BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry.expired());
if (m_fibEntry != nullptr) {
m_fibEntry->m_nameTreeEntry.reset();
}
- m_fibEntry = fibEntry;
+ m_fibEntry = std::move(fibEntry);
if (m_fibEntry != nullptr) {
m_fibEntry->m_nameTreeEntry = this->shared_from_this();
diff --git a/daemon/table/name-tree-entry.hpp b/daemon/table/name-tree-entry.hpp
index 378d285..c650587 100644
--- a/daemon/table/name-tree-entry.hpp
+++ b/daemon/table/name-tree-entry.hpp
@@ -96,9 +96,9 @@
public: // attached table entries
void
- setFibEntry(shared_ptr<fib::Entry> fibEntry);
+ setFibEntry(unique_ptr<fib::Entry> fibEntry);
- shared_ptr<fib::Entry>
+ fib::Entry*
getFibEntry() const;
void
@@ -133,7 +133,7 @@
Name m_prefix;
shared_ptr<Entry> m_parent; // Pointing to the parent entry.
std::vector<shared_ptr<Entry> > m_children; // Children pointers.
- shared_ptr<fib::Entry> m_fibEntry;
+ unique_ptr<fib::Entry> m_fibEntry;
std::vector<shared_ptr<pit::Entry> > m_pitEntries;
shared_ptr<measurements::Entry> m_measurementsEntry;
shared_ptr<strategy_choice::Entry> m_strategyChoiceEntry;
@@ -187,10 +187,10 @@
return !m_children.empty();
}
-inline shared_ptr<fib::Entry>
+inline fib::Entry*
Entry::getFibEntry() const
{
- return m_fibEntry;
+ return m_fibEntry.get();
}
inline bool
diff --git a/daemon/table/name-tree.cpp b/daemon/table/name-tree.cpp
index 87d4154..796e701 100644
--- a/daemon/table/name-tree.cpp
+++ b/daemon/table/name-tree.cpp
@@ -244,7 +244,7 @@
NameTree::lookup(const fib::Entry& fibEntry) const
{
shared_ptr<name_tree::Entry> nte = this->getEntry(fibEntry);
- BOOST_ASSERT(nte == nullptr || nte->getFibEntry().get() == &fibEntry);
+ BOOST_ASSERT(nte == nullptr || nte->getFibEntry() == &fibEntry);
return nte;
}