Browse Source

Avoid 1GB memory leak. Downside: will seriously slow down mining.

cl-refactor
Gav Wood 10 years ago
parent
commit
51b2617b2e
  1. 3
      libethcore/Ethash.cpp
  2. 32
      libethcore/EthashAux.cpp
  3. 14
      libethcore/EthashAux.h

3
libethcore/Ethash.cpp

@ -132,7 +132,8 @@ void Ethash::CPUMiner::workLoop()
WorkPackage w = work();
auto p = EthashAux::params(w.seedHash);
void const* dagPointer = EthashAux::full(w.seedHash).data();
auto dag = EthashAux::full(w.seedHash);
auto dagPointer = dag->data.data();
uint8_t const* headerHashPointer = w.headerHash.data();
h256 boundary = w.boundary;
unsigned hashCount = 1;

32
libethcore/EthashAux.cpp

@ -135,29 +135,23 @@ void const* EthashAux::light(h256 const& _seedHash)
return get()->m_lights[_seedHash];
}
bytesConstRef EthashAux::full(BlockInfo const& _header, bytesRef _dest)
EthashAux::FullType EthashAux::full(BlockInfo const& _header, bytesRef _dest)
{
return full(_header.seedHash(), _dest);
}
bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest)
EthashAux::FullType EthashAux::full(h256 const& _seedHash, bytesRef _dest)
{
RecursiveGuard l(get()->x_this);
if (get()->m_fulls.count(_seedHash) && _dest)
FullType ret = get()->m_fulls[_seedHash].lock();
if (ret && _dest)
{
assert(get()->m_fulls.size() <= _dest.size());
get()->m_fulls.at(_seedHash).copyTo(_dest);
return _dest;
assert(ret->data.size() <= _dest.size());
ret->data.copyTo(_dest);
return FullType();
}
if (!get()->m_fulls.count(_seedHash))
if (!ret)
{
// @memoryleak @bug place it on a pile for deletion - perhaps use shared_ptr.
/* if (!m_fulls.empty())
{
delete [] m_fulls.begin()->second.data();
m_fulls.erase(m_fulls.begin());
}*/
try {
boost::filesystem::create_directories(getDataDir("ethash"));
} catch (...) {}
@ -190,10 +184,11 @@ bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest)
writeFile(memoFile, r);
}
if (_dest)
return _dest;
get()->m_fulls[_seedHash] = r;
return FullType();
ret = make_shared<FullTypeAllocation>(r);
get()->m_fulls[_seedHash] = ret;
}
return get()->m_fulls[_seedHash];
return ret;
}
Ethash::Result EthashAux::eval(BlockInfo const& _header, Nonce const& _nonce)
@ -205,8 +200,9 @@ Ethash::Result EthashAux::eval(h256 const& _seedHash, h256 const& _headerHash, N
{
auto p = EthashAux::params(_seedHash);
ethash_return_value r;
auto dag = EthashAux::get()->full(_seedHash);
if (EthashAux::get()->m_fulls.count(_seedHash))
ethash_compute_full(&r, EthashAux::get()->full(_seedHash).data(), &p, _headerHash.data(), (uint64_t)(u64)_nonce);
ethash_compute_full(&r, dag->data.data(), &p, _headerHash.data(), (uint64_t)(u64)_nonce);
else
ethash_compute_light(&r, EthashAux::get()->light(_seedHash), &p, _headerHash.data(), (uint64_t)(u64)_nonce);
// cdebug << "EthashAux::eval sha3(cache):" << sha3(EthashAux::get()->cache(_header)) << "hh:" << _header.headerHash(WithoutNonce) << "nonce:" << _nonce << " => " << h256(r.result, h256::ConstructFromPointer);

14
libethcore/EthashAux.h

@ -33,8 +33,14 @@ public:
static EthashAux* get() { if (!s_this) s_this = new EthashAux(); return s_this; }
struct FullTypeAllocation
{
FullTypeAllocation(bytesConstRef _d): data(_d) {}
~FullTypeAllocation() { delete [] data.data(); }
bytesConstRef const data;
};
using LightType = void const*;
using FullType = void const*;
using FullType = std::shared_ptr<FullTypeAllocation>;
static h256 seedHash(unsigned _number);
static ethash_params params(BlockInfo const& _header);
@ -42,8 +48,8 @@ public:
static ethash_params params(unsigned _n);
static LightType light(BlockInfo const& _header);
static LightType light(h256 const& _seedHash);
static bytesConstRef full(BlockInfo const& _header, bytesRef _dest = bytesRef());
static bytesConstRef full(h256 const& _header, bytesRef _dest = bytesRef());
static FullType full(BlockInfo const& _header, bytesRef _dest = bytesRef());
static FullType full(h256 const& _header, bytesRef _dest = bytesRef());
static Ethash::Result eval(BlockInfo const& _header) { return eval(_header, _header.nonce); }
static Ethash::Result eval(BlockInfo const& _header, Nonce const& _nonce);
@ -58,7 +64,7 @@ private:
RecursiveMutex x_this;
std::map<h256, LightType> m_lights;
std::map<h256, bytesRef> m_fulls;
std::map<h256, std::weak_ptr<FullTypeAllocation>> m_fulls;
std::map<h256, unsigned> m_epochs;
h256s m_seedHashes;
};

Loading…
Cancel
Save