Skip to content

Commit 108d1e9

Browse files
committed
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.
1 parent 8898c1e commit 108d1e9

File tree

6 files changed

+4
-213
lines changed

6 files changed

+4
-213
lines changed

ext/mysqlnd/mysqlnd.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,6 @@ ZEND_BEGIN_MODULE_GLOBALS(mysqlnd)
308308
zend_long log_mask;
309309
zend_long net_read_timeout;
310310
zend_long mempool_default_size;
311-
zend_long debug_malloc_fail_threshold;
312-
zend_long debug_calloc_fail_threshold;
313-
zend_long debug_realloc_fail_threshold;
314311
char * sha256_server_public_key;
315312
zend_bool collect_statistics;
316313
zend_bool collect_memory_statistics;

ext/mysqlnd/mysqlnd_alloc.c

Lines changed: 0 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ static const char mysqlnd_erealloc_name[] = "_mysqlnd_erealloc";
3232
static const char mysqlnd_perealloc_name[] = "_mysqlnd_perealloc";
3333
static const char mysqlnd_efree_name[] = "_mysqlnd_efree";
3434
static const char mysqlnd_pefree_name[] = "_mysqlnd_pefree";
35-
static const char mysqlnd_malloc_name[] = "_mysqlnd_malloc";
36-
static const char mysqlnd_calloc_name[] = "_mysqlnd_calloc";
37-
static const char mysqlnd_realloc_name[] = "_mysqlnd_realloc";
38-
static const char mysqlnd_free_name[] = "_mysqlnd_free";
3935
static const char mysqlnd_pememdup_name[] = "_mysqlnd_pememdup";
4036
static const char mysqlnd_pestrndup_name[] = "_mysqlnd_pestrndup";
4137
static const char mysqlnd_pestrdup_name[] = "_mysqlnd_pestrdup";
@@ -50,10 +46,6 @@ PHPAPI const char * mysqlnd_debug_std_no_trace_funcs[] =
5046
mysqlnd_pecalloc_name,
5147
mysqlnd_pefree_name,
5248
mysqlnd_perealloc_name,
53-
mysqlnd_malloc_name,
54-
mysqlnd_calloc_name,
55-
mysqlnd_realloc_name,
56-
mysqlnd_free_name,
5749
mysqlnd_pestrndup_name,
5850
mysqlnd_read_header_name,
5951
mysqlnd_read_body_name,
@@ -262,147 +254,6 @@ static void _mysqlnd_pefree(void *ptr, zend_bool persistent MYSQLND_MEM_D)
262254
/* }}} */
263255

264256

265-
/* {{{ _mysqlnd_malloc */
266-
static void * _mysqlnd_malloc(size_t size MYSQLND_MEM_D)
267-
{
268-
void *ret;
269-
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
270-
#if PHP_DEBUG
271-
zend_long * threshold = &MYSQLND_G(debug_malloc_fail_threshold);
272-
TRACE_ALLOC_ENTER(mysqlnd_malloc_name);
273-
274-
{
275-
char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
276-
TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
277-
}
278-
279-
if (*threshold == 0) {
280-
ret = NULL;
281-
} else {
282-
ret = malloc(REAL_SIZE(size));
283-
--*threshold;
284-
}
285-
#else
286-
TRACE_ALLOC_ENTER(mysqlnd_malloc_name);
287-
ret = malloc(REAL_SIZE(size));
288-
#endif
289-
290-
TRACE_ALLOC_INF_FMT("size=%lu ptr=%p", size, ret);
291-
if (ret && collect_memory_statistics) {
292-
*(size_t *) ret = size;
293-
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_MALLOC_COUNT, 1, STAT_MEM_MALLOC_AMOUNT, size);
294-
}
295-
TRACE_ALLOC_RETURN(FAKE_PTR(ret));
296-
}
297-
/* }}} */
298-
299-
300-
/* {{{ _mysqlnd_calloc */
301-
static void * _mysqlnd_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
302-
{
303-
void *ret;
304-
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
305-
#if PHP_DEBUG
306-
zend_long * threshold = &MYSQLND_G(debug_calloc_fail_threshold);
307-
TRACE_ALLOC_ENTER(mysqlnd_calloc_name);
308-
309-
{
310-
char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
311-
TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
312-
}
313-
314-
if (*threshold == 0) {
315-
ret = NULL;
316-
} else {
317-
ret = calloc(nmemb, REAL_SIZE(size));
318-
--*threshold;
319-
}
320-
#else
321-
TRACE_ALLOC_ENTER(mysqlnd_calloc_name);
322-
ret = calloc(nmemb, REAL_SIZE(size));
323-
#endif
324-
325-
TRACE_ALLOC_INF_FMT("size=%lu ptr=%p", size, ret);
326-
if (ret && collect_memory_statistics) {
327-
*(size_t *) ret = size;
328-
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_CALLOC_COUNT, 1, STAT_MEM_CALLOC_AMOUNT, size);
329-
}
330-
TRACE_ALLOC_RETURN(FAKE_PTR(ret));
331-
}
332-
/* }}} */
333-
334-
335-
/* {{{ _mysqlnd_realloc */
336-
static void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D)
337-
{
338-
void *ret;
339-
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
340-
#if PHP_DEBUG
341-
zend_long * threshold = &MYSQLND_G(debug_realloc_fail_threshold);
342-
TRACE_ALLOC_ENTER(mysqlnd_realloc_name);
343-
344-
{
345-
char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
346-
TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
347-
}
348-
TRACE_ALLOC_INF_FMT("ptr=%p new_size=%lu ", new_size, ptr);
349-
TRACE_ALLOC_INF_FMT("before: %lu", zend_memory_usage(TRUE));
350-
351-
if (*threshold == 0) {
352-
ret = NULL;
353-
} else {
354-
ret = realloc(REAL_PTR(ptr), REAL_SIZE(new_size));
355-
--*threshold;
356-
}
357-
#else
358-
TRACE_ALLOC_ENTER(mysqlnd_realloc_name);
359-
TRACE_ALLOC_INF_FMT("ptr=%p new_size=%lu ", new_size, ptr);
360-
TRACE_ALLOC_INF_FMT("before: %lu", zend_memory_usage(TRUE));
361-
ret = realloc(REAL_PTR(ptr), REAL_SIZE(new_size));
362-
#endif
363-
364-
TRACE_ALLOC_INF_FMT("new_ptr=%p", (char*)ret);
365-
366-
if (ret && collect_memory_statistics) {
367-
*(size_t *) ret = new_size;
368-
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_REALLOC_COUNT, 1, STAT_MEM_REALLOC_AMOUNT, new_size);
369-
}
370-
TRACE_ALLOC_RETURN(FAKE_PTR(ret));
371-
}
372-
/* }}} */
373-
374-
375-
/* {{{ _mysqlnd_free */
376-
static void _mysqlnd_free(void *ptr MYSQLND_MEM_D)
377-
{
378-
size_t free_amount = 0;
379-
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
380-
TRACE_ALLOC_ENTER(mysqlnd_free_name);
381-
382-
#if PHP_DEBUG
383-
{
384-
char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
385-
TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
386-
}
387-
#endif
388-
TRACE_ALLOC_INF_FMT("ptr=%p", ptr);
389-
390-
if (ptr) {
391-
if (collect_memory_statistics) {
392-
free_amount = *(size_t *)(((char*)ptr) - sizeof(size_t));
393-
TRACE_ALLOC_INF_FMT("ptr=%p size=%u", ((char*)ptr) - sizeof(size_t), (unsigned int) free_amount);
394-
}
395-
free(REAL_PTR(ptr));
396-
}
397-
398-
if (collect_memory_statistics) {
399-
MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_FREE_COUNT, 1, STAT_MEM_FREE_AMOUNT, free_amount);
400-
}
401-
TRACE_ALLOC_VOID_RETURN;
402-
}
403-
/* }}} */
404-
405-
406257
/* {{{ _mysqlnd_pememdup */
407258
static char * _mysqlnd_pememdup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
408259
{
@@ -607,38 +458,6 @@ static void mysqlnd_zend_mm_pefree(void * ptr, zend_bool persistent MYSQLND_MEM_
607458
/* }}} */
608459

609460

610-
/* {{{ mysqlnd_zend_mm_malloc */
611-
static void * mysqlnd_zend_mm_malloc(size_t size MYSQLND_MEM_D)
612-
{
613-
return malloc(size);
614-
}
615-
/* }}} */
616-
617-
618-
/* {{{ mysqlnd_zend_mm_calloc */
619-
static void * mysqlnd_zend_mm_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
620-
{
621-
return calloc(nmemb, size);
622-
}
623-
/* }}} */
624-
625-
626-
/* {{{ mysqlnd_zend_mm_realloc */
627-
static void * mysqlnd_zend_mm_realloc(void * ptr, size_t new_size MYSQLND_MEM_D)
628-
{
629-
return realloc(ptr, new_size);
630-
}
631-
/* }}} */
632-
633-
634-
/* {{{ mysqlnd_zend_mm_free */
635-
static void mysqlnd_zend_mm_free(void * ptr MYSQLND_MEM_D)
636-
{
637-
free(ptr);
638-
}
639-
/* }}} */
640-
641-
642461
/* {{{ mysqlnd_zend_mm_pememdup */
643462
static char * mysqlnd_zend_mm_pememdup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
644463
{
@@ -680,10 +499,6 @@ PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator =
680499
_mysqlnd_perealloc,
681500
_mysqlnd_efree,
682501
_mysqlnd_pefree,
683-
_mysqlnd_malloc,
684-
_mysqlnd_calloc,
685-
_mysqlnd_realloc,
686-
_mysqlnd_free,
687502
_mysqlnd_pememdup,
688503
_mysqlnd_pestrndup,
689504
_mysqlnd_pestrdup,
@@ -699,10 +514,6 @@ PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator =
699514
mysqlnd_zend_mm_perealloc,
700515
mysqlnd_zend_mm_efree,
701516
mysqlnd_zend_mm_pefree,
702-
mysqlnd_zend_mm_malloc,
703-
mysqlnd_zend_mm_calloc,
704-
mysqlnd_zend_mm_realloc,
705-
mysqlnd_zend_mm_free,
706517
mysqlnd_zend_mm_pememdup,
707518
mysqlnd_zend_mm_pestrndup,
708519
mysqlnd_zend_mm_pestrdup,

ext/mysqlnd/mysqlnd_alloc.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ struct st_mysqlnd_allocator_methods
3333
void * (*m_perealloc)(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D);
3434
void (*m_efree)(void *ptr MYSQLND_MEM_D);
3535
void (*m_pefree)(void *ptr, zend_bool persistent MYSQLND_MEM_D);
36-
void * (*m_malloc)(size_t size MYSQLND_MEM_D);
37-
void * (*m_calloc)(unsigned int nmemb, size_t size MYSQLND_MEM_D);
38-
void * (*m_realloc)(void *ptr, size_t new_size MYSQLND_MEM_D);
39-
void (*m_free)(void *ptr MYSQLND_MEM_D);
4036
char * (*m_pememdup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
4137
char * (*m_pestrndup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
4238
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;
5551
#define mnd_perealloc(ptr, new_size, p) mysqlnd_allocator.m_perealloc((ptr), (new_size), (p) MYSQLND_MEM_C)
5652
#define mnd_efree(ptr) mysqlnd_allocator.m_efree((ptr) MYSQLND_MEM_C)
5753
#define mnd_pefree(ptr, pers) mysqlnd_allocator.m_pefree((ptr), (pers) MYSQLND_MEM_C)
58-
#define mnd_malloc(size) mysqlnd_allocator.m_malloc((size) MYSQLND_MEM_C)
59-
#define mnd_calloc(nmemb, size) mysqlnd_allocator.m_calloc((nmemb), (size) MYSQLND_MEM_C)
60-
#define mnd_realloc(ptr, new_size) mysqlnd_allocator.m_realloc((ptr), (new_size) MYSQLND_MEM_C)
61-
#define mnd_free(ptr) mysqlnd_allocator.m_free((ptr) MYSQLND_MEM_C)
6254
#define mnd_pememdup(ptr, size, pers) mysqlnd_allocator.m_pememdup((ptr), (size), (pers) MYSQLND_MEM_C)
6355
#define mnd_pestrndup(ptr, size, pers) mysqlnd_allocator.m_pestrndup((ptr), (size), (pers) MYSQLND_MEM_C)
6456
#define mnd_pestrdup(ptr, pers) mysqlnd_allocator.m_pestrdup((ptr), (pers) MYSQLND_MEM_C)

ext/mysqlnd/mysqlnd_protocol_frame_codec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static ssize_t write_compressed_packet(
7777
#ifdef WHEN_WE_NEED_TO_CHECK_WHETHER_COMPRESSION_WORKS_CORRECTLY
7878
if (res == Z_OK) {
7979
size_t decompressed_size = left + MYSQLND_HEADER_SIZE;
80-
zend_uchar * decompressed_data = mnd_malloc(decompressed_size);
80+
zend_uchar * decompressed_data = mnd_emalloc(decompressed_size);
8181
int error = pfc->data->m.decode(decompressed_data, decompressed_size,
8282
compress_buf + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE, payload_size);
8383
if (error == Z_OK) {
@@ -93,7 +93,7 @@ static ssize_t write_compressed_packet(
9393
} else {
9494
DBG_INF("error decompressing");
9595
}
96-
mnd_free(decompressed_data);
96+
mnd_efree(decompressed_data);
9797
}
9898
#endif /* WHEN_WE_NEED_TO_CHECK_WHETHER_COMPRESSION_WORKS_CORRECTLY */
9999
DBG_RETURN(bytes_sent);

ext/mysqlnd/mysqlnd_ps.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,11 @@ MYSQLND_METHOD(mysqlnd_stmt, prepare)(MYSQLND_STMT * const s, const char * const
437437
if (stmt_to_prepare != stmt) {
438438
/* swap */
439439
size_t real_size = sizeof(MYSQLND_STMT) + mysqlnd_plugin_count() * sizeof(void *);
440-
char * tmp_swap = mnd_malloc(real_size);
440+
char * tmp_swap = mnd_emalloc(real_size);
441441
memcpy(tmp_swap, s, real_size);
442442
memcpy(s, s_to_prepare, real_size);
443443
memcpy(s_to_prepare, tmp_swap, real_size);
444-
mnd_free(tmp_swap);
444+
mnd_efree(tmp_swap);
445445
{
446446
MYSQLND_STMT_DATA * tmp_swap_data = stmt_to_prepare;
447447
stmt_to_prepare = stmt;

ext/mysqlnd/php_mysqlnd.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,6 @@ static PHP_GINIT_FUNCTION(mysqlnd)
149149
mysqlnd_globals->net_read_timeout = 31536000;
150150
mysqlnd_globals->log_mask = 0;
151151
mysqlnd_globals->mempool_default_size = 16000;
152-
mysqlnd_globals->debug_malloc_fail_threshold = -1;
153-
mysqlnd_globals->debug_calloc_fail_threshold = -1;
154-
mysqlnd_globals->debug_realloc_fail_threshold = -1;
155152
mysqlnd_globals->sha256_server_public_key = NULL;
156153
}
157154
/* }}} */
@@ -185,12 +182,6 @@ PHP_INI_BEGIN()
185182
STD_PHP_INI_ENTRY("mysqlnd.log_mask", "0", PHP_INI_ALL, OnUpdateLong, log_mask, zend_mysqlnd_globals, mysqlnd_globals)
186183
STD_PHP_INI_ENTRY("mysqlnd.mempool_default_size","16000", PHP_INI_ALL, OnUpdateLong, mempool_default_size, zend_mysqlnd_globals, mysqlnd_globals)
187184
STD_PHP_INI_ENTRY("mysqlnd.sha256_server_public_key",NULL, PHP_INI_PERDIR, OnUpdateString, sha256_server_public_key, zend_mysqlnd_globals, mysqlnd_globals)
188-
#if PHP_DEBUG
189-
190-
STD_PHP_INI_ENTRY("mysqlnd.debug_malloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_malloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals)
191-
STD_PHP_INI_ENTRY("mysqlnd.debug_calloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_calloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals)
192-
STD_PHP_INI_ENTRY("mysqlnd.debug_realloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_realloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals)
193-
#endif
194185
PHP_INI_END()
195186
/* }}} */
196187

0 commit comments

Comments
 (0)