Skip to content

Commit a19173e

Browse files
committed
- Make functions static, as they are not needed to be public - exported
through a structure - Fixed typo in statistic name - Added 2 static functions for copying a MYSQLND_CSTRING and converting a CSTRING to STRING.
1 parent c1b73ff commit a19173e

File tree

5 files changed

+97
-43
lines changed

5 files changed

+97
-43
lines changed

ext/mysqli/tests/mysqli_get_client_stats.phpt

+6-2
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ if (!mysqli_query($link, "DROP SERVER IF EXISTS myself"))
958958
mysqli_close($link);
959959
?>
960960
--EXPECTF--
961-
array(161) {
961+
array(163) {
962962
[%u|b%"bytes_sent"]=>
963963
%unicode|string%(1) "0"
964964
[%u|b%"bytes_received"]=>
@@ -1125,10 +1125,14 @@ array(161) {
11251125
%unicode|string%(1) "0"
11261126
[%u|b%"mem_strndup_count"]=>
11271127
%unicode|string%(1) "0"
1128-
[%u|b%"mem_estndup_count"]=>
1128+
[%u|b%"mem_estrdup_count"]=>
11291129
%unicode|string%(1) "0"
11301130
[%u|b%"mem_strdup_count"]=>
11311131
%unicode|string%(1) "0"
1132+
[%u|b%"mem_edupl_count"]=>
1133+
%unicode|string%(1) "0"
1134+
[%u|b%"mem_dupl_count"]=>
1135+
%unicode|string%(1) "0"
11321136
[%u|b%"proto_text_fetched_null"]=>
11331137
%unicode|string%(1) "0"
11341138
[%u|b%"proto_text_fetched_bit"]=>

ext/mysqlnd/mysqlnd_alloc.c

+69-22
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static const char mysqlnd_malloc_name[] = "_mysqlnd_malloc";
3838
static const char mysqlnd_calloc_name[] = "_mysqlnd_calloc";
3939
static const char mysqlnd_realloc_name[] = "_mysqlnd_realloc";
4040
static const char mysqlnd_free_name[] = "_mysqlnd_free";
41+
static const char mysqlnd_pememdup_name[] = "_mysqlnd_pememdup";
4142
static const char mysqlnd_pestrndup_name[] = "_mysqlnd_pestrndup";
4243
static const char mysqlnd_pestrdup_name[] = "_mysqlnd_pestrdup";
4344

@@ -73,7 +74,7 @@ PHPAPI const char * mysqlnd_debug_std_no_trace_funcs[] =
7374
#define FAKE_PTR(p) (collect_memory_statistics && (p)? (((char *)(p)) + sizeof(size_t)) : (p))
7475

7576
/* {{{ _mysqlnd_emalloc */
76-
void * _mysqlnd_emalloc(size_t size MYSQLND_MEM_D)
77+
static void * _mysqlnd_emalloc(size_t size MYSQLND_MEM_D)
7778
{
7879
void *ret;
7980
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -113,7 +114,7 @@ void * _mysqlnd_emalloc(size_t size MYSQLND_MEM_D)
113114

114115

115116
/* {{{ _mysqlnd_pemalloc */
116-
void * _mysqlnd_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D)
117+
static void * _mysqlnd_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D)
117118
{
118119
void *ret;
119120
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -156,7 +157,7 @@ void * _mysqlnd_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D)
156157

157158

158159
/* {{{ _mysqlnd_ecalloc */
159-
void * _mysqlnd_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
160+
static void * _mysqlnd_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
160161
{
161162
void *ret;
162163
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -197,7 +198,7 @@ void * _mysqlnd_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
197198

198199

199200
/* {{{ _mysqlnd_pecalloc */
200-
void * _mysqlnd_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent MYSQLND_MEM_D)
201+
static void * _mysqlnd_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent MYSQLND_MEM_D)
201202
{
202203
void *ret;
203204
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -239,7 +240,7 @@ void * _mysqlnd_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent M
239240

240241

241242
/* {{{ _mysqlnd_erealloc */
242-
void * _mysqlnd_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D)
243+
static void * _mysqlnd_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D)
243244
{
244245
void *ret;
245246
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -280,7 +281,7 @@ void * _mysqlnd_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D)
280281

281282

282283
/* {{{ _mysqlnd_perealloc */
283-
void * _mysqlnd_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D)
284+
static void * _mysqlnd_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D)
284285
{
285286
void *ret;
286287
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -324,7 +325,7 @@ void * _mysqlnd_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQL
324325

325326

326327
/* {{{ _mysqlnd_efree */
327-
void _mysqlnd_efree(void *ptr MYSQLND_MEM_D)
328+
static void _mysqlnd_efree(void *ptr MYSQLND_MEM_D)
328329
{
329330
size_t free_amount = 0;
330331
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -355,7 +356,7 @@ void _mysqlnd_efree(void *ptr MYSQLND_MEM_D)
355356

356357

357358
/* {{{ _mysqlnd_pefree */
358-
void _mysqlnd_pefree(void *ptr, zend_bool persistent MYSQLND_MEM_D)
359+
static void _mysqlnd_pefree(void *ptr, zend_bool persistent MYSQLND_MEM_D)
359360
{
360361
size_t free_amount = 0;
361362
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -387,7 +388,7 @@ void _mysqlnd_pefree(void *ptr, zend_bool persistent MYSQLND_MEM_D)
387388

388389

389390
/* {{{ _mysqlnd_malloc */
390-
void * _mysqlnd_malloc(size_t size MYSQLND_MEM_D)
391+
static void * _mysqlnd_malloc(size_t size MYSQLND_MEM_D)
391392
{
392393
void *ret;
393394
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -426,7 +427,7 @@ void * _mysqlnd_malloc(size_t size MYSQLND_MEM_D)
426427

427428

428429
/* {{{ _mysqlnd_calloc */
429-
void * _mysqlnd_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
430+
static void * _mysqlnd_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
430431
{
431432
void *ret;
432433
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -465,7 +466,7 @@ void * _mysqlnd_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
465466

466467

467468
/* {{{ _mysqlnd_realloc */
468-
void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D)
469+
static void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D)
469470
{
470471
void *ret;
471472
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -507,7 +508,7 @@ void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D)
507508

508509

509510
/* {{{ _mysqlnd_free */
510-
void _mysqlnd_free(void *ptr MYSQLND_MEM_D)
511+
static void _mysqlnd_free(void *ptr MYSQLND_MEM_D)
511512
{
512513
size_t free_amount = 0;
513514
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -536,13 +537,40 @@ void _mysqlnd_free(void *ptr MYSQLND_MEM_D)
536537
}
537538
/* }}} */
538539

539-
#define SMART_STR_START_SIZE 2048
540-
#define SMART_STR_PREALLOC 512
541-
#include "zend_smart_str.h"
540+
541+
/* {{{ _mysqlnd_pememdup */
542+
static char * _mysqlnd_pememdup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
543+
{
544+
char * ret;
545+
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
546+
TRACE_ALLOC_ENTER(mysqlnd_pememdup_name);
547+
548+
#if PHP_DEBUG
549+
{
550+
char * fn = strrchr(__zend_orig_filename, PHP_DIR_SEPARATOR);
551+
TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_orig_filename, __zend_orig_lineno);
552+
}
553+
#endif
554+
TRACE_ALLOC_INF_FMT("ptr=%p", ptr);
555+
556+
ret = (persistent) ? __zend_malloc(REAL_SIZE(length + 1)) : _emalloc(REAL_SIZE(length + 1) ZEND_FILE_LINE_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
557+
{
558+
char * dest = (char *) FAKE_PTR(ret);
559+
memcpy(dest, ptr, length);
560+
}
561+
562+
if (collect_memory_statistics) {
563+
*(size_t *) ret = length;
564+
MYSQLND_INC_GLOBAL_STATISTIC(persistent? STAT_MEM_DUP_COUNT : STAT_MEM_EDUP_COUNT);
565+
}
566+
567+
TRACE_ALLOC_RETURN(FAKE_PTR(ret));
568+
}
569+
/* }}} */
542570

543571

544572
/* {{{ _mysqlnd_pestrndup */
545-
char * _mysqlnd_pestrndup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
573+
static char * _mysqlnd_pestrndup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
546574
{
547575
char * ret;
548576
zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
@@ -577,8 +605,13 @@ char * _mysqlnd_pestrndup(const char * const ptr, size_t length, zend_bool persi
577605
/* }}} */
578606

579607

608+
#define SMART_STR_START_SIZE 2048
609+
#define SMART_STR_PREALLOC 512
610+
#include "zend_smart_str.h"
611+
612+
580613
/* {{{ _mysqlnd_pestrdup */
581-
char * _mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_MEM_D)
614+
static char * _mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_MEM_D)
582615
{
583616
char * ret;
584617
smart_str tmp_str = {0, 0};
@@ -611,7 +644,7 @@ char * _mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_ME
611644

612645

613646
/* {{{ _mysqlnd_sprintf */
614-
PHPAPI int _mysqlnd_sprintf(char ** pbuf, size_t max_len, const char *format, ...)
647+
static int _mysqlnd_sprintf(char ** pbuf, size_t max_len, const char *format, ...)
615648
{
616649
int len;
617650
va_list ap;
@@ -624,14 +657,14 @@ PHPAPI int _mysqlnd_sprintf(char ** pbuf, size_t max_len, const char *format, ..
624657

625658

626659
/* {{{ _mysqlnd_sprintf_free */
627-
PHPAPI void _mysqlnd_sprintf_free(char * p)
660+
static void _mysqlnd_sprintf_free(char * p)
628661
{
629662
efree(p);
630663
}
631664
/* }}} */
632665

633666
/* {{{ _mysqlnd_vsprintf */
634-
PHPAPI int _mysqlnd_vsprintf(char ** pbuf, size_t max_len, const char * format, va_list ap)
667+
static int _mysqlnd_vsprintf(char ** pbuf, size_t max_len, const char * format, va_list ap)
635668
{
636669
return vspprintf(pbuf, max_len, format, ap);
637670
}
@@ -738,6 +771,18 @@ static void mysqlnd_zend_mm_free(void * ptr MYSQLND_MEM_D)
738771
/* }}} */
739772

740773

774+
/* {{{ mysqlnd_zend_mm_pememdup */
775+
static char * mysqlnd_zend_mm_pememdup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
776+
{
777+
char * dest = pemalloc(length, persistent);
778+
if (dest) {
779+
memcpy(dest, ptr, length);
780+
}
781+
return dest;
782+
}
783+
/* }}} */
784+
785+
741786
/* {{{ mysqlnd_zend_mm_pestrndup */
742787
static char * mysqlnd_zend_mm_pestrndup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
743788
{
@@ -771,6 +816,7 @@ PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator =
771816
_mysqlnd_calloc,
772817
_mysqlnd_realloc,
773818
_mysqlnd_free,
819+
_mysqlnd_pememdup,
774820
_mysqlnd_pestrndup,
775821
_mysqlnd_pestrdup,
776822
_mysqlnd_sprintf,
@@ -789,9 +835,10 @@ PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator =
789835
mysqlnd_zend_mm_calloc,
790836
mysqlnd_zend_mm_realloc,
791837
mysqlnd_zend_mm_free,
838+
mysqlnd_zend_mm_pememdup,
792839
mysqlnd_zend_mm_pestrndup,
793-
mysqlnd_zend_mm_pestrdup
794-
sprintf,
840+
mysqlnd_zend_mm_pestrdup,
841+
vsprintf,
795842
mysqlnd_zend_mm_efree,
796843
#endif
797844
};

ext/mysqlnd/mysqlnd_alloc.h

+17-18
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct st_mysqlnd_allocator_methods
3939
void * (*m_calloc)(unsigned int nmemb, size_t size MYSQLND_MEM_D);
4040
void * (*m_realloc)(void *ptr, size_t new_size MYSQLND_MEM_D);
4141
void (*m_free)(void *ptr MYSQLND_MEM_D);
42+
char * (*m_pememdup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
4243
char * (*m_pestrndup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
4344
char * (*m_pestrdup)(const char * const ptr, zend_bool persistent MYSQLND_MEM_D);
4445
int (*m_sprintf)(char **pbuf, size_t max_len, const char *format, ...);
@@ -48,24 +49,6 @@ struct st_mysqlnd_allocator_methods
4849

4950
PHPAPI extern struct st_mysqlnd_allocator_methods mysqlnd_allocator;
5051

51-
PHPAPI void * _mysqlnd_emalloc(size_t size MYSQLND_MEM_D);
52-
PHPAPI void * _mysqlnd_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D);
53-
PHPAPI void * _mysqlnd_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D);
54-
PHPAPI void * _mysqlnd_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent MYSQLND_MEM_D);
55-
PHPAPI void * _mysqlnd_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D);
56-
PHPAPI void * _mysqlnd_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D);
57-
PHPAPI void _mysqlnd_efree(void *ptr MYSQLND_MEM_D);
58-
PHPAPI void _mysqlnd_pefree(void *ptr, zend_bool persistent MYSQLND_MEM_D);
59-
PHPAPI void * _mysqlnd_malloc(size_t size MYSQLND_MEM_D);
60-
PHPAPI void * _mysqlnd_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D);
61-
PHPAPI void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D);
62-
PHPAPI void _mysqlnd_free(void *ptr MYSQLND_MEM_D);
63-
PHPAPI char * _mysqlnd_pestrndup(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
64-
PHPAPI char * _mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_MEM_D);
65-
PHPAPI int _mysqlnd_sprintf(char **pbuf, size_t max_len, const char *format, ...);
66-
PHPAPI void _mysqlnd_sprintf_free(char * p);
67-
PHPAPI int _mysqlnd_vsprintf(char **pbuf, size_t max_len, const char *format, va_list ap);
68-
6952
#define mnd_emalloc(size) mysqlnd_allocator.m_emalloc((size) MYSQLND_MEM_C)
7053
#define mnd_pemalloc(size, pers) mysqlnd_allocator.m_pemalloc((size), (pers) MYSQLND_MEM_C)
7154
#define mnd_ecalloc(nmemb, size) mysqlnd_allocator.m_ecalloc((nmemb), (size) MYSQLND_MEM_C)
@@ -84,6 +67,22 @@ PHPAPI int _mysqlnd_vsprintf(char **pbuf, size_t max_len, const char *format, v
8467
#define mnd_vsprintf(p, mx_len, fmt,ap) mysqlnd_allocator.m_vsprintf((p), (mx_len), (fmt), (ap))
8568
#define mnd_sprintf_free(p) mysqlnd_allocator.m_sprintf_free((p))
8669

70+
static inline MYSQLND_STRING mnd_dup_cstring(const MYSQLND_CSTRING str, const zend_bool persistent)
71+
{
72+
const MYSQLND_STRING ret = {(char*) mnd_pemalloc(str.l, persistent) + 1, str.l};
73+
if (ret.s) {
74+
memcpy(ret.s, str.s, str.l);
75+
ret.s[str.l] = '\0';
76+
}
77+
return ret;
78+
}
79+
80+
static inline MYSQLND_CSTRING mnd_str2c(const MYSQLND_STRING str)
81+
{
82+
const MYSQLND_CSTRING ret = {str.s, str.l};
83+
return ret;
84+
}
85+
8786
#endif /* MYSQLND_ALLOC_H */
8887

8988
/*

ext/mysqlnd/mysqlnd_enum_n_def.h

+2
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ typedef enum mysqlnd_collected_stats
518518
STAT_MEM_STRNDUP_COUNT,
519519
STAT_MEM_ESTRDUP_COUNT,
520520
STAT_MEM_STRDUP_COUNT,
521+
STAT_MEM_EDUP_COUNT,
522+
STAT_MEM_DUP_COUNT,
521523
STAT_TEXT_TYPE_FETCHED_NULL,
522524
STAT_TEXT_TYPE_FETCHED_BIT,
523525
STAT_TEXT_TYPE_FETCHED_INT8,

ext/mysqlnd/mysqlnd_statistics.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ const MYSQLND_STRING mysqlnd_stats_values_names[STAT_LAST] =
109109
{ MYSQLND_STR_W_LEN("mem_free_amount") },
110110
{ MYSQLND_STR_W_LEN("mem_estrndup_count") },
111111
{ MYSQLND_STR_W_LEN("mem_strndup_count") },
112-
{ MYSQLND_STR_W_LEN("mem_estndup_count") },
112+
{ MYSQLND_STR_W_LEN("mem_estrdup_count") },
113113
{ MYSQLND_STR_W_LEN("mem_strdup_count") },
114+
{ MYSQLND_STR_W_LEN("mem_edupl_count") },
115+
{ MYSQLND_STR_W_LEN("mem_dupl_count") },
114116
{ MYSQLND_STR_W_LEN("proto_text_fetched_null") },
115117
{ MYSQLND_STR_W_LEN("proto_text_fetched_bit") },
116118
{ MYSQLND_STR_W_LEN("proto_text_fetched_tinyint") },

0 commit comments

Comments
 (0)