mysqlnd_alloc.c 23 KB

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