2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "base/intmath.hh"
30 #include "base/str.hh"
31 #include "mem/ruby/filters/NonCountingBloomFilter.hh"
32 #include "mem/ruby/system/System.hh"
36 NonCountingBloomFilter::NonCountingBloomFilter(string str
)
42 split_first(str
, head
, tail
, '_');
45 // head contains filter size, tail contains bit offset from block number
46 m_filter_size
= atoi(head
.c_str());
47 m_offset
= atoi(tail
.c_str());
48 m_filter_size_bits
= floorLog2(m_filter_size
);
50 m_filter
.resize(m_filter_size
);
54 NonCountingBloomFilter::~NonCountingBloomFilter()
59 NonCountingBloomFilter::clear()
61 for (int i
= 0; i
< m_filter_size
; i
++) {
67 NonCountingBloomFilter::increment(const Address
& addr
)
73 NonCountingBloomFilter::decrement(const Address
& addr
)
79 NonCountingBloomFilter::merge(AbstractBloomFilter
*other_filter
)
81 // assumes both filters are the same size!
82 NonCountingBloomFilter
* temp
= (NonCountingBloomFilter
*) other_filter
;
83 for(int i
= 0; i
< m_filter_size
; ++i
){
84 m_filter
[i
] |= (*temp
)[i
];
89 NonCountingBloomFilter::set(const Address
& addr
)
91 int i
= get_index(addr
);
96 NonCountingBloomFilter::unset(const Address
& addr
)
98 int i
= get_index(addr
);
103 NonCountingBloomFilter::isSet(const Address
& addr
)
105 int i
= get_index(addr
);
106 return (m_filter
[i
]);
111 NonCountingBloomFilter::getCount(const Address
& addr
)
113 return m_filter
[get_index(addr
)];
117 NonCountingBloomFilter::getTotalCount()
121 for (int i
= 0; i
< m_filter_size
; i
++) {
122 count
+= m_filter
[i
];
128 NonCountingBloomFilter::print(ostream
& out
) const
133 NonCountingBloomFilter::getIndex(const Address
& addr
)
135 return get_index(addr
);
139 NonCountingBloomFilter::readBit(const int index
)
141 return m_filter
[index
];
145 NonCountingBloomFilter::writeBit(const int index
, const int value
)
147 m_filter
[index
] = value
;
151 NonCountingBloomFilter::get_index(const Address
& addr
)
153 return addr
.bitSelect(RubySystem::getBlockSizeBits() + m_offset
,
154 RubySystem::getBlockSizeBits() + m_offset
+
155 m_filter_size_bits
- 1);