mysqlnd_alloc.c 22 KB

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