Added memory hooks for CURL

This commit is contained in:
SChernykh 2022-06-07 16:02:08 +02:00
parent 75b9a0228b
commit 8b681d6efb
5 changed files with 33 additions and 13 deletions

View file

@ -333,6 +333,7 @@ void* malloc_hook(size_t n) noexcept;
void* realloc_hook(void* ptr, size_t size) noexcept;
void* calloc_hook(size_t count, size_t size) noexcept;
void free_hook(void* p) noexcept;
char* strdup_hook(const char* s) noexcept;
extern const char* BLOCK_FOUND;

View file

@ -20,6 +20,7 @@
#include "p2pool.h"
#include "stratum_server.h"
#include "p2p_server.h"
#include <curl/curl.h>
void p2pool_usage()
{
@ -81,12 +82,15 @@ int main(int argc, char* argv[])
}
}
int result;
memory_tracking_start();
p2pool::init_crypto_cache();
int result = static_cast<int>(curl_global_init_mem(CURL_GLOBAL_ALL, p2pool::malloc_hook, p2pool::free_hook, p2pool::realloc_hook, p2pool::strdup_hook, p2pool::calloc_hook));
if (result != CURLE_OK) {
return result;
}
try {
p2pool::p2pool pool(argc, argv);
result = pool.run();
@ -95,6 +99,8 @@ int main(int argc, char* argv[])
result = 1;
}
curl_global_cleanup();
p2pool::destroy_crypto_cache();
memory_tracking_stop();

View file

@ -144,6 +144,19 @@ void free_hook(void* p) noexcept
free(p);
}
char* strdup_hook(const char* s) noexcept
{
#ifdef _MSC_VER
char* s1 = _strdup(s);
#else
char* s1 = strdup(s);
#endif
if (s1) {
add_alocation(s1, strlen(s) + 1);
}
return s1;
}
void* realloc_hook(void* ptr, size_t size) noexcept
{
remove_allocation(ptr);
@ -248,5 +261,14 @@ void* realloc_hook(void* ptr, size_t size) noexcept { return realloc(ptr, size);
void* calloc_hook(size_t count, size_t size) noexcept { return calloc(count, size); }
void free_hook(void* p) noexcept { free(p); }
char* strdup_hook(const char* s) noexcept
{
#ifdef _MSC_VER
return _strdup(s);
#else
return strdup(s);
#endif
}
}
#endif

View file

@ -38,7 +38,6 @@
#include "keccak.h"
#include <thread>
#include <fstream>
#include <curl/curl.h>
constexpr char log_category_prefix[] = "P2Pool ";
constexpr int BLOCK_HEADERS_REQUIRED = 720;
@ -89,13 +88,7 @@ p2pool::p2pool(int argc, char* argv[])
LOGWARN(1, "Mining to a stagenet wallet address");
}
int err = static_cast<int>(curl_global_init(CURL_GLOBAL_ALL));
if (err != CURLE_OK) {
LOGERR(1, "Failed to initialize curl, error " << err);
throw std::exception();
}
err = uv_async_init(uv_default_loop_checked(), &m_submitBlockAsync, on_submit_block);
int err = uv_async_init(uv_default_loop_checked(), &m_submitBlockAsync, on_submit_block);
if (err) {
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
throw std::exception();
@ -183,8 +176,6 @@ p2pool::~p2pool()
delete m_mempool;
delete m_params;
delete m_consoleCommands;
curl_global_cleanup();
}
bool p2pool::calculate_hash(const void* data, size_t size, uint64_t height, const hash& seed, hash& result)

View file

@ -31,7 +31,7 @@ static constexpr uint64_t MIN_DIFF = 1000;
static constexpr uint64_t AUTO_DIFF_TARGET_TIME = 30;
// Use short target format (4 bytes) for diff <= 4 million
static constexpr uint64_t TARGET_4_BYTES_LIMIT = std::numeric_limits<uint64_t>::max() / 4000000 + 1;
static constexpr uint64_t TARGET_4_BYTES_LIMIT = std::numeric_limits<uint64_t>::max() / 4000001;
#include "tcp_server.inl"