Ethereum
PoC-8
The C++ Implementation of Ethereum
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
36
class
CodeSizeCache
37
{
38
public
:
39
void
store
(
h256
const
& _hash,
size_t
size)
40
{
41
UniqueGuard
g(x_cache);
42
if
(m_cache.size() >= c_maxSize)
43
removeRandomElement();
44
m_cache[_hash] = size;
45
}
46
bool
contains
(
h256
const
& _hash)
const
47
{
48
UniqueGuard
g(x_cache);
49
return
m_cache.count(_hash);
50
}
51
size_t
get
(
h256
const
& _hash)
const
52
{
53
UniqueGuard
g(x_cache);
54
return
m_cache.at(_hash);
55
}
56
57
static
CodeSizeCache
&
instance
() {
static
CodeSizeCache
cache;
return
cache; }
58
59
private
:
61
void
removeRandomElement()
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
dev::UniqueGuard
std::unique_lock< std::mutex > UniqueGuard
Definition:
Guards.h:42
dev::eth::CodeSizeCache::instance
static CodeSizeCache & instance()
Definition:
CodeSizeCache.h:57
FixedHash.h
dev::eth::CodeSizeCache::get
size_t get(h256 const &_hash) const
Definition:
CodeSizeCache.h:51
dev::eth::CodeSizeCache::store
void store(h256 const &_hash, size_t size)
Definition:
CodeSizeCache.h:39
dev::FixedHash< 32 >
dev::eth::CodeSizeCache
Simple thread-safe cache to store a mapping from code hash to code size. If the cache is full,...
Definition:
CodeSizeCache.h:37
dev::FixedHash< 32 >::random
static FixedHash random()
Definition:
FixedHash.h:167
dev::eth::CodeSizeCache::contains
bool contains(h256 const &_hash) const
Definition:
CodeSizeCache.h:46
dev::Mutex
std::mutex Mutex
Definition:
Guards.h:37
dev
Definition:
Address.cpp:21
Guards.h
libethereum
CodeSizeCache.h
Generated on Fri Sep 20 2019 00:00:00 for Ethereum by
1.8.18