Skip to content

Commit d37820f

Browse files
committed
Introduced get_resources() function.
It may be used for debugging and testing to identify resource leaks. Changed ext/standard/tests/http/bug60570.phpt to use this function instead of unsafe attempt to catch resource leaks using get_memory_usage()
1 parent 634d6ab commit d37820f

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

Zend/zend_builtin_functions.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static ZEND_FUNCTION(get_defined_functions);
7878
static ZEND_FUNCTION(get_defined_vars);
7979
static ZEND_FUNCTION(create_function);
8080
static ZEND_FUNCTION(get_resource_type);
81+
static ZEND_FUNCTION(get_resources);
8182
static ZEND_FUNCTION(get_loaded_extensions);
8283
static ZEND_FUNCTION(extension_loaded);
8384
static ZEND_FUNCTION(get_extension_funcs);
@@ -218,6 +219,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_get_resource_type, 0, 0, 1)
218219
ZEND_ARG_INFO(0, res)
219220
ZEND_END_ARG_INFO()
220221

222+
ZEND_BEGIN_ARG_INFO_EX(arginfo_get_resources, 0, 0, 0)
223+
ZEND_ARG_INFO(0, type)
224+
ZEND_END_ARG_INFO()
225+
221226
ZEND_BEGIN_ARG_INFO_EX(arginfo_get_loaded_extensions, 0, 0, 0)
222227
ZEND_ARG_INFO(0, zend_extensions)
223228
ZEND_END_ARG_INFO()
@@ -291,6 +296,7 @@ static const zend_function_entry builtin_functions[] = { /* {{{ */
291296
ZEND_FE(get_defined_vars, arginfo_zend__void)
292297
ZEND_FE(create_function, arginfo_create_function)
293298
ZEND_FE(get_resource_type, arginfo_get_resource_type)
299+
ZEND_FE(get_resources, arginfo_get_resources)
294300
ZEND_FE(get_loaded_extensions, arginfo_get_loaded_extensions)
295301
ZEND_FE(extension_loaded, arginfo_extension_loaded)
296302
ZEND_FE(get_extension_funcs, arginfo_extension_loaded)
@@ -1936,6 +1942,54 @@ ZEND_FUNCTION(get_resource_type)
19361942
}
19371943
/* }}} */
19381944

1945+
/* {{{ proto array get_resources()
1946+
Get an array with all active resources */
1947+
ZEND_FUNCTION(get_resources)
1948+
{
1949+
zend_string *type = NULL;
1950+
zend_string *key;
1951+
ulong index;
1952+
zval *val;
1953+
1954+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|S", &type) == FAILURE) {
1955+
return;
1956+
}
1957+
1958+
if (!type) {
1959+
array_init(return_value);
1960+
ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1961+
if (!key) {
1962+
Z_ADDREF_P(val);
1963+
zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1964+
}
1965+
} ZEND_HASH_FOREACH_END();
1966+
} else if (type->len == sizeof("Unknown")-1 &&
1967+
memcmp(type->val, "Unknown", sizeof("Unknown")-1) == 0) {
1968+
array_init(return_value);
1969+
ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1970+
if (!key && Z_RES_TYPE_P(val) <= 0) {
1971+
Z_ADDREF_P(val);
1972+
zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1973+
}
1974+
} ZEND_HASH_FOREACH_END();
1975+
} else {
1976+
int id = zend_fetch_list_dtor_id(type->val);
1977+
1978+
if (id <= 0) {
1979+
zend_error(E_WARNING, "get_resources(): Unknown resource type '%s'", type->val);
1980+
RETURN_FALSE;
1981+
}
1982+
1983+
array_init(return_value);
1984+
ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
1985+
if (!key && Z_RES_TYPE_P(val) == id) {
1986+
Z_ADDREF_P(val);
1987+
zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
1988+
}
1989+
} ZEND_HASH_FOREACH_END();
1990+
}
1991+
}
1992+
/* }}} */
19391993

19401994
static int add_extension_info(zval *item, void *arg TSRMLS_DC)
19411995
{

ext/standard/tests/http/bug60570.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function do_test() {
1919

2020
$pid = http_server("tcp://127.0.0.1:12342", $responses, $output);
2121

22-
$a = $b = null;
22+
$a = $b = count(get_resources());
2323

2424
$i = 3;
2525
while ($i--) {
@@ -28,7 +28,7 @@ function do_test() {
2828
unset($context);
2929

3030
$b = $a;
31-
$a = memory_get_usage();
31+
$a = count(get_resources());
3232
}
3333

3434
http_server_kill($pid);

0 commit comments

Comments
 (0)