Annotation of embedaddon/php/ext/mysqlnd/mysqlnd_alloc.c, revision 1.1.1.1
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
5: | Copyright (c) 2006-2012 The PHP Group |
6: +----------------------------------------------------------------------+
7: | This source file is subject to version 3.01 of the PHP license, |
8: | that is bundled with this package in the file LICENSE, and is |
9: | available through the world-wide-web at the following url: |
10: | http://www.php.net/license/3_01.txt |
11: | If you did not receive a copy of the PHP license and are unable to |
12: | obtain it through the world-wide-web, please send a note to |
13: | license@php.net so we can mail you a copy immediately. |
14: +----------------------------------------------------------------------+
15: | Authors: Georg Richter <georg@mysql.com> |
16: | Andrey Hristov <andrey@mysql.com> |
17: | Ulf Wendel <uwendel@mysql.com> |
18: +----------------------------------------------------------------------+
19: */
20:
21: /* $Id: mysqlnd_debug.c 309303 2011-03-16 12:42:59Z andrey $ */
22: #include "php.h"
23: #include "mysqlnd.h"
24: #include "mysqlnd_priv.h"
25: #include "mysqlnd_debug.h"
26: #include "mysqlnd_wireprotocol.h"
27: #include "mysqlnd_statistics.h"
28:
29:
30: static const char mysqlnd_emalloc_name[] = "_mysqlnd_emalloc";
31: static const char mysqlnd_pemalloc_name[] = "_mysqlnd_pemalloc";
32: static const char mysqlnd_ecalloc_name[] = "_mysqlnd_ecalloc";
33: static const char mysqlnd_pecalloc_name[] = "_mysqlnd_pecalloc";
34: static const char mysqlnd_erealloc_name[] = "_mysqlnd_erealloc";
35: static const char mysqlnd_perealloc_name[] = "_mysqlnd_perealloc";
36: static const char mysqlnd_efree_name[] = "_mysqlnd_efree";
37: static const char mysqlnd_pefree_name[] = "_mysqlnd_pefree";
38: static const char mysqlnd_malloc_name[] = "_mysqlnd_malloc";
39: static const char mysqlnd_calloc_name[] = "_mysqlnd_calloc";
40: static const char mysqlnd_realloc_name[] = "_mysqlnd_realloc";
41: static const char mysqlnd_free_name[] = "_mysqlnd_free";
42: static const char mysqlnd_pestrndup_name[] = "_mysqlnd_pestrndup";
43: static const char mysqlnd_pestrdup_name[] = "_mysqlnd_pestrdup";
44:
45: const char * mysqlnd_debug_std_no_trace_funcs[] =
46: {
47: mysqlnd_emalloc_name,
48: mysqlnd_ecalloc_name,
49: mysqlnd_efree_name,
50: mysqlnd_erealloc_name,
51: mysqlnd_pemalloc_name,
52: mysqlnd_pecalloc_name,
53: mysqlnd_pefree_name,
54: mysqlnd_perealloc_name,
55: mysqlnd_malloc_name,
56: mysqlnd_calloc_name,
57: mysqlnd_realloc_name,
58: mysqlnd_free_name,
59: mysqlnd_pestrndup_name,
60: mysqlnd_read_header_name,
61: mysqlnd_read_body_name,
62: NULL /* must be always last */
63: };
64:
65:
66: #if ZEND_DEBUG
67: #else
68: #define __zend_filename "/unknown/unknown"
69: #define __zend_lineno 0
70: #endif
71:
72: #define REAL_SIZE(s) (collect_memory_statistics? (s) + sizeof(size_t) : (s))
73: #define REAL_PTR(p) (collect_memory_statistics && (p)? (((char *)(p)) - sizeof(size_t)) : (p))
74: #define FAKE_PTR(p) (collect_memory_statistics && (p)? (((char *)(p)) + sizeof(size_t)) : (p))
75:
76: /* {{{ _mysqlnd_emalloc */
77: void * _mysqlnd_emalloc(size_t size MYSQLND_MEM_D)
78: {
79: void *ret;
80: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
81: #if PHP_DEBUG
82: long * threshold = &MYSQLND_G(debug_emalloc_fail_threshold);
83: #endif
84: DBG_ENTER(mysqlnd_emalloc_name);
85:
86: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
87:
88: #if PHP_DEBUG
89: /* -1 is also "true" */
90: if (*threshold) {
91: #endif
92: ret = emalloc(REAL_SIZE(size));
93: #if PHP_DEBUG
94: --*threshold;
95: } else if (*threshold == 0) {
96: ret = NULL;
97: }
98: #endif
99:
100: DBG_INF_FMT("size=%lu ptr=%p", size, ret);
101:
102: if (ret && collect_memory_statistics) {
103: *(size_t *) ret = size;
104: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_EMALLOC_COUNT, 1, STAT_MEM_EMALLOC_AMOUNT, size);
105: }
106: DBG_RETURN(FAKE_PTR(ret));
107: }
108: /* }}} */
109:
110:
111: /* {{{ _mysqlnd_pemalloc */
112: void * _mysqlnd_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D)
113: {
114: void *ret;
115: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
116: #if PHP_DEBUG
117: long * threshold = persistent? &MYSQLND_G(debug_malloc_fail_threshold):&MYSQLND_G(debug_emalloc_fail_threshold);
118: #endif
119: DBG_ENTER(mysqlnd_pemalloc_name);
120: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
121:
122: #if PHP_DEBUG
123: /* -1 is also "true" */
124: if (*threshold) {
125: #endif
126: ret = pemalloc(REAL_SIZE(size), persistent);
127: #if PHP_DEBUG
128: --*threshold;
129: } else if (*threshold == 0) {
130: ret = NULL;
131: }
132: #endif
133:
134: DBG_INF_FMT("size=%lu ptr=%p persistent=%u", size, ret, persistent);
135:
136: if (ret && collect_memory_statistics) {
137: enum mysqlnd_collected_stats s1 = persistent? STAT_MEM_MALLOC_COUNT:STAT_MEM_EMALLOC_COUNT;
138: enum mysqlnd_collected_stats s2 = persistent? STAT_MEM_MALLOC_AMOUNT:STAT_MEM_EMALLOC_AMOUNT;
139: *(size_t *) ret = size;
140: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(s1, 1, s2, size);
141: }
142:
143: DBG_RETURN(FAKE_PTR(ret));
144: }
145: /* }}} */
146:
147:
148: /* {{{ _mysqlnd_ecalloc */
149: void * _mysqlnd_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
150: {
151: void *ret;
152: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
153: #if PHP_DEBUG
154: long * threshold = &MYSQLND_G(debug_ecalloc_fail_threshold);
155: #endif
156: DBG_ENTER(mysqlnd_ecalloc_name);
157: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
158: DBG_INF_FMT("before: %lu", zend_memory_usage(FALSE TSRMLS_CC));
159:
160: #if PHP_DEBUG
161: /* -1 is also "true" */
162: if (*threshold) {
163: #endif
164: ret = ecalloc(nmemb, REAL_SIZE(size));
165: #if PHP_DEBUG
166: --*threshold;
167: } else if (*threshold == 0) {
168: ret = NULL;
169: }
170: #endif
171:
172: DBG_INF_FMT("after : %lu", zend_memory_usage(FALSE TSRMLS_CC));
173: DBG_INF_FMT("size=%lu ptr=%p", size, ret);
174: if (ret && collect_memory_statistics) {
175: *(size_t *) ret = size;
176: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_ECALLOC_COUNT, 1, STAT_MEM_ECALLOC_AMOUNT, size);
177: }
178: DBG_RETURN(FAKE_PTR(ret));
179: }
180: /* }}} */
181:
182:
183: /* {{{ _mysqlnd_pecalloc */
184: void * _mysqlnd_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent MYSQLND_MEM_D)
185: {
186: void *ret;
187: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
188: #if PHP_DEBUG
189: long * threshold = persistent? &MYSQLND_G(debug_calloc_fail_threshold):&MYSQLND_G(debug_ecalloc_fail_threshold);
190: #endif
191: DBG_ENTER(mysqlnd_pecalloc_name);
192: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
193:
194: #if PHP_DEBUG
195: /* -1 is also "true" */
196: if (*threshold) {
197: #endif
198: ret = pecalloc(nmemb, REAL_SIZE(size), persistent);
199: #if PHP_DEBUG
200: --*threshold;
201: } else if (*threshold == 0) {
202: ret = NULL;
203: }
204: #endif
205:
206: DBG_INF_FMT("size=%lu ptr=%p", size, ret);
207:
208: if (ret && collect_memory_statistics) {
209: enum mysqlnd_collected_stats s1 = persistent? STAT_MEM_CALLOC_COUNT:STAT_MEM_ECALLOC_COUNT;
210: enum mysqlnd_collected_stats s2 = persistent? STAT_MEM_CALLOC_AMOUNT:STAT_MEM_ECALLOC_AMOUNT;
211: *(size_t *) ret = size;
212: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(s1, 1, s2, size);
213: }
214:
215: DBG_RETURN(FAKE_PTR(ret));
216: }
217: /* }}} */
218:
219:
220: /* {{{ _mysqlnd_erealloc */
221: void * _mysqlnd_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D)
222: {
223: void *ret;
224: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
225: size_t old_size = collect_memory_statistics && ptr? *(size_t *) (((char*)ptr) - sizeof(size_t)) : 0;
226: #if PHP_DEBUG
227: long * threshold = &MYSQLND_G(debug_erealloc_fail_threshold);
228: #endif
229: DBG_ENTER(mysqlnd_erealloc_name);
230: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
231: DBG_INF_FMT("ptr=%p old_size=%lu, new_size=%lu", ptr, old_size, new_size);
232:
233: #if PHP_DEBUG
234: /* -1 is also "true" */
235: if (*threshold) {
236: #endif
237: ret = erealloc(REAL_PTR(ptr), REAL_SIZE(new_size));
238: #if PHP_DEBUG
239: --*threshold;
240: } else if (*threshold == 0) {
241: ret = NULL;
242: }
243: #endif
244:
245: DBG_INF_FMT("new_ptr=%p", (char*)ret);
246: if (ret && collect_memory_statistics) {
247: *(size_t *) ret = new_size;
248: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_EREALLOC_COUNT, 1, STAT_MEM_EREALLOC_AMOUNT, new_size);
249: }
250: DBG_RETURN(FAKE_PTR(ret));
251: }
252: /* }}} */
253:
254:
255: /* {{{ _mysqlnd_perealloc */
256: void * _mysqlnd_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D)
257: {
258: void *ret;
259: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
260: size_t old_size = collect_memory_statistics && ptr? *(size_t *) (((char*)ptr) - sizeof(size_t)) : 0;
261: #if PHP_DEBUG
262: long * threshold = persistent? &MYSQLND_G(debug_realloc_fail_threshold):&MYSQLND_G(debug_erealloc_fail_threshold);
263: #endif
264: DBG_ENTER(mysqlnd_perealloc_name);
265: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
266: DBG_INF_FMT("ptr=%p old_size=%lu new_size=%lu persistent=%u", ptr, old_size, new_size, persistent);
267:
268: #if PHP_DEBUG
269: /* -1 is also "true" */
270: if (*threshold) {
271: #endif
272: ret = perealloc(REAL_PTR(ptr), REAL_SIZE(new_size), persistent);
273: #if PHP_DEBUG
274: --*threshold;
275: } else if (*threshold == 0) {
276: ret = NULL;
277: }
278: #endif
279:
280: DBG_INF_FMT("new_ptr=%p", (char*)ret);
281:
282: if (ret && collect_memory_statistics) {
283: enum mysqlnd_collected_stats s1 = persistent? STAT_MEM_REALLOC_COUNT:STAT_MEM_EREALLOC_COUNT;
284: enum mysqlnd_collected_stats s2 = persistent? STAT_MEM_REALLOC_AMOUNT:STAT_MEM_EREALLOC_AMOUNT;
285: *(size_t *) ret = new_size;
286: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(s1, 1, s2, new_size);
287: }
288: DBG_RETURN(FAKE_PTR(ret));
289: }
290: /* }}} */
291:
292:
293: /* {{{ _mysqlnd_efree */
294: void _mysqlnd_efree(void *ptr MYSQLND_MEM_D)
295: {
296: size_t free_amount = 0;
297: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
298: DBG_ENTER(mysqlnd_efree_name);
299: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
300: DBG_INF_FMT("ptr=%p", ptr);
301:
302: if (ptr) {
303: if (collect_memory_statistics) {
304: free_amount = *(size_t *)(((char*)ptr) - sizeof(size_t));
305: DBG_INF_FMT("ptr=%p size=%u", ((char*)ptr) - sizeof(size_t), (unsigned int) free_amount);
306: }
307: efree(REAL_PTR(ptr));
308: }
309:
310: if (collect_memory_statistics) {
311: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_EFREE_COUNT, 1, STAT_MEM_EFREE_AMOUNT, free_amount);
312: }
313: DBG_VOID_RETURN;
314: }
315: /* }}} */
316:
317:
318: /* {{{ _mysqlnd_pefree */
319: void _mysqlnd_pefree(void *ptr, zend_bool persistent MYSQLND_MEM_D)
320: {
321: size_t free_amount = 0;
322: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
323: DBG_ENTER(mysqlnd_pefree_name);
324: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
325: DBG_INF_FMT("ptr=%p persistent=%u", ptr, persistent);
326:
327: if (ptr) {
328: if (collect_memory_statistics) {
329: free_amount = *(size_t *)(((char*)ptr) - sizeof(size_t));
330: DBG_INF_FMT("ptr=%p size=%u", ((char*)ptr) - sizeof(size_t), (unsigned int) free_amount);
331: }
332: pefree(REAL_PTR(ptr), persistent);
333: }
334:
335: if (collect_memory_statistics) {
336: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(persistent? STAT_MEM_FREE_COUNT:STAT_MEM_EFREE_COUNT, 1,
337: persistent? STAT_MEM_FREE_AMOUNT:STAT_MEM_EFREE_AMOUNT, free_amount);
338: }
339: DBG_VOID_RETURN;
340: }
341:
342:
343: /* {{{ _mysqlnd_malloc */
344: void * _mysqlnd_malloc(size_t size MYSQLND_MEM_D)
345: {
346: void *ret;
347: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
348: #if PHP_DEBUG
349: long * threshold = &MYSQLND_G(debug_malloc_fail_threshold);
350: #endif
351: DBG_ENTER(mysqlnd_malloc_name);
352: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
353:
354: #if PHP_DEBUG
355: /* -1 is also "true" */
356: if (*threshold) {
357: #endif
358: ret = malloc(REAL_SIZE(size));
359: #if PHP_DEBUG
360: --*threshold;
361: } else if (*threshold == 0) {
362: ret = NULL;
363: }
364: #endif
365:
366: DBG_INF_FMT("size=%lu ptr=%p", size, ret);
367: if (ret && collect_memory_statistics) {
368: *(size_t *) ret = size;
369: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_MALLOC_COUNT, 1, STAT_MEM_MALLOC_AMOUNT, size);
370: }
371: DBG_RETURN(FAKE_PTR(ret));
372: }
373: /* }}} */
374:
375:
376: /* {{{ _mysqlnd_calloc */
377: void * _mysqlnd_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
378: {
379: void *ret;
380: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
381: #if PHP_DEBUG
382: long * threshold = &MYSQLND_G(debug_calloc_fail_threshold);
383: #endif
384: DBG_ENTER(mysqlnd_calloc_name);
385: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
386:
387: #if PHP_DEBUG
388: /* -1 is also "true" */
389: if (*threshold) {
390: #endif
391: ret = calloc(nmemb, REAL_SIZE(size));
392: #if PHP_DEBUG
393: --*threshold;
394: } else if (*threshold == 0) {
395: ret = NULL;
396: }
397: #endif
398:
399: DBG_INF_FMT("size=%lu ptr=%p", size, ret);
400: if (ret && collect_memory_statistics) {
401: *(size_t *) ret = size;
402: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_CALLOC_COUNT, 1, STAT_MEM_CALLOC_AMOUNT, size);
403: }
404: DBG_RETURN(FAKE_PTR(ret));
405: }
406: /* }}} */
407:
408:
409: /* {{{ _mysqlnd_realloc */
410: void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D)
411: {
412: void *ret;
413: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
414: #if PHP_DEBUG
415: long * threshold = &MYSQLND_G(debug_realloc_fail_threshold);
416: #endif
417: DBG_ENTER(mysqlnd_realloc_name);
418: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
419: DBG_INF_FMT("ptr=%p new_size=%lu ", new_size, ptr);
420: DBG_INF_FMT("before: %lu", zend_memory_usage(TRUE TSRMLS_CC));
421:
422: #if PHP_DEBUG
423: /* -1 is also "true" */
424: if (*threshold) {
425: #endif
426: ret = realloc(REAL_PTR(ptr), REAL_SIZE(new_size));
427: #if PHP_DEBUG
428: --*threshold;
429: } else if (*threshold == 0) {
430: ret = NULL;
431: }
432: #endif
433:
434: DBG_INF_FMT("new_ptr=%p", (char*)ret);
435:
436: if (ret && collect_memory_statistics) {
437: *(size_t *) ret = new_size;
438: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_REALLOC_COUNT, 1, STAT_MEM_REALLOC_AMOUNT, new_size);
439: }
440: DBG_RETURN(FAKE_PTR(ret));
441: }
442: /* }}} */
443:
444:
445: /* {{{ _mysqlnd_free */
446: void _mysqlnd_free(void *ptr MYSQLND_MEM_D)
447: {
448: size_t free_amount = 0;
449: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
450: DBG_ENTER(mysqlnd_free_name);
451: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
452: DBG_INF_FMT("ptr=%p", ptr);
453:
454: if (ptr) {
455: if (collect_memory_statistics) {
456: free_amount = *(size_t *)(((char*)ptr) - sizeof(size_t));
457: DBG_INF_FMT("ptr=%p size=%u", ((char*)ptr) - sizeof(size_t), (unsigned int) free_amount);
458: }
459: free(REAL_PTR(ptr));
460: }
461:
462: if (collect_memory_statistics) {
463: MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(STAT_MEM_FREE_COUNT, 1, STAT_MEM_FREE_AMOUNT, free_amount);
464: }
465: DBG_VOID_RETURN;
466: }
467: /* }}} */
468:
469: #define SMART_STR_START_SIZE 2048
470: #define SMART_STR_PREALLOC 512
471: #include "ext/standard/php_smart_str.h"
472:
473:
474: /* {{{ _mysqlnd_pestrndup */
475: char * _mysqlnd_pestrndup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
476: {
477: char * ret;
478: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
479: DBG_ENTER(mysqlnd_pestrndup_name);
480: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
481: DBG_INF_FMT("ptr=%p", ptr);
482:
483: ret = pemalloc(REAL_SIZE(length) + 1, persistent);
484: {
485: size_t l = length;
486: char * p = (char *) ptr;
487: char * dest = (char *) FAKE_PTR(ret);
488: while (*p && l--) {
489: *dest++ = *p++;
490: }
491: *dest = '\0';
492: }
493:
494: if (collect_memory_statistics) {
495: *(size_t *) ret = length;
496: MYSQLND_INC_GLOBAL_STATISTIC(persistent? STAT_MEM_STRNDUP_COUNT : STAT_MEM_ESTRNDUP_COUNT);
497: }
498:
499: DBG_RETURN(FAKE_PTR(ret));
500: }
501: /* }}} */
502:
503:
504: /* {{{ _mysqlnd_pestrdup */
505: char * _mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_MEM_D)
506: {
507: char * ret;
508: smart_str tmp_str = {0, 0, 0};
509: const char * p = ptr;
510: zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics);
511: DBG_ENTER(mysqlnd_pestrdup_name);
512: DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno);
513: DBG_INF_FMT("ptr=%p", ptr);
514: do {
515: smart_str_appendc(&tmp_str, *p);
516: } while (*p++);
517:
518: ret = pemalloc(tmp_str.len + sizeof(size_t), persistent);
519: memcpy(FAKE_PTR(ret), tmp_str.c, tmp_str.len);
520:
521: if (ret && collect_memory_statistics) {
522: *(size_t *) ret = tmp_str.len;
523: MYSQLND_INC_GLOBAL_STATISTIC(persistent? STAT_MEM_STRDUP_COUNT : STAT_MEM_ESTRDUP_COUNT);
524: }
525: smart_str_free(&tmp_str);
526:
527: DBG_RETURN(FAKE_PTR(ret));
528: }
529: /* }}} */
530:
531:
532: /* {{{ _mysqlnd_sprintf */
533: PHPAPI int _mysqlnd_sprintf(char ** pbuf, size_t max_len, const char *format, ...)
534: {
535: int len;
536: va_list ap;
537: va_start(ap, format);
538: len = vspprintf(pbuf, max_len, format, ap);
539: va_end(ap);
540: return len;
541: }
542: /* }}} */
543:
544:
545: /* {{{ _mysqlnd_sprintf_free */
546: PHPAPI void _mysqlnd_sprintf_free(char * p)
547: {
548: efree(p);
549: }
550: /* }}} */
551:
552:
553: PHPAPI int _mysqlnd_vsprintf(char ** pbuf, size_t max_len, const char * format, va_list ap)
554: {
555: return vspprintf(pbuf, max_len, format, ap);
556: }
557: /* }}} */
558:
559:
560: #define MYSQLND_DEBUG_MEMORY 1
561:
562: #if MYSQLND_DEBUG_MEMORY == 0
563:
564: /* {{{ mysqlnd_zend_mm_emalloc */
565: static void * mysqlnd_zend_mm_emalloc(size_t size MYSQLND_MEM_D)
566: {
567: return emalloc(size);
568: }
569: /* }}} */
570:
571:
572: /* {{{ mysqlnd_zend_mm_pemalloc */
573: static void * mysqlnd_zend_mm_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D)
574: {
575: return pemalloc(size, persistent);
576: }
577: /* }}} */
578:
579:
580: /* {{{ mysqlnd_zend_mm_ecalloc */
581: static void * mysqlnd_zend_mm_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
582: {
583: return ecalloc(nmemb, size);
584: }
585: /* }}} */
586:
587:
588: /* {{{ mysqlnd_zend_mm_pecalloc */
589: static void * mysqlnd_zend_mm_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent MYSQLND_MEM_D)
590: {
591: return pecalloc(nmemb, size, persistent);
592: }
593: /* }}} */
594:
595:
596: /* {{{ mysqlnd_zend_mm_erealloc */
597: static void * mysqlnd_zend_mm_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D)
598: {
599: return erealloc(ptr, new_size);
600: }
601: /* }}} */
602:
603:
604: /* {{{ mysqlnd_zend_mm_perealloc */
605: static void * mysqlnd_zend_mm_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D)
606: {
607: return perealloc(ptr, new_size, persistent);
608: }
609: /* }}} */
610:
611:
612: /* {{{ mysqlnd_zend_mm_efree */
613: static void mysqlnd_zend_mm_efree(void * ptr MYSQLND_MEM_D)
614: {
615: efree(ptr);
616: }
617: /* }}} */
618:
619:
620: /* {{{ mysqlnd_zend_mm_pefree */
621: static void mysqlnd_zend_mm_pefree(void * ptr, zend_bool persistent MYSQLND_MEM_D)
622: {
623: pefree(ptr, persistent);
624: }
625: /* }}} */
626:
627:
628: /* {{{ mysqlnd_zend_mm_malloc */
629: static void * mysqlnd_zend_mm_malloc(size_t size MYSQLND_MEM_D)
630: {
631: return malloc(size);
632: }
633: /* }}} */
634:
635:
636: /* {{{ mysqlnd_zend_mm_calloc */
637: static void * mysqlnd_zend_mm_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
638: {
639: return calloc(nmemb, size);
640: }
641: /* }}} */
642:
643:
644: /* {{{ mysqlnd_zend_mm_realloc */
645: static void * mysqlnd_zend_mm_realloc(void * ptr, size_t new_size MYSQLND_MEM_D)
646: {
647: return realloc(ptr, new_size);
648: }
649: /* }}} */
650:
651:
652: /* {{{ mysqlnd_zend_mm_free */
653: static void mysqlnd_zend_mm_free(void * ptr MYSQLND_MEM_D)
654: {
655: free(ptr);
656: }
657: /* }}} */
658:
659:
660: /* {{{ mysqlnd_zend_mm_pestrndup */
661: static char * mysqlnd_zend_mm_pestrndup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
662: {
663: return pestrndup(ptr, length, persistent);
664: }
665: /* }}} */
666:
667:
668: /* {{{ mysqlnd_zend_mm_pestrdup */
669: static char * mysqlnd_zend_mm_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_MEM_D)
670: {
671: return pestrdup(ptr, persistent);
672: }
673: /* }}} */
674:
675: #endif
676:
677:
678: PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator =
679: {
680: #if MYSQLND_DEBUG_MEMORY
681: _mysqlnd_emalloc,
682: _mysqlnd_pemalloc,
683: _mysqlnd_ecalloc,
684: _mysqlnd_pecalloc,
685: _mysqlnd_erealloc,
686: _mysqlnd_perealloc,
687: _mysqlnd_efree,
688: _mysqlnd_pefree,
689: _mysqlnd_malloc,
690: _mysqlnd_calloc,
691: _mysqlnd_realloc,
692: _mysqlnd_free,
693: _mysqlnd_pestrndup,
694: _mysqlnd_pestrdup,
695: _mysqlnd_sprintf,
696: _mysqlnd_vsprintf,
697: _mysqlnd_sprintf_free
698: #else
699: mysqlnd_zend_mm_emalloc,
700: mysqlnd_zend_mm_pemalloc,
701: mysqlnd_zend_mm_ecalloc,
702: mysqlnd_zend_mm_pecalloc,
703: mysqlnd_zend_mm_erealloc,
704: mysqlnd_zend_mm_perealloc,
705: mysqlnd_zend_mm_efree,
706: mysqlnd_zend_mm_pefree,
707: mysqlnd_zend_mm_malloc,
708: mysqlnd_zend_mm_calloc,
709: mysqlnd_zend_mm_realloc,
710: mysqlnd_zend_mm_free,
711: mysqlnd_zend_mm_pestrndup,
712: mysqlnd_zend_mm_pestrdup
713: sprintf,
714: mysqlnd_zend_mm_efree,
715: #endif
716: };
717:
718:
719: /*
720: * Local variables:
721: * tab-width: 4
722: * c-basic-offset: 4
723: * End:
724: * vim600: noet sw=4 ts=4 fdm=marker
725: * vim<600: noet sw=4 ts=4
726: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>