2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
31 #include "timestamp.hpp"
35 template <typename K, typename T>
39 entry(K key, T value, const struct timeval& tv, list<struct entry> *parent) :
40 key(key), value(value), tv(tv), parent(parent) {}
41 bool expired(const Timestamp &now, const Timestamp &maxDiff) const
43 return now - Timestamp(tv) > maxDiff;
47 struct timeval tv; /* List time entry has been hit */
48 list<struct entry> *parent;
51 friend class FlowTable;
53 bool operator!=(const Iterator& other) {
54 return m_v != other.m_v ||
55 m_vec_pos != other.m_vec_pos ||
59 Iterator& operator++() {
61 while (m_vec_pos != m_v->size() - 1 && m_a == (*m_v)[m_vec_pos].end()) {
63 m_a = (*m_v)[m_vec_pos].begin();
68 struct entry &operator*() {
72 Iterator(uint32_t vec_pos, vector<list<struct entry> > *v)
73 : m_vec_pos(vec_pos), m_v(v)
75 m_a = (*m_v)[vec_pos].begin();
76 while (m_vec_pos != m_v->size() - 1 && m_a == (*m_v)[m_vec_pos].end()) {
78 m_a = (*m_v)[m_vec_pos].begin();
81 Iterator(uint32_t vec_pos, vector<list<struct entry> > *v, const typename list< struct entry>::iterator& a)
82 : m_vec_pos(vec_pos), m_v(v), m_a(a)
85 vector<list<struct entry> > *m_v;
86 typename list<struct entry>::iterator m_a;
88 uint32_t getEntryCount() const {return m_entryCount;}
89 FlowTable(uint32_t size);
90 void expire(const struct timeval& tv);
91 struct entry* lookup(const K& key);
92 void remove(struct FlowTable<K,T>::entry* entry);
93 struct entry* insert(const K& key, const T& value, const struct timeval& tv);
94 Iterator begin() {return Iterator(0, &m_elems);}
95 Iterator end() {return Iterator(m_elems.size() - 1, &m_elems, m_elems.back().end());}
98 void clearBucket(list<struct entry> *l);
99 vector<list<struct entry> > m_elems;
100 uint32_t m_entryCount;
103 template <typename K, typename T>
104 FlowTable<K, T>::FlowTable(uint32_t size)
105 : m_elems(), m_entryCount(0)
108 m_elems.resize(size);
111 template <typename K, typename T>
112 struct FlowTable<K, T>::entry* FlowTable<K, T>::lookup(const K& key)
114 uint32_t ret = crc32((uint8_t*)&key, sizeof(K), 0);
116 list<struct entry> &l = m_elems[ret % m_elems.size()];
121 for (typename list<struct entry>::iterator it = l.begin(); it != l.end(); ++it) {
122 if (memcmp(&((*it).key), &key, sizeof(key)) == 0)
128 template <typename K, typename T>
129 struct FlowTable<K, T>::entry *FlowTable<K, T>::insert(const K& key, const T& value, const struct timeval& tv)
131 uint32_t ret = crc32((uint8_t*)&key, sizeof(K), 0);
132 list<struct entry> &l = m_elems[ret % m_elems.size()];
134 l.push_back(entry(key, value, tv, &l));
136 struct entry &n = l.back();
143 template <typename K, typename T>
144 void FlowTable<K, T>::remove(struct FlowTable<K,T>::entry* entry)
146 list<struct entry> &l = *entry->parent;
148 for (typename list<struct entry>::iterator it = l.begin(); it != l.end(); ++it) {
149 if (memcmp(&((*it).key), &entry->key, sizeof(entry->key)) == 0) {
157 template <typename K, typename T>
158 void FlowTable<K, T>::clearBucket(list<struct entry> *l)
160 while (!l->empty()) {
162 l->erase(l->begin());
166 template <typename K, typename T>
167 void FlowTable<K, T>::clear()
169 for (size_t i = 0; i < m_elems.size(); ++i) {
170 clearBucket(&m_elems[i]);
174 #endif /* _FLOWTABLE_H_ */