/* Copyright (C) 2021-2024 Free Software Foundation, Inc. Contributed by Oracle. This file is part of GNU Binutils. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ /* * Cache Map implementation. * * Cache Map makes the following assumptions: * - Cache Map is used for very fast but not guaranteed mapping; * - only REL_EQ Relation can be used; * - all objects used as keys or values has to be managed * outside CacheMap (f.e. deletion); * - (Key_t)0 is invalid key; * - (Value_t)0 is invalid value; * - */ #ifndef _DBE_CACHEMAP_H #define _DBE_CACHEMAP_H #include #include #include template class CacheMap : public Map { public: CacheMap (); ~CacheMap (); void put (Key_t key, Value_t val); Value_t get (Key_t key); Value_t get (Key_t key, typename Map::Relation rel); Value_t remove (Key_t key); private: struct Entry { Key_t key; Value_t val; Entry () { key = (Key_t) 0; } }; static const int INIT_SIZE; static const int MAX_SIZE; static unsigned hash (Key_t key); Entry *getEntry (Key_t key); int cursize; int nputs; int nchunks; Entry **chunks; }; template const int CacheMap::INIT_SIZE = 1 << 14; template const int CacheMap::MAX_SIZE = 1 << 20; template CacheMap ::CacheMap () { cursize = INIT_SIZE; chunks = new Entry*[32]; nchunks = 0; chunks[nchunks++] = new Entry[cursize]; nputs = 0; } template CacheMap::~CacheMap () { for (int i = 0; i < nchunks; i++) delete[] chunks[i]; delete[] chunks; } template unsigned CacheMap::hash (Key_t key) { unsigned h = (unsigned) key ^ (unsigned) (key >> 32); h ^= (h >> 20) ^ (h >> 12); return h ^ (h >> 7) ^ (h >> 4); } template void CacheMap::put (Key_t key, Value_t val) { if (nputs >= cursize && cursize < MAX_SIZE) { // Allocate new chunk for entries. chunks[nchunks++] = new Entry[cursize]; cursize *= 2; // Copy all old entries to the newly allocated chunk Entry *newchunk = chunks[nchunks - 1]; int prevsz = 0; int nextsz = INIT_SIZE; for (int i = 0; i < nchunks - 1; i++) { Entry *oldchunk = chunks[i]; for (int j = prevsz; j < nextsz; j++) newchunk[j] = oldchunk[j - prevsz]; prevsz = nextsz; nextsz *= 2; } } Entry *entry = getEntry (key); entry->key = key; entry->val = val; nputs++; } template typename CacheMap::Entry * CacheMap::getEntry (Key_t key) { unsigned idx = hash (key); int i = nchunks - 1; int j = cursize / 2; for (; i > 0; i -= 1, j /= 2) if (idx & j) break; if (i == 0) j *= 2; return &chunks[i][idx & (j - 1)]; } template Value_t CacheMap::get (Key_t key) { Entry *entry = getEntry (key); return entry->key == key ? entry->val : (Value_t) 0; } template Value_t CacheMap::get (Key_t key, typename Map::Relation rel) { if (rel != Map::REL_EQ) return (Value_t) 0; return get (key); } template Value_t CacheMap::remove (Key_t key) { Entry *entry = getEntry (key); Value_t res = (Value_t) 0; if (entry->key == key) { res = entry->val; entry->val = (Value_t) 0; } return res; } #endif