blob: 02502abc4d0c8642e0fe06d51741459cefc9dec4 [file] [log] [blame]
Junxiao Shi651b75e2014-07-17 20:15:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "pit-nonce-list.hpp"
27
28namespace nfd {
29namespace pit {
30
31// The NonceList has limited capacity to avoid memory explosion
32// if PIT entry is constantly refreshed (NFD Bug #1770).
33// Current implementation keeps nonces in a set to detect duplicates,
34// and a queue to evict the oldest nonce when capacity limit is reached.
35// A limitation is that a nonce first appeared at time 0 and duplicated at time 10
36// could be evicted before a nonce appeared only once at time 5;
37// this limitation should not affect normal operation.
38
39const size_t NonceList::CAPACITY = 256;
40
41NonceList::NonceList()
42{
43}
44
45bool
46NonceList::add(uint32_t nonce)
47{
48 bool isNew = m_nonceSet.insert(nonce).second;
49 if (!isNew)
50 return false;
51
52 m_nonceQueue.push(nonce);
53 BOOST_ASSERT(m_nonceSet.size() == m_nonceQueue.size());
54
55 if (m_nonceSet.size() > CAPACITY) {
56 size_t nErased = m_nonceSet.erase(m_nonceQueue.front());
57 BOOST_ASSERT(nErased == 1);
58 m_nonceQueue.pop();
59 BOOST_ASSERT(m_nonceSet.size() == m_nonceQueue.size());
60 BOOST_ASSERT(m_nonceSet.size() <= CAPACITY);
61 }
62 return true;
63}
64
65} // namespace pit
66} // namespace nfd