OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [MpHash.cpp] - Blame information for rev 861

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 63 julius
// ----------------------------------------------------------------------------
2
 
3
// Matchpoint hash table: definition
4
 
5
// Copyright (C) 2008  Embecosm Limited <info@embecosm.com>
6
 
7
// Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
 
9
// This file is part of the cycle accurate model of the OpenRISC 1000 based
10
// system-on-chip, ORPSoC, built using Verilator.
11
 
12
// This program is free software: you can redistribute it and/or modify it
13
// under the terms of the GNU Lesser General Public License as published by
14
// the Free Software Foundation, either version 3 of the License, or (at your
15
// option) any later version.
16
 
17
// This program is distributed in the hope that it will be useful, but WITHOUT
18
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
20
// License for more details.
21
 
22
// You should have received a copy of the GNU Lesser General Public License
23
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 
25
// ----------------------------------------------------------------------------
26
 
27
// $Id: MpHash.cpp 331 2009-03-12 17:01:48Z jeremy $
28
 
29
#include <cstdlib>
30
 
31
#include "MpHash.h"
32
 
33
//! Constructor
34
 
35
//! Allocate the hash table
36
//! @param[in] size  Number of slots in the  hash table. Defaults to
37
//!                  DEFAULT_MP_HASH_SIZE.
38 462 julius
MpHash::MpHash(int _size):
39
size(_size)
40 63 julius
{
41 462 julius
        // Allocate and clear the hash table
42
        hashTab = new MpEntry *[size];
43 63 julius
 
44 462 julius
        for (int i = 0; i < size; i++) {
45
                hashTab[i] = NULL;
46
        }
47
}                               // MpHash ()
48 63 julius
 
49
//! Destructor
50
 
51
//! Free the hash table
52 462 julius
MpHash::~MpHash()
53 63 julius
{
54 462 julius
        delete[]hashTab;
55 63 julius
 
56 462 julius
}                               // ~MpHash ()
57 63 julius
 
58
//! Add an entry to the hash table
59
 
60
//! Add the entry if it wasn't already there. If it was there do nothing. The
61
//! match just be on type and addr. The instr need not match, since if this is
62
//! a duplicate insertion (perhaps due to a lost packet) they will be
63
//! different.
64
 
65
//! @note This method allocates memory. Care must be taken to delete it when
66
//! done.
67
 
68
//! @param[in] type   The type of matchpoint
69
//! @param[in] addr   The address of the matchpoint
70
//! @para[in]  instr  The instruction to associate with the address
71
void
72 462 julius
 MpHash::add(MpType type, uint32_t addr, uint32_t instr)
73 63 julius
{
74 462 julius
        int hv = addr % size;
75
        MpEntry *curr;
76 63 julius
 
77 462 julius
        // See if we already have the entry
78
        for (curr = hashTab[hv]; NULL != curr; curr = curr->next) {
79
                if ((type == curr->type) && (addr == curr->addr)) {
80
                        return; // We already have the entry
81
                }
82 63 julius
        }
83
 
84 462 julius
        // Insert the new entry at the head of the chain
85
        curr = new MpEntry();
86 63 julius
 
87 462 julius
        curr->type = type;
88
        curr->addr = addr;
89
        curr->instr = instr;
90
        curr->next = hashTab[hv];
91 63 julius
 
92 462 julius
        hashTab[hv] = curr;
93 63 julius
 
94 462 julius
}                               // add ()
95 63 julius
 
96
//!Look up an entry in the matchpoint hash table
97
 
98
//! The match must be on type AND addr.
99
 
100
//! @param[in] type   The type of matchpoint
101
//! @param[in] addr   The address of the matchpoint
102
 
103
//! @return  The entry found, or NULL if the entry was not found
104 462 julius
MpEntry *MpHash::lookup(MpType type, uint32_t addr)
105 63 julius
{
106 462 julius
        int hv = addr % size;
107 63 julius
 
108 462 julius
        // Search
109
        for (MpEntry * curr = hashTab[hv]; NULL != curr; curr = curr->next) {
110
                if ((type == curr->type) && (addr == curr->addr)) {
111
                        return curr;    // The entry found
112
                }
113 63 julius
        }
114
 
115 462 julius
        return NULL;            // Not found
116 63 julius
 
117 462 julius
}                               // lookup ()
118 63 julius
 
119
//! Delete an entry from the matchpoint hash table
120
 
121
//! If it is there the entry is deleted from the hash table. If it is not
122
//! there, no action is taken. The match must be on type AND addr. The entry
123
//! (MpEntry::) is itself deleted
124
 
125
//! The usual fun and games tracking the previous entry, so we can delete
126
//! things.
127
 
128
//! @param[in]  type   The type of matchpoint
129
//! @param[in]  addr   The address of the matchpoint
130
//! @param[out] instr  Location to place the instruction found. If NULL (the
131
//!                    default) then the instruction is not written back.
132
 
133
//! @return  TRUE if an entry was found and deleted
134 462 julius
bool MpHash::remove(MpType type, uint32_t addr, uint32_t * instr)
135 63 julius
{
136 462 julius
        int hv = addr % size;
137
        MpEntry *prev = NULL;
138
        MpEntry *curr;
139 63 julius
 
140 462 julius
        // Search
141
        for (curr = hashTab[hv]; NULL != curr; curr = curr->next) {
142
                if ((type == curr->type) && (addr == curr->addr)) {
143
                        // Found - delete. Method depends on whether we are the head of
144
                        // chain.
145
                        if (NULL == prev) {
146
                                hashTab[hv] = curr->next;
147
                        } else {
148
                                prev->next = curr->next;
149
                        }
150 63 julius
 
151 462 julius
                        if (NULL != instr) {
152
                                *instr = curr->instr;   // Return the found instruction
153
                        }
154 63 julius
 
155 462 julius
                        delete curr;
156
                        return true;    // Success
157
                }
158
 
159
                prev = curr;
160 63 julius
        }
161
 
162 462 julius
        return false;           // Not found
163 63 julius
 
164 462 julius
}                               // remove ()

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.