OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

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

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
 
34
//! Constructor
35
 
36
//! Allocate the hash table
37
//! @param[in] size  Number of slots in the  hash table. Defaults to
38
//!                  DEFAULT_MP_HASH_SIZE.
39
MpHash::MpHash (int  _size) :
40
  size (_size)
41
{
42
  // Allocate and clear the hash table
43
  hashTab = new MpEntry *[size];
44
 
45
  for (int  i = 0; i < size; i++)
46
    {
47
      hashTab[i] = NULL;
48
    }
49
}       // MpHash ()
50
 
51
 
52
//! Destructor
53
 
54
//! Free the hash table
55
MpHash::~MpHash ()
56
{
57
  delete [] hashTab;
58
 
59
}       // ~MpHash ()
60
 
61
 
62
//! Add an entry to the hash table
63
 
64
//! Add the entry if it wasn't already there. If it was there do nothing. The
65
//! match just be on type and addr. The instr need not match, since if this is
66
//! a duplicate insertion (perhaps due to a lost packet) they will be
67
//! different.
68
 
69
//! @note This method allocates memory. Care must be taken to delete it when
70
//! done.
71
 
72
//! @param[in] type   The type of matchpoint
73
//! @param[in] addr   The address of the matchpoint
74
//! @para[in]  instr  The instruction to associate with the address
75
void
76
MpHash::add (MpType    type,
77
             uint32_t  addr,
78
             uint32_t  instr)
79
{
80
  int      hv    = addr % size;
81
  MpEntry *curr;
82
 
83
  // See if we already have the entry
84
  for(curr = hashTab[hv]; NULL != curr; curr = curr->next)
85
    {
86
      if ((type == curr->type) && (addr == curr->addr))
87
        {
88
          return;               // We already have the entry
89
        }
90
    }
91
 
92
  // Insert the new entry at the head of the chain
93
  curr = new MpEntry ();
94
 
95
  curr->type  = type;
96
  curr->addr  = addr;
97
  curr->instr = instr;
98
  curr->next  = hashTab[hv];
99
 
100
  hashTab[hv] = curr;
101
 
102
}       // add ()
103
 
104
 
105
//!Look up an entry in the matchpoint hash table
106
 
107
//! The match must be on type AND addr.
108
 
109
//! @param[in] type   The type of matchpoint
110
//! @param[in] addr   The address of the matchpoint
111
 
112
//! @return  The entry found, or NULL if the entry was not found
113
MpEntry *
114
MpHash::lookup (MpType    type,
115
                uint32_t  addr)
116
{
117
  int      hv   = addr % size;
118
 
119
  // Search
120
  for (MpEntry *curr = hashTab[hv]; NULL != curr; curr = curr->next)
121
    {
122
      if ((type == curr->type) && (addr == curr->addr))
123
        {
124
          return  curr;         // The entry found
125
        }
126
    }
127
 
128
  return  NULL;                 // Not found
129
 
130
}       // lookup ()
131
 
132
 
133
//! Delete an entry from the matchpoint hash table
134
 
135
//! If it is there the entry is deleted from the hash table. If it is not
136
//! there, no action is taken. The match must be on type AND addr. The entry
137
//! (MpEntry::) is itself deleted
138
 
139
//! The usual fun and games tracking the previous entry, so we can delete
140
//! things.
141
 
142
//! @param[in]  type   The type of matchpoint
143
//! @param[in]  addr   The address of the matchpoint
144
//! @param[out] instr  Location to place the instruction found. If NULL (the
145
//!                    default) then the instruction is not written back.
146
 
147
//! @return  TRUE if an entry was found and deleted
148
bool
149
MpHash::remove (MpType    type,
150
                uint32_t  addr,
151
                uint32_t *instr)
152
{
153
  int      hv   = addr % size;
154
  MpEntry *prev = NULL;
155
  MpEntry *curr;
156
 
157
  // Search
158
  for (curr  = hashTab[hv]; NULL != curr; curr = curr->next)
159
    {
160
      if ((type == curr->type) && (addr == curr->addr))
161
        {
162
          // Found - delete. Method depends on whether we are the head of
163
          // chain.
164
          if (NULL == prev)
165
            {
166
              hashTab[hv] = curr->next;
167
            }
168
          else
169
            {
170
              prev->next = curr->next;
171
            }
172
 
173
          if (NULL != instr)
174
            {
175
              *instr = curr->instr;     // Return the found instruction
176
            }
177
 
178
          delete curr;
179
          return true;                  // Success
180
        }
181
 
182
      prev = curr;
183
    }
184
 
185
  return  false;                        // Not found
186
 
187
}       // remove ()

powered by: WebSVN 2.1.0

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