MySQLnd: Remove mnd_malloc/free etc.

There were only two uses of non-zmm allocation functions left,
which really did not need to use the system allocator. Remove
them, and remove the system allocator based APIs.
This commit is contained in:
Nikita Popov 2020-12-17 17:01:09 +01:00
parent 8898c1e75c
commit 108d1e9a50
6 changed files with 4 additions and 213 deletions

View File

@ -308,9 +308,6 @@ ZEND_BEGIN_MODULE_GLOBALS(mysqlnd)
zend_long log_mask;
zend_long net_read_timeout;
zend_long mempool_default_size;
zend_long debug_malloc_fail_threshold;
zend_long debug_calloc_fail_threshold;
zend_long debug_realloc_fail_threshold;
char * sha256_server_public_key;
zend_bool collect_statistics;
zend_bool collect_memory_statistics;

View File

@ -32,10 +32,6 @@ static const char mysqlnd_erealloc_name[] = "_mysqlnd_erealloc";
static const char mysqlnd_perealloc_name[] = "_mysqlnd_perealloc";
static const char mysqlnd_efree_name[] = "_mysqlnd_efree";
static const char mysqlnd_pefree_name[] = "_mysqlnd_pefree";
static const char mysqlnd_malloc_name[] = "_mysqlnd_malloc";
static const char mysqlnd_calloc_name[] = "_mysqlnd_calloc";
static const char mysqlnd_realloc_name[] = "_mysqlnd_realloc";
static const char mysqlnd_free_name[] = "_mysqlnd_free";
static const char mysqlnd_pememdup_name[] = "_mysqlnd_pememdup";
static const char mysqlnd_pestrndup_name[] = "_mysqlnd_pestrndup";
static const char mysqlnd_pestrdup_name[] = "_mysqlnd_pestrdup";
@ -50,10 +46,6 @@ PHPAPI const char * mysqlnd_debug_std_no_trace_funcs[] =
mysqlnd_pecalloc_name,
mysqlnd_pefree_name,
mysqlnd_perealloc_name,
mysqlnd_malloc_name,
mysqlnd_calloc_name,
mysqlnd_realloc_name,
mysqlnd_free_name,
mysqlnd_pestrndup_name,
mysqlnd_read_header_name,
mysqlnd_read_body_name,
@ -262,147 +254,6 @@ static void _mysqlnd_pefree(void *ptr, zend_bool persistent MYSQLND_MEM_D)
/* }}} */
/* {{{ _mysqlnd_malloc */
static void * _mysqlnd_malloc(size_t size MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
#if PHP_DEBUG
zend_long * threshold = &MYSQLND_G(debug_malloc_fail_threshold);
TRACE_ALLOC_ENTER(mysqlnd_malloc_name);
{
char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
}
if (*threshold == 0) {
ret = NULL;
} else {
ret = malloc(REAL_SIZE(size));
--*threshold;
}
#else
TRACE_ALLOC_ENTER(mysqlnd_malloc_name);
ret = malloc(REAL_SIZE(size));
#endif
TRACE_ALLOC_INF_FMT("size=%lu ptr=%p", size, ret);
if (ret && collect_memory_statistics) {
*(size_t *) ret = size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_MALLOC_COUNT, 1, STAT_MEM_MALLOC_AMOUNT, size);
}
TRACE_ALLOC_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_calloc */
static void * _mysqlnd_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
#if PHP_DEBUG
zend_long * threshold = &MYSQLND_G(debug_calloc_fail_threshold);
TRACE_ALLOC_ENTER(mysqlnd_calloc_name);
{
char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
}
if (*threshold == 0) {
ret = NULL;
} else {
ret = calloc(nmemb, REAL_SIZE(size));
--*threshold;
}
#else
TRACE_ALLOC_ENTER(mysqlnd_calloc_name);
ret = calloc(nmemb, REAL_SIZE(size));
#endif
TRACE_ALLOC_INF_FMT("size=%lu ptr=%p", size, ret);
if (ret && collect_memory_statistics) {
*(size_t *) ret = size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_CALLOC_COUNT, 1, STAT_MEM_CALLOC_AMOUNT, size);
}
TRACE_ALLOC_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_realloc */
static void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D)
{
void *ret;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
#if PHP_DEBUG
zend_long * threshold = &MYSQLND_G(debug_realloc_fail_threshold);
TRACE_ALLOC_ENTER(mysqlnd_realloc_name);
{
char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
}
TRACE_ALLOC_INF_FMT("ptr=%p new_size=%lu ", new_size, ptr);
TRACE_ALLOC_INF_FMT("before: %lu", zend_memory_usage(TRUE));
if (*threshold == 0) {
ret = NULL;
} else {
ret = realloc(REAL_PTR(ptr), REAL_SIZE(new_size));
--*threshold;
}
#else
TRACE_ALLOC_ENTER(mysqlnd_realloc_name);
TRACE_ALLOC_INF_FMT("ptr=%p new_size=%lu ", new_size, ptr);
TRACE_ALLOC_INF_FMT("before: %lu", zend_memory_usage(TRUE));
ret = realloc(REAL_PTR(ptr), REAL_SIZE(new_size));
#endif
TRACE_ALLOC_INF_FMT("new_ptr=%p", (char*)ret);
if (ret && collect_memory_statistics) {
*(size_t *) ret = new_size;
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_REALLOC_COUNT, 1, STAT_MEM_REALLOC_AMOUNT, new_size);
}
TRACE_ALLOC_RETURN(FAKE_PTR(ret));
}
/* }}} */
/* {{{ _mysqlnd_free */
static void _mysqlnd_free(void *ptr MYSQLND_MEM_D)
{
size_t free_amount = 0;
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
TRACE_ALLOC_ENTER(mysqlnd_free_name);
#if PHP_DEBUG
{
char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
}
#endif
TRACE_ALLOC_INF_FMT("ptr=%p", ptr);
if (ptr) {
if (collect_memory_statistics) {
free_amount = *(size_t *)(((char*)ptr) - sizeof(size_t));
TRACE_ALLOC_INF_FMT("ptr=%p size=%u", ((char*)ptr) - sizeof(size_t), (unsigned int) free_amount);
}
free(REAL_PTR(ptr));
}
if (collect_memory_statistics) {
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_FREE_COUNT, 1, STAT_MEM_FREE_AMOUNT, free_amount);
}
TRACE_ALLOC_VOID_RETURN;
}
/* }}} */
/* {{{ _mysqlnd_pememdup */
static char * _mysqlnd_pememdup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
{
@ -607,38 +458,6 @@ static void mysqlnd_zend_mm_pefree(void * ptr, zend_bool persistent MYSQLND_MEM_
/* }}} */
/* {{{ mysqlnd_zend_mm_malloc */
static void * mysqlnd_zend_mm_malloc(size_t size MYSQLND_MEM_D)
{
return malloc(size);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_calloc */
static void * mysqlnd_zend_mm_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
{
return calloc(nmemb, size);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_realloc */
static void * mysqlnd_zend_mm_realloc(void * ptr, size_t new_size MYSQLND_MEM_D)
{
return realloc(ptr, new_size);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_free */
static void mysqlnd_zend_mm_free(void * ptr MYSQLND_MEM_D)
{
free(ptr);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_pememdup */
static char * mysqlnd_zend_mm_pememdup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
{
@ -680,10 +499,6 @@ PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator =
_mysqlnd_perealloc,
_mysqlnd_efree,
_mysqlnd_pefree,
_mysqlnd_malloc,
_mysqlnd_calloc,
_mysqlnd_realloc,
_mysqlnd_free,
_mysqlnd_pememdup,
_mysqlnd_pestrndup,
_mysqlnd_pestrdup,
@ -699,10 +514,6 @@ PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator =
mysqlnd_zend_mm_perealloc,
mysqlnd_zend_mm_efree,
mysqlnd_zend_mm_pefree,
mysqlnd_zend_mm_malloc,
mysqlnd_zend_mm_calloc,
mysqlnd_zend_mm_realloc,
mysqlnd_zend_mm_free,
mysqlnd_zend_mm_pememdup,
mysqlnd_zend_mm_pestrndup,
mysqlnd_zend_mm_pestrdup,

View File

@ -33,10 +33,6 @@ struct st_mysqlnd_allocator_methods
void * (*m_perealloc)(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D);
void (*m_efree)(void *ptr MYSQLND_MEM_D);
void (*m_pefree)(void *ptr, zend_bool persistent MYSQLND_MEM_D);
void * (*m_malloc)(size_t size MYSQLND_MEM_D);
void * (*m_calloc)(unsigned int nmemb, size_t size MYSQLND_MEM_D);
void * (*m_realloc)(void *ptr, size_t new_size MYSQLND_MEM_D);
void (*m_free)(void *ptr MYSQLND_MEM_D);
char * (*m_pememdup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
char * (*m_pestrndup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
char * (*m_pestrdup)(const char * const ptr, zend_bool persistent MYSQLND_MEM_D);
@ -55,10 +51,6 @@ PHPAPI extern struct st_mysqlnd_allocator_methods mysqlnd_allocator;
#define mnd_perealloc(ptr, new_size, p) mysqlnd_allocator.m_perealloc((ptr), (new_size), (p) MYSQLND_MEM_C)
#define mnd_efree(ptr) mysqlnd_allocator.m_efree((ptr) MYSQLND_MEM_C)
#define mnd_pefree(ptr, pers) mysqlnd_allocator.m_pefree((ptr), (pers) MYSQLND_MEM_C)
#define mnd_malloc(size) mysqlnd_allocator.m_malloc((size) MYSQLND_MEM_C)
#define mnd_calloc(nmemb, size) mysqlnd_allocator.m_calloc((nmemb), (size) MYSQLND_MEM_C)
#define mnd_realloc(ptr, new_size) mysqlnd_allocator.m_realloc((ptr), (new_size) MYSQLND_MEM_C)
#define mnd_free(ptr) mysqlnd_allocator.m_free((ptr) MYSQLND_MEM_C)
#define mnd_pememdup(ptr, size, pers) mysqlnd_allocator.m_pememdup((ptr), (size), (pers) MYSQLND_MEM_C)
#define mnd_pestrndup(ptr, size, pers) mysqlnd_allocator.m_pestrndup((ptr), (size), (pers) MYSQLND_MEM_C)
#define mnd_pestrdup(ptr, pers) mysqlnd_allocator.m_pestrdup((ptr), (pers) MYSQLND_MEM_C)

View File

@ -77,7 +77,7 @@ static ssize_t write_compressed_packet(
#ifdef WHEN_WE_NEED_TO_CHECK_WHETHER_COMPRESSION_WORKS_CORRECTLY
if (res == Z_OK) {
size_t decompressed_size = left + MYSQLND_HEADER_SIZE;
zend_uchar * decompressed_data = mnd_malloc(decompressed_size);
zend_uchar * decompressed_data = mnd_emalloc(decompressed_size);
int error = pfc->data->m.decode(decompressed_data, decompressed_size,
compress_buf + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE, payload_size);
if (error == Z_OK) {
@ -93,7 +93,7 @@ static ssize_t write_compressed_packet(
} else {
DBG_INF("error decompressing");
}
mnd_free(decompressed_data);
mnd_efree(decompressed_data);
}
#endif /* WHEN_WE_NEED_TO_CHECK_WHETHER_COMPRESSION_WORKS_CORRECTLY */
DBG_RETURN(bytes_sent);

View File

@ -437,11 +437,11 @@ MYSQLND_METHOD(mysqlnd_stmt, prepare)(MYSQLND_STMT * const s, const char * const
if (stmt_to_prepare != stmt) {
/* swap */
size_t real_size = sizeof(MYSQLND_STMT) + mysqlnd_plugin_count() * sizeof(void *);
char * tmp_swap = mnd_malloc(real_size);
char * tmp_swap = mnd_emalloc(real_size);
memcpy(tmp_swap, s, real_size);
memcpy(s, s_to_prepare, real_size);
memcpy(s_to_prepare, tmp_swap, real_size);
mnd_free(tmp_swap);
mnd_efree(tmp_swap);
{
MYSQLND_STMT_DATA * tmp_swap_data = stmt_to_prepare;
stmt_to_prepare = stmt;

View File

@ -149,9 +149,6 @@ static PHP_GINIT_FUNCTION(mysqlnd)
mysqlnd_globals->net_read_timeout = 31536000;
mysqlnd_globals->log_mask = 0;
mysqlnd_globals->mempool_default_size = 16000;
mysqlnd_globals->debug_malloc_fail_threshold = -1;
mysqlnd_globals->debug_calloc_fail_threshold = -1;
mysqlnd_globals->debug_realloc_fail_threshold = -1;
mysqlnd_globals->sha256_server_public_key = NULL;
}
/* }}} */
@ -185,12 +182,6 @@ PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("mysqlnd.log_mask", "0", PHP_INI_ALL, OnUpdateLong, log_mask, zend_mysqlnd_globals, mysqlnd_globals)
STD_PHP_INI_ENTRY("mysqlnd.mempool_default_size","16000", PHP_INI_ALL, OnUpdateLong, mempool_default_size, zend_mysqlnd_globals, mysqlnd_globals)
STD_PHP_INI_ENTRY("mysqlnd.sha256_server_public_key",NULL, PHP_INI_PERDIR, OnUpdateString, sha256_server_public_key, zend_mysqlnd_globals, mysqlnd_globals)
#if PHP_DEBUG
STD_PHP_INI_ENTRY("mysqlnd.debug_malloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_malloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals)
STD_PHP_INI_ENTRY("mysqlnd.debug_calloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_calloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals)
STD_PHP_INI_ENTRY("mysqlnd.debug_realloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_realloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals)
#endif
PHP_INI_END()
/* }}} */