xref: /netbsd-src/external/gpl3/binutils.old/dist/gprofng/src/CacheMap.h (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1*c42dbd0eSchristos /* Copyright (C) 2021 Free Software Foundation, Inc.
2*c42dbd0eSchristos    Contributed by Oracle.
3*c42dbd0eSchristos 
4*c42dbd0eSchristos    This file is part of GNU Binutils.
5*c42dbd0eSchristos 
6*c42dbd0eSchristos    This program is free software; you can redistribute it and/or modify
7*c42dbd0eSchristos    it under the terms of the GNU General Public License as published by
8*c42dbd0eSchristos    the Free Software Foundation; either version 3, or (at your option)
9*c42dbd0eSchristos    any later version.
10*c42dbd0eSchristos 
11*c42dbd0eSchristos    This program is distributed in the hope that it will be useful,
12*c42dbd0eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*c42dbd0eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*c42dbd0eSchristos    GNU General Public License for more details.
15*c42dbd0eSchristos 
16*c42dbd0eSchristos    You should have received a copy of the GNU General Public License
17*c42dbd0eSchristos    along with this program; if not, write to the Free Software
18*c42dbd0eSchristos    Foundation, 51 Franklin Street - Fifth Floor, Boston,
19*c42dbd0eSchristos    MA 02110-1301, USA.  */
20*c42dbd0eSchristos 
21*c42dbd0eSchristos /*
22*c42dbd0eSchristos  *  Cache Map implementation.
23*c42dbd0eSchristos  *
24*c42dbd0eSchristos  *  Cache Map makes the following assumptions:
25*c42dbd0eSchristos  *    - Cache Map is used for very fast but not guaranteed mapping;
26*c42dbd0eSchristos  *    - only REL_EQ Relation can be used;
27*c42dbd0eSchristos  *    - all objects used as keys or values has to be managed
28*c42dbd0eSchristos  *      outside CacheMap (f.e. deletion);
29*c42dbd0eSchristos  *    - (Key_t)0 is invalid key;
30*c42dbd0eSchristos  *    - (Value_t)0 is invalid value;
31*c42dbd0eSchristos  *    - <TBC>
32*c42dbd0eSchristos  */
33*c42dbd0eSchristos 
34*c42dbd0eSchristos #ifndef _DBE_CACHEMAP_H
35*c42dbd0eSchristos #define _DBE_CACHEMAP_H
36*c42dbd0eSchristos 
37*c42dbd0eSchristos #include <assert.h>
38*c42dbd0eSchristos #include <vec.h>
39*c42dbd0eSchristos #include <Map.h>
40*c42dbd0eSchristos 
41*c42dbd0eSchristos template <typename Key_t, typename Value_t>
42*c42dbd0eSchristos class CacheMap : public Map<Key_t, Value_t>
43*c42dbd0eSchristos {
44*c42dbd0eSchristos public:
45*c42dbd0eSchristos 
46*c42dbd0eSchristos   CacheMap ();
47*c42dbd0eSchristos   ~CacheMap ();
48*c42dbd0eSchristos   void put (Key_t key, Value_t val);
49*c42dbd0eSchristos   Value_t get (Key_t key);
50*c42dbd0eSchristos   Value_t get (Key_t key, typename Map<Key_t, Value_t>::Relation rel);
51*c42dbd0eSchristos   Value_t
52*c42dbd0eSchristos   remove (Key_t key);
53*c42dbd0eSchristos 
54*c42dbd0eSchristos private:
55*c42dbd0eSchristos 
56*c42dbd0eSchristos   struct Entry
57*c42dbd0eSchristos   {
58*c42dbd0eSchristos     Key_t key;
59*c42dbd0eSchristos     Value_t val;
60*c42dbd0eSchristos 
EntryEntry61*c42dbd0eSchristos     Entry ()
62*c42dbd0eSchristos     {
63*c42dbd0eSchristos       key = (Key_t) 0;
64*c42dbd0eSchristos     }
65*c42dbd0eSchristos   };
66*c42dbd0eSchristos 
67*c42dbd0eSchristos   static const int INIT_SIZE;
68*c42dbd0eSchristos   static const int MAX_SIZE;
69*c42dbd0eSchristos 
70*c42dbd0eSchristos   static unsigned hash (Key_t key);
71*c42dbd0eSchristos   Entry *getEntry (Key_t key);
72*c42dbd0eSchristos 
73*c42dbd0eSchristos   int cursize;
74*c42dbd0eSchristos   int nputs;
75*c42dbd0eSchristos   int nchunks;
76*c42dbd0eSchristos   Entry **chunks;
77*c42dbd0eSchristos };
78*c42dbd0eSchristos 
79*c42dbd0eSchristos template <typename Key_t, typename Value_t>
80*c42dbd0eSchristos const int CacheMap<Key_t, Value_t>::INIT_SIZE = 1 << 14;
81*c42dbd0eSchristos template <typename Key_t, typename Value_t>
82*c42dbd0eSchristos const int CacheMap<Key_t, Value_t>::MAX_SIZE = 1 << 20;
83*c42dbd0eSchristos 
84*c42dbd0eSchristos template <typename Key_t, typename Value_t>CacheMap<Key_t, Value_t>
CacheMap()85*c42dbd0eSchristos ::CacheMap ()
86*c42dbd0eSchristos {
87*c42dbd0eSchristos   cursize = INIT_SIZE;
88*c42dbd0eSchristos   chunks = new Entry*[32];
89*c42dbd0eSchristos   nchunks = 0;
90*c42dbd0eSchristos   chunks[nchunks++] = new Entry[cursize];
91*c42dbd0eSchristos   nputs = 0;
92*c42dbd0eSchristos }
93*c42dbd0eSchristos 
94*c42dbd0eSchristos template <typename Key_t, typename Value_t>
~CacheMap()95*c42dbd0eSchristos CacheMap<Key_t, Value_t>::~CacheMap ()
96*c42dbd0eSchristos {
97*c42dbd0eSchristos   for (int i = 0; i < nchunks; i++)
98*c42dbd0eSchristos     delete[] chunks[i];
99*c42dbd0eSchristos   delete[] chunks;
100*c42dbd0eSchristos }
101*c42dbd0eSchristos 
102*c42dbd0eSchristos template <typename Key_t, typename Value_t>
103*c42dbd0eSchristos unsigned
hash(Key_t key)104*c42dbd0eSchristos CacheMap<Key_t, Value_t>::hash (Key_t key)
105*c42dbd0eSchristos {
106*c42dbd0eSchristos   unsigned h = (unsigned) key ^ (unsigned) (key >> 32);
107*c42dbd0eSchristos   h ^= (h >> 20) ^ (h >> 12);
108*c42dbd0eSchristos   return h ^ (h >> 7) ^ (h >> 4);
109*c42dbd0eSchristos }
110*c42dbd0eSchristos 
111*c42dbd0eSchristos template <typename Key_t, typename Value_t>
112*c42dbd0eSchristos void
put(Key_t key,Value_t val)113*c42dbd0eSchristos CacheMap<Key_t, Value_t>::put (Key_t key, Value_t val)
114*c42dbd0eSchristos {
115*c42dbd0eSchristos   if (nputs >= cursize && cursize < MAX_SIZE)
116*c42dbd0eSchristos     {
117*c42dbd0eSchristos       // Allocate new chunk for entries.
118*c42dbd0eSchristos       chunks[nchunks++] = new Entry[cursize];
119*c42dbd0eSchristos       cursize *= 2;
120*c42dbd0eSchristos 
121*c42dbd0eSchristos       // Copy all old entries to the newly allocated chunk
122*c42dbd0eSchristos       Entry *newchunk = chunks[nchunks - 1];
123*c42dbd0eSchristos       int prevsz = 0;
124*c42dbd0eSchristos       int nextsz = INIT_SIZE;
125*c42dbd0eSchristos       for (int i = 0; i < nchunks - 1; i++)
126*c42dbd0eSchristos 	{
127*c42dbd0eSchristos 	  Entry *oldchunk = chunks[i];
128*c42dbd0eSchristos 	  for (int j = prevsz; j < nextsz; j++)
129*c42dbd0eSchristos 	    newchunk[j] = oldchunk[j - prevsz];
130*c42dbd0eSchristos 	  prevsz = nextsz;
131*c42dbd0eSchristos 	  nextsz *= 2;
132*c42dbd0eSchristos 	}
133*c42dbd0eSchristos     }
134*c42dbd0eSchristos   Entry *entry = getEntry (key);
135*c42dbd0eSchristos   entry->key = key;
136*c42dbd0eSchristos   entry->val = val;
137*c42dbd0eSchristos   nputs++;
138*c42dbd0eSchristos }
139*c42dbd0eSchristos 
140*c42dbd0eSchristos template <typename Key_t, typename Value_t>
141*c42dbd0eSchristos typename CacheMap<Key_t, Value_t>::Entry *
getEntry(Key_t key)142*c42dbd0eSchristos CacheMap<Key_t, Value_t>::getEntry (Key_t key)
143*c42dbd0eSchristos {
144*c42dbd0eSchristos   unsigned idx = hash (key);
145*c42dbd0eSchristos   int i = nchunks - 1;
146*c42dbd0eSchristos   int j = cursize / 2;
147*c42dbd0eSchristos   for (; i > 0; i -= 1, j /= 2)
148*c42dbd0eSchristos     if (idx & j)
149*c42dbd0eSchristos       break;
150*c42dbd0eSchristos   if (i == 0)
151*c42dbd0eSchristos     j *= 2;
152*c42dbd0eSchristos   return &chunks[i][idx & (j - 1)];
153*c42dbd0eSchristos }
154*c42dbd0eSchristos 
155*c42dbd0eSchristos template <typename Key_t, typename Value_t>
156*c42dbd0eSchristos Value_t
get(Key_t key)157*c42dbd0eSchristos CacheMap<Key_t, Value_t>::get (Key_t key)
158*c42dbd0eSchristos {
159*c42dbd0eSchristos   Entry *entry = getEntry (key);
160*c42dbd0eSchristos   return entry->key == key ? entry->val : (Value_t) 0;
161*c42dbd0eSchristos }
162*c42dbd0eSchristos 
163*c42dbd0eSchristos template <typename Key_t, typename Value_t>
164*c42dbd0eSchristos Value_t
get(Key_t key,typename Map<Key_t,Value_t>::Relation rel)165*c42dbd0eSchristos CacheMap<Key_t, Value_t>::get (Key_t key, typename Map<Key_t, Value_t>::Relation rel)
166*c42dbd0eSchristos {
167*c42dbd0eSchristos   if (rel != Map<Key_t, Value_t>::REL_EQ)
168*c42dbd0eSchristos     return (Value_t) 0;
169*c42dbd0eSchristos   return get (key);
170*c42dbd0eSchristos }
171*c42dbd0eSchristos 
172*c42dbd0eSchristos template <typename Key_t, typename Value_t>
173*c42dbd0eSchristos Value_t
remove(Key_t key)174*c42dbd0eSchristos CacheMap<Key_t, Value_t>::remove (Key_t key)
175*c42dbd0eSchristos {
176*c42dbd0eSchristos   Entry *entry = getEntry (key);
177*c42dbd0eSchristos   Value_t res = (Value_t) 0;
178*c42dbd0eSchristos   if (entry->key == key)
179*c42dbd0eSchristos     {
180*c42dbd0eSchristos       res = entry->val;
181*c42dbd0eSchristos       entry->val = (Value_t) 0;
182*c42dbd0eSchristos     }
183*c42dbd0eSchristos   return res;
184*c42dbd0eSchristos }
185*c42dbd0eSchristos 
186*c42dbd0eSchristos #endif
187