2 * Copyright (c) 2019 Inria
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Authors: Daniel Carvalho
32 #ifndef __BASE_FILTERS_BASE_HH__
33 #define __BASE_FILTERS_BASE_HH__
37 #include "base/intmath.hh"
38 #include "base/sat_counter.hh"
39 #include "base/types.hh"
40 #include "params/BloomFilterBase.hh"
41 #include "sim/sim_object.hh"
43 namespace BloomFilter {
45 class Base : public SimObject
48 /** Number of LSB bits to ignore from the the addresses. */
49 const unsigned offsetBits;
51 /** The filter itself. */
52 std::vector<SatCounter> filter;
54 /** Number of bits needed to represent the size of the filter. */
57 /** Threshold at which a filter entry starts being considered as set. */
58 const int setThreshold;
62 * Create and clear the filter.
64 Base(const BloomFilterBaseParams* p)
65 : SimObject(p), offsetBits(p->offset_bits),
66 filter(p->size, SatCounter(p->num_bits)),
67 sizeBits(floorLog2(p->size)), setThreshold(p->threshold)
74 * Clear the filter by resetting all values.
78 for (auto& entry : filter) {
84 * Merges the contents of both filters into this' (Bloom Filter union).
85 * Both must have the same number of entries.
87 * @param other The other bloom filter to merge with.
90 merge(const Base* other)
92 assert(filter.size() == other->filter.size());
93 for (int i = 0; i < filter.size(); ++i){
94 filter[i] += other->filter[i];
99 * Perform the filter specific function to set the corresponding
100 * entries (can be multiple) of an address.
102 * @param addr The address being parsed.
104 virtual void set(Addr addr) = 0;
107 * Perform the filter specific function to clear the corresponding
108 * entries (can be multiple) of an address. By default a bloom
109 * filter does not support element deletion.
111 * @param addr The address being parsed.
113 virtual void unset(Addr addr) {};
116 * Check if the corresponding filter entries of an address should be
119 * @param addr The address being parsed.
120 * @return Whether the respective filter entry is set.
123 isSet(Addr addr) const
125 return getCount(addr) >= setThreshold;
129 * Get the value stored in the corresponding filter entry of an address.
131 * @param addr The address being parsed.
132 * @param Get the value stored in the respective filter entry.
134 virtual int getCount(Addr addr) const { return 0; }
137 * Get the total value stored in the filter entries.
139 * @return The sum of all filter entries.
141 virtual int getTotalCount() const
144 for (const auto& entry : filter) {
151 } // namespace BloomFilter
153 #endif // __BASE_FILTERS_BASE_HH__