From 04d24bde20f1374d73fc88853bae4320d5573461 Mon Sep 17 00:00:00 2001 From: Andrey Hristov Date: Tue, 4 May 2010 13:49:43 +0000 Subject: [PATCH] Add possibility to control the allocator in when PHP is compiled with debug --- ext/mysqlnd/mysqlnd.h | 6 ++ ext/mysqlnd/mysqlnd_debug.c | 120 +++++++++++++++++++++++++++++++++--- ext/mysqlnd/php_mysqlnd.c | 16 +++++ 3 files changed, 132 insertions(+), 10 deletions(-) diff --git a/ext/mysqlnd/mysqlnd.h b/ext/mysqlnd/mysqlnd.h index 0c076f7f846..fab2997129b 100644 --- a/ext/mysqlnd/mysqlnd.h +++ b/ext/mysqlnd/mysqlnd.h @@ -278,6 +278,12 @@ ZEND_BEGIN_MODULE_GLOBALS(mysqlnd) long log_mask; long net_read_timeout; long mempool_default_size; + long debug_emalloc_fail_threshold; + long debug_ecalloc_fail_threshold; + long debug_erealloc_fail_threshold; + long debug_malloc_fail_threshold; + long debug_calloc_fail_threshold; + long debug_realloc_fail_threshold; ZEND_END_MODULE_GLOBALS(mysqlnd) ZEND_EXTERN_MODULE_GLOBALS(mysqlnd); diff --git a/ext/mysqlnd/mysqlnd_debug.c b/ext/mysqlnd/mysqlnd_debug.c index b301f1d8c7b..0df48551e5b 100644 --- a/ext/mysqlnd/mysqlnd_debug.c +++ b/ext/mysqlnd/mysqlnd_debug.c @@ -660,12 +660,22 @@ void * _mysqlnd_emalloc(size_t size MYSQLND_MEM_D) { void *ret; zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics); - + long * threshold = &MYSQLND_G(debug_emalloc_fail_threshold); DBG_ENTER(mysqlnd_emalloc_name); DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno); - ret = emalloc(REAL_SIZE(size)); +#ifdef PHP_DEBUG + /* -1 is also "true" */ + if (*threshold) { +#endif + ret = emalloc(REAL_SIZE(size)); +#ifdef PHP_DEBUG + --*threshold; + } else if (*threshold == 0) { + ret = NULL; + } +#endif DBG_INF_FMT("size=%lu ptr=%p", size, ret); @@ -683,10 +693,22 @@ void * _mysqlnd_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D) { void *ret; zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics); + long * threshold = persistent? &MYSQLND_G(debug_malloc_fail_threshold):&MYSQLND_G(debug_emalloc_fail_threshold); DBG_ENTER(mysqlnd_pemalloc_name); DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno); - ret = pemalloc(REAL_SIZE(size), persistent); +#ifdef PHP_DEBUG + /* -1 is also "true" */ + if (*threshold) { +#endif + ret = pemalloc(REAL_SIZE(size), persistent); +#ifdef PHP_DEBUG + --*threshold; + } else if (*threshold == 0) { + ret = NULL; + } +#endif + DBG_INF_FMT("size=%lu ptr=%p persistent=%d", size, ret, persistent); if (collect_memory_statistics) { @@ -706,11 +728,22 @@ void * _mysqlnd_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D) { void *ret; zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics); + long * threshold = &MYSQLND_G(debug_ecalloc_fail_threshold); DBG_ENTER(mysqlnd_ecalloc_name); DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno); DBG_INF_FMT("before: %lu", zend_memory_usage(FALSE TSRMLS_CC)); - ret = ecalloc(nmemb, REAL_SIZE(size)); +#ifdef PHP_DEBUG + /* -1 is also "true" */ + if (*threshold) { +#endif + ret = ecalloc(nmemb, REAL_SIZE(size)); +#ifdef PHP_DEBUG + --*threshold; + } else if (*threshold == 0) { + ret = NULL; + } +#endif DBG_INF_FMT("after : %lu", zend_memory_usage(FALSE TSRMLS_CC)); DBG_INF_FMT("size=%lu ptr=%p", size, ret); @@ -728,10 +761,22 @@ void * _mysqlnd_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent M { void *ret; zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics); + long * threshold = persistent? &MYSQLND_G(debug_calloc_fail_threshold):&MYSQLND_G(debug_ecalloc_fail_threshold); DBG_ENTER(mysqlnd_pecalloc_name); DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno); - ret = pecalloc(nmemb, REAL_SIZE(size), persistent); +#ifdef PHP_DEBUG + /* -1 is also "true" */ + if (*threshold) { +#endif + ret = pecalloc(nmemb, REAL_SIZE(size), persistent); +#ifdef PHP_DEBUG + --*threshold; + } else if (*threshold == 0) { + ret = NULL; + } +#endif + DBG_INF_FMT("size=%lu ptr=%p", size, ret); if (collect_memory_statistics) { @@ -752,11 +797,22 @@ void * _mysqlnd_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D) void *ret; zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics); size_t old_size = collect_memory_statistics && ptr? *(size_t *) (((char*)ptr) - sizeof(size_t)) : 0; + long * threshold = &MYSQLND_G(debug_erealloc_fail_threshold); DBG_ENTER(mysqlnd_erealloc_name); DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno); DBG_INF_FMT("ptr=%p old_size=%lu, new_size=%lu", ptr, old_size, new_size); - ret = erealloc(REAL_PTR(ptr), REAL_SIZE(new_size)); +#ifdef PHP_DEBUG + /* -1 is also "true" */ + if (*threshold) { +#endif + ret = erealloc(REAL_PTR(ptr), REAL_SIZE(new_size)); +#ifdef PHP_DEBUG + --*threshold; + } else if (*threshold == 0) { + ret = NULL; + } +#endif DBG_INF_FMT("new_ptr=%p", (char*)ret); if (collect_memory_statistics) { @@ -774,11 +830,22 @@ void * _mysqlnd_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQL void *ret; zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics); size_t old_size = collect_memory_statistics && ptr? *(size_t *) (((char*)ptr) - sizeof(size_t)) : 0; + long * threshold = persistent? &MYSQLND_G(debug_realloc_fail_threshold):&MYSQLND_G(debug_erealloc_fail_threshold); DBG_ENTER(mysqlnd_perealloc_name); DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno); DBG_INF_FMT("ptr=%p old_size=%lu new_size=%lu persist=%d", ptr, old_size, new_size, persistent); - ret = perealloc(REAL_PTR(ptr), REAL_SIZE(new_size), persistent); +#ifdef PHP_DEBUG + /* -1 is also "true" */ + if (*threshold) { +#endif + ret = perealloc(REAL_PTR(ptr), REAL_SIZE(new_size), persistent); +#ifdef PHP_DEBUG + --*threshold; + } else if (*threshold == 0) { + ret = NULL; + } +#endif DBG_INF_FMT("new_ptr=%p", (char*)ret); @@ -848,10 +915,21 @@ void * _mysqlnd_malloc(size_t size MYSQLND_MEM_D) { void *ret; zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics); + long * threshold = &MYSQLND_G(debug_malloc_fail_threshold); DBG_ENTER(mysqlnd_malloc_name); DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno); - ret = malloc(REAL_SIZE(size)); +#ifdef PHP_DEBUG + /* -1 is also "true" */ + if (*threshold) { +#endif + ret = malloc(REAL_SIZE(size)); +#ifdef PHP_DEBUG + --*threshold; + } else if (*threshold == 0) { + ret = NULL; + } +#endif DBG_INF_FMT("size=%lu ptr=%p", size, ret); if (collect_memory_statistics) { @@ -868,10 +946,21 @@ void * _mysqlnd_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D) { void *ret; zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics); + long * threshold = &MYSQLND_G(debug_calloc_fail_threshold); DBG_ENTER(mysqlnd_calloc_name); DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno); - ret = calloc(nmemb, REAL_SIZE(size)); +#ifdef PHP_DEBUG + /* -1 is also "true" */ + if (*threshold) { +#endif + ret = calloc(nmemb, REAL_SIZE(size)); +#ifdef PHP_DEBUG + --*threshold; + } else if (*threshold == 0) { + ret = NULL; + } +#endif DBG_INF_FMT("size=%lu ptr=%p", size, ret); if (collect_memory_statistics) { @@ -888,12 +977,23 @@ void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D) { void *ret; zend_bool collect_memory_statistics = MYSQLND_G(collect_memory_statistics); + long * threshold = &MYSQLND_G(debug_realloc_fail_threshold); DBG_ENTER(mysqlnd_realloc_name); DBG_INF_FMT("file=%-15s line=%4d", strrchr(__zend_filename, PHP_DIR_SEPARATOR) + 1, __zend_lineno); DBG_INF_FMT("ptr=%p new_size=%lu ", new_size, ptr); DBG_INF_FMT("before: %lu", zend_memory_usage(TRUE TSRMLS_CC)); - ret = realloc(REAL_PTR(ptr), REAL_SIZE(new_size)); +#ifdef PHP_DEBUG + /* -1 is also "true" */ + if (*threshold) { +#endif + ret = realloc(REAL_PTR(ptr), REAL_SIZE(new_size)); +#ifdef PHP_DEBUG + --*threshold; + } else if (*threshold == 0) { + ret = NULL; + } +#endif DBG_INF_FMT("new_ptr=%p", (char*)ret); diff --git a/ext/mysqlnd/php_mysqlnd.c b/ext/mysqlnd/php_mysqlnd.c index cd9048c3668..6f39b3bbd9f 100644 --- a/ext/mysqlnd/php_mysqlnd.c +++ b/ext/mysqlnd/php_mysqlnd.c @@ -152,6 +152,12 @@ static PHP_GINIT_FUNCTION(mysqlnd) mysqlnd_globals->net_read_timeout = 31536000; mysqlnd_globals->log_mask = 0; mysqlnd_globals->mempool_default_size = 16000; + mysqlnd_globals->debug_emalloc_fail_threshold = -1; + mysqlnd_globals->debug_ecalloc_fail_threshold = -1; + mysqlnd_globals->debug_erealloc_fail_threshold = -1; + mysqlnd_globals->debug_malloc_fail_threshold = -1; + mysqlnd_globals->debug_calloc_fail_threshold = -1; + mysqlnd_globals->debug_realloc_fail_threshold = -1; } /* }}} */ @@ -178,6 +184,16 @@ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("mysqlnd.net_read_timeout", "31536000", PHP_INI_SYSTEM, OnUpdateLong, net_read_timeout, zend_mysqlnd_globals, mysqlnd_globals) STD_PHP_INI_ENTRY("mysqlnd.log_mask", "0", PHP_INI_ALL, OnUpdateLong, log_mask, zend_mysqlnd_globals, mysqlnd_globals) STD_PHP_INI_ENTRY("mysqlnd.mempool_default_size","16000", PHP_INI_ALL, OnUpdateLong, mempool_default_size, zend_mysqlnd_globals, mysqlnd_globals) + +#ifdef PHP_DEBUG + STD_PHP_INI_ENTRY("mysqlnd.debug_emalloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_emalloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals) + STD_PHP_INI_ENTRY("mysqlnd.debug_ecalloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_ecalloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals) + STD_PHP_INI_ENTRY("mysqlnd.debug_erealloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_erealloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals) + + STD_PHP_INI_ENTRY("mysqlnd.debug_malloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_malloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals) + STD_PHP_INI_ENTRY("mysqlnd.debug_calloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_calloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals) + STD_PHP_INI_ENTRY("mysqlnd.debug_realloc_fail_threshold","-1", PHP_INI_SYSTEM, OnUpdateLong, debug_realloc_fail_threshold, zend_mysqlnd_globals, mysqlnd_globals) +#endif PHP_INI_END() /* }}} */