php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
mysqlnd_alloc.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Andrey Hristov <andrey@php.net> |
14 | Ulf Wendel <uw@php.net> |
15 +----------------------------------------------------------------------+
16*/
17
18#include "php.h"
19#include "mysqlnd.h"
20#include "mysqlnd_priv.h"
21#include "mysqlnd_debug.h"
23#include "mysqlnd_statistics.h"
24
25#define MYSQLND_DEBUG_MEMORY 1
26
27static const char mysqlnd_emalloc_name[] = "_mysqlnd_emalloc";
28static const char mysqlnd_pemalloc_name[] = "_mysqlnd_pemalloc";
29static const char mysqlnd_ecalloc_name[] = "_mysqlnd_ecalloc";
30static const char mysqlnd_pecalloc_name[] = "_mysqlnd_pecalloc";
31static const char mysqlnd_erealloc_name[] = "_mysqlnd_erealloc";
32static const char mysqlnd_perealloc_name[] = "_mysqlnd_perealloc";
33static const char mysqlnd_efree_name[] = "_mysqlnd_efree";
34static const char mysqlnd_pefree_name[] = "_mysqlnd_pefree";
35static const char mysqlnd_pememdup_name[] = "_mysqlnd_pememdup";
36static const char mysqlnd_pestrndup_name[] = "_mysqlnd_pestrndup";
37static const char mysqlnd_pestrdup_name[] = "_mysqlnd_pestrdup";
38
40{
41 mysqlnd_emalloc_name,
42 mysqlnd_ecalloc_name,
43 mysqlnd_efree_name,
44 mysqlnd_erealloc_name,
45 mysqlnd_pemalloc_name,
46 mysqlnd_pecalloc_name,
47 mysqlnd_pefree_name,
48 mysqlnd_perealloc_name,
49 mysqlnd_pestrndup_name,
52 NULL /* must be always last */
53};
54
55#if MYSQLND_DEBUG_MEMORY
56
57
58#if ZEND_DEBUG
59#else
60#define __zend_orig_filename "/unknown/unknown"
61#define __zend_orig_lineno 0
62#endif
63
64#define EXTRA_SIZE ZEND_MM_ALIGNED_SIZE(sizeof(size_t))
65#define REAL_SIZE(s) (collect_memory_statistics? (s) + EXTRA_SIZE : (s))
66#define REAL_PTR(p) (collect_memory_statistics && (p)? (((char *)(p)) - EXTRA_SIZE) : (p))
67#define FAKE_PTR(p) (collect_memory_statistics && (p)? (((char *)(p)) + EXTRA_SIZE) : (p))
68
69/* {{{ _mysqlnd_emalloc */
70static void * _mysqlnd_emalloc(size_t size MYSQLND_MEM_D)
71{
72 void *ret;
74 TRACE_ALLOC_ENTER(mysqlnd_emalloc_name);
76
77 TRACE_ALLOC_INF_FMT("size=%zu ptr=%p", size, ret);
78
80 *(size_t *) ret = size;
82 }
84}
85/* }}} */
86
87
88/* {{{ _mysqlnd_pemalloc */
89static void * _mysqlnd_pemalloc(size_t size, bool persistent MYSQLND_MEM_D)
90{
91 void *ret;
93 TRACE_ALLOC_ENTER(mysqlnd_pemalloc_name);
95
96 TRACE_ALLOC_INF_FMT("size=%zu ptr=%p persistent=%u", size, ret, persistent);
97
101 *(size_t *) ret = size;
103 }
104
106}
107/* }}} */
108
109
110/* {{{ _mysqlnd_ecalloc */
111static void * _mysqlnd_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
112{
113 void *ret;
115 TRACE_ALLOC_ENTER(mysqlnd_ecalloc_name);
116 TRACE_ALLOC_INF_FMT("before: %zu", zend_memory_usage(FALSE));
117 ret = ecalloc_rel(nmemb, REAL_SIZE(size));
118
119 TRACE_ALLOC_INF_FMT("after : %zu", zend_memory_usage(FALSE));
120 TRACE_ALLOC_INF_FMT("size=%zu ptr=%p", size, ret);
122 *(size_t *) ret = size;
124 }
126}
127/* }}} */
128
129
130/* {{{ _mysqlnd_pecalloc */
131static void * _mysqlnd_pecalloc(unsigned int nmemb, size_t size, bool persistent MYSQLND_MEM_D)
132{
133 void *ret;
135 TRACE_ALLOC_ENTER(mysqlnd_pecalloc_name);
137
138 TRACE_ALLOC_INF_FMT("size=%zu ptr=%p", size, ret);
139
143 *(size_t *) ret = size;
145 }
146
148}
149/* }}} */
150
151
152/* {{{ _mysqlnd_erealloc */
153static void * _mysqlnd_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D)
154{
155 void *ret;
157 size_t old_size = collect_memory_statistics && ptr? *(size_t *) (((char*)ptr) - sizeof(size_t)) : 0;
158 TRACE_ALLOC_ENTER(mysqlnd_erealloc_name);
159 TRACE_ALLOC_INF_FMT("ptr=%p old_size=%zu, new_size=%zu", ptr, old_size, new_size);
160 ret = erealloc_rel(REAL_PTR(ptr), REAL_SIZE(new_size));
161
162 TRACE_ALLOC_INF_FMT("new_ptr=%p", (char*)ret);
164 *(size_t *) ret = new_size;
166 }
168}
169/* }}} */
170
171
172/* {{{ _mysqlnd_perealloc */
173static void * _mysqlnd_perealloc(void *ptr, size_t new_size, bool persistent MYSQLND_MEM_D)
174{
175 void *ret;
177 size_t old_size = collect_memory_statistics && ptr? *(size_t *) (((char*)ptr) - sizeof(size_t)) : 0;
178 TRACE_ALLOC_ENTER(mysqlnd_perealloc_name);
179 TRACE_ALLOC_INF_FMT("ptr=%p old_size=%zu new_size=%zu persistent=%u", ptr, old_size, new_size, persistent);
181
182 TRACE_ALLOC_INF_FMT("new_ptr=%p", (char*)ret);
183
187 *(size_t *) ret = new_size;
188 MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(s1, 1, s2, new_size);
189 }
191}
192/* }}} */
193
194
195/* {{{ _mysqlnd_efree */
196static void _mysqlnd_efree(void *ptr MYSQLND_MEM_D)
197{
198 size_t free_amount = 0;
200 TRACE_ALLOC_ENTER(mysqlnd_efree_name);
201
202#if PHP_DEBUG
203 {
204 char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
205 TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
206 }
207#endif
208 TRACE_ALLOC_INF_FMT("ptr=%p", ptr);
209
210 if (ptr) {
212 free_amount = *(size_t *)(((char*)ptr) - sizeof(size_t));
213 TRACE_ALLOC_INF_FMT("ptr=%p size=%zu", ((char*)ptr) - sizeof(size_t), free_amount);
214 }
216 }
217
220 }
222}
223/* }}} */
224
225
226/* {{{ _mysqlnd_pefree */
227static void _mysqlnd_pefree(void *ptr, bool persistent MYSQLND_MEM_D)
228{
229 size_t free_amount = 0;
231 TRACE_ALLOC_ENTER(mysqlnd_pefree_name);
232
233#if PHP_DEBUG
234 {
235 char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
236 TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
237 }
238#endif
239 TRACE_ALLOC_INF_FMT("ptr=%p persistent=%u", ptr, persistent);
240
241 if (ptr) {
243 free_amount = *(size_t *)(((char*)ptr) - sizeof(size_t));
244 TRACE_ALLOC_INF_FMT("ptr=%p size=%zu", ((char*)ptr) - sizeof(size_t), free_amount);
245 }
247 }
248
252 }
254}
255/* }}} */
256
257
258/* {{{ _mysqlnd_pememdup */
259static char * _mysqlnd_pememdup(const char * const ptr, size_t length, bool persistent MYSQLND_MEM_D)
260{
261 char * ret;
263 TRACE_ALLOC_ENTER(mysqlnd_pememdup_name);
264
265#if PHP_DEBUG
266 {
267 char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
268 TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
269 }
270#endif
271 TRACE_ALLOC_INF_FMT("ptr=%p", ptr);
272
273 ret = pemalloc_rel(REAL_SIZE(length + 1), persistent);
274 {
275 char * dest = (char *) FAKE_PTR(ret);
276 memcpy(dest, ptr, length);
277 }
278
280 *(size_t *) ret = length;
282 }
283
285}
286/* }}} */
287
288
289/* {{{ _mysqlnd_pestrndup */
290static char * _mysqlnd_pestrndup(const char * const ptr, size_t length, bool persistent MYSQLND_MEM_D)
291{
292 char * ret;
294 TRACE_ALLOC_ENTER(mysqlnd_pestrndup_name);
295
296#if PHP_DEBUG
297 {
298 char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
299 TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
300 }
301#endif
302 TRACE_ALLOC_INF_FMT("ptr=%p", ptr);
303
304 ret = pemalloc_rel(REAL_SIZE(length + 1), persistent);
305 {
306 size_t l = length;
307 char * p = (char *) ptr;
308 char * dest = (char *) FAKE_PTR(ret);
309 while (*p && l--) {
310 *dest++ = *p++;
311 }
312 *dest = '\0';
313 }
314
316 *(size_t *) ret = length;
318 }
319
321}
322/* }}} */
323
324
325#define SMART_STR_START_SIZE 2048
326#include "zend_smart_str.h"
327
328
329/* {{{ _mysqlnd_pestrdup */
330static char * _mysqlnd_pestrdup(const char * const ptr, bool persistent MYSQLND_MEM_D)
331{
332 char * ret;
333 smart_str tmp_str = {0, 0};
334 const char * p = ptr;
336 TRACE_ALLOC_ENTER(mysqlnd_pestrdup_name);
337#if PHP_DEBUG
338 {
339 char * fn = strrchr(__zend_filename, PHP_DIR_SEPARATOR);
340 TRACE_ALLOC_INF_FMT("file=%-15s line=%4d", fn? fn + 1:__zend_filename, __zend_lineno);
341 }
342#endif
343 TRACE_ALLOC_INF_FMT("ptr=%p", ptr);
344 do {
345 smart_str_appendc(&tmp_str, *p);
346 } while (*p++);
347
349 memcpy(FAKE_PTR(ret), ZSTR_VAL(tmp_str.s), ZSTR_LEN(tmp_str.s));
350
352 *(size_t *) ret = ZSTR_LEN(tmp_str.s);
354 }
355 smart_str_free(&tmp_str);
356
358}
359/* }}} */
360
361
362#else
363
364/* {{{ mysqlnd_zend_mm_emalloc */
365static void * mysqlnd_zend_mm_emalloc(size_t size MYSQLND_MEM_D)
366{
367 return emalloc_rel(size);
368}
369/* }}} */
370
371
372/* {{{ mysqlnd_zend_mm_pemalloc */
373static void * mysqlnd_zend_mm_pemalloc(size_t size, bool persistent MYSQLND_MEM_D)
374{
376}
377/* }}} */
378
379
380/* {{{ mysqlnd_zend_mm_ecalloc */
381static void * mysqlnd_zend_mm_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
382{
383 return ecalloc_rel(nmemb, size);
384}
385/* }}} */
386
387
388/* {{{ mysqlnd_zend_mm_pecalloc */
389static void * mysqlnd_zend_mm_pecalloc(unsigned int nmemb, size_t size, bool persistent MYSQLND_MEM_D)
390{
391 return pecalloc_rel(nmemb, size, persistent);
392}
393/* }}} */
394
395
396/* {{{ mysqlnd_zend_mm_erealloc */
397static void * mysqlnd_zend_mm_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D)
398{
399 return erealloc_rel(ptr, new_size);
400}
401/* }}} */
402
403
404/* {{{ mysqlnd_zend_mm_perealloc */
405static void * mysqlnd_zend_mm_perealloc(void *ptr, size_t new_size, bool persistent MYSQLND_MEM_D)
406{
407 return perealloc_rel(ptr, new_size, persistent);
408}
409/* }}} */
410
411
412/* {{{ mysqlnd_zend_mm_efree */
413static void mysqlnd_zend_mm_efree(void * ptr MYSQLND_MEM_D)
414{
415 efree_rel(ptr);
416}
417/* }}} */
418
419
420/* {{{ mysqlnd_zend_mm_pefree */
421static void mysqlnd_zend_mm_pefree(void * ptr, bool persistent MYSQLND_MEM_D)
422{
424}
425/* }}} */
426
427
428/* {{{ mysqlnd_zend_mm_pememdup */
429static char * mysqlnd_zend_mm_pememdup(const char * const ptr, size_t length, bool persistent MYSQLND_MEM_D)
430{
431 char * dest = pemalloc_rel(length, persistent);
432 if (dest) {
433 memcpy(dest, ptr, length);
434 }
435 return dest;
436}
437/* }}} */
438
439
440/* {{{ mysqlnd_zend_mm_pestrndup */
441static char * mysqlnd_zend_mm_pestrndup(const char * const ptr, size_t length, bool persistent MYSQLND_MEM_D)
442{
443 return persistent? zend_strndup(ptr, length ) : estrndup_rel(ptr, length);
444}
445/* }}} */
446
447
448/* {{{ mysqlnd_zend_mm_pestrdup */
449static char * mysqlnd_zend_mm_pestrdup(const char * const ptr, bool persistent MYSQLND_MEM_D)
450{
451 return pestrdup_rel(ptr, persistent);
452}
453/* }}} */
454
455#endif /* MYSQLND_DEBUG_MEMORY */
456
457
459{
460#if MYSQLND_DEBUG_MEMORY == 1
461 _mysqlnd_emalloc,
462 _mysqlnd_pemalloc,
463 _mysqlnd_ecalloc,
464 _mysqlnd_pecalloc,
465 _mysqlnd_erealloc,
466 _mysqlnd_perealloc,
467 _mysqlnd_efree,
468 _mysqlnd_pefree,
469 _mysqlnd_pememdup,
470 _mysqlnd_pestrndup,
471 _mysqlnd_pestrdup
472#else
473 mysqlnd_zend_mm_emalloc,
474 mysqlnd_zend_mm_pemalloc,
475 mysqlnd_zend_mm_ecalloc,
476 mysqlnd_zend_mm_pecalloc,
477 mysqlnd_zend_mm_erealloc,
478 mysqlnd_zend_mm_perealloc,
479 mysqlnd_zend_mm_efree,
480 mysqlnd_zend_mm_pefree,
481 mysqlnd_zend_mm_pememdup,
482 mysqlnd_zend_mm_pestrndup,
483 mysqlnd_zend_mm_pestrdup
484#endif
485};
strrchr(string $haystack, string $needle, bool $before_needle=false)
new_type size
Definition ffi.c:4365
void * ptr
Definition ffi.c:3814
memcpy(ptr1, ptr2, size)
ffi persistent
Definition ffi.c:3633
#define FALSE
Definition gd_gd.c:8
#define NULL
Definition gdcache.h:45
#define MYSQLND_INC_GLOBAL_STATISTIC(statistic)
Definition mysqlnd.h:255
#define MYSQLND_G(v)
bool collect_memory_statistics
Definition mysqlnd.h:309
#define MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(statistic1, value1, statistic2, value2)
Definition mysqlnd.h:261
PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator
#define FAKE_PTR(p)
#define REAL_SIZE(s)
PHPAPI const char * mysqlnd_debug_std_no_trace_funcs[]
#define REAL_PTR(p)
#define MYSQLND_MEM_D
#define TRACE_ALLOC_VOID_RETURN
#define TRACE_ALLOC_RETURN(value)
mysqlnd_collected_stats
@ STAT_MEM_FREE_AMOUNT
@ STAT_MEM_FREE_COUNT
@ STAT_MEM_ECALLOC_COUNT
@ STAT_MEM_MALLOC_AMOUNT
@ STAT_MEM_EREALLOC_COUNT
@ STAT_MEM_MALLOC_COUNT
@ STAT_MEM_EMALLOC_AMOUNT
@ STAT_MEM_CALLOC_COUNT
@ STAT_MEM_EFREE_AMOUNT
@ STAT_MEM_ECALLOC_AMOUNT
@ STAT_MEM_EMALLOC_COUNT
@ STAT_MEM_STRNDUP_COUNT
@ STAT_MEM_STRDUP_COUNT
@ STAT_MEM_REALLOC_COUNT
@ STAT_MEM_DUP_COUNT
@ STAT_MEM_EFREE_COUNT
@ STAT_MEM_EREALLOC_AMOUNT
@ STAT_MEM_ESTRNDUP_COUNT
@ STAT_MEM_ESTRDUP_COUNT
@ STAT_MEM_EDUP_COUNT
@ STAT_MEM_CALLOC_AMOUNT
@ STAT_MEM_REALLOC_AMOUNT
const char mysqlnd_read_body_name[]
const char mysqlnd_read_header_name[]
#define PHP_DIR_SEPARATOR
Definition php.h:73
#define PHPAPI
Definition php.h:71
p
Definition session.c:1105
zend_string * s
ZEND_API size_t zend_memory_usage(bool real_usage)
ZEND_API char *ZEND_FASTCALL zend_strndup(const char *s, size_t length)
#define perealloc_rel(ptr, size, persistent)
Definition zend_alloc.h:212
#define estrndup_rel(s, length)
Definition zend_alloc.h:179
#define erealloc_rel(ptr, size)
Definition zend_alloc.h:173
#define pestrdup_rel(s, persistent)
Definition zend_alloc.h:216
#define ecalloc_rel(nmemb, size)
Definition zend_alloc.h:172
#define emalloc_rel(size)
Definition zend_alloc.h:169
#define efree_rel(ptr)
Definition zend_alloc.h:171
#define pefree_rel(ptr, persistent)
Definition zend_alloc.h:210
#define pemalloc_rel(size, persistent)
Definition zend_alloc.h:209
#define pecalloc_rel(nmemb, size, persistent)
Definition zend_alloc.h:211
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
zval * ret