1/**
2 * Copyright (c) 2018 Inria
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Daniel Carvalho
29 */
30
31#ifndef __MEM_CACHE_REPLACEMENT_POLICIES_BASE_HH__
32#define __MEM_CACHE_REPLACEMENT_POLICIES_BASE_HH__
33
34#include <memory>
35
36#include "mem/cache/replacement_policies/replaceable_entry.hh"
37#include "params/BaseReplacementPolicy.hh"
38#include "sim/sim_object.hh"
39
40/**
41 * Replacement candidates as chosen by the indexing policy.
42 */
43typedef std::vector<ReplaceableEntry*> ReplacementCandidates;
44
45/**
46 * A common base class of cache replacement policy objects.
47 */
48class BaseReplacementPolicy : public SimObject
49{
50  public:
51    /**
52      * Convenience typedef.
53      */
54    typedef BaseReplacementPolicyParams Params;
55
56    /**
57     * Construct and initiliaze this replacement policy.
58     */
59    BaseReplacementPolicy(const Params *p) : SimObject(p) {}
60
61    /**
62     * Destructor.
63     */
64    virtual ~BaseReplacementPolicy() {}
65
66    /**
67     * Invalidate replacement data to set it as the next probable victim.
68     *
69     * @param replacement_data Replacement data to be invalidated.
70     */
71    virtual void invalidate(const std::shared_ptr<ReplacementData>&
72                                                replacement_data) const = 0;
73
74    /**
75     * Update replacement data.
76     *
77     * @param replacement_data Replacement data to be touched.
78     */
79    virtual void touch(const std::shared_ptr<ReplacementData>&
80                                                replacement_data) const = 0;
81
82    /**
83     * Reset replacement data. Used when it's holder is inserted/validated.
84     *
85     * @param replacement_data Replacement data to be reset.
86     */
87    virtual void reset(const std::shared_ptr<ReplacementData>&
88                                                replacement_data) const = 0;
89
90    /**
91     * Find replacement victim among candidates.
92     *
93     * @param candidates Replacement candidates, selected by indexing policy.
94     * @return Replacement entry to be replaced.
95     */
96    virtual ReplaceableEntry* getVictim(
97                           const ReplacementCandidates& candidates) const = 0;
98
99    /**
100     * Instantiate a replacement data entry.
101     *
102     * @return A shared pointer to the new replacement data.
103     */
104    virtual std::shared_ptr<ReplacementData> instantiateEntry() = 0;
105};
106
107#endif // __MEM_CACHE_REPLACEMENT_POLICIES_BASE_HH__
108