Fabcoin Core  0.16.2
P2P Digital Currency
CodeSizeCache.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
21 #pragma once
22 
23 #include <map>
24 #include <libdevcore/FixedHash.h>
25 #include <libdevcore/Guards.h>
26 
27 namespace dev
28 {
29 namespace eth
30 {
31 
37 {
38 public:
39  void store(h256 const& _hash, size_t size)
40  {
42  if (m_cache.size() >= c_maxSize)
44  m_cache[_hash] = size;
45  }
46  bool contains(h256 const& _hash) const
47  {
49  return m_cache.count(_hash);
50  }
51  size_t get(h256 const& _hash) const
52  {
54  return m_cache.at(_hash);
55  }
56 
57  static CodeSizeCache& instance() { static CodeSizeCache cache; return cache; }
58 
59 private:
62  {
63  if (!m_cache.empty())
64  {
65  auto it = m_cache.lower_bound(h256::random());
66  if (it == m_cache.end())
67  it = m_cache.begin();
68  m_cache.erase(it);
69  }
70  }
71 
72  static const size_t c_maxSize = 50000;
73  mutable Mutex x_cache;
74  std::map<h256, size_t> m_cache;
75 };
76 
77 }
78 }
79 
void removeRandomElement()
Removes a random element from the cache.
Definition: CodeSizeCache.h:61
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
#define g(i)
Definition: sha.cpp:735
std::unique_lock< std::mutex > UniqueGuard
Definition: Guards.h:42
void store(h256 const &_hash, size_t size)
Definition: CodeSizeCache.h:39
static const size_t c_maxSize
Definition: CodeSizeCache.h:72
Simple thread-safe cache to store a mapping from code hash to code size.
Definition: CodeSizeCache.h:36
bool contains(h256 const &_hash) const
Definition: CodeSizeCache.h:46
static FixedHash random()
Definition: FixedHash.h:162
std::map< h256, size_t > m_cache
Definition: CodeSizeCache.h:74
uint8_t const size_t const size
Definition: sha3.h:20
static CodeSizeCache & instance()
Definition: CodeSizeCache.h:57
std::mutex Mutex
Definition: Guards.h:37