aboutsummaryrefslogtreecommitdiff
path: root/gprofng/src/DbeCacheMap.h
blob: c8a6475c70a1a8859d47bf805f7c386f5b6cf832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Copyright (C) 2021-2023 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.  */

/*
 *	Dbe Cache Map implementation.
 *
 *    Simple Cache Map makes the following assumptions:
 *    - Cache Map is used for very fast but not guaranteed mapping;
 *    - No Relations can be used;
 *    - all objects used as keys or values has to be managed
 *      outside CacheMap (f.e. deletion);
 */

#ifndef _DbeCacheMap_h
#define _DbeCacheMap_h

#include <Map.h>

template <typename Key_t, class ITEM>
class DbeCacheMap : public Map<Key_t, ITEM *>
{
public:

  DbeCacheMap (int _size = DefaultSize)
  { // _size should be 2 ** N
    size = _size;
    table = new DbeCache_T[size];
    memset (table, 0, size * sizeof (DbeCache_T));
  };

  ~DbeCacheMap ()
  {
    delete[] table;
  };

  void
  put (Key_t key, ITEM *val)
  {
    int ind = get_hash (key);
    table[ind].key = key;
    table[ind].value = val;
  };

  ITEM *
  get (Key_t key)
  {
    int ind = get_hash (key);
    if (table[ind].key == key)
      return table[ind].value;
    return (ITEM *) NULL;
  };

  ITEM *
  remove (Key_t key)
  {
    int ind = get_hash (key);
    ITEM *v = table[ind].value;
    table[ind].value = (ITEM *) NULL;
    return v;
  };

  ITEM *
  get (Key_t /* key */, typename Map<Key_t, ITEM *>::Relation /* rel */)
  {
    return (ITEM *) NULL;
  };

private:

  enum
  {
    DefaultSize     = (1 << 13)
  };

  typedef struct DbeCache_S
  {
    Key_t key;
    ITEM *value;
  } DbeCache_T;
  DbeCache_T *table;
  int size;

  int
  get_hash (Key_t key)
  {
    unsigned long long h = (unsigned long long) key;
    h ^= (h >> 20) ^ (h >> 12);
    return (h ^ (h >> 7) ^ (h >> 4)) & (size - 1);
  }
};

#endif