From a9352925744d93f771f429dc6af6ea54cc13d9c3 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 01:22:45 +0100 Subject: [PATCH 001/103] start to migrate for pass building on php 7 --- libevent.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libevent.c b/libevent.c index 56c572d..58bcd36 100644 --- a/libevent.c +++ b/libevent.c @@ -39,7 +39,7 @@ #ifndef ZEND_FETCH_RESOURCE_NO_RETURN # define ZEND_FETCH_RESOURCE_NO_RETURN(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type) \ - (rsrc = (rsrc_type) zend_fetch_resource(passed_id TSRMLS_CC, default_id, resource_type_name, NULL, 1, resource_type)) + (rsrc = (rsrc_type) zend_fetch_resource2(passed_id TSRMLS_CC, default_id, resource_type_name, NULL, 1, resource_type)) #endif #ifdef PHP_WIN32 @@ -86,7 +86,7 @@ ZEND_GET_MODULE(libevent) typedef struct _php_event_base_t { /* {{{ */ struct event_base *base; int rsrc_id; - zend_uint events; + zend_uintptr_t events; } php_event_base_t; /* }}} */ @@ -124,13 +124,13 @@ typedef struct _php_bufferevent_t { /* {{{ */ /* }}} */ #define ZVAL_TO_BASE(zval, base) \ - ZEND_FETCH_RESOURCE(base, php_event_base_t *, &zval, -1, "event base", le_event_base) + zend_fetch_resource2_ex(base, php_event_base_t *, &zval, -1, "event base", le_event_base) #define ZVAL_TO_EVENT(zval, event) \ - ZEND_FETCH_RESOURCE(event, php_event_t *, &zval, -1, "event", le_event) + zend_fetch_resource2_ex(event, php_event_t *, &zval, -1, "event", le_event) #define ZVAL_TO_BEVENT(zval, bevent) \ - ZEND_FETCH_RESOURCE(bevent, php_bufferevent_t *, &zval, -1, "buffer event", le_bufferevent) + zend_fetch_resource2_ex(bevent, php_bufferevent_t *, &zval, -1, "buffer event", le_bufferevent) /* {{{ internal funcs */ @@ -148,18 +148,18 @@ static inline void _php_event_callback_free(php_event_callback_t *callback) /* { } /* }}} */ -static void _php_event_base_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */ +ZEND_RSRC_DTOR_FUNC(_php_event_base_dtor) /* {{{ */ { - php_event_base_t *base = (php_event_base_t*)rsrc->ptr; + php_event_base_t *base = (php_event_base_t*)res->ptr; event_base_free(base->base); efree(base); } /* }}} */ -static void _php_event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */ +ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ { - php_event_t *event = (php_event_t*)rsrc->ptr; + php_event_t *event = (php_event_t*)res->ptr; int base_id = -1; if (event->in_free) { @@ -187,9 +187,9 @@ static void _php_event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */ } /* }}} */ -static void _php_bufferevent_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */ +ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ { - php_bufferevent_t *bevent = (php_bufferevent_t*)rsrc->ptr; + php_bufferevent_t *bevent = (php_bufferevent_t*)res->ptr; int base_id = -1; if (bevent->base) { From 35def1c7d17559c07d2f2e6c2670dc7779e70f4d Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 01:41:10 +0100 Subject: [PATCH 002/103] RETURN_RESOURCE -> RETVAL_OBJ & fix ZVAL_TO_* macro --- libevent.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libevent.c b/libevent.c index 58bcd36..9ca86fa 100644 --- a/libevent.c +++ b/libevent.c @@ -124,13 +124,13 @@ typedef struct _php_bufferevent_t { /* {{{ */ /* }}} */ #define ZVAL_TO_BASE(zval, base) \ - zend_fetch_resource2_ex(base, php_event_base_t *, &zval, -1, "event base", le_event_base) + (php_event_base_t *)zend_fetch_resource2_ex(zval, -1, "event base", le_event_base) #define ZVAL_TO_EVENT(zval, event) \ - zend_fetch_resource2_ex(event, php_event_t *, &zval, -1, "event", le_event) + (php_event_t *)zend_fetch_resource2_ex(zval, -1, "event", le_event) #define ZVAL_TO_BEVENT(zval, bevent) \ - zend_fetch_resource2_ex(bevent, php_bufferevent_t *, &zval, -1, "buffer event", le_bufferevent) + (php_bufferevent_t *)zend_fetch_resource2_exzval, -1, "buffer event", le_bufferevent) /* {{{ internal funcs */ @@ -374,7 +374,7 @@ static PHP_FUNCTION(event_base_new) #else base->rsrc_id = zend_list_insert(base, le_event_base); #endif - RETURN_RESOURCE(base->rsrc_id); + RETVAL_OBJ(base->rsrc_id); } /* }}} */ @@ -586,7 +586,7 @@ static PHP_FUNCTION(event_new) #else event->rsrc_id = zend_list_insert(event, le_event); #endif - RETURN_RESOURCE(event->rsrc_id); + RETVAL_OBJ(event->rsrc_id); } /* }}} */ @@ -996,7 +996,7 @@ static PHP_FUNCTION(event_buffer_new) #else bevent->rsrc_id = zend_list_insert(bevent, le_bufferevent); #endif - RETURN_RESOURCE(bevent->rsrc_id); + RETVAL_OBJ(bevent->rsrc_id); } /* }}} */ From 8f3031821343cfa082c1797bdb796cbcaddb27f5 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 01:48:22 +0100 Subject: [PATCH 003/103] ZEND_FETCH_RESOURCE2_NO_RETURN -> zend_fetch_resource2_ex --- libevent.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libevent.c b/libevent.c index 9ca86fa..2114666 100644 --- a/libevent.c +++ b/libevent.c @@ -676,13 +676,13 @@ static PHP_FUNCTION(event_set) } } else { if (Z_TYPE_PP(fd) == IS_RESOURCE) { - if (ZEND_FETCH_RESOURCE2_NO_RETURN(stream, php_stream *, fd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { + if ((php_stream *)zend_fetch_resource2_ex(fd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&file_desc, 1) != SUCCESS || file_desc < 0) { RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - if (ZEND_FETCH_RESOURCE_NO_RETURN(php_sock, php_socket *, fd, -1, NULL, php_sockets_le_socket())) { + if ((php_socket *)zend_fetch_resource2_ex(fd, -1, NULL, php_sockets_le_socket())) { file_desc = php_sock->bsd_socket; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource"); @@ -907,13 +907,13 @@ static PHP_FUNCTION(event_buffer_new) } if (Z_TYPE_P(zfd) == IS_RESOURCE) { - if (ZEND_FETCH_RESOURCE2_NO_RETURN(stream, php_stream *, &zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { + if ((php_stream *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) != SUCCESS || fd < 0) { RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - if (ZEND_FETCH_RESOURCE_NO_RETURN(php_sock, php_socket *, &zfd, -1, NULL, php_sockets_le_socket())) { + if ((php_socket *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_sockets_le_socket())) { fd = php_sock->bsd_socket; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); From 83885e2153ca6376a6252af67cdd3862809b9c61 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 01:49:33 +0100 Subject: [PATCH 004/103] syntax --- libevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libevent.c b/libevent.c index 2114666..dd12717 100644 --- a/libevent.c +++ b/libevent.c @@ -130,7 +130,7 @@ typedef struct _php_bufferevent_t { /* {{{ */ (php_event_t *)zend_fetch_resource2_ex(zval, -1, "event", le_event) #define ZVAL_TO_BEVENT(zval, bevent) \ - (php_bufferevent_t *)zend_fetch_resource2_exzval, -1, "buffer event", le_bufferevent) + (php_bufferevent_t *)zend_fetch_resource2_ex(zval, -1, "buffer event", le_bufferevent) /* {{{ internal funcs */ From ee164cb58b84a402ce43d3d161ed5a6c3f0dde45 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 02:09:19 +0100 Subject: [PATCH 005/103] remove MAKE_STD_ZVAL usage --- libevent.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/libevent.c b/libevent.c index dd12717..c960c78 100644 --- a/libevent.c +++ b/libevent.c @@ -42,6 +42,7 @@ (rsrc = (rsrc_type) zend_fetch_resource2(passed_id TSRMLS_CC, default_id, resource_type_name, NULL, 1, resource_type)) #endif + #ifdef PHP_WIN32 /* XXX compiling with 2.x on Windows. Luckily the ext code works thanks to the compat exports from the libevent. However it might need to be adapted to the @@ -232,18 +233,16 @@ static void _php_event_callback(int fd, short events, void *arg) /* {{{ */ callback = event->callback; - MAKE_STD_ZVAL(args[0]); if (event->stream_id >= 0) { - ZVAL_RESOURCE(args[0], event->stream_id); + ZVAL_RESOURCE(&args[0], event->stream_id); zend_list_addref(event->stream_id); } else if (events & EV_SIGNAL) { ZVAL_LONG(args[0], fd); } else { ZVAL_NULL(args[0]); } - - MAKE_STD_ZVAL(args[1]); - ZVAL_LONG(args[1], events); + + ZVAL_LONG(&args[1], events); args[2] = callback->arg; Z_ADDREF_P(callback->arg); @@ -270,8 +269,7 @@ static void _php_bufferevent_readcb(struct bufferevent *be, void *arg) /* {{{ */ return; } - MAKE_STD_ZVAL(args[0]); - ZVAL_RESOURCE(args[0], bevent->rsrc_id); + ZVAL_RESOURCE(&args[0], bevent->rsrc_id); zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ args[1] = bevent->arg; @@ -298,8 +296,7 @@ static void _php_bufferevent_writecb(struct bufferevent *be, void *arg) /* {{{ * return; } - MAKE_STD_ZVAL(args[0]); - ZVAL_RESOURCE(args[0], bevent->rsrc_id); + ZVAL_RESOURCE(&args[0], bevent->rsrc_id); zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ args[1] = bevent->arg; @@ -326,12 +323,10 @@ static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *a return; } - MAKE_STD_ZVAL(args[0]); - ZVAL_RESOURCE(args[0], bevent->rsrc_id); + ZVAL_RESOURCE(&args[0], bevent->rsrc_id); zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ - - MAKE_STD_ZVAL(args[1]); - ZVAL_LONG(args[1], what); + + ZVAL_LONG(&args[1], what); args[2] = bevent->arg; Z_ADDREF_P(args[2]); @@ -1255,13 +1250,13 @@ static PHP_FUNCTION(event_buffer_fd_set) ZVAL_TO_BEVENT(zbevent, bevent); if (Z_TYPE_P(zfd) == IS_RESOURCE) { - if (ZEND_FETCH_RESOURCE2_NO_RETURN(stream, php_stream *, &zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { + if (( php_stream *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) != SUCCESS || fd < 0) { RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - if (ZEND_FETCH_RESOURCE_NO_RETURN(php_sock, php_socket *, &zfd, -1, NULL, php_sockets_le_socket())) { + if ((php_socket *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_sockets_le_socket())) { fd = php_sock->bsd_socket; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); From 0a035bcec2e3b2a6c63930b5c23ca378df7076b0 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 02:27:39 +0100 Subject: [PATCH 006/103] fix ZVAL_RESOURCE & removes all zend_list_addref --- libevent.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/libevent.c b/libevent.c index c960c78..d759c20 100644 --- a/libevent.c +++ b/libevent.c @@ -234,8 +234,8 @@ static void _php_event_callback(int fd, short events, void *arg) /* {{{ */ callback = event->callback; if (event->stream_id >= 0) { - ZVAL_RESOURCE(&args[0], event->stream_id); - zend_list_addref(event->stream_id); + ZVAL_RES(args[0], zend_register_resource(event->stream_id, le_event)); + /*zend_list_addref(event->stream_id);*/ } else if (events & EV_SIGNAL) { ZVAL_LONG(args[0], fd); } else { @@ -269,8 +269,8 @@ static void _php_bufferevent_readcb(struct bufferevent *be, void *arg) /* {{{ */ return; } - ZVAL_RESOURCE(&args[0], bevent->rsrc_id); - zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ + ZVAL_RES(args[0], zend_register_resource(bevent->rsrc_id, le_bufferevent)); + /*zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ args[1] = bevent->arg; Z_ADDREF_P(args[1]); @@ -296,8 +296,8 @@ static void _php_bufferevent_writecb(struct bufferevent *be, void *arg) /* {{{ * return; } - ZVAL_RESOURCE(&args[0], bevent->rsrc_id); - zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ + ZVAL_RES(args[0], zend_register_resource( bevent->rsrc_id, le_bufferevent)); + /*zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ args[1] = bevent->arg; Z_ADDREF_P(args[1]); @@ -323,8 +323,8 @@ static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *a return; } - ZVAL_RESOURCE(&args[0], bevent->rsrc_id); - zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ + ZVAL_RES(args[0], zend_register_resource( bevent->rsrc_id, le_bufferevent)); + /*zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ ZVAL_LONG(&args[1], what); @@ -429,7 +429,7 @@ static PHP_FUNCTION(event_base_loop) } ZVAL_TO_BASE(zbase, base); - zend_list_addref(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */ + /*zend_list_addref(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */ ret = event_base_loop(base->base, flags); zend_list_delete(base->rsrc_id); @@ -512,7 +512,7 @@ static PHP_FUNCTION(event_base_set) if (ret == 0) { if (base != old_base) { /* make sure the base is destroyed after the event */ - zend_list_addref(base->rsrc_id); + /*zend_list_addref(base->rsrc_id);*/ ++base->events; } @@ -727,7 +727,7 @@ static PHP_FUNCTION(event_set) if (events & EV_SIGNAL) { event->stream_id = -1; } else { - zend_list_addref(Z_LVAL_PP(fd)); + /*zend_list_addref(Z_LVAL_PP(fd));*/ event->stream_id = Z_LVAL_PP(fd); } @@ -1033,7 +1033,7 @@ static PHP_FUNCTION(event_buffer_base_set) if (ret == 0) { if (base != old_base) { /* make sure the base is destroyed after the event */ - zend_list_addref(base->rsrc_id); + /*zend_list_addref(base->rsrc_id);*/ ++base->events; } From 33820721c24c0beda2cd284d00494a162beed29a Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 02:30:13 +0100 Subject: [PATCH 007/103] fix Z_LVAL_PP -> Z_LVAL_P --- libevent.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libevent.c b/libevent.c index d759c20..ee7d5dd 100644 --- a/libevent.c +++ b/libevent.c @@ -643,7 +643,7 @@ static PHP_FUNCTION(event_add) */ static PHP_FUNCTION(event_set) { - zval *zevent, **fd, *zcallback, *zarg = NULL; + zval *zevent, *fd, *zcallback, *zarg = NULL; php_event_t *event; long events; php_event_callback_t *callback, *old_callback; @@ -664,13 +664,13 @@ static PHP_FUNCTION(event_set) if (events & EV_SIGNAL) { /* signal support */ convert_to_long_ex(fd); - file_desc = Z_LVAL_PP(fd); + file_desc = Z_LVAL_P(fd); if (file_desc < 0 || file_desc >= NSIG) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid signal passed"); RETURN_FALSE; } } else { - if (Z_TYPE_PP(fd) == IS_RESOURCE) { + if (Z_TYPE_P(fd) == IS_RESOURCE) { if ((php_stream *)zend_fetch_resource2_ex(fd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&file_desc, 1) != SUCCESS || file_desc < 0) { RETURN_FALSE; @@ -688,8 +688,8 @@ static PHP_FUNCTION(event_set) RETURN_FALSE; #endif } - } else if (Z_TYPE_PP(fd) == IS_LONG) { - file_desc = Z_LVAL_PP(fd); + } else if (Z_TYPE_P(fd) == IS_LONG) { + file_desc = Z_LVAL_P(fd); if (file_desc < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid file descriptor passed"); RETURN_FALSE; @@ -728,7 +728,7 @@ static PHP_FUNCTION(event_set) event->stream_id = -1; } else { /*zend_list_addref(Z_LVAL_PP(fd));*/ - event->stream_id = Z_LVAL_PP(fd); + event->stream_id = Z_LVAL_P(fd); } event_set(event->event, (int)file_desc, (short)events, _php_event_callback, event); From ebf1974fb5a6d613d33c0200dbf5de1d6bd5c101 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 02:47:43 +0100 Subject: [PATCH 008/103] fixing ALLOC_INIT_ZVAL --- libevent.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/libevent.c b/libevent.c index ee7d5dd..828d239 100644 --- a/libevent.c +++ b/libevent.c @@ -712,15 +712,10 @@ static PHP_FUNCTION(event_set) efree(func_name); zval_add_ref(&zcallback); - if (zarg) { - zval_add_ref(&zarg); - } else { - ALLOC_INIT_ZVAL(zarg); - } callback = emalloc(sizeof(php_event_callback_t)); callback->func = zcallback; - callback->arg = zarg; + callback->arg = &zarg; old_callback = event->callback; event->callback = callback; @@ -824,15 +819,10 @@ static PHP_FUNCTION(event_timer_set) efree(func_name); zval_add_ref(&zcallback); - if (zarg) { - zval_add_ref(&zarg); - } else { - ALLOC_INIT_ZVAL(zarg); - } callback = emalloc(sizeof(php_event_callback_t)); callback->func = zcallback; - callback->arg = zarg; + callback->arg = &zarg; old_callback = event->callback; event->callback = callback; @@ -978,10 +968,7 @@ static PHP_FUNCTION(event_buffer_new) bevent->errorcb = zerrorcb; if (zarg) { - zval_add_ref(&zarg); - bevent->arg = zarg; - } else { - ALLOC_INIT_ZVAL(bevent->arg); + bevent->arg = &zarg; } TSRMLS_SET_CTX(bevent->thread_ctx); @@ -1371,7 +1358,7 @@ static PHP_FUNCTION(event_buffer_set_callback) if (bevent->arg) { zval_ptr_dtor(&bevent->arg); } - bevent->arg = zarg; + bevent->arg = &zarg; } RETURN_TRUE; From ae0c4dd1cf0dfc3618c8d915c3e8a59c62d50518 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 03:51:19 +0100 Subject: [PATCH 009/103] fix resources creation --- libevent.c | 52 ++++++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/libevent.c b/libevent.c index 828d239..9d9085c 100644 --- a/libevent.c +++ b/libevent.c @@ -350,6 +350,7 @@ static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *a static PHP_FUNCTION(event_base_new) { php_event_base_t *base; + zval *tmp; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") != SUCCESS) { return; @@ -364,12 +365,10 @@ static PHP_FUNCTION(event_base_new) base->events = 0; -#if PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4 - base->rsrc_id = zend_list_insert(base, le_event_base TSRMLS_CC); -#else - base->rsrc_id = zend_list_insert(base, le_event_base); -#endif - RETVAL_OBJ(base->rsrc_id); + tmp = zend_list_insert(base, le_event_base); + base->rsrc_id = Z_RES_P(tmp); + + RETVAL_RES(zend_register_resource(base->rsrc_id, le_event_base)); } /* }}} */ @@ -562,6 +561,7 @@ static PHP_FUNCTION(event_base_priority_init) static PHP_FUNCTION(event_new) { php_event_t *event; + zval *tmp; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") != SUCCESS) { return; @@ -569,19 +569,16 @@ static PHP_FUNCTION(event_new) event = emalloc(sizeof(php_event_t)); event->event = ecalloc(1, sizeof(struct event)); - event->stream_id = -1; event->callback = NULL; event->base = NULL; event->in_free = 0; TSRMLS_SET_CTX(event->thread_ctx); -#if PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4 - event->rsrc_id = zend_list_insert(event, le_event TSRMLS_CC); -#else - event->rsrc_id = zend_list_insert(event, le_event); -#endif - RETVAL_OBJ(event->rsrc_id); + tmp = zend_list_insert(event, le_event); + event->rsrc_id = Z_RES_P(tmp); + + RETVAL_RES(zend_register_resource(event->rsrc_id, le_event)); } /* }}} */ @@ -711,7 +708,7 @@ static PHP_FUNCTION(event_set) } efree(func_name); - zval_add_ref(&zcallback); + zval_addref_p(zcallback); callback = emalloc(sizeof(php_event_callback_t)); callback->func = zcallback; @@ -818,7 +815,7 @@ static PHP_FUNCTION(event_timer_set) } efree(func_name); - zval_add_ref(&zcallback); + zval_addref_p(zcallback); callback = emalloc(sizeof(php_event_callback_t)); callback->func = zcallback; @@ -883,6 +880,7 @@ static PHP_FUNCTION(event_buffer_new) zval *zfd, *zreadcb, *zwritecb, *zerrorcb, *zarg = NULL; php_socket_t fd; char *func_name; + zval *tmp; #ifdef LIBEVENT_SOCKETS_SUPPORT php_socket *php_sock; #endif @@ -955,16 +953,16 @@ static PHP_FUNCTION(event_buffer_new) bevent->base = NULL; if (zreadcb) { - zval_add_ref(&zreadcb); + zval_addref_p(zreadcb); } bevent->readcb = zreadcb; if (zwritecb) { - zval_add_ref(&zwritecb); + zval_addref_p(zwritecb); } bevent->writecb = zwritecb; - zval_add_ref(&zerrorcb); + zval_addref_p(zerrorcb); bevent->errorcb = zerrorcb; if (zarg) { @@ -973,12 +971,10 @@ static PHP_FUNCTION(event_buffer_new) TSRMLS_SET_CTX(bevent->thread_ctx); -#if PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4 - bevent->rsrc_id = zend_list_insert(bevent, le_bufferevent TSRMLS_CC); -#else - bevent->rsrc_id = zend_list_insert(bevent, le_bufferevent); -#endif - RETVAL_OBJ(bevent->rsrc_id); + tmp = zend_list_insert(bevent, le_bufferevent); + bevent->rsrc_id = Z_RES_P(tmp); + + RETVAL_RES(zend_register_resource(bevent->rsrc_id, le_bufferevent)); } /* }}} */ @@ -1317,7 +1313,7 @@ static PHP_FUNCTION(event_buffer_set_callback) } if (zreadcb) { - zval_add_ref(&zreadcb); + zval_addref_p(zreadcb); if (bevent->readcb) { zval_ptr_dtor(&bevent->readcb); @@ -1331,7 +1327,7 @@ static PHP_FUNCTION(event_buffer_set_callback) } if (zwritecb) { - zval_add_ref(&zwritecb); + zval_addref_p(zwritecb); if (bevent->writecb) { zval_ptr_dtor(&bevent->writecb); @@ -1345,7 +1341,7 @@ static PHP_FUNCTION(event_buffer_set_callback) } if (zerrorcb) { - zval_add_ref(&zerrorcb); + zval_addref_p(zerrorcb); if (bevent->errorcb) { zval_ptr_dtor(&bevent->errorcb); @@ -1354,7 +1350,7 @@ static PHP_FUNCTION(event_buffer_set_callback) } if (zarg) { - zval_add_ref(&zarg); + zval_addref_p(zarg); if (bevent->arg) { zval_ptr_dtor(&bevent->arg); } From 7b09a31ee2823aa60fec1a5a434092a4deb1ae64 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 04:04:46 +0100 Subject: [PATCH 010/103] change args --- libevent.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libevent.c b/libevent.c index 9d9085c..4a229e4 100644 --- a/libevent.c +++ b/libevent.c @@ -652,7 +652,7 @@ static PHP_FUNCTION(event_set) #endif int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rZlz|z", &zevent, &fd, &events, &zcallback, &zarg) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rzlz|z", &zevent, &fd, &events, &zcallback, &zarg) != SUCCESS) { return; } @@ -746,7 +746,7 @@ static PHP_FUNCTION(event_del) zval *zevent; php_event_t *event; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zevent) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zevent) != SUCCESS) { return; } @@ -773,7 +773,7 @@ static PHP_FUNCTION(event_priority_set) long priority; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zevent, &priority) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zevent, &priority) != SUCCESS) { return; } @@ -802,7 +802,7 @@ static PHP_FUNCTION(event_timer_set) php_event_callback_t *callback, *old_callback; char *func_name; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|z", &zevent, &zcallback, &zarg) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz|z", &zevent, &zcallback, &zarg) != SUCCESS) { return; } @@ -846,7 +846,7 @@ static PHP_FUNCTION(event_timer_pending) int ret; long timeout = -1; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zevent, &timeout) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zevent, &timeout) != SUCCESS) { return; } @@ -885,7 +885,7 @@ static PHP_FUNCTION(event_buffer_new) php_socket *php_sock; #endif - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzz|z", &zfd, &zreadcb, &zwritecb, &zerrorcb, &zarg) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zzzz|z", &zfd, &zreadcb, &zwritecb, &zerrorcb, &zarg) != SUCCESS) { return; } @@ -985,7 +985,7 @@ static PHP_FUNCTION(event_buffer_free) zval *zbevent; php_bufferevent_t *bevent; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zbevent) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zbevent) != SUCCESS) { return; } @@ -1003,7 +1003,7 @@ static PHP_FUNCTION(event_buffer_base_set) php_bufferevent_t *bevent; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zbevent, &zbase) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &zbevent, &zbase) != SUCCESS) { return; } @@ -1041,7 +1041,7 @@ static PHP_FUNCTION(event_buffer_priority_set) long priority; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zbevent, &priority) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbevent, &priority) != SUCCESS) { return; } @@ -1072,7 +1072,7 @@ static PHP_FUNCTION(event_buffer_write) long data_size = -1; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zbevent, &data, &data_len, &data_size) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &zbevent, &data, &data_len, &data_size) != SUCCESS) { return; } @@ -1104,7 +1104,7 @@ static PHP_FUNCTION(event_buffer_read) long data_size; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zbevent, &data_size) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbevent, &data_size) != SUCCESS) { return; } @@ -1141,7 +1141,7 @@ static PHP_FUNCTION(event_buffer_enable) long events; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zbevent, &events) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbevent, &events) != SUCCESS) { return; } @@ -1165,7 +1165,7 @@ static PHP_FUNCTION(event_buffer_disable) long events; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zbevent, &events) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbevent, &events) != SUCCESS) { return; } @@ -1188,7 +1188,7 @@ static PHP_FUNCTION(event_buffer_timeout_set) php_bufferevent_t *bevent; long read_timeout, write_timeout; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &zbevent, &read_timeout, &write_timeout) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rll", &zbevent, &read_timeout, &write_timeout) != SUCCESS) { return; } @@ -1205,7 +1205,7 @@ static PHP_FUNCTION(event_buffer_watermark_set) php_bufferevent_t *bevent; long events, lowmark, highmark; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &zbevent, &events, &lowmark, &highmark) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &zbevent, &events, &lowmark, &highmark) != SUCCESS) { return; } @@ -1226,7 +1226,7 @@ static PHP_FUNCTION(event_buffer_fd_set) php_socket *php_sock; #endif - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &zbevent, &zfd) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &zbevent, &zfd) != SUCCESS) { return; } From bed5505131b1fc9470a06427ee9c5d7896df8aee Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 04:43:10 +0100 Subject: [PATCH 011/103] various --- libevent.c | 77 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/libevent.c b/libevent.c index 4a229e4..2400cd2 100644 --- a/libevent.c +++ b/libevent.c @@ -124,14 +124,14 @@ typedef struct _php_bufferevent_t { /* {{{ */ } php_bufferevent_t; /* }}} */ -#define ZVAL_TO_BASE(zval, base) \ - (php_event_base_t *)zend_fetch_resource2_ex(zval, -1, "event base", le_event_base) +#define ZVAL_TO_BASE(zval) \ + (php_event_base_t *)zend_fetch_resource2_ex(zval, NULL, "event base", le_event_base) -#define ZVAL_TO_EVENT(zval, event) \ - (php_event_t *)zend_fetch_resource2_ex(zval, -1, "event", le_event) +#define ZVAL_TO_EVENT(zval) \ + (php_event_t *)zend_fetch_resource2_ex(zval, NULL, "event", le_event) -#define ZVAL_TO_BEVENT(zval, bevent) \ - (php_bufferevent_t *)zend_fetch_resource2_ex(zval, -1, "buffer event", le_bufferevent) +#define ZVAL_TO_BEVENT(zval) \ + (php_bufferevent_t *)zend_fetch_resource2_ex(zval, NULL, "buffer event", le_bufferevent) /* {{{ internal funcs */ @@ -382,7 +382,7 @@ static PHP_FUNCTION(event_base_reinit) { return; } - ZVAL_TO_BASE(zbase, base); + base = ZVAL_TO_BASE(zbase); r = event_reinit(base->base); if (r == -1) { RETURN_FALSE @@ -403,7 +403,7 @@ static PHP_FUNCTION(event_base_free) return; } - ZVAL_TO_BASE(zbase, base); + base = ZVAL_TO_BASE(zbase); if (base->events > 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "base has events attached to it and cannot be freed"); @@ -427,7 +427,7 @@ static PHP_FUNCTION(event_base_loop) return; } - ZVAL_TO_BASE(zbase, base); + base = ZVAL_TO_BASE(zbase); /*zend_list_addref(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */ ret = event_base_loop(base->base, flags); zend_list_delete(base->rsrc_id); @@ -448,7 +448,7 @@ static PHP_FUNCTION(event_base_loopbreak) return; } - ZVAL_TO_BASE(zbase, base); + base = ZVAL_TO_BASE(zbase); ret = event_base_loopbreak(base->base); if (ret == 0) { RETURN_TRUE; @@ -470,7 +470,7 @@ static PHP_FUNCTION(event_base_loopexit) return; } - ZVAL_TO_BASE(zbase, base); + base = ZVAL_TO_BASE(zbase); if (timeout < 0) { ret = event_base_loopexit(base->base, NULL); @@ -502,8 +502,8 @@ static PHP_FUNCTION(event_base_set) return; } - ZVAL_TO_BASE(zbase, base); - ZVAL_TO_EVENT(zevent, event); + base = ZVAL_TO_BASE(zbase); + event = ZVAL_TO_EVENT(zevent); old_base = event->base; ret = event_base_set(base->base, event->event); @@ -540,7 +540,7 @@ static PHP_FUNCTION(event_base_priority_init) return; } - ZVAL_TO_BASE(zbase, base); + base = ZVAL_TO_BASE(zbase); if (npriorities < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "npriorities cannot be less than zero"); @@ -593,7 +593,7 @@ static PHP_FUNCTION(event_free) return; } - ZVAL_TO_EVENT(zevent, event); + event = ZVAL_TO_EVENT(zevent); zend_list_delete(event->rsrc_id); } /* }}} */ @@ -611,7 +611,7 @@ static PHP_FUNCTION(event_add) return; } - ZVAL_TO_EVENT(zevent, event); + event = ZVAL_TO_EVENT(zevent); if (!event->base) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to add event without an event base"); @@ -645,7 +645,7 @@ static PHP_FUNCTION(event_set) long events; php_event_callback_t *callback, *old_callback; char *func_name; - php_stream *stream; + php_stream *stream = NULL; php_socket_t file_desc; #ifdef LIBEVENT_SOCKETS_SUPPORT php_socket *php_sock; @@ -656,7 +656,7 @@ static PHP_FUNCTION(event_set) return; } - ZVAL_TO_EVENT(zevent, event); + event = ZVAL_TO_EVENT(zevent); if (events & EV_SIGNAL) { /* signal support */ @@ -668,7 +668,8 @@ static PHP_FUNCTION(event_set) } } else { if (Z_TYPE_P(fd) == IS_RESOURCE) { - if ((php_stream *)zend_fetch_resource2_ex(fd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { + stream = (php_stream *)zend_fetch_resource2_ex(fd, -1, NULL, php_file_le_stream(), php_file_le_pstream()); + if (stream) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&file_desc, 1) != SUCCESS || file_desc < 0) { RETURN_FALSE; } @@ -750,7 +751,7 @@ static PHP_FUNCTION(event_del) return; } - ZVAL_TO_EVENT(zevent, event); + event = ZVAL_TO_EVENT(zevent); if (!event->base) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to delete event without an event base"); @@ -777,7 +778,7 @@ static PHP_FUNCTION(event_priority_set) return; } - ZVAL_TO_EVENT(zevent, event); + event = ZVAL_TO_EVENT(zevent); if (!event->base) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set event priority without an event base"); @@ -806,7 +807,7 @@ static PHP_FUNCTION(event_timer_set) return; } - ZVAL_TO_EVENT(zevent, event); + event = ZVAL_TO_EVENT(zevent); if (!zend_is_callable(zcallback, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid callback", func_name); @@ -850,7 +851,7 @@ static PHP_FUNCTION(event_timer_pending) return; } - ZVAL_TO_EVENT(zevent, event); + event= ZVAL_TO_EVENT(zevent); if (timeout < 0) { ret = event_pending(event->event, EV_TIMEOUT, NULL); @@ -890,7 +891,8 @@ static PHP_FUNCTION(event_buffer_new) } if (Z_TYPE_P(zfd) == IS_RESOURCE) { - if ((php_stream *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { + stream = (php_stream *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream()); + if (stream) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) != SUCCESS || fd < 0) { RETURN_FALSE; } @@ -989,7 +991,7 @@ static PHP_FUNCTION(event_buffer_free) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent = ZVAL_TO_BEVENT(zbevent); zend_list_delete(bevent->rsrc_id); } /* }}} */ @@ -1007,8 +1009,8 @@ static PHP_FUNCTION(event_buffer_base_set) return; } - ZVAL_TO_BASE(zbase, base); - ZVAL_TO_BEVENT(zbevent, bevent); + base = ZVAL_TO_BASE(zbase); + bevent = ZVAL_TO_BEVENT(zbevent); old_base = bevent->base; ret = bufferevent_base_set(base->base, bevent->bevent); @@ -1045,7 +1047,7 @@ static PHP_FUNCTION(event_buffer_priority_set) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent = ZVAL_TO_BEVENT(zbevent); if (!bevent->base) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set event priority without an event base"); @@ -1076,7 +1078,7 @@ static PHP_FUNCTION(event_buffer_write) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent = ZVAL_TO_BEVENT(zbevent); if (ZEND_NUM_ARGS() < 3 || data_size < 0) { data_size = data_len; @@ -1108,7 +1110,7 @@ static PHP_FUNCTION(event_buffer_read) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent= ZVAL_TO_BEVENT(zbevent); if (data_size == 0) { RETURN_EMPTY_STRING(); @@ -1145,7 +1147,7 @@ static PHP_FUNCTION(event_buffer_enable) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent = ZVAL_TO_BEVENT(zbevent); ret = bufferevent_enable(bevent->bevent, events); @@ -1169,7 +1171,7 @@ static PHP_FUNCTION(event_buffer_disable) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent = ZVAL_TO_BEVENT(zbevent); ret = bufferevent_disable(bevent->bevent, events); @@ -1192,7 +1194,7 @@ static PHP_FUNCTION(event_buffer_timeout_set) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent = ZVAL_TO_BEVENT(zbevent); bufferevent_settimeout(bevent->bevent, read_timeout, write_timeout); } /* }}} */ @@ -1209,7 +1211,7 @@ static PHP_FUNCTION(event_buffer_watermark_set) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent = ZVAL_TO_BEVENT(zbevent); bufferevent_setwatermark(bevent->bevent, events, lowmark, highmark); } /* }}} */ @@ -1230,10 +1232,11 @@ static PHP_FUNCTION(event_buffer_fd_set) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent= ZVAL_TO_BEVENT(zbevent); if (Z_TYPE_P(zfd) == IS_RESOURCE) { - if (( php_stream *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream())) { + stream = ( php_stream *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream()); + if (stream) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) != SUCCESS || fd < 0) { RETURN_FALSE; } @@ -1277,7 +1280,7 @@ static PHP_FUNCTION(event_buffer_set_callback) return; } - ZVAL_TO_BEVENT(zbevent, bevent); + bevent= ZVAL_TO_BEVENT(zbevent); if (Z_TYPE_P(zreadcb) != IS_NULL) { if (!zend_is_callable(zreadcb, 0, &func_name TSRMLS_CC)) { From 0664767b4819930df7731a21220f19b7cf13aa34 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 13:09:41 +0100 Subject: [PATCH 012/103] change RETVAL_RES to ZVAL_COPY_VALUE --- libevent.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/libevent.c b/libevent.c index 2400cd2..cd3b5e9 100644 --- a/libevent.c +++ b/libevent.c @@ -366,9 +366,8 @@ static PHP_FUNCTION(event_base_new) base->events = 0; tmp = zend_list_insert(base, le_event_base); - base->rsrc_id = Z_RES_P(tmp); - - RETVAL_RES(zend_register_resource(base->rsrc_id, le_event_base)); + base->rsrc_id = Z_RES_HANDLE_P(tmp); + ZVAL_COPY_VALUE(return_value, tmp); } /* }}} */ @@ -576,9 +575,8 @@ static PHP_FUNCTION(event_new) TSRMLS_SET_CTX(event->thread_ctx); tmp = zend_list_insert(event, le_event); - event->rsrc_id = Z_RES_P(tmp); - - RETVAL_RES(zend_register_resource(event->rsrc_id, le_event)); + event->rsrc_id = Z_RES_HANDLE_P(tmp); + ZVAL_COPY_VALUE(return_value, tmp); } /* }}} */ @@ -974,9 +972,8 @@ static PHP_FUNCTION(event_buffer_new) TSRMLS_SET_CTX(bevent->thread_ctx); tmp = zend_list_insert(bevent, le_bufferevent); - bevent->rsrc_id = Z_RES_P(tmp); - - RETVAL_RES(zend_register_resource(bevent->rsrc_id, le_bufferevent)); + bevent->rsrc_id = Z_RES_HANDLE_P(tmp); + ZVAL_COPY_VALUE(return_value, tmp); } /* }}} */ From 40f877c90b9ed02c6cbb5599eaf92484743dc32e Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 13:52:12 +0100 Subject: [PATCH 013/103] change long to zend_long --- libevent.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/libevent.c b/libevent.c index cd3b5e9..40d650c 100644 --- a/libevent.c +++ b/libevent.c @@ -419,7 +419,7 @@ static PHP_FUNCTION(event_base_loop) { zval *zbase; php_event_base_t *base; - long flags = 0; + zend_long flags = 0; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zbase, &flags) != SUCCESS) { @@ -463,7 +463,7 @@ static PHP_FUNCTION(event_base_loopexit) zval *zbase; php_event_base_t *base; int ret; - long timeout = -1; + zend_long timeout = -1; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zbase, &timeout) != SUCCESS) { return; @@ -532,7 +532,7 @@ static PHP_FUNCTION(event_base_priority_init) { zval *zbase; php_event_base_t *base; - long npriorities; + zend_long npriorities; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zbase, &npriorities) != SUCCESS) { @@ -603,7 +603,7 @@ static PHP_FUNCTION(event_add) zval *zevent; php_event_t *event; int ret; - long timeout = -1; + zend_long timeout = -1; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zevent, &timeout) != SUCCESS) { return; @@ -640,7 +640,7 @@ static PHP_FUNCTION(event_set) { zval *zevent, *fd, *zcallback, *zarg = NULL; php_event_t *event; - long events; + zend_long events = 0; php_event_callback_t *callback, *old_callback; char *func_name; php_stream *stream = NULL; @@ -769,7 +769,7 @@ static PHP_FUNCTION(event_priority_set) { zval *zevent; php_event_t *event; - long priority; + zend_long priority = 0; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zevent, &priority) != SUCCESS) { @@ -843,7 +843,7 @@ static PHP_FUNCTION(event_timer_pending) zval *zevent; php_event_t *event; int ret; - long timeout = -1; + zend_long timeout = -1; if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zevent, &timeout) != SUCCESS) { return; @@ -1037,7 +1037,7 @@ static PHP_FUNCTION(event_buffer_priority_set) { zval *zbevent; php_bufferevent_t *bevent; - long priority; + zend_long priority = 0; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbevent, &priority) != SUCCESS) { @@ -1068,7 +1068,7 @@ static PHP_FUNCTION(event_buffer_write) php_bufferevent_t *bevent; char *data; int data_len; - long data_size = -1; + zend_long data_size = -1; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &zbevent, &data, &data_len, &data_size) != SUCCESS) { @@ -1100,7 +1100,7 @@ static PHP_FUNCTION(event_buffer_read) zval *zbevent; php_bufferevent_t *bevent; char *data; - long data_size; + zend_long data_size = 0; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbevent, &data_size) != SUCCESS) { @@ -1137,7 +1137,7 @@ static PHP_FUNCTION(event_buffer_enable) { zval *zbevent; php_bufferevent_t *bevent; - long events; + zend_long events = 0; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbevent, &events) != SUCCESS) { @@ -1161,7 +1161,7 @@ static PHP_FUNCTION(event_buffer_disable) { zval *zbevent; php_bufferevent_t *bevent; - long events; + zend_long events = 0; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbevent, &events) != SUCCESS) { @@ -1185,7 +1185,7 @@ static PHP_FUNCTION(event_buffer_timeout_set) { zval *zbevent; php_bufferevent_t *bevent; - long read_timeout, write_timeout; + zend_long read_timeout, write_timeout; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rll", &zbevent, &read_timeout, &write_timeout) != SUCCESS) { return; @@ -1202,7 +1202,7 @@ static PHP_FUNCTION(event_buffer_watermark_set) { zval *zbevent; php_bufferevent_t *bevent; - long events, lowmark, highmark; + zend_long events, lowmark, highmark; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &zbevent, &events, &lowmark, &highmark) != SUCCESS) { return; From ebab8743bc1f59eae6641bfaa82cad0415e391a9 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 15:19:29 +0100 Subject: [PATCH 014/103] replace streams with zvals --- libevent.c | 87 ++++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/libevent.c b/libevent.c index 40d650c..053b01c 100644 --- a/libevent.c +++ b/libevent.c @@ -86,7 +86,7 @@ ZEND_GET_MODULE(libevent) typedef struct _php_event_base_t { /* {{{ */ struct event_base *base; - int rsrc_id; + zval *rsrc_id; zend_uintptr_t events; } php_event_base_t; /* }}} */ @@ -99,8 +99,8 @@ typedef struct _php_event_callback_t { /* {{{ */ typedef struct _php_event_t { /* {{{ */ struct event *event; - int rsrc_id; - int stream_id; + zval *rsrc_id; + zval *stream_id; php_event_base_t *base; php_event_callback_t *callback; #ifdef ZTS @@ -112,7 +112,7 @@ typedef struct _php_event_t { /* {{{ */ typedef struct _php_bufferevent_t { /* {{{ */ struct bufferevent *bevent; - int rsrc_id; + zval *rsrc_id; php_event_base_t *base; zval *readcb; zval *writecb; @@ -173,7 +173,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ base_id = event->base->rsrc_id; --event->base->events; } - if (event->stream_id >= 0) { + if (event->stream_id) { zend_list_delete(event->stream_id); } event_del(event->event); @@ -221,7 +221,7 @@ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ static void _php_event_callback(int fd, short events, void *arg) /* {{{ */ { - zval *args[3]; + zval args[3]; php_event_t *event = (php_event_t *)arg; php_event_callback_t *callback; zval retval; @@ -233,27 +233,30 @@ static void _php_event_callback(int fd, short events, void *arg) /* {{{ */ callback = event->callback; - if (event->stream_id >= 0) { - ZVAL_RES(args[0], zend_register_resource(event->stream_id, le_event)); - /*zend_list_addref(event->stream_id);*/ + if (event->stream_id) { + ZVAL_COPY_VALUE(&args[0], event->stream_id); } else if (events & EV_SIGNAL) { - ZVAL_LONG(args[0], fd); + ZVAL_LONG(&args[0], (zend_long)fd); } else { - ZVAL_NULL(args[0]); + ZVAL_NULL(&args[0]); } - ZVAL_LONG(&args[1], events); + ZVAL_LONG(&args[1], (zend_long)events); + + if (callback->arg) { + ZVAL_COPY_VALUE(&args[2], callback->arg); + Z_ADDREF_P(&args[2]); + } else { + ZVAL_NULL(&args[2]); + } - args[2] = callback->arg; - Z_ADDREF_P(callback->arg); - if (call_user_function(EG(function_table), NULL, callback->func, &retval, 3, args TSRMLS_CC) == SUCCESS) { zval_dtor(&retval); } - zval_ptr_dtor(&(args[0])); - zval_ptr_dtor(&(args[1])); - zval_ptr_dtor(&(args[2])); + zval_ptr_dtor(&args[0]); + zval_ptr_dtor(&args[1]); + zval_ptr_dtor(&args[2]); } /* }}} */ @@ -269,8 +272,8 @@ static void _php_bufferevent_readcb(struct bufferevent *be, void *arg) /* {{{ */ return; } - ZVAL_RES(args[0], zend_register_resource(bevent->rsrc_id, le_bufferevent)); - /*zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ + args[0] = bevent->rsrc_id; + Z_ADDREF_P(args[0]); /* we do refcount-- later in zval_ptr_dtor */ args[1] = bevent->arg; Z_ADDREF_P(args[1]); @@ -296,9 +299,9 @@ static void _php_bufferevent_writecb(struct bufferevent *be, void *arg) /* {{{ * return; } - ZVAL_RES(args[0], zend_register_resource( bevent->rsrc_id, le_bufferevent)); - /*zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ - + args[0] = bevent->rsrc_id; + Z_ADDREF_P(args[0]); /* we do refcount-- later in zval_ptr_dtor */ + args[1] = bevent->arg; Z_ADDREF_P(args[1]); @@ -323,8 +326,8 @@ static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *a return; } - ZVAL_RES(args[0], zend_register_resource( bevent->rsrc_id, le_bufferevent)); - /*zend_list_addref(bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ + args[0] = bevent->rsrc_id; + Z_ADDREF_P(args[0]); /* we do refcount-- later in zval_ptr_dtor */ ZVAL_LONG(&args[1], what); @@ -350,7 +353,6 @@ static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *a static PHP_FUNCTION(event_base_new) { php_event_base_t *base; - zval *tmp; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") != SUCCESS) { return; @@ -365,9 +367,8 @@ static PHP_FUNCTION(event_base_new) base->events = 0; - tmp = zend_list_insert(base, le_event_base); - base->rsrc_id = Z_RES_HANDLE_P(tmp); - ZVAL_COPY_VALUE(return_value, tmp); + base->rsrc_id = zend_list_insert(base, le_event_base); + ZVAL_COPY_VALUE(return_value, base->rsrc_id); } /* }}} */ @@ -560,7 +561,6 @@ static PHP_FUNCTION(event_base_priority_init) static PHP_FUNCTION(event_new) { php_event_t *event; - zval *tmp; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") != SUCCESS) { return; @@ -568,15 +568,14 @@ static PHP_FUNCTION(event_new) event = emalloc(sizeof(php_event_t)); event->event = ecalloc(1, sizeof(struct event)); - event->stream_id = -1; + event->stream_id = NULL; event->callback = NULL; event->base = NULL; event->in_free = 0; TSRMLS_SET_CTX(event->thread_ctx); - tmp = zend_list_insert(event, le_event); - event->rsrc_id = Z_RES_HANDLE_P(tmp); - ZVAL_COPY_VALUE(return_value, tmp); + event->rsrc_id = zend_list_insert(event, le_event); + ZVAL_COPY_VALUE(return_value, event->rsrc_id); } /* }}} */ @@ -711,15 +710,15 @@ static PHP_FUNCTION(event_set) callback = emalloc(sizeof(php_event_callback_t)); callback->func = zcallback; - callback->arg = &zarg; + callback->arg = zarg; old_callback = event->callback; event->callback = callback; if (events & EV_SIGNAL) { - event->stream_id = -1; + event->stream_id = NULL; } else { - /*zend_list_addref(Z_LVAL_PP(fd));*/ - event->stream_id = Z_LVAL_P(fd); + event->stream_id = Z_RES_P(fd); + Z_ADDREF_P(fd); } event_set(event->event, (int)file_desc, (short)events, _php_event_callback, event); @@ -818,14 +817,14 @@ static PHP_FUNCTION(event_timer_set) callback = emalloc(sizeof(php_event_callback_t)); callback->func = zcallback; - callback->arg = &zarg; + callback->arg = zarg; old_callback = event->callback; event->callback = callback; - if (event->stream_id >= 0) { + if (event->stream_id) { zend_list_delete(event->stream_id); } - event->stream_id = -1; + event->stream_id = NULL; event_set(event->event, -1, 0, _php_event_callback, event); @@ -879,7 +878,6 @@ static PHP_FUNCTION(event_buffer_new) zval *zfd, *zreadcb, *zwritecb, *zerrorcb, *zarg = NULL; php_socket_t fd; char *func_name; - zval *tmp; #ifdef LIBEVENT_SOCKETS_SUPPORT php_socket *php_sock; #endif @@ -971,9 +969,8 @@ static PHP_FUNCTION(event_buffer_new) TSRMLS_SET_CTX(bevent->thread_ctx); - tmp = zend_list_insert(bevent, le_bufferevent); - bevent->rsrc_id = Z_RES_HANDLE_P(tmp); - ZVAL_COPY_VALUE(return_value, tmp); + bevent->rsrc_id = zend_list_insert(bevent, le_bufferevent); + ZVAL_COPY_VALUE(return_value, bevent->rsrc_id); } /* }}} */ From e8f1fb9fa5741b7981eab2cb68da5a93323ec5fb Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 15:48:33 +0100 Subject: [PATCH 015/103] fix the callback structure --- libevent.c | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/libevent.c b/libevent.c index 053b01c..628d4c8 100644 --- a/libevent.c +++ b/libevent.c @@ -92,8 +92,8 @@ typedef struct _php_event_base_t { /* {{{ */ /* }}} */ typedef struct _php_event_callback_t { /* {{{ */ - zval *func; - zval *arg; + zval func; + zval arg; } php_event_callback_t; /* }}} */ @@ -140,11 +140,8 @@ static inline void _php_event_callback_free(php_event_callback_t *callback) /* { if (!callback) { return; } - zval_ptr_dtor(&callback->func); - if (callback->arg) { - zval_ptr_dtor(&callback->arg); - } + zval_ptr_dtor(&callback->arg); efree(callback); } /* }}} */ @@ -242,18 +239,13 @@ static void _php_event_callback(int fd, short events, void *arg) /* {{{ */ } ZVAL_LONG(&args[1], (zend_long)events); + ZVAL_COPY_VALUE(&args[2], &callback->arg); - if (callback->arg) { - ZVAL_COPY_VALUE(&args[2], callback->arg); - Z_ADDREF_P(&args[2]); - } else { - ZVAL_NULL(&args[2]); - } - - if (call_user_function(EG(function_table), NULL, callback->func, &retval, 3, args TSRMLS_CC) == SUCCESS) { + if (call_user_function(EG(function_table), NULL, &callback->func, &retval, 3, args) == SUCCESS) { zval_dtor(&retval); } + zval_ptr_dtor(&args[0]); zval_ptr_dtor(&args[1]); zval_ptr_dtor(&args[2]); @@ -428,7 +420,7 @@ static PHP_FUNCTION(event_base_loop) } base = ZVAL_TO_BASE(zbase); - /*zend_list_addref(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */ + Z_ADDREF_P(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */ ret = event_base_loop(base->base, flags); zend_list_delete(base->rsrc_id); @@ -511,7 +503,7 @@ static PHP_FUNCTION(event_base_set) if (ret == 0) { if (base != old_base) { /* make sure the base is destroyed after the event */ - /*zend_list_addref(base->rsrc_id);*/ + Z_ADDREF_P(base->rsrc_id); ++base->events; } @@ -709,8 +701,12 @@ static PHP_FUNCTION(event_set) zval_addref_p(zcallback); callback = emalloc(sizeof(php_event_callback_t)); - callback->func = zcallback; - callback->arg = zarg; + ZVAL_COPY_VALUE(&callback->func, zcallback); + if(zarg) { + ZVAL_COPY_VALUE(&callback->arg, zarg); + } else { + ZVAL_NULL(&callback->arg); + } old_callback = event->callback; event->callback = callback; @@ -816,8 +812,8 @@ static PHP_FUNCTION(event_timer_set) zval_addref_p(zcallback); callback = emalloc(sizeof(php_event_callback_t)); - callback->func = zcallback; - callback->arg = zarg; + ZVAL_COPY_VALUE(&callback->func, zcallback); + ZVAL_COPY_VALUE(&callback->arg, zarg); old_callback = event->callback; event->callback = callback; @@ -1012,7 +1008,7 @@ static PHP_FUNCTION(event_buffer_base_set) if (ret == 0) { if (base != old_base) { /* make sure the base is destroyed after the event */ - /*zend_list_addref(base->rsrc_id);*/ + Z_ADDREF_P(base->rsrc_id); ++base->events; } From 810164dcfb3384119cd7dd575a3cf9ab13c9309d Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 15:58:24 +0100 Subject: [PATCH 016/103] migrate signature of zend_fetch_resource2_ex --- libevent.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libevent.c b/libevent.c index 628d4c8..cee8f24 100644 --- a/libevent.c +++ b/libevent.c @@ -657,14 +657,14 @@ static PHP_FUNCTION(event_set) } } else { if (Z_TYPE_P(fd) == IS_RESOURCE) { - stream = (php_stream *)zend_fetch_resource2_ex(fd, -1, NULL, php_file_le_stream(), php_file_le_pstream()); + stream = (php_stream *)zend_fetch_resource2_ex(fd, NULL, php_file_le_stream(), php_file_le_pstream()); if (stream) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&file_desc, 1) != SUCCESS || file_desc < 0) { RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - if ((php_socket *)zend_fetch_resource2_ex(fd, -1, NULL, php_sockets_le_socket())) { + if ((php_socket *)zend_fetch_resource2_ex(fd, NULL, php_sockets_le_socket())) { file_desc = php_sock->bsd_socket; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource"); @@ -883,14 +883,14 @@ static PHP_FUNCTION(event_buffer_new) } if (Z_TYPE_P(zfd) == IS_RESOURCE) { - stream = (php_stream *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream()); + stream = (php_stream *)zend_fetch_resource2_ex(&zfd, NULL, php_file_le_stream(), php_file_le_pstream()); if (stream) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) != SUCCESS || fd < 0) { RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - if ((php_socket *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_sockets_le_socket())) { + if ((php_socket *)zend_fetch_resource2_ex(&zfd, NULL, php_sockets_le_socket())) { fd = php_sock->bsd_socket; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); @@ -1225,14 +1225,14 @@ static PHP_FUNCTION(event_buffer_fd_set) bevent= ZVAL_TO_BEVENT(zbevent); if (Z_TYPE_P(zfd) == IS_RESOURCE) { - stream = ( php_stream *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_file_le_stream(), php_file_le_pstream()); + stream = ( php_stream *)zend_fetch_resource2_ex(&zfd, NULL, php_file_le_stream(), php_file_le_pstream()); if (stream) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) != SUCCESS || fd < 0) { RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - if ((php_socket *)zend_fetch_resource2_ex(&zfd, -1, NULL, php_sockets_le_socket())) { + if ((php_socket *)zend_fetch_resource2_ex(&zfd, NULL, php_sockets_le_socket())) { fd = php_sock->bsd_socket; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); From 45d4fe14d9dd190961226fbcb266dc8704b6f892 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 16:17:38 +0100 Subject: [PATCH 017/103] fixed zend_fetch_resource2_ex calls --- libevent.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/libevent.c b/libevent.c index cee8f24..216cc8c 100644 --- a/libevent.c +++ b/libevent.c @@ -657,14 +657,15 @@ static PHP_FUNCTION(event_set) } } else { if (Z_TYPE_P(fd) == IS_RESOURCE) { - stream = (php_stream *)zend_fetch_resource2_ex(fd, NULL, php_file_le_stream(), php_file_le_pstream()); + stream = zend_fetch_resource2_ex(fd, NULL, php_file_le_stream(), php_file_le_pstream()); if (stream) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&file_desc, 1) != SUCCESS || file_desc < 0) { RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - if ((php_socket *)zend_fetch_resource2_ex(fd, NULL, php_sockets_le_socket())) { + php_sock = (php_socket *)zend_fetch_resource2_ex(fd, NULL, php_sockets_le_socket()); + if (php_sock) { file_desc = php_sock->bsd_socket; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource"); @@ -883,14 +884,15 @@ static PHP_FUNCTION(event_buffer_new) } if (Z_TYPE_P(zfd) == IS_RESOURCE) { - stream = (php_stream *)zend_fetch_resource2_ex(&zfd, NULL, php_file_le_stream(), php_file_le_pstream()); + stream = zend_fetch_resource2_ex(zfd, NULL, php_file_le_stream(), php_file_le_pstream()); if (stream) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) != SUCCESS || fd < 0) { RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - if ((php_socket *)zend_fetch_resource2_ex(&zfd, NULL, php_sockets_le_socket())) { + php_sock = (php_socket *)zend_fetch_resource2_ex(zfd, NULL, php_sockets_le_socket()); + if (php_sock) { fd = php_sock->bsd_socket; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); @@ -1225,14 +1227,15 @@ static PHP_FUNCTION(event_buffer_fd_set) bevent= ZVAL_TO_BEVENT(zbevent); if (Z_TYPE_P(zfd) == IS_RESOURCE) { - stream = ( php_stream *)zend_fetch_resource2_ex(&zfd, NULL, php_file_le_stream(), php_file_le_pstream()); + stream = zend_fetch_resource2_ex(zfd, NULL, php_file_le_stream(), php_file_le_pstream()); if (stream) { if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) != SUCCESS || fd < 0) { RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - if ((php_socket *)zend_fetch_resource2_ex(&zfd, NULL, php_sockets_le_socket())) { + php_sock = (php_socket *)zend_fetch_resource2_ex(zfd, NULL, php_sockets_le_socket()) + if (php_sock) { fd = php_sock->bsd_socket; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); From 34b0d6d6f3eddc223e74300da4c37fc4b28fcb13 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 16:29:02 +0100 Subject: [PATCH 018/103] convert char to zend_string --- libevent.c | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/libevent.c b/libevent.c index 216cc8c..516094c 100644 --- a/libevent.c +++ b/libevent.c @@ -633,7 +633,7 @@ static PHP_FUNCTION(event_set) php_event_t *event; zend_long events = 0; php_event_callback_t *callback, *old_callback; - char *func_name; + zend_string *func_name; php_stream *stream = NULL; php_socket_t file_desc; #ifdef LIBEVENT_SOCKETS_SUPPORT @@ -694,10 +694,10 @@ static PHP_FUNCTION(event_set) if (!zend_is_callable(zcallback, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid callback", func_name); - efree(func_name); + zend_string_release(func_name); RETURN_FALSE; } - efree(func_name); + zend_string_release(func_name); zval_addref_p(zcallback); @@ -795,7 +795,7 @@ static PHP_FUNCTION(event_timer_set) zval *zevent, *zcallback, *zarg = NULL; php_event_t *event; php_event_callback_t *callback, *old_callback; - char *func_name; + zend_string *func_name; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz|z", &zevent, &zcallback, &zarg) != SUCCESS) { return; @@ -805,10 +805,10 @@ static PHP_FUNCTION(event_timer_set) if (!zend_is_callable(zcallback, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid callback", func_name); - efree(func_name); + zend_string_release(func_name); RETURN_FALSE; } - efree(func_name); + zend_string_release(func_name); zval_addref_p(zcallback); @@ -874,7 +874,7 @@ static PHP_FUNCTION(event_buffer_new) php_stream *stream; zval *zfd, *zreadcb, *zwritecb, *zerrorcb, *zarg = NULL; php_socket_t fd; - char *func_name; + zend_string *func_name; #ifdef LIBEVENT_SOCKETS_SUPPORT php_socket *php_sock; #endif @@ -917,10 +917,10 @@ static PHP_FUNCTION(event_buffer_new) if (Z_TYPE_P(zreadcb) != IS_NULL) { if (!zend_is_callable(zreadcb, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid read callback", func_name); - efree(func_name); + zend_string_release(func_name); RETURN_FALSE; } - efree(func_name); + zend_string_release(func_name); } else { zreadcb = NULL; } @@ -928,20 +928,20 @@ static PHP_FUNCTION(event_buffer_new) if (Z_TYPE_P(zwritecb) != IS_NULL) { if (!zend_is_callable(zwritecb, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid write callback", func_name); - efree(func_name); + zend_string_release(func_name); RETURN_FALSE; } - efree(func_name); + zend_string_release(func_name); } else { zwritecb = NULL; } if (!zend_is_callable(zerrorcb, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid error callback", func_name); - efree(func_name); + zend_string_release(func_name); RETURN_FALSE; } - efree(func_name); + zend_string_release(func_name); bevent = emalloc(sizeof(php_bufferevent_t)); bevent->bevent = bufferevent_new(fd, _php_bufferevent_readcb, _php_bufferevent_writecb, _php_bufferevent_errorcb, bevent); @@ -1094,7 +1094,7 @@ static PHP_FUNCTION(event_buffer_read) { zval *zbevent; php_bufferevent_t *bevent; - char *data; + zend_string *data; zend_long data_size = 0; int ret; @@ -1110,18 +1110,15 @@ static PHP_FUNCTION(event_buffer_read) php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_size cannot be less than zero"); RETURN_FALSE; } - - data = safe_emalloc((int)data_size, sizeof(char), 1); - + data = zend_string_alloc(data_size, 0); ret = bufferevent_read(bevent->bevent, data, data_size); if (ret > 0) { if (ret > data_size) { /* paranoia */ ret = data_size; } - data[ret] = '\0'; - RETURN_STRINGL(data, ret, 0); + RETVAL_STRINGL(data, ret); } - efree(data); + zend_string_release(data); RETURN_EMPTY_STRING(); } /* }}} */ @@ -1267,7 +1264,7 @@ static PHP_FUNCTION(event_buffer_set_callback) { php_bufferevent_t *bevent; zval *zbevent, *zreadcb, *zwritecb, *zerrorcb, *zarg = NULL; - char *func_name; + zend_string *func_name; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzzz|z", &zbevent, &zreadcb, &zwritecb, &zerrorcb, &zarg) != SUCCESS) { return; @@ -1278,10 +1275,10 @@ static PHP_FUNCTION(event_buffer_set_callback) if (Z_TYPE_P(zreadcb) != IS_NULL) { if (!zend_is_callable(zreadcb, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid read callback", func_name); - efree(func_name); + zend_string_release(func_name); RETURN_FALSE; } - efree(func_name); + zend_string_release(func_name); } else { zreadcb = NULL; } @@ -1289,10 +1286,10 @@ static PHP_FUNCTION(event_buffer_set_callback) if (Z_TYPE_P(zwritecb) != IS_NULL) { if (!zend_is_callable(zwritecb, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid write callback", func_name); - efree(func_name); + zend_string_release(func_name); RETURN_FALSE; } - efree(func_name); + zend_string_release(func_name); } else { zwritecb = NULL; } @@ -1300,10 +1297,10 @@ static PHP_FUNCTION(event_buffer_set_callback) if (Z_TYPE_P(zerrorcb) != IS_NULL) { if (!zend_is_callable(zerrorcb, 0, &func_name TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid error callback", func_name); - efree(func_name); + zend_string_release(func_name); RETURN_FALSE; } - efree(func_name); + zend_string_release(func_name); } else { zerrorcb = NULL; } From bb3630ff50236a6217bdc2867b51f39a654489e5 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 16:40:55 +0100 Subject: [PATCH 019/103] fix warn --- libevent.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libevent.c b/libevent.c index 516094c..e5421b8 100644 --- a/libevent.c +++ b/libevent.c @@ -125,13 +125,13 @@ typedef struct _php_bufferevent_t { /* {{{ */ /* }}} */ #define ZVAL_TO_BASE(zval) \ - (php_event_base_t *)zend_fetch_resource2_ex(zval, NULL, "event base", le_event_base) + (php_event_base_t *)zend_fetch_resource2_ex(zval, "event base", le_event_base, le_event_base) #define ZVAL_TO_EVENT(zval) \ - (php_event_t *)zend_fetch_resource2_ex(zval, NULL, "event", le_event) + (php_event_t *)zend_fetch_resource2_ex(zval, "event", le_event, le_event) #define ZVAL_TO_BEVENT(zval) \ - (php_bufferevent_t *)zend_fetch_resource2_ex(zval, NULL, "buffer event", le_bufferevent) + (php_bufferevent_t *)zend_fetch_resource2_ex(zval, "buffer event", le_bufferevent, le_bufferevent) /* {{{ internal funcs */ From 08795996d77244fa5d951ce1e44ccc6ceb173e55 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 17:02:53 +0100 Subject: [PATCH 020/103] start to migrate _php_bufferevent_t structure --- libevent.c | 87 +++++++++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 53 deletions(-) diff --git a/libevent.c b/libevent.c index e5421b8..b993fdf 100644 --- a/libevent.c +++ b/libevent.c @@ -114,10 +114,10 @@ typedef struct _php_bufferevent_t { /* {{{ */ struct bufferevent *bevent; zval *rsrc_id; php_event_base_t *base; - zval *readcb; - zval *writecb; - zval *errorcb; - zval *arg; + zval readcb; + zval writecb; + zval errorcb; + zval arg; #ifdef ZTS void ***thread_ctx; #endif @@ -158,7 +158,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_base_dtor) /* {{{ */ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ { php_event_t *event = (php_event_t*)res->ptr; - int base_id = -1; + zval *base_id = NULL; if (event->in_free) { return; @@ -171,7 +171,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ --event->base->events; } if (event->stream_id) { - zend_list_delete(event->stream_id); + zend_list_delete(Z_RES_P(event->stream_id)); } event_del(event->event); @@ -179,8 +179,8 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ efree(event->event); efree(event); - if (base_id >= 0) { - zend_list_delete(base_id); + if (base_id) { + zend_list_delete(Z_RES_P(base_id)); } } /* }}} */ @@ -188,30 +188,21 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ { php_bufferevent_t *bevent = (php_bufferevent_t*)res->ptr; - int base_id = -1; + zval *base_id = NULL; if (bevent->base) { base_id = bevent->base->rsrc_id; --bevent->base->events; } - if (bevent->readcb) { - zval_ptr_dtor(&(bevent->readcb)); - } - if (bevent->writecb) { - zval_ptr_dtor(&(bevent->writecb)); - } - if (bevent->errorcb) { - zval_ptr_dtor(&(bevent->errorcb)); - } - if (bevent->arg) { - zval_ptr_dtor(&(bevent->arg)); - } - + zval_ptr_dtor(&bevent->readcb); + zval_ptr_dtor(&bevent->writecb); + zval_ptr_dtor(&bevent->errorcb); + zval_ptr_dtor(&bevent->arg); bufferevent_free(bevent->bevent); efree(bevent); - if (base_id >= 0) { - zend_list_delete(base_id); + if (base_id) { + zend_list_delete(Z_RES_P(base_id)); } } /* }}} */ @@ -255,49 +246,43 @@ static void _php_event_callback(int fd, short events, void *arg) /* {{{ */ static void _php_bufferevent_readcb(struct bufferevent *be, void *arg) /* {{{ */ { - zval *args[2]; + zval args[2]; zval retval; php_bufferevent_t *bevent = (php_bufferevent_t *)arg; TSRMLS_FETCH_FROM_CTX(bevent ? bevent->thread_ctx : NULL); - if (!bevent || !bevent->base || !bevent->readcb) { + if (!bevent || !bevent->base || Z_ISUNDEF(bevent->readcb)) { return; } - args[0] = bevent->rsrc_id; - Z_ADDREF_P(args[0]); /* we do refcount-- later in zval_ptr_dtor */ - - args[1] = bevent->arg; - Z_ADDREF_P(args[1]); - - if (call_user_function(EG(function_table), NULL, bevent->readcb, &retval, 2, args TSRMLS_CC) == SUCCESS) { + ZVAL_COPY_VALUE(&args[0], bevent->rsrc_id); + ZVAL_COPY_VALUE(&args[1], &bevent->arg); + + if (call_user_function(EG(function_table), NULL, &bevent->readcb, &retval, 2, args) == SUCCESS) { zval_dtor(&retval); } - zval_ptr_dtor(&(args[0])); - zval_ptr_dtor(&(args[1])); + zval_ptr_dtor(&args[0]); + zval_ptr_dtor(&args[1]); } /* }}} */ static void _php_bufferevent_writecb(struct bufferevent *be, void *arg) /* {{{ */ { - zval *args[2]; + zval args[2]; zval retval; php_bufferevent_t *bevent = (php_bufferevent_t *)arg; TSRMLS_FETCH_FROM_CTX(bevent ? bevent->thread_ctx : NULL); - if (!bevent || !bevent->base || !bevent->writecb) { + if (!bevent || !bevent->base || Z_ISUNDEF(bevent->writecb)) { return; } - args[0] = bevent->rsrc_id; - Z_ADDREF_P(args[0]); /* we do refcount-- later in zval_ptr_dtor */ - - args[1] = bevent->arg; - Z_ADDREF_P(args[1]); + ZVAL_COPY_VALUE(&args[0], bevent->rsrc_id); + ZVAL_COPY_VALUE(&args[1], &bevent->arg); - if (call_user_function(EG(function_table), NULL, bevent->writecb, &retval, 2, args TSRMLS_CC) == SUCCESS) { + if (call_user_function(EG(function_table), NULL, &bevent->writecb, &retval, 2, args) == SUCCESS) { zval_dtor(&retval); } @@ -309,24 +294,20 @@ static void _php_bufferevent_writecb(struct bufferevent *be, void *arg) /* {{{ * static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *arg) /* {{{ */ { - zval *args[3]; + zval args[3]; zval retval; php_bufferevent_t *bevent = (php_bufferevent_t *)arg; TSRMLS_FETCH_FROM_CTX(bevent ? bevent->thread_ctx : NULL); - if (!bevent || !bevent->base || !bevent->errorcb) { + if (!bevent || !bevent->base || Z_ISUNDEF(bevent->errorcb)) { return; } - args[0] = bevent->rsrc_id; - Z_ADDREF_P(args[0]); /* we do refcount-- later in zval_ptr_dtor */ + ZVAL_COPY_VALUE(&args[0], bevent->rsrc_id); + ZVAL_LONG(&args[1], (zend_long)what); + ZVAL_COPY_VALUE(&args[2], &bevent->arg); - ZVAL_LONG(&args[1], what); - - args[2] = bevent->arg; - Z_ADDREF_P(args[2]); - - if (call_user_function(EG(function_table), NULL, bevent->errorcb, &retval, 3, args TSRMLS_CC) == SUCCESS) { + if (call_user_function(EG(function_table), NULL, &bevent->errorcb, &retval, 3, args) == SUCCESS) { zval_dtor(&retval); } From 6c6f62fdd459b1f60eb8b9ffaedbebb93b439315 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 18:56:34 +0100 Subject: [PATCH 021/103] usage of Z_RES_P --- libevent.c | 87 +++++++++++++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 47 deletions(-) diff --git a/libevent.c b/libevent.c index b993fdf..d32f7d7 100644 --- a/libevent.c +++ b/libevent.c @@ -383,7 +383,7 @@ static PHP_FUNCTION(event_base_free) RETURN_FALSE; } - zend_list_delete(base->rsrc_id); + zend_list_delete(Z_RES_P(base->rsrc_id)); } /* }}} */ @@ -403,7 +403,7 @@ static PHP_FUNCTION(event_base_loop) base = ZVAL_TO_BASE(zbase); Z_ADDREF_P(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */ ret = event_base_loop(base->base, flags); - zend_list_delete(base->rsrc_id); + zend_list_delete(Z_RES_P(base->rsrc_id)); RETURN_LONG(ret); } @@ -490,7 +490,7 @@ static PHP_FUNCTION(event_base_set) if (old_base && base != old_base) { --old_base->events; - zend_list_delete(old_base->rsrc_id); + zend_list_delete(Z_RES_P(old_base->rsrc_id)); } event->base = base; @@ -564,7 +564,7 @@ static PHP_FUNCTION(event_free) } event = ZVAL_TO_EVENT(zevent); - zend_list_delete(event->rsrc_id); + zend_list_delete(Z_RES_P(event->rsrc_id)); } /* }}} */ @@ -695,7 +695,7 @@ static PHP_FUNCTION(event_set) if (events & EV_SIGNAL) { event->stream_id = NULL; } else { - event->stream_id = Z_RES_P(fd); + event->stream_id = fd; Z_ADDREF_P(fd); } @@ -800,7 +800,7 @@ static PHP_FUNCTION(event_timer_set) old_callback = event->callback; event->callback = callback; if (event->stream_id) { - zend_list_delete(event->stream_id); + zend_list_delete(Z_RES_P(event->stream_id)); } event->stream_id = NULL; @@ -930,20 +930,19 @@ static PHP_FUNCTION(event_buffer_new) bevent->base = NULL; if (zreadcb) { - zval_addref_p(zreadcb); + ZVAL_COPY(&bevent->readcb, zreadcb); } - bevent->readcb = zreadcb; - + if (zwritecb) { - zval_addref_p(zwritecb); + ZVAL_COPY(&bevent->writecb, zwritecb); } - bevent->writecb = zwritecb; - zval_addref_p(zerrorcb); - bevent->errorcb = zerrorcb; + if (zerrorcb) { + ZVAL_COPY(&bevent->errorcb, zerrorcb); + } if (zarg) { - bevent->arg = &zarg; + ZVAL_COPY(&bevent->arg, zarg); } TSRMLS_SET_CTX(bevent->thread_ctx); @@ -965,7 +964,7 @@ static PHP_FUNCTION(event_buffer_free) } bevent = ZVAL_TO_BEVENT(zbevent); - zend_list_delete(bevent->rsrc_id); + zend_list_delete(Z_RES_P(bevent->rsrc_id)); } /* }}} */ @@ -997,7 +996,9 @@ static PHP_FUNCTION(event_buffer_base_set) if (old_base) { --old_base->events; - zend_list_delete(old_base->rsrc_id); + if (old_base->rsrc_id) { + zend_list_delete(Z_RES_P(old_base->rsrc_id)); + } } bevent->base = base; @@ -1286,49 +1287,41 @@ static PHP_FUNCTION(event_buffer_set_callback) zerrorcb = NULL; } + if (!Z_ISUNDEF(bevent->readcb)) { + zval_ptr_dtor(&bevent->readcb); + } + if (!Z_ISUNDEF(bevent->writecb)) { + zval_ptr_dtor(&bevent->writecb); + } + if (!Z_ISUNDEF(bevent->errorcb)) { + zval_ptr_dtor(&bevent->errorcb); + } + if (!Z_ISUNDEF(bevent->arg)) { + zval_ptr_dtor(&bevent->arg); + } + if (zreadcb) { - zval_addref_p(zreadcb); - - if (bevent->readcb) { - zval_ptr_dtor(&bevent->readcb); - } - bevent->readcb = zreadcb; + ZVAL_COPY_VALUE(&bevent->readcb, zreadcb); } else { - if (bevent->readcb) { - zval_ptr_dtor(&bevent->readcb); - } - bevent->readcb = NULL; + ZVAL_UNDEF(&bevent->readcb); } if (zwritecb) { - zval_addref_p(zwritecb); - - if (bevent->writecb) { - zval_ptr_dtor(&bevent->writecb); - } - bevent->writecb = zwritecb; + ZVAL_COPY_VALUE(&bevent->writecb, zwritecb); } else { - if (bevent->writecb) { - zval_ptr_dtor(&bevent->writecb); - } - bevent->writecb = NULL; + ZVAL_UNDEF(&bevent->writecb); } if (zerrorcb) { - zval_addref_p(zerrorcb); - - if (bevent->errorcb) { - zval_ptr_dtor(&bevent->errorcb); - } - bevent->errorcb = zerrorcb; + ZVAL_COPY_VALUE(&bevent->errorcb, zerrorcb); + } else { + ZVAL_UNDEF(&bevent->errorcb); } if (zarg) { - zval_addref_p(zarg); - if (bevent->arg) { - zval_ptr_dtor(&bevent->arg); - } - bevent->arg = &zarg; + ZVAL_COPY_VALUE(&bevent->arg, zarg); + } else { + ZVAL_UNDEF(&bevent->arg); } RETURN_TRUE; From 0a152d8885215fd14ce1e4d7c1e761f3934cae95 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 26 Dec 2015 20:31:54 +0100 Subject: [PATCH 022/103] fix notices + revert a zend_string usage --- libevent.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/libevent.c b/libevent.c index d32f7d7..5dacc2a 100644 --- a/libevent.c +++ b/libevent.c @@ -402,7 +402,7 @@ static PHP_FUNCTION(event_base_loop) base = ZVAL_TO_BASE(zbase); Z_ADDREF_P(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */ - ret = event_base_loop(base->base, flags); + ret = event_base_loop(base->base, (int)flags); zend_list_delete(Z_RES_P(base->rsrc_id)); RETURN_LONG(ret); @@ -451,7 +451,7 @@ static PHP_FUNCTION(event_base_loopexit) struct timeval time; time.tv_usec = timeout % 1000000; - time.tv_sec = timeout / 1000000; + time.tv_sec = (long)timeout / 1000000; ret = event_base_loopexit(base->base, &time); } @@ -1076,7 +1076,7 @@ static PHP_FUNCTION(event_buffer_read) { zval *zbevent; php_bufferevent_t *bevent; - zend_string *data; + char *data; zend_long data_size = 0; int ret; @@ -1092,13 +1092,14 @@ static PHP_FUNCTION(event_buffer_read) php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_size cannot be less than zero"); RETURN_FALSE; } - data = zend_string_alloc(data_size, 0); + data = safe_emalloc((int)data_size, sizeof(char), 1); ret = bufferevent_read(bevent->bevent, data, data_size); if (ret > 0) { if (ret > data_size) { /* paranoia */ ret = data_size; } - RETVAL_STRINGL(data, ret); + data[ret] = '\0'; + RETURN_STRINGL(data, ret); } zend_string_release(data); RETURN_EMPTY_STRING(); From 53cdb70204a173c0b07a7a8afc5dc6e4e4170f48 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Wed, 30 Dec 2015 15:12:14 +0100 Subject: [PATCH 023/103] fix inconsistent usage between zval and *zval --- libevent.c | 98 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/libevent.c b/libevent.c index 5dacc2a..9412682 100644 --- a/libevent.c +++ b/libevent.c @@ -100,7 +100,7 @@ typedef struct _php_event_callback_t { /* {{{ */ typedef struct _php_event_t { /* {{{ */ struct event *event; zval *rsrc_id; - zval *stream_id; + zval stream_id; php_event_base_t *base; php_event_callback_t *callback; #ifdef ZTS @@ -170,8 +170,9 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ base_id = event->base->rsrc_id; --event->base->events; } - if (event->stream_id) { - zend_list_delete(Z_RES_P(event->stream_id)); + if (Z_TYPE_P(&event->stream_id) != IS_NULL) { + zend_list_delete(Z_RES_P(&event->stream_id)); + ZVAL_NULL(&event->stream_id); } event_del(event->event); @@ -221,8 +222,9 @@ static void _php_event_callback(int fd, short events, void *arg) /* {{{ */ callback = event->callback; - if (event->stream_id) { - ZVAL_COPY_VALUE(&args[0], event->stream_id); + if (Z_TYPE_P(&event->stream_id) != IS_NULL) { + args[0] = event->stream_id; + Z_ADDREF_P(&args[0]); /* we do refcount-- later in zval_ptr_dtor */ } else if (events & EV_SIGNAL) { ZVAL_LONG(&args[0], (zend_long)fd); } else { @@ -230,7 +232,12 @@ static void _php_event_callback(int fd, short events, void *arg) /* {{{ */ } ZVAL_LONG(&args[1], (zend_long)events); - ZVAL_COPY_VALUE(&args[2], &callback->arg); + + args[2] = callback->arg; + if (Z_TYPE_P(&args[2]) != IS_NULL) { + Z_ADDREF_P(&args[2]); + } + if (call_user_function(EG(function_table), NULL, &callback->func, &retval, 3, args) == SUCCESS) { zval_dtor(&retval); @@ -255,8 +262,13 @@ static void _php_bufferevent_readcb(struct bufferevent *be, void *arg) /* {{{ */ return; } - ZVAL_COPY_VALUE(&args[0], bevent->rsrc_id); - ZVAL_COPY_VALUE(&args[1], &bevent->arg); + + ZVAL_COPY(&args[0], bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ + + args[1] = bevent->arg; + if (Z_TYPE_P(&args[1]) != IS_NULL) { + Z_ADDREF_P(&args[1]); + } if (call_user_function(EG(function_table), NULL, &bevent->readcb, &retval, 2, args) == SUCCESS) { zval_dtor(&retval); @@ -279,15 +291,19 @@ static void _php_bufferevent_writecb(struct bufferevent *be, void *arg) /* {{{ * return; } - ZVAL_COPY_VALUE(&args[0], bevent->rsrc_id); - ZVAL_COPY_VALUE(&args[1], &bevent->arg); - + ZVAL_COPY(&args[0], bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ + + args[1] = bevent->arg; + if (Z_TYPE_P(&args[1]) != IS_NULL) { + Z_ADDREF_P(&args[1]); + } + if (call_user_function(EG(function_table), NULL, &bevent->writecb, &retval, 2, args) == SUCCESS) { zval_dtor(&retval); } - zval_ptr_dtor(&(args[0])); - zval_ptr_dtor(&(args[1])); + zval_ptr_dtor(&args[0]); + zval_ptr_dtor(&args[1]); } /* }}} */ @@ -296,6 +312,7 @@ static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *a { zval args[3]; zval retval; + int args_size = 2; php_bufferevent_t *bevent = (php_bufferevent_t *)arg; TSRMLS_FETCH_FROM_CTX(bevent ? bevent->thread_ctx : NULL); @@ -303,18 +320,22 @@ static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *a return; } - ZVAL_COPY_VALUE(&args[0], bevent->rsrc_id); + ZVAL_COPY(&args[0], bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ + ZVAL_LONG(&args[1], (zend_long)what); - ZVAL_COPY_VALUE(&args[2], &bevent->arg); + args[2] = bevent->arg; + if (Z_TYPE_P(&args[2]) != IS_NULL) { + Z_ADDREF_P(&args[2]); + } + if (call_user_function(EG(function_table), NULL, &bevent->errorcb, &retval, 3, args) == SUCCESS) { zval_dtor(&retval); } - zval_ptr_dtor(&(args[0])); - zval_ptr_dtor(&(args[1])); - zval_ptr_dtor(&(args[2])); - + zval_ptr_dtor(&args[0]); + zval_ptr_dtor(&args[1]); + zval_ptr_dtor(&args[2]); } /* }}} */ @@ -332,6 +353,7 @@ static PHP_FUNCTION(event_base_new) } base = emalloc(sizeof(php_event_base_t)); + base->rsrc_id = NULL; base->base = event_base_new(); if (!base->base) { efree(base); @@ -488,7 +510,7 @@ static PHP_FUNCTION(event_base_set) ++base->events; } - if (old_base && base != old_base) { + if (old_base && base != old_base && old_base->rsrc_id != NULL) { --old_base->events; zend_list_delete(Z_RES_P(old_base->rsrc_id)); } @@ -541,7 +563,7 @@ static PHP_FUNCTION(event_new) event = emalloc(sizeof(php_event_t)); event->event = ecalloc(1, sizeof(struct event)); - event->stream_id = NULL; + event->rsrc_id = NULL; event->callback = NULL; event->base = NULL; event->in_free = 0; @@ -693,10 +715,10 @@ static PHP_FUNCTION(event_set) old_callback = event->callback; event->callback = callback; if (events & EV_SIGNAL) { - event->stream_id = NULL; + ZVAL_NULL(&event->stream_id); } else { - event->stream_id = fd; - Z_ADDREF_P(fd); + ZVAL_COPY_VALUE(&event->stream_id, fd); + Z_ADDREF_P(&event->stream_id); } event_set(event->event, (int)file_desc, (short)events, _php_event_callback, event); @@ -795,14 +817,19 @@ static PHP_FUNCTION(event_timer_set) callback = emalloc(sizeof(php_event_callback_t)); ZVAL_COPY_VALUE(&callback->func, zcallback); - ZVAL_COPY_VALUE(&callback->arg, zarg); + if (zarg) { + ZVAL_COPY_VALUE(&callback->arg, zarg); + } + else { + ZVAL_NULL(&callback->arg); + } old_callback = event->callback; event->callback = callback; - if (event->stream_id) { - zend_list_delete(Z_RES_P(event->stream_id)); + if (Z_TYPE_P(&event->stream_id) != IS_NULL) { + zend_list_delete(Z_RES_P(&event->stream_id)); + ZVAL_NULL(&event->stream_id); } - event->stream_id = NULL; event_set(event->event, -1, 0, _php_event_callback, event); @@ -926,7 +953,7 @@ static PHP_FUNCTION(event_buffer_new) bevent = emalloc(sizeof(php_bufferevent_t)); bevent->bevent = bufferevent_new(fd, _php_bufferevent_readcb, _php_bufferevent_writecb, _php_bufferevent_errorcb, bevent); - + bevent->rsrc_id = NULL; bevent->base = NULL; if (zreadcb) { @@ -942,7 +969,10 @@ static PHP_FUNCTION(event_buffer_new) } if (zarg) { - ZVAL_COPY(&bevent->arg, zarg); + ZVAL_COPY_VALUE(&bevent->arg, zarg); + } + else { + ZVAL_NULL(&bevent->arg); } TSRMLS_SET_CTX(bevent->thread_ctx); @@ -996,7 +1026,7 @@ static PHP_FUNCTION(event_buffer_base_set) if (old_base) { --old_base->events; - if (old_base->rsrc_id) { + if (old_base->rsrc_id && Z_TYPE_P(old_base->rsrc_id) != IS_NULL) { zend_list_delete(Z_RES_P(old_base->rsrc_id)); } } @@ -1044,7 +1074,7 @@ static PHP_FUNCTION(event_buffer_write) zval *zbevent; php_bufferevent_t *bevent; char *data; - int data_len; + size_t data_len; zend_long data_size = -1; int ret; @@ -1101,7 +1131,7 @@ static PHP_FUNCTION(event_buffer_read) data[ret] = '\0'; RETURN_STRINGL(data, ret); } - zend_string_release(data); + efree(data); RETURN_EMPTY_STRING(); } /* }}} */ @@ -1322,7 +1352,7 @@ static PHP_FUNCTION(event_buffer_set_callback) if (zarg) { ZVAL_COPY_VALUE(&bevent->arg, zarg); } else { - ZVAL_UNDEF(&bevent->arg); + ZVAL_NULL(&bevent->arg); } RETURN_TRUE; From 47889182eb698c3fbfdefc3f71a7e9f29fa102f3 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Wed, 30 Dec 2015 15:16:14 +0100 Subject: [PATCH 024/103] Add docs --- CREDITS | 2 +- README.md | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 README.md diff --git a/CREDITS b/CREDITS index 7781f4a..d341180 100644 --- a/CREDITS +++ b/CREDITS @@ -1 +1 @@ -Antony Dovgal, Arnaud Le Blanc +Antony Dovgal, Arnaud Le Blanc, Ioan Chiriac diff --git a/README.md b/README.md new file mode 100644 index 0000000..f4d6d00 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +Libevent Bindings +================= + +Libevent is a library that provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. + +More information about Libevent can be found at » http://libevent.org/. + + + +# Credits + +Antony Dovgal, Arnaud Le Blanc, Ioan Chiriac \ No newline at end of file From 510162677f21bd9f10dfd678194a19e620d37c34 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Wed, 30 Dec 2015 15:22:27 +0100 Subject: [PATCH 025/103] add more infos --- README.md | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f4d6d00..9ef4b8d 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,101 @@ -Libevent Bindings -================= +Libevent bindings for PHP +========================= Libevent is a library that provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. More information about Libevent can be found at » http://libevent.org/. +# Requirements +This extension requires (» libevent)[http://libevent.org/] library. Minimal required version is 1.4.0. + +# Installation + +Information for installing this PECL extension may be found in the manual chapter titled (Installation of PECL extensions)[http://php.net/manual/en/install.pecl.php]. Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: » http://pecl.php.net/package/libevent. + +# Predefined Constants + +The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime. + +EV_TIMEOUT (integer) +EV_READ (integer) +EV_WRITE (integer) +EV_SIGNAL (integer) +EV_PERSIST (integer) +EVLOOP_NONBLOCK (integer) +EVLOOP_ONCE (integer) + +# Examples + +Example #1 polling STDIN using basic API + +``` + Date: Wed, 30 Dec 2015 15:25:22 +0100 Subject: [PATCH 026/103] Update README.md --- README.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 9ef4b8d..0a7fd2e 100644 --- a/README.md +++ b/README.md @@ -5,31 +5,31 @@ Libevent is a library that provides a mechanism to execute a callback function w More information about Libevent can be found at » http://libevent.org/. -# Requirements +## Requirements -This extension requires (» libevent)[http://libevent.org/] library. Minimal required version is 1.4.0. +This extension requires [» libevent](http://libevent.org/) library. Minimal required version is 1.4.0. -# Installation +## Installation -Information for installing this PECL extension may be found in the manual chapter titled (Installation of PECL extensions)[http://php.net/manual/en/install.pecl.php]. Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: » http://pecl.php.net/package/libevent. +Information for installing this PECL extension may be found in the manual chapter titled [Installation of PECL extensions](http://php.net/manual/en/install.pecl.php). Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: » http://pecl.php.net/package/libevent. -# Predefined Constants +## Predefined Constants The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime. -EV_TIMEOUT (integer) -EV_READ (integer) -EV_WRITE (integer) -EV_SIGNAL (integer) -EV_PERSIST (integer) -EVLOOP_NONBLOCK (integer) -EVLOOP_ONCE (integer) +* EV_TIMEOUT (integer) +* EV_READ (integer) +* EV_WRITE (integer) +* EV_SIGNAL (integer) +* EV_PERSIST (integer) +* EVLOOP_NONBLOCK (integer) +* EVLOOP_ONCE (integer) -# Examples +## Examples Example #1 polling STDIN using basic API -``` +```php Date: Wed, 30 Dec 2015 15:29:36 +0100 Subject: [PATCH 027/103] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 0a7fd2e..c160466 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ Libevent is a library that provides a mechanism to execute a callback function w More information about Libevent can be found at » http://libevent.org/. +-- +This fork is the port of the module to [PHP7 / PHPNG](https://wiki.php.net/phpng-upgrading) +-- + ## Requirements This extension requires [» libevent](http://libevent.org/) library. Minimal required version is 1.4.0. From 80bdb12d002899cd5bb77e8a0b814d0a512befeb Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Wed, 30 Dec 2015 15:30:53 +0100 Subject: [PATCH 028/103] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c160466..5914175 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ Libevent is a library that provides a mechanism to execute a callback function w More information about Libevent can be found at » http://libevent.org/. -- -This fork is the port of the module to [PHP7 / PHPNG](https://wiki.php.net/phpng-upgrading) --- +**NOTE :** This fork is the port of the module to [PHP7 / PHPNG](https://wiki.php.net/phpng-upgrading) + ## Requirements From 680643ec38d09f6af41615395804ab0eabe661aa Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Thu, 31 Dec 2015 11:39:46 +0100 Subject: [PATCH 029/103] fix memory leak && references counter --- libevent.c | 65 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/libevent.c b/libevent.c index 9412682..2ef8a21 100644 --- a/libevent.c +++ b/libevent.c @@ -172,7 +172,6 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ } if (Z_TYPE_P(&event->stream_id) != IS_NULL) { zend_list_delete(Z_RES_P(&event->stream_id)); - ZVAL_NULL(&event->stream_id); } event_del(event->event); @@ -508,11 +507,14 @@ static PHP_FUNCTION(event_base_set) /* make sure the base is destroyed after the event */ Z_ADDREF_P(base->rsrc_id); ++base->events; - } - if (old_base && base != old_base && old_base->rsrc_id != NULL) { - --old_base->events; - zend_list_delete(Z_RES_P(old_base->rsrc_id)); + /* deference the event from the old base */ + if (old_base) { + --old_base->events; + if (old_base->rsrc_id != NULL) { + zend_list_delete(Z_RES_P(old_base->rsrc_id)); + } + } } event->base = base; @@ -705,9 +707,9 @@ static PHP_FUNCTION(event_set) zval_addref_p(zcallback); callback = emalloc(sizeof(php_event_callback_t)); - ZVAL_COPY_VALUE(&callback->func, zcallback); + ZVAL_COPY(&callback->func, zcallback); if(zarg) { - ZVAL_COPY_VALUE(&callback->arg, zarg); + ZVAL_COPY(&callback->arg, zarg); } else { ZVAL_NULL(&callback->arg); } @@ -717,8 +719,7 @@ static PHP_FUNCTION(event_set) if (events & EV_SIGNAL) { ZVAL_NULL(&event->stream_id); } else { - ZVAL_COPY_VALUE(&event->stream_id, fd); - Z_ADDREF_P(&event->stream_id); + ZVAL_COPY(&event->stream_id, fd); } event_set(event->event, (int)file_desc, (short)events, _php_event_callback, event); @@ -816,9 +817,9 @@ static PHP_FUNCTION(event_timer_set) zval_addref_p(zcallback); callback = emalloc(sizeof(php_event_callback_t)); - ZVAL_COPY_VALUE(&callback->func, zcallback); + ZVAL_COPY(&callback->func, zcallback); if (zarg) { - ZVAL_COPY_VALUE(&callback->arg, zarg); + ZVAL_COPY(&callback->arg, zarg); } else { ZVAL_NULL(&callback->arg); @@ -969,7 +970,7 @@ static PHP_FUNCTION(event_buffer_new) } if (zarg) { - ZVAL_COPY_VALUE(&bevent->arg, zarg); + ZVAL_COPY(&bevent->arg, zarg); } else { ZVAL_NULL(&bevent->arg); @@ -994,7 +995,11 @@ static PHP_FUNCTION(event_buffer_free) } bevent = ZVAL_TO_BEVENT(zbevent); - zend_list_delete(Z_RES_P(bevent->rsrc_id)); + + // Z_DELREF_P(bevent->rsrc_id); + if (bevent) { + zend_list_close(Z_RES_P(bevent->rsrc_id)); + } } /* }}} */ @@ -1022,12 +1027,13 @@ static PHP_FUNCTION(event_buffer_base_set) /* make sure the base is destroyed after the event */ Z_ADDREF_P(base->rsrc_id); ++base->events; - } - if (old_base) { - --old_base->events; - if (old_base->rsrc_id && Z_TYPE_P(old_base->rsrc_id) != IS_NULL) { - zend_list_delete(Z_RES_P(old_base->rsrc_id)); + /* deference the event from the old base */ + if (old_base) { + --old_base->events; + if (old_base->rsrc_id) { + zend_list_delete(Z_RES_P(old_base->rsrc_id)); + } } } @@ -1108,6 +1114,7 @@ static PHP_FUNCTION(event_buffer_read) php_bufferevent_t *bevent; char *data; zend_long data_size = 0; + zend_string *str = NULL; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbevent, &data_size) != SUCCESS) { @@ -1122,17 +1129,19 @@ static PHP_FUNCTION(event_buffer_read) php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_size cannot be less than zero"); RETURN_FALSE; } + data = safe_emalloc((int)data_size, sizeof(char), 1); ret = bufferevent_read(bevent->bevent, data, data_size); + if (ret > 0) { if (ret > data_size) { /* paranoia */ ret = data_size; } data[ret] = '\0'; - RETURN_STRINGL(data, ret); } + str = zend_string_init(data, ret, 0); efree(data); - RETURN_EMPTY_STRING(); + RETURN_STR(str); } /* }}} */ @@ -1175,10 +1184,12 @@ static PHP_FUNCTION(event_buffer_disable) bevent = ZVAL_TO_BEVENT(zbevent); - ret = bufferevent_disable(bevent->bevent, events); + if (bevent) { + ret = bufferevent_disable(bevent->bevent, events); - if (ret == 0) { - RETURN_TRUE; + if (ret == 0) { + RETURN_TRUE; + } } RETURN_FALSE; } @@ -1332,25 +1343,25 @@ static PHP_FUNCTION(event_buffer_set_callback) } if (zreadcb) { - ZVAL_COPY_VALUE(&bevent->readcb, zreadcb); + ZVAL_COPY(&bevent->readcb, zreadcb); } else { ZVAL_UNDEF(&bevent->readcb); } if (zwritecb) { - ZVAL_COPY_VALUE(&bevent->writecb, zwritecb); + ZVAL_COPY(&bevent->writecb, zwritecb); } else { ZVAL_UNDEF(&bevent->writecb); } if (zerrorcb) { - ZVAL_COPY_VALUE(&bevent->errorcb, zerrorcb); + ZVAL_COPY(&bevent->errorcb, zerrorcb); } else { ZVAL_UNDEF(&bevent->errorcb); } if (zarg) { - ZVAL_COPY_VALUE(&bevent->arg, zarg); + ZVAL_COPY(&bevent->arg, zarg); } else { ZVAL_NULL(&bevent->arg); } From bfb8db2687bde416fafc0ca73d577d6d068f5136 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Fri, 1 Jan 2016 19:52:14 +0100 Subject: [PATCH 030/103] bugfix https://github.com/expressif/pecl-event-libevent/issues/1 --- libevent.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libevent.c b/libevent.c index 2ef8a21..3e9394e 100644 --- a/libevent.c +++ b/libevent.c @@ -234,7 +234,7 @@ static void _php_event_callback(int fd, short events, void *arg) /* {{{ */ args[2] = callback->arg; if (Z_TYPE_P(&args[2]) != IS_NULL) { - Z_ADDREF_P(&args[2]); + Z_TRY_ADDREF_P(&args[2]); } @@ -266,7 +266,7 @@ static void _php_bufferevent_readcb(struct bufferevent *be, void *arg) /* {{{ */ args[1] = bevent->arg; if (Z_TYPE_P(&args[1]) != IS_NULL) { - Z_ADDREF_P(&args[1]); + Z_TRY_ADDREF_P(&args[1]); } if (call_user_function(EG(function_table), NULL, &bevent->readcb, &retval, 2, args) == SUCCESS) { @@ -294,7 +294,7 @@ static void _php_bufferevent_writecb(struct bufferevent *be, void *arg) /* {{{ * args[1] = bevent->arg; if (Z_TYPE_P(&args[1]) != IS_NULL) { - Z_ADDREF_P(&args[1]); + Z_TRY_ADDREF_P(&args[1]); } if (call_user_function(EG(function_table), NULL, &bevent->writecb, &retval, 2, args) == SUCCESS) { @@ -324,7 +324,7 @@ static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *a ZVAL_LONG(&args[1], (zend_long)what); args[2] = bevent->arg; if (Z_TYPE_P(&args[2]) != IS_NULL) { - Z_ADDREF_P(&args[2]); + Z_TRY_ADDREF_P(&args[2]); } From 64d3da71ecbf35342db31fc2489bbb9a5165e777 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 11:32:32 +0100 Subject: [PATCH 031/103] add travis pecl module --- .gitmodules | 3 +++ travis/pecl | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 travis/pecl diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..fa3cea4 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "travis/pecl"] + path = travis/pecl + url = https://github.com/m6w6/travis-pecl.git diff --git a/travis/pecl b/travis/pecl new file mode 160000 index 0000000..7d1a61d --- /dev/null +++ b/travis/pecl @@ -0,0 +1 @@ +Subproject commit 7d1a61ddce20446912ed1d5c988f801903972a34 From 093dfa6bdfd0140618a3effff70cdcc42aa69e5b Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 11:46:34 +0100 Subject: [PATCH 032/103] add a travis file --- .travis.yml | 23 +++++++++++++++++++++++ scripts/gen_travis_yml.php | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 .travis.yml create mode 100644 scripts/gen_travis_yml.php diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e3f659c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +# autogenerated file; do not edit +sudo: false +language: c + +addons: + apt: + packages: + - php5-cli + - php-pear + +env: + matrix: + - PHP=master enable_debug=no enable_maintainer_zts=no + - PHP=master enable_debug=yes enable_maintainer_zts=no + - PHP=master enable_debug=no enable_maintainer_zts=yes + - PHP=master enable_debug=yes enable_maintainer_zts=yes + +before_script: + - make -f travis/pecl/Makefile php + - make -f travis/pecl/Makefile ext PECL=libevent + +script: + - make -f travis/pecl/Makefile test diff --git a/scripts/gen_travis_yml.php b/scripts/gen_travis_yml.php new file mode 100644 index 0000000..de83bc7 --- /dev/null +++ b/scripts/gen_travis_yml.php @@ -0,0 +1,33 @@ +#!/usr/bin/env php +# autogenerated file; do not edit +sudo: false +language: c + +addons: + apt: + packages: + - php5-cli + - php-pear + +env: + matrix: + ["master"], + "enable_debug", + "enable_maintainer_zts", +]); +foreach ($env as $e) { + printf(" - %s\n", $e); +} + +?> + +before_script: + - make -f travis/pecl/Makefile php + - make -f travis/pecl/Makefile ext PECL=libevent + +script: + - make -f travis/pecl/Makefile test From 9b974727ecd0e2b3be4378484b176ef23108c5db Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 11:49:31 +0100 Subject: [PATCH 033/103] add build status --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5914175..625f86f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ Libevent bindings for PHP ========================= +[![Build Status](https://travis-ci.org/expressif/pecl-event-libevent.svg)](https://travis-ci.org/expressif/pecl-event-libevent) + Libevent is a library that provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. More information about Libevent can be found at » http://libevent.org/. From 29823a50810846901cb64cce45be14c3285e5fcc Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 12:01:09 +0100 Subject: [PATCH 034/103] add a generic test --- tests/libevent001.phpt | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/libevent001.phpt diff --git a/tests/libevent001.phpt b/tests/libevent001.phpt new file mode 100644 index 0000000..e945d35 --- /dev/null +++ b/tests/libevent001.phpt @@ -0,0 +1,40 @@ +--TEST-- +pecl/libevent - general +--SKIPIF-- + +--EXPECTF-- +bool(true) +bool(true) +bool(true) +int(-1) From 657ad92f5fc850c87c4704c1a8678c989562a176 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 12:06:36 +0100 Subject: [PATCH 035/103] add php 7.0.2 tests --- .travis.yml | 4 ++++ scripts/gen_travis_yml.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e3f659c..5901188 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,9 +11,13 @@ addons: env: matrix: - PHP=master enable_debug=no enable_maintainer_zts=no + - PHP=7.0.2 enable_debug=no enable_maintainer_zts=no - PHP=master enable_debug=yes enable_maintainer_zts=no + - PHP=7.0.2 enable_debug=yes enable_maintainer_zts=no - PHP=master enable_debug=no enable_maintainer_zts=yes + - PHP=7.0.2 enable_debug=no enable_maintainer_zts=yes - PHP=master enable_debug=yes enable_maintainer_zts=yes + - PHP=7.0.2 enable_debug=yes enable_maintainer_zts=yes before_script: - make -f travis/pecl/Makefile php diff --git a/scripts/gen_travis_yml.php b/scripts/gen_travis_yml.php index de83bc7..065fa81 100644 --- a/scripts/gen_travis_yml.php +++ b/scripts/gen_travis_yml.php @@ -15,7 +15,7 @@ $gen = include __DIR__ . "/../travis/pecl/gen-matrix.php"; $env = $gen([ - "PHP" => ["master"], + "PHP" => ["master", "7.0.2"], "enable_debug", "enable_maintainer_zts", ]); From 6ce6b02fc779ba21e5f5f0e492329d3f5c80ed5a Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 12:09:05 +0100 Subject: [PATCH 036/103] test 7.0.1 --- .travis.yml | 8 ++++---- scripts/gen_travis_yml.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5901188..7e3a1be 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,13 +11,13 @@ addons: env: matrix: - PHP=master enable_debug=no enable_maintainer_zts=no - - PHP=7.0.2 enable_debug=no enable_maintainer_zts=no + - PHP=7.0.1 enable_debug=no enable_maintainer_zts=no - PHP=master enable_debug=yes enable_maintainer_zts=no - - PHP=7.0.2 enable_debug=yes enable_maintainer_zts=no + - PHP=7.0.1 enable_debug=yes enable_maintainer_zts=no - PHP=master enable_debug=no enable_maintainer_zts=yes - - PHP=7.0.2 enable_debug=no enable_maintainer_zts=yes + - PHP=7.0.1 enable_debug=no enable_maintainer_zts=yes - PHP=master enable_debug=yes enable_maintainer_zts=yes - - PHP=7.0.2 enable_debug=yes enable_maintainer_zts=yes + - PHP=7.0.1 enable_debug=yes enable_maintainer_zts=yes before_script: - make -f travis/pecl/Makefile php diff --git a/scripts/gen_travis_yml.php b/scripts/gen_travis_yml.php index 065fa81..2544258 100644 --- a/scripts/gen_travis_yml.php +++ b/scripts/gen_travis_yml.php @@ -15,7 +15,7 @@ $gen = include __DIR__ . "/../travis/pecl/gen-matrix.php"; $env = $gen([ - "PHP" => ["master", "7.0.2"], + "PHP" => ["master", "7.0.1"], "enable_debug", "enable_maintainer_zts", ]); From 5938b7e17ca36dba10e68ddbcf8aedf8087faf4e Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 13:21:00 +0100 Subject: [PATCH 037/103] fix event dtor https://github.com/expressif/pecl-event-libevent/issues/2 --- libevent.c | 1 + tests/libevent002.phpt | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 tests/libevent002.phpt diff --git a/libevent.c b/libevent.c index 3e9394e..691f9ec 100644 --- a/libevent.c +++ b/libevent.c @@ -569,6 +569,7 @@ static PHP_FUNCTION(event_new) event->callback = NULL; event->base = NULL; event->in_free = 0; + ZVAL_NULL(&event->stream_id); TSRMLS_SET_CTX(event->thread_ctx); event->rsrc_id = zend_list_insert(event, le_event); diff --git a/tests/libevent002.phpt b/tests/libevent002.phpt new file mode 100644 index 0000000..94ab700 --- /dev/null +++ b/tests/libevent002.phpt @@ -0,0 +1,14 @@ +--TEST-- +pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/2 +--SKIPIF-- + +Done +--EXPECTF-- +Done \ No newline at end of file From f1475505274113f71f39ae7a09c4e6f6304a8957 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 13:39:08 +0100 Subject: [PATCH 038/103] fix test --- tests/libevent002.phpt | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/libevent002.phpt b/tests/libevent002.phpt index 94ab700..d2661b6 100644 --- a/tests/libevent002.phpt +++ b/tests/libevent002.phpt @@ -5,7 +5,6 @@ pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/2 if (!extension_loaded("libevent")) die("skip pecl/libevent needed"); --FILE-- From ca3652cc55a400378e8e6c5f57476c7f0af5df23 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 13:39:36 +0100 Subject: [PATCH 039/103] add test for bug https://github.com/expressif/pecl-event-libevent/issues/3 --- tests/libevent003.phpt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/libevent003.phpt diff --git a/tests/libevent003.phpt b/tests/libevent003.phpt new file mode 100644 index 0000000..42c0de7 --- /dev/null +++ b/tests/libevent003.phpt @@ -0,0 +1,22 @@ +--TEST-- +pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/3 +--SKIPIF-- + +--EXPECTF-- +0 +1 \ No newline at end of file From 45f5c026fef0c00f3fdde06abdaba928871948c2 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 14:53:08 +0100 Subject: [PATCH 040/103] use file input instead of STDIN --- tests/input.txt | 1 + tests/libevent001.phpt | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 tests/input.txt diff --git a/tests/input.txt b/tests/input.txt new file mode 100644 index 0000000..454f6b3 --- /dev/null +++ b/tests/input.txt @@ -0,0 +1 @@ +0123456789abcdef \ No newline at end of file diff --git a/tests/libevent001.phpt b/tests/libevent001.phpt index e945d35..2a74fa4 100644 --- a/tests/libevent001.phpt +++ b/tests/libevent001.phpt @@ -6,7 +6,6 @@ if (!extension_loaded("libevent")) die("skip pecl/libevent needed"); --FILE-- Date: Sun, 3 Jan 2016 15:17:11 +0100 Subject: [PATCH 041/103] using a stream instead a file --- tests/libevent001.phpt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/libevent001.phpt b/tests/libevent001.phpt index 2a74fa4..180615d 100644 --- a/tests/libevent001.phpt +++ b/tests/libevent001.phpt @@ -22,13 +22,11 @@ function foo($fd, $events, $arg) $base = event_base_new(); $event = event_new(); -$fd = fopen(__DIR__ . '/input.txt', 'r'); +$fd = fopen('https://raw.githubusercontent.com/expressif/pecl-event-libevent/master/tests/input.txt', 'r'); var_dump(event_set($event, $fd, EV_READ | EV_PERSIST, "foo", array($event, $base))); var_dump(event_set($event, $fd, EV_READ | EV_PERSIST, "foo", array($event, $base))); - -event_base_set($event, $base); - +var_dump(event_base_set($event, $base)); var_dump(event_add($event)); var_dump(event_base_loop($base)); ?> @@ -36,6 +34,7 @@ var_dump(event_base_loop($base)); bool(true) bool(true) bool(true) +bool(true) string(1) "0" string(1) "1" string(1) "2" From 1577a70b2a6ee1afd75f1e15d4732fc513974440 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 15:28:36 +0100 Subject: [PATCH 042/103] add ssl support on builds --- .travis.yml | 16 ++++++++-------- scripts/gen_travis_yml.php | 3 ++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7e3a1be..2b3f631 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,14 +10,14 @@ addons: env: matrix: - - PHP=master enable_debug=no enable_maintainer_zts=no - - PHP=7.0.1 enable_debug=no enable_maintainer_zts=no - - PHP=master enable_debug=yes enable_maintainer_zts=no - - PHP=7.0.1 enable_debug=yes enable_maintainer_zts=no - - PHP=master enable_debug=no enable_maintainer_zts=yes - - PHP=7.0.1 enable_debug=no enable_maintainer_zts=yes - - PHP=master enable_debug=yes enable_maintainer_zts=yes - - PHP=7.0.1 enable_debug=yes enable_maintainer_zts=yes + - PHP=master enable_debug=no enable_maintainer_zts=no with_openssl=no + - PHP=master enable_debug=yes enable_maintainer_zts=no with_openssl=no + - PHP=master enable_debug=no enable_maintainer_zts=yes with_openssl=no + - PHP=master enable_debug=yes enable_maintainer_zts=yes with_openssl=no + - PHP=master enable_debug=no enable_maintainer_zts=no with_openssl=yes + - PHP=master enable_debug=yes enable_maintainer_zts=no with_openssl=yes + - PHP=master enable_debug=no enable_maintainer_zts=yes with_openssl=yes + - PHP=master enable_debug=yes enable_maintainer_zts=yes with_openssl=yes before_script: - make -f travis/pecl/Makefile php diff --git a/scripts/gen_travis_yml.php b/scripts/gen_travis_yml.php index 2544258..654b246 100644 --- a/scripts/gen_travis_yml.php +++ b/scripts/gen_travis_yml.php @@ -15,9 +15,10 @@ $gen = include __DIR__ . "/../travis/pecl/gen-matrix.php"; $env = $gen([ - "PHP" => ["master", "7.0.1"], + "PHP" => ["master"], "enable_debug", "enable_maintainer_zts", + "with_openssl" ]); foreach ($env as $e) { printf(" - %s\n", $e); From d1e581bebe5a2abf5fb6cadec6f08417fafb7902 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 15:29:02 +0100 Subject: [PATCH 043/103] ignore tests outputs --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..246302f --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/tests/*.diff +/tests/*.exp +/tests/*.log +/tests/*.out +/tests/*.sh +/tests/*.php From dbe5f5c3f423bc290514635c2e54b7c7b7ec4107 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 15:39:34 +0100 Subject: [PATCH 044/103] force ssl + fix output on test 001 --- .travis.yml | 4 ---- scripts/gen_travis_yml.php | 2 +- tests/libevent001.phpt | 2 ++ 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2b3f631..f168d51 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,10 +10,6 @@ addons: env: matrix: - - PHP=master enable_debug=no enable_maintainer_zts=no with_openssl=no - - PHP=master enable_debug=yes enable_maintainer_zts=no with_openssl=no - - PHP=master enable_debug=no enable_maintainer_zts=yes with_openssl=no - - PHP=master enable_debug=yes enable_maintainer_zts=yes with_openssl=no - PHP=master enable_debug=no enable_maintainer_zts=no with_openssl=yes - PHP=master enable_debug=yes enable_maintainer_zts=no with_openssl=yes - PHP=master enable_debug=no enable_maintainer_zts=yes with_openssl=yes diff --git a/scripts/gen_travis_yml.php b/scripts/gen_travis_yml.php index 654b246..52438ec 100644 --- a/scripts/gen_travis_yml.php +++ b/scripts/gen_travis_yml.php @@ -18,7 +18,7 @@ "PHP" => ["master"], "enable_debug", "enable_maintainer_zts", - "with_openssl" + "with_openssl" => ["yes"] ]); foreach ($env as $e) { printf(" - %s\n", $e); diff --git a/tests/libevent001.phpt b/tests/libevent001.phpt index 180615d..03ec0fb 100644 --- a/tests/libevent001.phpt +++ b/tests/libevent001.phpt @@ -44,4 +44,6 @@ string(1) "5" string(1) "6" string(1) "7" string(1) "8" +string(1) "9" +string(1) "a" int(0) \ No newline at end of file From b06a5eea1e516d44af6ae96398d6059205d2097c Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 3 Jan 2016 15:44:48 +0100 Subject: [PATCH 045/103] fix warn message from libevent --- tests/libevent002.phpt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/libevent002.phpt b/tests/libevent002.phpt index d2661b6..4377379 100644 --- a/tests/libevent002.phpt +++ b/tests/libevent002.phpt @@ -7,7 +7,9 @@ if (!extension_loaded("libevent")) die("skip pecl/libevent needed"); Done --EXPECTF-- +bool(true) Done \ No newline at end of file From 0cf97b8df9bf11fa6b49da934fedb06b2d82c762 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Mon, 4 Jan 2016 20:58:34 +0100 Subject: [PATCH 046/103] fix undef $event warning --- tests/libevent002.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/libevent002.phpt b/tests/libevent002.phpt index 4377379..8385892 100644 --- a/tests/libevent002.phpt +++ b/tests/libevent002.phpt @@ -7,7 +7,7 @@ if (!extension_loaded("libevent")) die("skip pecl/libevent needed"); Done --EXPECTF-- From 021555173146120c6b34942d9776a955622efe67 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 9 Jan 2016 18:24:42 +0100 Subject: [PATCH 047/103] fix https://github.com/expressif/pecl-event-libevent/issues/3#issuecomment-169082480 proposed by https://github.com/Joungkyun --- libevent.c | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/libevent.c b/libevent.c index 691f9ec..4000b61 100644 --- a/libevent.c +++ b/libevent.c @@ -148,18 +148,25 @@ static inline void _php_event_callback_free(php_event_callback_t *callback) /* { ZEND_RSRC_DTOR_FUNC(_php_event_base_dtor) /* {{{ */ { + if (!res || !res->ptr) { + return; + } php_event_base_t *base = (php_event_base_t*)res->ptr; - event_base_free(base->base); - efree(base); } /* }}} */ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ { + if (!res || !res->ptr) { + return; + } + php_event_t *event = (php_event_t*)res->ptr; zval *base_id = NULL; + if (!event) return; + if (event->in_free) { return; } @@ -177,7 +184,6 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ _php_event_callback_free(event->callback); efree(event->event); - efree(event); if (base_id) { zend_list_delete(Z_RES_P(base_id)); @@ -187,6 +193,11 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ { + + if (!res || !res->ptr) { + return; + } + php_bufferevent_t *bevent = (php_bufferevent_t*)res->ptr; zval *base_id = NULL; @@ -199,7 +210,6 @@ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ zval_ptr_dtor(&bevent->errorcb); zval_ptr_dtor(&bevent->arg); bufferevent_free(bevent->bevent); - efree(bevent); if (base_id) { zend_list_delete(Z_RES_P(base_id)); @@ -386,7 +396,7 @@ static PHP_FUNCTION(event_base_reinit) { } /* }}} */ -/* {{{ proto void event_base_free(resource base) +/* {{{ proto boolean event_base_free(resource base) */ static PHP_FUNCTION(event_base_free) { @@ -399,12 +409,16 @@ static PHP_FUNCTION(event_base_free) base = ZVAL_TO_BASE(zbase); - if (base->events > 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "base has events attached to it and cannot be freed"); - RETURN_FALSE; + if (base) { + if (base->events > 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "base has events attached to it and cannot be freed"); + } + else { + zend_list_close(Z_RES_P(base->rsrc_id)); + RETURN_TRUE; + } } - - zend_list_delete(Z_RES_P(base->rsrc_id)); + RETURN_FALSE; } /* }}} */ @@ -589,7 +603,7 @@ static PHP_FUNCTION(event_free) } event = ZVAL_TO_EVENT(zevent); - zend_list_delete(Z_RES_P(event->rsrc_id)); + zend_list_close(Z_RES_P(event->rsrc_id)); } /* }}} */ @@ -670,7 +684,7 @@ static PHP_FUNCTION(event_set) } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - php_sock = (php_socket *)zend_fetch_resource2_ex(fd, NULL, php_sockets_le_socket()); + php_sock = (php_socket *)zend_fetch_resource_ex(fd, NULL, php_sockets_le_socket()); if (php_sock) { file_desc = php_sock->bsd_socket; } else { @@ -901,7 +915,7 @@ static PHP_FUNCTION(event_buffer_new) } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - php_sock = (php_socket *)zend_fetch_resource2_ex(zfd, NULL, php_sockets_le_socket()); + php_sock = (php_socket *)zend_fetch_resource_ex(zfd, NULL, php_sockets_le_socket()); if (php_sock) { fd = php_sock->bsd_socket; } else { @@ -997,7 +1011,6 @@ static PHP_FUNCTION(event_buffer_free) bevent = ZVAL_TO_BEVENT(zbevent); - // Z_DELREF_P(bevent->rsrc_id); if (bevent) { zend_list_close(Z_RES_P(bevent->rsrc_id)); } @@ -1256,7 +1269,7 @@ static PHP_FUNCTION(event_buffer_fd_set) } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - php_sock = (php_socket *)zend_fetch_resource2_ex(zfd, NULL, php_sockets_le_socket()) + php_sock = (php_socket *)zend_fetch_resource_ex(zfd, NULL, php_sockets_le_socket()) if (php_sock) { fd = php_sock->bsd_socket; } else { From b8576a1655a650fc6a5ab4ff01fd783e0e6adf5f Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 9 Jan 2016 18:33:07 +0100 Subject: [PATCH 048/103] fix output of test 3 --- tests/libevent003.phpt | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/libevent003.phpt b/tests/libevent003.phpt index 42c0de7..d04aa35 100644 --- a/tests/libevent003.phpt +++ b/tests/libevent003.phpt @@ -8,15 +8,34 @@ if (!extension_loaded("libevent")) die("skip pecl/libevent needed"); function ec() { } $sock = stream_socket_server ('tcp://0.0.0.0:2000', $errno, $errstr); + for ($i=0; $i<2; $i++) { + echo "$i\n"; $base = event_base_new(); + var_dump($base); $ev = event_buffer_new($sock, NULL, NULL, 'ec'); + var_dump($ev); + var_dump(event_buffer_base_set($ev, $base)); event_buffer_free($ev); + var_dump($ev); event_base_free($base); - echo "$i\n"; + var_dump($base); + unset($base); + unset($ev); + } fclose ($sock); ?> --EXPECTF-- 0 -1 \ No newline at end of file +resource(6) of type (event base) +resource(7) of type (buffer event) +bool(true) +resource(7) of type (Unknown) +resource(6) of type (Unknown) +1 +resource(8) of type (event base) +resource(9) of type (buffer event) +bool(true) +resource(9) of type (Unknown) +resource(8) of type (Unknown) \ No newline at end of file From 7f782df75c05a6284df6ede3f967a155b41f8d8f Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 9 Jan 2016 18:43:31 +0100 Subject: [PATCH 049/103] fix test output --- tests/libevent002.phpt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/libevent002.phpt b/tests/libevent002.phpt index 8385892..9d805ab 100644 --- a/tests/libevent002.phpt +++ b/tests/libevent002.phpt @@ -5,11 +5,15 @@ pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/2 if (!extension_loaded("libevent")) die("skip pecl/libevent needed"); --FILE-- Done --EXPECTF-- bool(true) +bool(true) Done \ No newline at end of file From f71ed976eef9224ba325c70fa8fbaaacb3358c0c Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 9 Jan 2016 18:47:18 +0100 Subject: [PATCH 050/103] fix syntax error --- tests/libevent002.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/libevent002.phpt b/tests/libevent002.phpt index 9d805ab..10444da 100644 --- a/tests/libevent002.phpt +++ b/tests/libevent002.phpt @@ -9,7 +9,7 @@ $f = function() {}; $fd = fopen('https://raw.githubusercontent.com/expressif/pecl-event-libevent/master/tests/input.txt', 'r'); $base = event_base_new (); $e = event_new(); -var_dump(event_set($e, $fd, EV_READ | EV_PERSIST, $f))); +var_dump(event_set($e, $fd, EV_READ | EV_PERSIST, $f)); var_dump(event_base_set($e, $base)); ?> Done From 1cbe4992ee6c0fb8c6bba0cfdd061101cbd4cbcf Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Mon, 25 Jan 2016 16:52:56 +0900 Subject: [PATCH 051/103] fixed syntax error #4 --- libevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libevent.c b/libevent.c index 4000b61..d1c4b7a 100644 --- a/libevent.c +++ b/libevent.c @@ -1269,7 +1269,7 @@ static PHP_FUNCTION(event_buffer_fd_set) } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - php_sock = (php_socket *)zend_fetch_resource_ex(zfd, NULL, php_sockets_le_socket()) + php_sock = (php_socket *)zend_fetch_resource_ex(zfd, NULL, php_sockets_le_socket()); if (php_sock) { fd = php_sock->bsd_socket; } else { From b040d5a7e3400e8c6a992c7e60f79301ca2777f7 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Mon, 25 Jan 2016 17:03:05 +0900 Subject: [PATCH 052/103] change zend_list_delete to zend_list_close as guide on https://wiki.php.net/phpng-upgrading and enhanced event_base_free --- libevent.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/libevent.c b/libevent.c index d1c4b7a..72ed9a1 100644 --- a/libevent.c +++ b/libevent.c @@ -178,7 +178,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ --event->base->events; } if (Z_TYPE_P(&event->stream_id) != IS_NULL) { - zend_list_delete(Z_RES_P(&event->stream_id)); + zend_list_close(Z_RES_P(&event->stream_id)); } event_del(event->event); @@ -186,7 +186,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ efree(event->event); if (base_id) { - zend_list_delete(Z_RES_P(base_id)); + zend_list_close(Z_RES_P(base_id)); } } /* }}} */ @@ -212,7 +212,7 @@ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ bufferevent_free(bevent->bevent); if (base_id) { - zend_list_delete(Z_RES_P(base_id)); + zend_list_close(Z_RES_P(base_id)); } } /* }}} */ @@ -396,7 +396,8 @@ static PHP_FUNCTION(event_base_reinit) { } /* }}} */ -/* {{{ proto boolean event_base_free(resource base) +/* {{{ proto void event_base_free(resource base) + * return type is defined void at http://php.net/manual/en/function.event-base-free.php */ static PHP_FUNCTION(event_base_free) { @@ -407,18 +408,15 @@ static PHP_FUNCTION(event_base_free) return; } - base = ZVAL_TO_BASE(zbase); + if (!(base = ZVAL_TO_BASE(zbase))) + return; - if (base) { - if (base->events > 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "base has events attached to it and cannot be freed"); - } - else { - zend_list_close(Z_RES_P(base->rsrc_id)); - RETURN_TRUE; - } + if (base->events > 0) { + php_error_docref(NULL, E_WARNING, "base has events attached to it and cannot be freed"); + RETURN_FALSE; } - RETURN_FALSE; + + zend_list_close(Z_RES_P(base->rsrc_id)); } /* }}} */ @@ -438,7 +436,7 @@ static PHP_FUNCTION(event_base_loop) base = ZVAL_TO_BASE(zbase); Z_ADDREF_P(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */ ret = event_base_loop(base->base, (int)flags); - zend_list_delete(Z_RES_P(base->rsrc_id)); + zend_list_close(Z_RES_P(base->rsrc_id)); RETURN_LONG(ret); } @@ -526,7 +524,7 @@ static PHP_FUNCTION(event_base_set) if (old_base) { --old_base->events; if (old_base->rsrc_id != NULL) { - zend_list_delete(Z_RES_P(old_base->rsrc_id)); + zend_list_close(Z_RES_P(old_base->rsrc_id)); } } } @@ -843,7 +841,7 @@ static PHP_FUNCTION(event_timer_set) old_callback = event->callback; event->callback = callback; if (Z_TYPE_P(&event->stream_id) != IS_NULL) { - zend_list_delete(Z_RES_P(&event->stream_id)); + zend_list_close(Z_RES_P(&event->stream_id)); ZVAL_NULL(&event->stream_id); } @@ -1046,7 +1044,7 @@ static PHP_FUNCTION(event_buffer_base_set) if (old_base) { --old_base->events; if (old_base->rsrc_id) { - zend_list_delete(Z_RES_P(old_base->rsrc_id)); + zend_list_close(Z_RES_P(old_base->rsrc_id)); } } } From a38b8823015d32faf3390f2270ade68735e1ede0 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Mon, 25 Jan 2016 17:09:51 +0900 Subject: [PATCH 053/103] refix #3 travis-ci.org libevent003.phpt --- libevent.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/libevent.c b/libevent.c index 72ed9a1..05a5cc1 100644 --- a/libevent.c +++ b/libevent.c @@ -204,15 +204,16 @@ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ if (bevent->base) { base_id = bevent->base->rsrc_id; --bevent->base->events; - } - zval_ptr_dtor(&bevent->readcb); - zval_ptr_dtor(&bevent->writecb); - zval_ptr_dtor(&bevent->errorcb); - zval_ptr_dtor(&bevent->arg); - bufferevent_free(bevent->bevent); - if (base_id) { - zend_list_close(Z_RES_P(base_id)); + zval_ptr_dtor(&bevent->readcb); + zval_ptr_dtor(&bevent->writecb); + zval_ptr_dtor(&bevent->errorcb); + zval_ptr_dtor(&bevent->arg); + bufferevent_free(bevent->bevent); + + if (base_id) { + zend_list_close(Z_RES_P(base_id)); + } } } /* }}} */ @@ -1010,6 +1011,8 @@ static PHP_FUNCTION(event_buffer_free) bevent = ZVAL_TO_BEVENT(zbevent); if (bevent) { + --bevent->base->events; + bevent->base = NULL; zend_list_close(Z_RES_P(bevent->rsrc_id)); } } From cc0eb5356852fe78fc0b5741302bd965d21e113f Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Tue, 26 Jan 2016 18:02:45 +0900 Subject: [PATCH 054/103] fixed memory leak for libevent001.phpt --- libevent.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libevent.c b/libevent.c index 05a5cc1..d0fa6a2 100644 --- a/libevent.c +++ b/libevent.c @@ -437,7 +437,6 @@ static PHP_FUNCTION(event_base_loop) base = ZVAL_TO_BASE(zbase); Z_ADDREF_P(base->rsrc_id); /* make sure the base cannot be destroyed during the loop */ ret = event_base_loop(base->base, (int)flags); - zend_list_close(Z_RES_P(base->rsrc_id)); RETURN_LONG(ret); } @@ -601,7 +600,15 @@ static PHP_FUNCTION(event_free) return; } - event = ZVAL_TO_EVENT(zevent); + if (!(event = ZVAL_TO_EVENT(zevent))) + return; + + event->in_free = 1; + if (event->base) { + --event->base->events; + event->base = NULL; + } + event_del (event->event); zend_list_close(Z_RES_P(event->rsrc_id)); } /* }}} */ From 72b742d63db35fa2d391f83f9abbf8719b74615c Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Tue, 26 Jan 2016 19:48:15 +0900 Subject: [PATCH 055/103] fiexed segfault #3 by other case --- libevent.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libevent.c b/libevent.c index d0fa6a2..3e1bee4 100644 --- a/libevent.c +++ b/libevent.c @@ -153,6 +153,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_base_dtor) /* {{{ */ } php_event_base_t *base = (php_event_base_t*)res->ptr; event_base_free(base->base); + efree(base); } /* }}} */ @@ -210,6 +211,7 @@ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ zval_ptr_dtor(&bevent->errorcb); zval_ptr_dtor(&bevent->arg); bufferevent_free(bevent->bevent); + efree(bevent); if (base_id) { zend_list_close(Z_RES_P(base_id)); @@ -1015,13 +1017,14 @@ static PHP_FUNCTION(event_buffer_free) return; } - bevent = ZVAL_TO_BEVENT(zbevent); + if (!(bevent = ZVAL_TO_BEVENT(zbevent))) + return; - if (bevent) { + if (bevent->base) { --bevent->base->events; bevent->base = NULL; - zend_list_close(Z_RES_P(bevent->rsrc_id)); } + zend_list_close(Z_RES_P(bevent->rsrc_id)); } /* }}} */ From fef8215dde8ede92d1bd7b3904e2e88b63f851b0 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Tue, 26 Jan 2016 20:01:09 +0900 Subject: [PATCH 056/103] missing efree at 72b742d --- libevent.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libevent.c b/libevent.c index 3e1bee4..8d3afcf 100644 --- a/libevent.c +++ b/libevent.c @@ -185,6 +185,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ _php_event_callback_free(event->callback); efree(event->event); + efree(event); if (base_id) { zend_list_close(Z_RES_P(base_id)); From 43e70b82a3978508b6131c51c80ff37883de4fd0 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Tue, 26 Jan 2016 20:49:45 +0900 Subject: [PATCH 057/103] fixed leak on debug_mode=yes on event_base_new --- libevent.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/libevent.c b/libevent.c index 8d3afcf..16dc53d 100644 --- a/libevent.c +++ b/libevent.c @@ -142,7 +142,7 @@ static inline void _php_event_callback_free(php_event_callback_t *callback) /* { } zval_ptr_dtor(&callback->func); zval_ptr_dtor(&callback->arg); - efree(callback); + safe_efree(callback); } /* }}} */ @@ -153,7 +153,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_base_dtor) /* {{{ */ } php_event_base_t *base = (php_event_base_t*)res->ptr; event_base_free(base->base); - efree(base); + safe_efree(base); } /* }}} */ @@ -169,6 +169,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ if (!event) return; if (event->in_free) { + safe_efree(event); return; } @@ -184,8 +185,8 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ event_del(event->event); _php_event_callback_free(event->callback); - efree(event->event); - efree(event); + safe_efree(event->event); + safe_efree(event); if (base_id) { zend_list_close(Z_RES_P(base_id)); @@ -203,6 +204,9 @@ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ php_bufferevent_t *bevent = (php_bufferevent_t*)res->ptr; zval *base_id = NULL; + if (!bevent) + return; + if (bevent->base) { base_id = bevent->base->rsrc_id; --bevent->base->events; @@ -212,12 +216,13 @@ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ zval_ptr_dtor(&bevent->errorcb); zval_ptr_dtor(&bevent->arg); bufferevent_free(bevent->bevent); - efree(bevent); if (base_id) { zend_list_close(Z_RES_P(base_id)); } } + + safe_efree(bevent); } /* }}} */ @@ -369,7 +374,7 @@ static PHP_FUNCTION(event_base_new) base->rsrc_id = NULL; base->base = event_base_new(); if (!base->base) { - efree(base); + safe_efree(base); RETURN_FALSE; } @@ -1166,7 +1171,7 @@ static PHP_FUNCTION(event_buffer_read) data[ret] = '\0'; } str = zend_string_init(data, ret, 0); - efree(data); + safe_efree(data); RETURN_STR(str); } /* }}} */ From 768bc55fce966530b046986e76ad697e44446806 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Tue, 26 Jan 2016 20:53:54 +0900 Subject: [PATCH 058/103] fixed undefined symbol safe_efree --- php_libevent.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/php_libevent.h b/php_libevent.h index ceab3f3..6abfc5b 100644 --- a/php_libevent.h +++ b/php_libevent.h @@ -55,6 +55,10 @@ static zend_always_inline zend_uint zval_delref_p(zval* pz) { } #endif +#ifndef safe_efree +#define safe_efree(x) do {if(x){efree(x); x=NULL;}} while(0) +#endif + #endif /* PHP_LIBEVENT_H */ From 3de42595088e19536b5bcc3b7672b979a20f3172 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Tue, 26 Jan 2016 21:26:33 +0900 Subject: [PATCH 059/103] fixed wrong varibale type on php_error_docref --- libevent.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libevent.c b/libevent.c index 16dc53d..0370efd 100644 --- a/libevent.c +++ b/libevent.c @@ -726,8 +726,8 @@ static PHP_FUNCTION(event_set) } } - if (!zend_is_callable(zcallback, 0, &func_name TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid callback", func_name); + if (!zend_is_callable(zcallback, 0, &func_name)) { + php_error_docref(NULL, E_WARNING, "'%s' is not a valid callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } @@ -836,8 +836,8 @@ static PHP_FUNCTION(event_timer_set) event = ZVAL_TO_EVENT(zevent); - if (!zend_is_callable(zcallback, 0, &func_name TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid callback", func_name); + if (!zend_is_callable(zcallback, 0, &func_name)) { + php_error_docref(NULL, E_WARNING, "'%s' is not a valid callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } @@ -953,8 +953,8 @@ static PHP_FUNCTION(event_buffer_new) } if (Z_TYPE_P(zreadcb) != IS_NULL) { - if (!zend_is_callable(zreadcb, 0, &func_name TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid read callback", func_name); + if (!zend_is_callable(zreadcb, 0, &func_name)) { + php_error_docref(NULL, E_WARNING, "'%s' is not a valid read callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } @@ -964,8 +964,8 @@ static PHP_FUNCTION(event_buffer_new) } if (Z_TYPE_P(zwritecb) != IS_NULL) { - if (!zend_is_callable(zwritecb, 0, &func_name TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid write callback", func_name); + if (!zend_is_callable(zwritecb, 0, &func_name)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid write callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } @@ -974,8 +974,8 @@ static PHP_FUNCTION(event_buffer_new) zwritecb = NULL; } - if (!zend_is_callable(zerrorcb, 0, &func_name TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid error callback", func_name); + if (!zend_is_callable(zerrorcb, 0, &func_name)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid error callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } @@ -1328,8 +1328,8 @@ static PHP_FUNCTION(event_buffer_set_callback) bevent= ZVAL_TO_BEVENT(zbevent); if (Z_TYPE_P(zreadcb) != IS_NULL) { - if (!zend_is_callable(zreadcb, 0, &func_name TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid read callback", func_name); + if (!zend_is_callable(zreadcb, 0, &func_name)) { + php_error_docref(NULL, E_WARNING, "'%s' is not a valid read callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } @@ -1339,8 +1339,8 @@ static PHP_FUNCTION(event_buffer_set_callback) } if (Z_TYPE_P(zwritecb) != IS_NULL) { - if (!zend_is_callable(zwritecb, 0, &func_name TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid write callback", func_name); + if (!zend_is_callable(zwritecb, 0, &func_name)) { + php_error_docref(NULL, E_WARNING, "'%s' is not a valid write callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } @@ -1350,8 +1350,8 @@ static PHP_FUNCTION(event_buffer_set_callback) } if (Z_TYPE_P(zerrorcb) != IS_NULL) { - if (!zend_is_callable(zerrorcb, 0, &func_name TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid error callback", func_name); + if (!zend_is_callable(zerrorcb, 0, &func_name)) { + php_error_docref(NULL, E_WARNING, "'%s' is not a valid error callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } From 021ca5f35352ad6154b074227bb64f5ee81e853d Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Tue, 26 Jan 2016 21:42:53 +0900 Subject: [PATCH 060/103] remove unsed code --- libevent.c | 20 -------------------- php_libevent.h | 14 -------------- 2 files changed, 34 deletions(-) diff --git a/libevent.c b/libevent.c index 0370efd..35349ba 100644 --- a/libevent.c +++ b/libevent.c @@ -56,26 +56,6 @@ never version, so this ifdefs would go away. */ # include #endif -#if PHP_MAJOR_VERSION < 5 -# ifdef PHP_WIN32 -typedef SOCKET php_socket_t; -# else -typedef int php_socket_t; -# endif - -# ifdef ZTS -# define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = (void ***) ctx -# define TSRMLS_SET_CTX(ctx) ctx = (void ***) tsrm_ls -# else -# define TSRMLS_FETCH_FROM_CTX(ctx) -# define TSRMLS_SET_CTX(ctx) -# endif - -# ifndef Z_ADDREF_P -# define Z_ADDREF_P(x) (x)->refcount++ -# endif -#endif - static int le_event_base; static int le_event; static int le_bufferevent; diff --git a/php_libevent.h b/php_libevent.h index 6abfc5b..7a3a8ad 100644 --- a/php_libevent.h +++ b/php_libevent.h @@ -41,20 +41,6 @@ extern zend_module_entry libevent_module_entry; # endif #endif -#ifndef Z_ADDREF_P -#define Z_ADDREF_P(pz) zval_addref_p(pz) -static zend_always_inline zend_uint zval_addref_p(zval* pz) { - return ++pz->refcount; -} -#endif - -#ifndef Z_DELREF_P -#define Z_DELREF_P(pz) zval_delref_p(pz) -static zend_always_inline zend_uint zval_delref_p(zval* pz) { - return --pz->refcount; -} -#endif - #ifndef safe_efree #define safe_efree(x) do {if(x){efree(x); x=NULL;}} while(0) #endif From bd3fcd363e57985e302d89219adc7f4822a4cbe7 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Wed, 27 Jan 2016 02:13:57 +0900 Subject: [PATCH 061/103] fixed segfault when reousrce argument is not resource type or invalid resource type --- libevent.c | 67 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/libevent.c b/libevent.c index 35349ba..5ab74af 100644 --- a/libevent.c +++ b/libevent.c @@ -375,7 +375,9 @@ static PHP_FUNCTION(event_base_reinit) { return; } - base = ZVAL_TO_BASE(zbase); + if (!(base = ZVAL_TO_BASE(zbase))) + RETURN_FALSE; + r = event_reinit(base->base); if (r == -1) { RETURN_FALSE @@ -464,7 +466,8 @@ static PHP_FUNCTION(event_base_loopexit) return; } - base = ZVAL_TO_BASE(zbase); + if (!(base = ZVAL_TO_BASE(zbase))) + RETURN_FALSE; if (timeout < 0) { ret = event_base_loopexit(base->base, NULL); @@ -496,8 +499,10 @@ static PHP_FUNCTION(event_base_set) return; } - base = ZVAL_TO_BASE(zbase); - event = ZVAL_TO_EVENT(zevent); + if (!(base = ZVAL_TO_BASE(zbase))) + RETURN_FALSE; + if (!(event = ZVAL_TO_EVENT(zevent))) + RETURN_FALSE; old_base = event->base; ret = event_base_set(base->base, event->event); @@ -537,7 +542,8 @@ static PHP_FUNCTION(event_base_priority_init) return; } - base = ZVAL_TO_BASE(zbase); + if (!(base = ZVAL_TO_BASE(zbase))) + RETURN_FALSE; if (npriorities < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "npriorities cannot be less than zero"); @@ -614,7 +620,8 @@ static PHP_FUNCTION(event_add) return; } - event = ZVAL_TO_EVENT(zevent); + if (!(event = ZVAL_TO_EVENT(zevent))) + RETURN_FALSE; if (!event->base) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to add event without an event base"); @@ -659,7 +666,8 @@ static PHP_FUNCTION(event_set) return; } - event = ZVAL_TO_EVENT(zevent); + if (!(event = ZVAL_TO_EVENT(zevent))) + RETURN_FALSE; if (events & EV_SIGNAL) { /* signal support */ @@ -758,7 +766,8 @@ static PHP_FUNCTION(event_del) return; } - event = ZVAL_TO_EVENT(zevent); + if (!(event = ZVAL_TO_EVENT(zevent))) + RETURN_FALSE; if (!event->base) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to delete event without an event base"); @@ -785,7 +794,8 @@ static PHP_FUNCTION(event_priority_set) return; } - event = ZVAL_TO_EVENT(zevent); + if (!(event = ZVAL_TO_EVENT(zevent))) + RETURN_FALSE; if (!event->base) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set event priority without an event base"); @@ -814,7 +824,8 @@ static PHP_FUNCTION(event_timer_set) return; } - event = ZVAL_TO_EVENT(zevent); + if (!(event = ZVAL_TO_EVENT(zevent))) + RETURN_FALSE; if (!zend_is_callable(zcallback, 0, &func_name)) { php_error_docref(NULL, E_WARNING, "'%s' is not a valid callback", ZSTR_VAL(func_name)); @@ -863,7 +874,8 @@ static PHP_FUNCTION(event_timer_pending) return; } - event= ZVAL_TO_EVENT(zevent); + if (!(event = ZVAL_TO_EVENT(zevent))) + RETURN_FALSE; if (timeout < 0) { ret = event_pending(event->event, EV_TIMEOUT, NULL); @@ -1027,8 +1039,10 @@ static PHP_FUNCTION(event_buffer_base_set) return; } - base = ZVAL_TO_BASE(zbase); - bevent = ZVAL_TO_BEVENT(zbevent); + if (!(base = ZVAL_TO_BASE(zbase))) + RETURN_FALSE; + if (!(bevent = ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; old_base = bevent->base; ret = bufferevent_base_set(base->base, bevent->bevent); @@ -1068,7 +1082,8 @@ static PHP_FUNCTION(event_buffer_priority_set) return; } - bevent = ZVAL_TO_BEVENT(zbevent); + if (!(bevent = ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; if (!bevent->base) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set event priority without an event base"); @@ -1099,7 +1114,8 @@ static PHP_FUNCTION(event_buffer_write) return; } - bevent = ZVAL_TO_BEVENT(zbevent); + if (!(bevent = ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; if (ZEND_NUM_ARGS() < 3 || data_size < 0) { data_size = data_len; @@ -1132,7 +1148,8 @@ static PHP_FUNCTION(event_buffer_read) return; } - bevent= ZVAL_TO_BEVENT(zbevent); + if (!(bevent= ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; if (data_size == 0) { RETURN_EMPTY_STRING(); @@ -1169,7 +1186,8 @@ static PHP_FUNCTION(event_buffer_enable) return; } - bevent = ZVAL_TO_BEVENT(zbevent); + if (!(bevent = ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; ret = bufferevent_enable(bevent->bevent, events); @@ -1193,7 +1211,8 @@ static PHP_FUNCTION(event_buffer_disable) return; } - bevent = ZVAL_TO_BEVENT(zbevent); + if (!(bevent = ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; if (bevent) { ret = bufferevent_disable(bevent->bevent, events); @@ -1218,7 +1237,8 @@ static PHP_FUNCTION(event_buffer_timeout_set) return; } - bevent = ZVAL_TO_BEVENT(zbevent); + if (!(bevent = ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; bufferevent_settimeout(bevent->bevent, read_timeout, write_timeout); } /* }}} */ @@ -1235,7 +1255,8 @@ static PHP_FUNCTION(event_buffer_watermark_set) return; } - bevent = ZVAL_TO_BEVENT(zbevent); + if (!(bevent = ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; bufferevent_setwatermark(bevent->bevent, events, lowmark, highmark); } /* }}} */ @@ -1256,7 +1277,8 @@ static PHP_FUNCTION(event_buffer_fd_set) return; } - bevent= ZVAL_TO_BEVENT(zbevent); + if(!(bevent= ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; if (Z_TYPE_P(zfd) == IS_RESOURCE) { stream = zend_fetch_resource2_ex(zfd, NULL, php_file_le_stream(), php_file_le_pstream()); @@ -1305,7 +1327,8 @@ static PHP_FUNCTION(event_buffer_set_callback) return; } - bevent= ZVAL_TO_BEVENT(zbevent); + if (!(bevent= ZVAL_TO_BEVENT(zbevent))) + RETURN_FALSE; if (Z_TYPE_P(zreadcb) != IS_NULL) { if (!zend_is_callable(zreadcb, 0, &func_name)) { From f6b7a1e53c4df695f0ec8811e14f74fbbb17241e Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Wed, 27 Jan 2016 02:53:18 +0900 Subject: [PATCH 062/103] fixed segfualt when call error callback after calling event_buffer_free --- libevent.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libevent.c b/libevent.c index 5ab74af..5d4af5c 100644 --- a/libevent.c +++ b/libevent.c @@ -318,6 +318,9 @@ static void _php_bufferevent_errorcb(struct bufferevent *be, short what, void *a return; } + if (!bevent->rsrc_id) + return; + ZVAL_COPY(&args[0], bevent->rsrc_id); /* we do refcount-- later in zval_ptr_dtor */ ZVAL_LONG(&args[1], (zend_long)what); From 817b2fedc32624e4f29e777dc049fd7e27d4dcc1 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Wed, 27 Jan 2016 05:41:19 +0900 Subject: [PATCH 063/103] add EVBUFFER_CONNECTED constant for libevent2 linking --- libevent.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libevent.c b/libevent.c index 5d4af5c..dc8c1ac 100644 --- a/libevent.c +++ b/libevent.c @@ -1429,6 +1429,9 @@ static PHP_MINIT_FUNCTION(libevent) REGISTER_LONG_CONSTANT("EVBUFFER_EOF", EVBUFFER_EOF, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("EVBUFFER_ERROR", EVBUFFER_ERROR, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("EVBUFFER_TIMEOUT", EVBUFFER_TIMEOUT, CONST_CS | CONST_PERSISTENT); +#ifdef BEV_EVENT_CONNECTED // for libevent2 + REGISTER_LONG_CONSTANT("EVBUFFER_CONNECTED", BEV_EVENT_CONNECTED, CONST_CS | CONST_PERSISTENT); +#endif return SUCCESS; } From d4beb1becc0d6acdb70e5e26b5a0414259ad8480 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Wed, 27 Jan 2016 05:49:09 +0900 Subject: [PATCH 064/103] fixed indent --- libevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libevent.c b/libevent.c index dc8c1ac..4409423 100644 --- a/libevent.c +++ b/libevent.c @@ -1042,7 +1042,7 @@ static PHP_FUNCTION(event_buffer_base_set) return; } - if (!(base = ZVAL_TO_BASE(zbase))) + if (!(base = ZVAL_TO_BASE(zbase))) RETURN_FALSE; if (!(bevent = ZVAL_TO_BEVENT(zbevent))) RETURN_FALSE; From 317c6c5e825f856c95cb25b47ee2a266dee9f559 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Wed, 27 Jan 2016 11:49:23 +0100 Subject: [PATCH 065/103] use php 7.0 for tests --- .travis.yml | 8 ++++---- scripts/gen_travis_yml.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index f168d51..0809229 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,10 +10,10 @@ addons: env: matrix: - - PHP=master enable_debug=no enable_maintainer_zts=no with_openssl=yes - - PHP=master enable_debug=yes enable_maintainer_zts=no with_openssl=yes - - PHP=master enable_debug=no enable_maintainer_zts=yes with_openssl=yes - - PHP=master enable_debug=yes enable_maintainer_zts=yes with_openssl=yes + - PHP=7.0 enable_debug=no enable_maintainer_zts=no with_openssl=yes + - PHP=7.0 enable_debug=yes enable_maintainer_zts=no with_openssl=yes + - PHP=7.0 enable_debug=no enable_maintainer_zts=yes with_openssl=yes + - PHP=7.0 enable_debug=yes enable_maintainer_zts=yes with_openssl=yes before_script: - make -f travis/pecl/Makefile php diff --git a/scripts/gen_travis_yml.php b/scripts/gen_travis_yml.php index 52438ec..8fa5e40 100644 --- a/scripts/gen_travis_yml.php +++ b/scripts/gen_travis_yml.php @@ -15,7 +15,7 @@ $gen = include __DIR__ . "/../travis/pecl/gen-matrix.php"; $env = $gen([ - "PHP" => ["master"], + "PHP" => ["7.0"], "enable_debug", "enable_maintainer_zts", "with_openssl" => ["yes"] From d92015d3ed9eb120491b0b458334b6796dda6201 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Wed, 27 Jan 2016 19:55:15 +0900 Subject: [PATCH 066/103] remove regacy code --- libevent.c | 76 +++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/libevent.c b/libevent.c index 4409423..e98c4ee 100644 --- a/libevent.c +++ b/libevent.c @@ -37,12 +37,6 @@ # define LIBEVENT_SOCKETS_SUPPORT #endif -#ifndef ZEND_FETCH_RESOURCE_NO_RETURN -# define ZEND_FETCH_RESOURCE_NO_RETURN(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type) \ - (rsrc = (rsrc_type) zend_fetch_resource2(passed_id TSRMLS_CC, default_id, resource_type_name, NULL, 1, resource_type)) -#endif - - #ifdef PHP_WIN32 /* XXX compiling with 2.x on Windows. Luckily the ext code works thanks to the compat exports from the libevent. However it might need to be adapted to the @@ -349,7 +343,7 @@ static PHP_FUNCTION(event_base_new) { php_event_base_t *base; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "") != SUCCESS) { return; } @@ -374,7 +368,7 @@ static PHP_FUNCTION(event_base_reinit) { zval *zbase; php_event_base_t *base; int r = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zbase) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zbase) != SUCCESS) { return; } @@ -398,7 +392,7 @@ static PHP_FUNCTION(event_base_free) zval *zbase; php_event_base_t *base; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zbase) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zbase) != SUCCESS) { return; } @@ -423,7 +417,7 @@ static PHP_FUNCTION(event_base_loop) zend_long flags = 0; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zbase, &flags) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zbase, &flags) != SUCCESS) { return; } @@ -443,7 +437,7 @@ static PHP_FUNCTION(event_base_loopbreak) php_event_base_t *base; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zbase) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zbase) != SUCCESS) { return; } @@ -465,7 +459,7 @@ static PHP_FUNCTION(event_base_loopexit) int ret; zend_long timeout = -1; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zbase, &timeout) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zbase, &timeout) != SUCCESS) { return; } @@ -498,7 +492,7 @@ static PHP_FUNCTION(event_base_set) php_event_t *event; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zevent, &zbase) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &zevent, &zbase) != SUCCESS) { return; } @@ -541,7 +535,7 @@ static PHP_FUNCTION(event_base_priority_init) zend_long npriorities; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zbase, &npriorities) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zbase, &npriorities) != SUCCESS) { return; } @@ -549,7 +543,7 @@ static PHP_FUNCTION(event_base_priority_init) RETURN_FALSE; if (npriorities < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "npriorities cannot be less than zero"); + php_error_docref(NULL, E_WARNING, "npriorities cannot be less than zero"); RETURN_FALSE; } @@ -568,7 +562,7 @@ static PHP_FUNCTION(event_new) { php_event_t *event; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "") != SUCCESS) { return; } @@ -593,7 +587,7 @@ static PHP_FUNCTION(event_free) zval *zevent; php_event_t *event; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zevent) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zevent) != SUCCESS) { return; } @@ -619,7 +613,7 @@ static PHP_FUNCTION(event_add) int ret; zend_long timeout = -1; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zevent, &timeout) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zevent, &timeout) != SUCCESS) { return; } @@ -627,7 +621,7 @@ static PHP_FUNCTION(event_add) RETURN_FALSE; if (!event->base) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to add event without an event base"); + php_error_docref(NULL, E_WARNING, "Unable to add event without an event base"); RETURN_FALSE; } @@ -677,7 +671,7 @@ static PHP_FUNCTION(event_set) convert_to_long_ex(fd); file_desc = Z_LVAL_P(fd); if (file_desc < 0 || file_desc >= NSIG) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid signal passed"); + php_error_docref(NULL, E_WARNING, "invalid signal passed"); RETURN_FALSE; } } else { @@ -693,25 +687,25 @@ static PHP_FUNCTION(event_set) if (php_sock) { file_desc = php_sock->bsd_socket; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource"); + php_error_docref(NULL, E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource"); RETURN_FALSE; } #else - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream resource"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource"); RETURN_FALSE; #endif } } else if (Z_TYPE_P(fd) == IS_LONG) { file_desc = Z_LVAL_P(fd); if (file_desc < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid file descriptor passed"); + php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); RETURN_FALSE; } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); #else - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); #endif RETURN_FALSE; } @@ -773,7 +767,7 @@ static PHP_FUNCTION(event_del) RETURN_FALSE; if (!event->base) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to delete event without an event base"); + php_error_docref(NULL, E_WARNING, "Unable to delete event without an event base"); RETURN_FALSE; } @@ -801,7 +795,7 @@ static PHP_FUNCTION(event_priority_set) RETURN_FALSE; if (!event->base) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set event priority without an event base"); + php_error_docref(NULL, E_WARNING, "Unable to set event priority without an event base"); RETURN_FALSE; } @@ -928,11 +922,11 @@ static PHP_FUNCTION(event_buffer_new) if (php_sock) { fd = php_sock->bsd_socket; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); RETURN_FALSE; } #else - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); RETURN_FALSE; #endif } @@ -940,9 +934,9 @@ static PHP_FUNCTION(event_buffer_new) fd = Z_LVAL_P(zfd); } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); #else - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); #endif RETURN_FALSE; } @@ -960,7 +954,7 @@ static PHP_FUNCTION(event_buffer_new) if (Z_TYPE_P(zwritecb) != IS_NULL) { if (!zend_is_callable(zwritecb, 0, &func_name)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid write callback", ZSTR_VAL(func_name)); + php_error_docref(NULL, E_WARNING, "'%s' is not a valid write callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } @@ -970,7 +964,7 @@ static PHP_FUNCTION(event_buffer_new) } if (!zend_is_callable(zerrorcb, 0, &func_name)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid error callback", ZSTR_VAL(func_name)); + php_error_docref(NULL, E_WARNING, "'%s' is not a valid error callback", ZSTR_VAL(func_name)); zend_string_release(func_name); RETURN_FALSE; } @@ -1089,7 +1083,7 @@ static PHP_FUNCTION(event_buffer_priority_set) RETURN_FALSE; if (!bevent->base) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set event priority without an event base"); + php_error_docref(NULL, E_WARNING, "Unable to set event priority without an event base"); RETURN_FALSE; } @@ -1123,7 +1117,7 @@ static PHP_FUNCTION(event_buffer_write) if (ZEND_NUM_ARGS() < 3 || data_size < 0) { data_size = data_len; } else if (data_size > data_len) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_size out of range"); + php_error_docref(NULL, E_WARNING, "data_size out of range"); RETURN_FALSE; } @@ -1157,7 +1151,7 @@ static PHP_FUNCTION(event_buffer_read) if (data_size == 0) { RETURN_EMPTY_STRING(); } else if (data_size < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_size cannot be less than zero"); + php_error_docref(NULL, E_WARNING, "data_size cannot be less than zero"); RETURN_FALSE; } @@ -1295,11 +1289,11 @@ static PHP_FUNCTION(event_buffer_fd_set) if (php_sock) { fd = php_sock->bsd_socket; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); RETURN_FALSE; } #else - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); RETURN_FALSE; #endif } @@ -1307,9 +1301,9 @@ static PHP_FUNCTION(event_buffer_fd_set) fd = Z_LVAL_P(zfd); } else { #ifdef LIBEVENT_SOCKETS_SUPPORT - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); #else - php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); + php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream resource or a file descriptor of type long"); #endif RETURN_FALSE; } @@ -1326,7 +1320,7 @@ static PHP_FUNCTION(event_buffer_set_callback) zval *zbevent, *zreadcb, *zwritecb, *zerrorcb, *zarg = NULL; zend_string *func_name; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzzz|z", &zbevent, &zreadcb, &zwritecb, &zerrorcb, &zarg) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rzzz|z", &zbevent, &zreadcb, &zwritecb, &zerrorcb, &zarg) != SUCCESS) { return; } From c910b81f6b6f29714be66289857390f3bbef3b93 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Thu, 28 Jan 2016 17:56:18 +0900 Subject: [PATCH 067/103] fixed assert error of libevent001.phpt on enable_debug=yes at issue #6 --- libevent.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libevent.c b/libevent.c index e98c4ee..b86b29b 100644 --- a/libevent.c +++ b/libevent.c @@ -718,7 +718,7 @@ static PHP_FUNCTION(event_set) } zend_string_release(func_name); - zval_addref_p(zcallback); + Z_TRY_ADDREF_P(zcallback); callback = emalloc(sizeof(php_event_callback_t)); ZVAL_COPY(&callback->func, zcallback); @@ -831,7 +831,7 @@ static PHP_FUNCTION(event_timer_set) } zend_string_release(func_name); - zval_addref_p(zcallback); + Z_TRY_ADDREF_P(zcallback); callback = emalloc(sizeof(php_event_callback_t)); ZVAL_COPY(&callback->func, zcallback); From 6fb562277bbf359099638cf51ae6004b656bf414 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Thu, 28 Jan 2016 21:00:36 +0900 Subject: [PATCH 068/103] fixed leak of libevent002.phpt at #6 --- libevent.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libevent.c b/libevent.c index b86b29b..e58fb8c 100644 --- a/libevent.c +++ b/libevent.c @@ -718,8 +718,6 @@ static PHP_FUNCTION(event_set) } zend_string_release(func_name); - Z_TRY_ADDREF_P(zcallback); - callback = emalloc(sizeof(php_event_callback_t)); ZVAL_COPY(&callback->func, zcallback); if(zarg) { @@ -831,8 +829,6 @@ static PHP_FUNCTION(event_timer_set) } zend_string_release(func_name); - Z_TRY_ADDREF_P(zcallback); - callback = emalloc(sizeof(php_event_callback_t)); ZVAL_COPY(&callback->func, zcallback); if (zarg) { From 68a79894365765d535e3e1f634cfa8e5f251438c Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Thu, 28 Jan 2016 23:34:45 +0900 Subject: [PATCH 069/103] fixed #6 libevent003.phpt's leak on enable_debug=on and enable_zts=on --- libevent.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/libevent.c b/libevent.c index e58fb8c..1859aa8 100644 --- a/libevent.c +++ b/libevent.c @@ -126,7 +126,11 @@ ZEND_RSRC_DTOR_FUNC(_php_event_base_dtor) /* {{{ */ return; } php_event_base_t *base = (php_event_base_t*)res->ptr; - event_base_free(base->base); + if (!base) + return; + + if (base->base) + event_base_free(base->base); safe_efree(base); } /* }}} */ @@ -181,16 +185,16 @@ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ if (!bevent) return; + zval_ptr_dtor(&bevent->readcb); + zval_ptr_dtor(&bevent->writecb); + zval_ptr_dtor(&bevent->errorcb); + zval_ptr_dtor(&bevent->arg); + bufferevent_free(bevent->bevent); + if (bevent->base) { base_id = bevent->base->rsrc_id; --bevent->base->events; - zval_ptr_dtor(&bevent->readcb); - zval_ptr_dtor(&bevent->writecb); - zval_ptr_dtor(&bevent->errorcb); - zval_ptr_dtor(&bevent->arg); - bufferevent_free(bevent->bevent); - if (base_id) { zend_list_close(Z_RES_P(base_id)); } From 2774f09d8d362ebb3b218e9d94943f12b64dcc76 Mon Sep 17 00:00:00 2001 From: JoungKyun Kim Date: Fri, 29 Jan 2016 00:16:05 +0900 Subject: [PATCH 070/103] update credit --- CREDITS | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CREDITS b/CREDITS index d341180..20fb624 100644 --- a/CREDITS +++ b/CREDITS @@ -1 +1 @@ -Antony Dovgal, Arnaud Le Blanc, Ioan Chiriac +Antony Dovgal, Arnaud Le Blanc, Ioan Chiriac, JoungKyun Kim diff --git a/README.md b/README.md index 625f86f..742fffb 100644 --- a/README.md +++ b/README.md @@ -105,4 +105,4 @@ event_base_loop($base); ## Credits -Antony Dovgal, Arnaud Le Blanc, Ioan Chiriac +Antony Dovgal, Arnaud Le Blanc, Ioan Chiriac, JoungKyun KIm From d8590027d168fe37bb9782fdbcf946729c588c60 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Wed, 24 Feb 2016 16:10:06 +0100 Subject: [PATCH 071/103] Create libevent004.phpt --- tests/libevent004.phpt | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/libevent004.phpt diff --git a/tests/libevent004.phpt b/tests/libevent004.phpt new file mode 100644 index 0000000..3d9e0bd --- /dev/null +++ b/tests/libevent004.phpt @@ -0,0 +1,31 @@ + --TEST-- +pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/9 +--SKIPIF-- + +--EXPECTF-- +tick From a48491ef2ea98fb9cbca9fdc84c02fb7790e065d Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Wed, 24 Feb 2016 16:14:13 +0100 Subject: [PATCH 072/103] extra space --- tests/libevent004.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/libevent004.phpt b/tests/libevent004.phpt index 3d9e0bd..7e7f261 100644 --- a/tests/libevent004.phpt +++ b/tests/libevent004.phpt @@ -1,4 +1,4 @@ - --TEST-- +--TEST-- pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/9 --SKIPIF-- Date: Fri, 8 Apr 2016 20:26:01 +0200 Subject: [PATCH 073/103] try patch as suggested in https://github.com/expressif/pecl-event-libevent/issues/8 --- libevent.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/libevent.c b/libevent.c index 1859aa8..7db5096 100644 --- a/libevent.c +++ b/libevent.c @@ -99,13 +99,13 @@ typedef struct _php_bufferevent_t { /* {{{ */ /* }}} */ #define ZVAL_TO_BASE(zval) \ - (php_event_base_t *)zend_fetch_resource2_ex(zval, "event base", le_event_base, le_event_base) + (php_event_base_t *)zend_fetch_resource_ex(zval, "event base", le_event_base) #define ZVAL_TO_EVENT(zval) \ - (php_event_t *)zend_fetch_resource2_ex(zval, "event", le_event, le_event) + (php_event_t *)zend_fetch_resource_ex(zval, "event", le_event) #define ZVAL_TO_BEVENT(zval) \ - (php_bufferevent_t *)zend_fetch_resource2_ex(zval, "buffer event", le_bufferevent, le_bufferevent) + (php_bufferevent_t *)zend_fetch_resource_ex(zval, "buffer event", le_bufferevent) /* {{{ internal funcs */ @@ -361,8 +361,9 @@ static PHP_FUNCTION(event_base_new) base->events = 0; - base->rsrc_id = zend_list_insert(base, le_event_base); - ZVAL_COPY_VALUE(return_value, base->rsrc_id); + RETVAL_RES(zend_register_resource(base, le_event_base)); + base->rsrc_id = return_value; + Z_TRY_ADDREF_P(return_value); } /* }}} */ @@ -408,7 +409,7 @@ static PHP_FUNCTION(event_base_free) RETURN_FALSE; } - zend_list_close(Z_RES_P(base->rsrc_id)); + zend_list_close(Z_RES_P(zbase)); } /* }}} */ @@ -579,8 +580,9 @@ static PHP_FUNCTION(event_new) ZVAL_NULL(&event->stream_id); TSRMLS_SET_CTX(event->thread_ctx); - event->rsrc_id = zend_list_insert(event, le_event); - ZVAL_COPY_VALUE(return_value, event->rsrc_id); + RETVAL_RES(zend_register_resource(event, le_event)); + event->rsrc_id = return_value; + Z_TRY_ADDREF_P(return_value); } /* }}} */ @@ -604,7 +606,7 @@ static PHP_FUNCTION(event_free) event->base = NULL; } event_del (event->event); - zend_list_close(Z_RES_P(event->rsrc_id)); + zend_list_close(Z_RES_P(zevent)); } /* }}} */ @@ -996,8 +998,9 @@ static PHP_FUNCTION(event_buffer_new) TSRMLS_SET_CTX(bevent->thread_ctx); - bevent->rsrc_id = zend_list_insert(bevent, le_bufferevent); - ZVAL_COPY_VALUE(return_value, bevent->rsrc_id); + RETVAL_RES(zend_register_resource(bevent, le_bufferevent)); + bevent->rsrc_id = return_value; + Z_TRY_ADDREF_P(return_value); } /* }}} */ @@ -1019,7 +1022,7 @@ static PHP_FUNCTION(event_buffer_free) --bevent->base->events; bevent->base = NULL; } - zend_list_close(Z_RES_P(bevent->rsrc_id)); + zend_list_close(Z_RES_P(zbevent)); } /* }}} */ From 6887d83fcb315d5dfb6d27e92bc2e979bea69169 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Fri, 8 Apr 2016 21:41:17 +0200 Subject: [PATCH 074/103] remove old recursive trick in_free & free stream_id --- libevent.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/libevent.c b/libevent.c index 7db5096..0f1adba 100644 --- a/libevent.c +++ b/libevent.c @@ -80,7 +80,6 @@ typedef struct _php_event_t { /* {{{ */ #ifdef ZTS void ***thread_ctx; #endif - int in_free; } php_event_t; /* }}} */ @@ -146,13 +145,6 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ if (!event) return; - if (event->in_free) { - safe_efree(event); - return; - } - - event->in_free = 1; - if (event->base) { base_id = event->base->rsrc_id; --event->base->events; @@ -160,9 +152,11 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ if (Z_TYPE_P(&event->stream_id) != IS_NULL) { zend_list_close(Z_RES_P(&event->stream_id)); } - event_del(event->event); + zval_ptr_dtor(&event->stream_id); _php_event_callback_free(event->callback); + event_del(event->event); + safe_efree(event->event); safe_efree(event); @@ -361,9 +355,9 @@ static PHP_FUNCTION(event_base_new) base->events = 0; - RETVAL_RES(zend_register_resource(base, le_event_base)); - base->rsrc_id = return_value; + ZVAL_RES(return_value, zend_register_resource(base, le_event_base)); Z_TRY_ADDREF_P(return_value); + base->rsrc_id = return_value; } /* }}} */ @@ -576,13 +570,12 @@ static PHP_FUNCTION(event_new) event->rsrc_id = NULL; event->callback = NULL; event->base = NULL; - event->in_free = 0; ZVAL_NULL(&event->stream_id); TSRMLS_SET_CTX(event->thread_ctx); - RETVAL_RES(zend_register_resource(event, le_event)); - event->rsrc_id = return_value; + ZVAL_RES(return_value, zend_register_resource(event, le_event)); Z_TRY_ADDREF_P(return_value); + event->rsrc_id = return_value; } /* }}} */ @@ -600,13 +593,14 @@ static PHP_FUNCTION(event_free) if (!(event = ZVAL_TO_EVENT(zevent))) return; - event->in_free = 1; if (event->base) { --event->base->events; event->base = NULL; } + event_del (event->event); - zend_list_close(Z_RES_P(zevent)); + zend_list_close(Z_RES_P(event->rsrc_id)); + } /* }}} */ @@ -998,9 +992,9 @@ static PHP_FUNCTION(event_buffer_new) TSRMLS_SET_CTX(bevent->thread_ctx); - RETVAL_RES(zend_register_resource(bevent, le_bufferevent)); - bevent->rsrc_id = return_value; + ZVAL_RES(return_value, zend_register_resource(bevent, le_bufferevent)); Z_TRY_ADDREF_P(return_value); + bevent->rsrc_id = return_value; } /* }}} */ From ac255b86138de94d53388487e52d87fa9b92e278 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Fri, 8 Apr 2016 21:49:44 +0200 Subject: [PATCH 075/103] fix proposed patch at https://github.com/expressif/pecl-event-libevent/issues/8 (introduced memory leaks) --- libevent.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/libevent.c b/libevent.c index 0f1adba..1578ca4 100644 --- a/libevent.c +++ b/libevent.c @@ -98,13 +98,13 @@ typedef struct _php_bufferevent_t { /* {{{ */ /* }}} */ #define ZVAL_TO_BASE(zval) \ - (php_event_base_t *)zend_fetch_resource_ex(zval, "event base", le_event_base) + (php_event_base_t *)zend_fetch_resource2_ex(zval, "event base", le_event_base, le_event_base) #define ZVAL_TO_EVENT(zval) \ - (php_event_t *)zend_fetch_resource_ex(zval, "event", le_event) + (php_event_t *)zend_fetch_resource2_ex(zval, "event", le_event, le_event) #define ZVAL_TO_BEVENT(zval) \ - (php_bufferevent_t *)zend_fetch_resource_ex(zval, "buffer event", le_bufferevent) + (php_bufferevent_t *)zend_fetch_resource2_ex(zval, "buffer event", le_bufferevent, le_bufferevent) /* {{{ internal funcs */ @@ -355,9 +355,8 @@ static PHP_FUNCTION(event_base_new) base->events = 0; - ZVAL_RES(return_value, zend_register_resource(base, le_event_base)); - Z_TRY_ADDREF_P(return_value); - base->rsrc_id = return_value; + base->rsrc_id = zend_list_insert(base, le_event_base); + ZVAL_COPY_VALUE(return_value, base->rsrc_id); } /* }}} */ @@ -403,7 +402,7 @@ static PHP_FUNCTION(event_base_free) RETURN_FALSE; } - zend_list_close(Z_RES_P(zbase)); + zend_list_close(Z_RES_P(base->rsrc_id)); } /* }}} */ @@ -573,9 +572,8 @@ static PHP_FUNCTION(event_new) ZVAL_NULL(&event->stream_id); TSRMLS_SET_CTX(event->thread_ctx); - ZVAL_RES(return_value, zend_register_resource(event, le_event)); - Z_TRY_ADDREF_P(return_value); - event->rsrc_id = return_value; + event->rsrc_id = zend_list_insert(event, le_event); + ZVAL_COPY_VALUE(return_value, event->rsrc_id); } /* }}} */ @@ -992,9 +990,8 @@ static PHP_FUNCTION(event_buffer_new) TSRMLS_SET_CTX(bevent->thread_ctx); - ZVAL_RES(return_value, zend_register_resource(bevent, le_bufferevent)); - Z_TRY_ADDREF_P(return_value); - bevent->rsrc_id = return_value; + bevent->rsrc_id = zend_list_insert(bevent, le_bufferevent); + ZVAL_COPY_VALUE(return_value, bevent->rsrc_id); } /* }}} */ @@ -1016,7 +1013,7 @@ static PHP_FUNCTION(event_buffer_free) --bevent->base->events; bevent->base = NULL; } - zend_list_close(Z_RES_P(zbevent)); + zend_list_close(Z_RES_P(bevent->rsrc_id)); } /* }}} */ From 842d53b9023b30a0c6416b32d7530b2acef9e7f6 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 9 Apr 2016 13:36:15 +0200 Subject: [PATCH 076/103] add a new test --- tests/libevent005.phpt | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/libevent005.phpt diff --git a/tests/libevent005.phpt b/tests/libevent005.phpt new file mode 100644 index 0000000..5085f64 --- /dev/null +++ b/tests/libevent005.phpt @@ -0,0 +1,40 @@ +--TEST-- +pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/8 +--SKIPIF-- + +--EXPECTF-- +ticktick From 9e72744ce6224beafc7b54ce2a3a990f1c552a5a Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sat, 9 Apr 2016 13:37:56 +0200 Subject: [PATCH 077/103] fix - descrease refcount instead of closing the base @ event & buffer destruction --- libevent.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libevent.c b/libevent.c index 1578ca4..80751cf 100644 --- a/libevent.c +++ b/libevent.c @@ -155,13 +155,14 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ zval_ptr_dtor(&event->stream_id); _php_event_callback_free(event->callback); + + event_del(event->event); - safe_efree(event->event); safe_efree(event); if (base_id) { - zend_list_close(Z_RES_P(base_id)); + zend_list_delete(Z_RES_P(base_id)); } } /* }}} */ @@ -190,7 +191,7 @@ ZEND_RSRC_DTOR_FUNC(_php_bufferevent_dtor) /* {{{ */ --bevent->base->events; if (base_id) { - zend_list_close(Z_RES_P(base_id)); + zend_list_delete(Z_RES_P(base_id)); } } From c52e9938cd11c4d31cbaa2b7d971946407c4014b Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Wed, 8 Mar 2017 21:41:52 +0100 Subject: [PATCH 078/103] https://github.com/expressif/pecl-event-libevent/issues/11 add a test to reproduce the error --- tests/libevent006.phpt | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/libevent006.phpt diff --git a/tests/libevent006.phpt b/tests/libevent006.phpt new file mode 100644 index 0000000..e7d1993 --- /dev/null +++ b/tests/libevent006.phpt @@ -0,0 +1,57 @@ +--TEST-- +pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/8 +--SKIPIF-- + +--EXPECTF-- +bool(true) +bool(true) +bool(true) +int(0) +string(1) "0" +bool(true) +bool(true) +bool(true) +bool(true) +int(0) +string(1) "1" +bool(true) From b27f21800a00e86d68da5ebbc1df1e94531e725e Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Thu, 9 Mar 2017 21:46:07 +0100 Subject: [PATCH 079/103] fix output --- tests/libevent006.phpt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/libevent006.phpt b/tests/libevent006.phpt index e7d1993..e51c42b 100644 --- a/tests/libevent006.phpt +++ b/tests/libevent006.phpt @@ -31,6 +31,7 @@ var_dump(event_base_set($ev1, $base)); var_dump(event_add($ev1)); var_dump(event_base_loop($base, EVLOOP_ONCE)); var_dump(event_del($ev1)); +unset($ev1); // second event reader $ev2 = event_new(); @@ -39,7 +40,7 @@ var_dump(event_base_set($ev2, $base)); var_dump(event_add($ev2)); var_dump(event_base_loop($base, EVLOOP_ONCE)); var_dump(event_del($ev2)); - +unset($ev2); ?> --EXPECTF-- From 85d51c27d1da46aab85f501233f927a3c25cc38f Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Thu, 9 Mar 2017 21:47:47 +0100 Subject: [PATCH 080/103] https://github.com/expressif/pecl-event-libevent/issues/14 reproduce the error --- tests/libevent007.phpt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/libevent007.phpt diff --git a/tests/libevent007.phpt b/tests/libevent007.phpt new file mode 100644 index 0000000..76c72b8 --- /dev/null +++ b/tests/libevent007.phpt @@ -0,0 +1,20 @@ +--TEST-- +pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/8 +--SKIPIF-- + +--EXPECTF-- +bool(false) +int(0) From a4a0bf5206dcb0e6d935225abdae11ad1b0df302 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Thu, 9 Mar 2017 22:03:52 +0100 Subject: [PATCH 081/103] https://github.com/expressif/pecl-event-libevent/issues/14 use Z_RES_P instead of Z_LVAL_P --- libevent.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libevent.c b/libevent.c index 80751cf..ed0fd98 100644 --- a/libevent.c +++ b/libevent.c @@ -695,8 +695,8 @@ static PHP_FUNCTION(event_set) #endif } } else if (Z_TYPE_P(fd) == IS_LONG) { - file_desc = Z_LVAL_P(fd); - if (file_desc < 0) { + file_desc = Z_RES_P(fd); + if (!file_desc) { php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); RETURN_FALSE; } From 6d4b59e1d9025418864b3fa1e06671f6cdc0235a Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Thu, 9 Mar 2017 22:15:00 +0100 Subject: [PATCH 082/103] fix output --- tests/libevent006.phpt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/libevent006.phpt b/tests/libevent006.phpt index e51c42b..8806d33 100644 --- a/tests/libevent006.phpt +++ b/tests/libevent006.phpt @@ -47,12 +47,12 @@ unset($ev2); bool(true) bool(true) bool(true) -int(0) string(1) "0" +int(0) bool(true) bool(true) bool(true) bool(true) -int(0) string(1) "1" +int(0) bool(true) From f7ae0d051579ddb750c951dec2e595462dc24070 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Thu, 9 Mar 2017 22:15:30 +0100 Subject: [PATCH 083/103] add php tag --- tests/libevent007.phpt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/libevent007.phpt b/tests/libevent007.phpt index 76c72b8..5059e9a 100644 --- a/tests/libevent007.phpt +++ b/tests/libevent007.phpt @@ -4,6 +4,7 @@ pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/8 Date: Thu, 9 Mar 2017 22:24:41 +0100 Subject: [PATCH 084/103] fix output test --- tests/libevent007.phpt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/libevent007.phpt b/tests/libevent007.phpt index 5059e9a..2f6cda4 100644 --- a/tests/libevent007.phpt +++ b/tests/libevent007.phpt @@ -1,5 +1,5 @@ --TEST-- -pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/8 +pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/14 --SKIPIF-- --EXPECTF-- bool(false) -int(0) +bool(false) +int(1) From 1fc4ff3e1c5528b7861f040b5fd0d4609619e682 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Thu, 9 Mar 2017 22:28:22 +0100 Subject: [PATCH 085/103] https://github.com/expressif/pecl-event-libevent/issues/14 replace Z_LVAL_P with Z_RES_P on any ressource --- libevent.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libevent.c b/libevent.c index ed0fd98..6b501b2 100644 --- a/libevent.c +++ b/libevent.c @@ -668,8 +668,8 @@ static PHP_FUNCTION(event_set) if (events & EV_SIGNAL) { /* signal support */ convert_to_long_ex(fd); - file_desc = Z_LVAL_P(fd); - if (file_desc < 0 || file_desc >= NSIG) { + file_desc = Z_RES_P(fd); + if (!file_desc) { php_error_docref(NULL, E_WARNING, "invalid signal passed"); RETURN_FALSE; } @@ -926,7 +926,11 @@ static PHP_FUNCTION(event_buffer_new) #endif } } else if (Z_TYPE_P(zfd) == IS_LONG) { - fd = Z_LVAL_P(zfd); + fd = Z_RES_P(zfd); + if (!fd) { + php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); + RETURN_FALSE; + } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); @@ -1293,7 +1297,11 @@ static PHP_FUNCTION(event_buffer_fd_set) #endif } } else if (Z_TYPE_P(zfd) == IS_LONG) { - fd = Z_LVAL_P(zfd); + fd = Z_RES_P(zfd); + if (!fd) { + php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); + RETURN_FALSE; + } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); From 79b99a058d064683bfdb5244598f1efbf20b3f22 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Thu, 9 Mar 2017 22:37:09 +0100 Subject: [PATCH 086/103] fix output / warn --- tests/libevent007.phpt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/libevent007.phpt b/tests/libevent007.phpt index 2f6cda4..0724154 100644 --- a/tests/libevent007.phpt +++ b/tests/libevent007.phpt @@ -20,3 +20,4 @@ var_dump(event_base_loop($base)); bool(false) bool(false) int(1) +[warn] event_del: event has no event_base set. From 05900f3b8d70deb424b3193a4eb850062f146638 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Thu, 9 Mar 2017 22:42:24 +0100 Subject: [PATCH 087/103] fix https://github.com/expressif/pecl-event-libevent/issues/11 issue with GC --- libevent.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libevent.c b/libevent.c index 6b501b2..c7d5103 100644 --- a/libevent.c +++ b/libevent.c @@ -358,6 +358,7 @@ static PHP_FUNCTION(event_base_new) base->rsrc_id = zend_list_insert(base, le_event_base); ZVAL_COPY_VALUE(return_value, base->rsrc_id); + Z_ADDREF_P(base->rsrc_id); } /* }}} */ @@ -513,7 +514,7 @@ static PHP_FUNCTION(event_base_set) if (old_base) { --old_base->events; if (old_base->rsrc_id != NULL) { - zend_list_close(Z_RES_P(old_base->rsrc_id)); + zend_list_delete(Z_RES_P(old_base->rsrc_id)); } } } @@ -594,6 +595,9 @@ static PHP_FUNCTION(event_free) if (event->base) { --event->base->events; + if (event->base->rsrc_id) { + zend_list_delete(Z_RES_P(event->base->rsrc_id)); + } event->base = NULL; } @@ -668,8 +672,8 @@ static PHP_FUNCTION(event_set) if (events & EV_SIGNAL) { /* signal support */ convert_to_long_ex(fd); - file_desc = Z_RES_P(fd); - if (!file_desc) { + file_desc = Z_LVAL_P(fd); + if (file_desc < 0 || file_desc >= NSIG) { php_error_docref(NULL, E_WARNING, "invalid signal passed"); RETURN_FALSE; } @@ -1016,6 +1020,9 @@ static PHP_FUNCTION(event_buffer_free) if (bevent->base) { --bevent->base->events; + if (bevent->base->rsrc_id) { + zend_list_delete(Z_RES_P(bevent->base->rsrc_id)); + } bevent->base = NULL; } zend_list_close(Z_RES_P(bevent->rsrc_id)); @@ -1053,7 +1060,7 @@ static PHP_FUNCTION(event_buffer_base_set) if (old_base) { --old_base->events; if (old_base->rsrc_id) { - zend_list_close(Z_RES_P(old_base->rsrc_id)); + zend_list_delete(Z_RES_P(old_base->rsrc_id)); } } } From 169a036fce899909b262d134565e93fc713e5f79 Mon Sep 17 00:00:00 2001 From: Jan-E Date: Sat, 11 Mar 2017 04:42:34 +0100 Subject: [PATCH 088/103] Windows: add dependency on ext/sockets --- config.w32 | 1 + 1 file changed, 1 insertion(+) diff --git a/config.w32 b/config.w32 index e431572..d81a19b 100644 --- a/config.w32 +++ b/config.w32 @@ -4,6 +4,7 @@ if (PHP_LIBEVENT != "no") { if (CHECK_HEADER_ADD_INCLUDE("event2/event.h", "CFLAGS_LIBEVENT", PHP_PHP_BUILD + "\\include;" + PHP_LIBEVENT) && CHECK_LIB("libevent.lib", "libevent", PHP_PHP_BUILD + "\\lib;" + PHP_LIBEVENT)) { + ADD_EXTENSION_DEP('libevent', 'sockets'); EXTENSION('libevent', 'libevent.c'); AC_DEFINE('HAVE_LIBEVENT', 1); } else { From 48bdb371de8676bcf4950343a371d1cc1d9f66cc Mon Sep 17 00:00:00 2001 From: sm2017 Date: Sun, 12 Mar 2017 12:12:19 +0330 Subject: [PATCH 089/103] to reproduce the error --- tests/libevent006.phpt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/libevent006.phpt b/tests/libevent006.phpt index 8806d33..38e7019 100644 --- a/tests/libevent006.phpt +++ b/tests/libevent006.phpt @@ -31,6 +31,7 @@ var_dump(event_base_set($ev1, $base)); var_dump(event_add($ev1)); var_dump(event_base_loop($base, EVLOOP_ONCE)); var_dump(event_del($ev1)); +event_free($ev1); unset($ev1); // second event reader From 2c45825c578b1a642625a996963fe789f0ebdcb7 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 12 Mar 2017 09:53:20 +0100 Subject: [PATCH 090/103] fix stream closing when calling event_free https://github.com/expressif/pecl-event-libevent/issues/11 --- libevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libevent.c b/libevent.c index c7d5103..5c6f9a9 100644 --- a/libevent.c +++ b/libevent.c @@ -602,7 +602,7 @@ static PHP_FUNCTION(event_free) } event_del (event->event); - zend_list_close(Z_RES_P(event->rsrc_id)); + zend_list_delete(Z_RES_P(event->rsrc_id)); } /* }}} */ From 34f70c6acce2620c8402255d9460cd17fda36e12 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 12 Mar 2017 10:04:16 +0100 Subject: [PATCH 091/103] Avoid closing the stream on event destructor https://github.com/expressif/pecl-event-libevent/issues/11 --- libevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libevent.c b/libevent.c index 5c6f9a9..327757d 100644 --- a/libevent.c +++ b/libevent.c @@ -150,7 +150,7 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ --event->base->events; } if (Z_TYPE_P(&event->stream_id) != IS_NULL) { - zend_list_close(Z_RES_P(&event->stream_id)); + zend_list_delete(Z_RES_P(&event->stream_id)); } zval_ptr_dtor(&event->stream_id); From 2a41881ac21ecd745a23188721a25c56b92e3b76 Mon Sep 17 00:00:00 2001 From: Jan-E Date: Sun, 12 Mar 2017 10:46:12 +0100 Subject: [PATCH 092/103] Add --disable-libevent-sockets config option --- config.w32 | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/config.w32 b/config.w32 index d81a19b..5175c9d 100644 --- a/config.w32 +++ b/config.w32 @@ -1,12 +1,26 @@ ARG_WITH("libevent", "libevent support", "no"); +ARG_ENABLE("libevent-sockets", "whether to enable libevent sockets support", "yes"); if (PHP_LIBEVENT != "no") { if (CHECK_HEADER_ADD_INCLUDE("event2/event.h", "CFLAGS_LIBEVENT", PHP_PHP_BUILD + "\\include;" + PHP_LIBEVENT) && CHECK_LIB("libevent.lib", "libevent", PHP_PHP_BUILD + "\\lib;" + PHP_LIBEVENT)) { - ADD_EXTENSION_DEP('libevent', 'sockets'); EXTENSION('libevent', 'libevent.c'); AC_DEFINE('HAVE_LIBEVENT', 1); + if (PHP_LIBEVENT_SOCKETS != "no") { + ADD_EXTENSION_DEP("libevent", "sockets"); + ADD_FLAG("CFLAGS_LIBEVENT", ' /D HAVE_SOCKETS '); + MESSAGE("\tlibevent sockets support enabled"); + } else { + if (typeof(PHP_SOCKETS) != "undefined" && PHP_SOCKETS != "no") { + WARNING("libevent sockets support enabled; sockets extension found"); + ADD_EXTENSION_DEP("libevent", "sockets"); + ADD_FLAG("CFLAGS_LIBEVENT", ' /D HAVE_SOCKETS '); + MESSAGE("\tlibevent sockets support enabled"); + } else { + MESSAGE("\tlibevent sockets support not enabled"); + } + } } else { WARNING("libevent not enabled; libraries and headers not found"); } From e81d9df5abdabfbafc863f38fc5eabcbe257626c Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 12 Mar 2017 10:50:19 +0100 Subject: [PATCH 093/103] https://github.com/expressif/pecl-event-libevent/issues/11 - fix stream destruction --- libevent.c | 1 - tests/libevent006.phpt | 10 +++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/libevent.c b/libevent.c index 327757d..940ea58 100644 --- a/libevent.c +++ b/libevent.c @@ -152,7 +152,6 @@ ZEND_RSRC_DTOR_FUNC(_php_event_dtor) /* {{{ */ if (Z_TYPE_P(&event->stream_id) != IS_NULL) { zend_list_delete(Z_RES_P(&event->stream_id)); } - zval_ptr_dtor(&event->stream_id); _php_event_callback_free(event->callback); diff --git a/tests/libevent006.phpt b/tests/libevent006.phpt index 38e7019..d56de76 100644 --- a/tests/libevent006.phpt +++ b/tests/libevent006.phpt @@ -31,16 +31,19 @@ var_dump(event_base_set($ev1, $base)); var_dump(event_add($ev1)); var_dump(event_base_loop($base, EVLOOP_ONCE)); var_dump(event_del($ev1)); -event_free($ev1); +var_dump(event_free($ev1)); unset($ev1); +var_dump($fd); // second event reader +echo "\n2nd try\n"; $ev2 = event_new(); var_dump(event_set($ev2, $fd, EV_READ | EV_PERSIST, "foo", array($ev2, $base))); var_dump(event_base_set($ev2, $base)); var_dump(event_add($ev2)); var_dump(event_base_loop($base, EVLOOP_ONCE)); var_dump(event_del($ev2)); +var_dump(event_free($ev2)); unset($ev2); ?> @@ -51,9 +54,14 @@ bool(true) string(1) "0" int(0) bool(true) +NULL +resource(6) of type (stream) + +2n try bool(true) bool(true) bool(true) string(1) "1" int(0) bool(true) +NULL From 0f53bbfce019e55b34dee4dc30dfbb0f4d2b9023 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Sun, 12 Mar 2017 10:52:49 +0100 Subject: [PATCH 094/103] ouch, typo --- tests/libevent006.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/libevent006.phpt b/tests/libevent006.phpt index d56de76..ea83b8d 100644 --- a/tests/libevent006.phpt +++ b/tests/libevent006.phpt @@ -57,7 +57,7 @@ bool(true) NULL resource(6) of type (stream) -2n try +2nd try bool(true) bool(true) bool(true) From 629b7334e20271f349a01de5b4f091887be38bf6 Mon Sep 17 00:00:00 2001 From: Jan-E Date: Sun, 12 Mar 2017 10:54:09 +0100 Subject: [PATCH 095/103] REmove --disable-libevent-sockets, check php_sockets directly --- config.w32 | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/config.w32 b/config.w32 index 5175c9d..23edb1a 100644 --- a/config.w32 +++ b/config.w32 @@ -1,5 +1,4 @@ ARG_WITH("libevent", "libevent support", "no"); -ARG_ENABLE("libevent-sockets", "whether to enable libevent sockets support", "yes"); if (PHP_LIBEVENT != "no") { if (CHECK_HEADER_ADD_INCLUDE("event2/event.h", "CFLAGS_LIBEVENT", PHP_PHP_BUILD + "\\include;" + PHP_LIBEVENT) @@ -7,19 +6,12 @@ if (PHP_LIBEVENT != "no") { { EXTENSION('libevent', 'libevent.c'); AC_DEFINE('HAVE_LIBEVENT', 1); - if (PHP_LIBEVENT_SOCKETS != "no") { + if (typeof(PHP_SOCKETS) != "undefined" && PHP_SOCKETS != "no") { ADD_EXTENSION_DEP("libevent", "sockets"); ADD_FLAG("CFLAGS_LIBEVENT", ' /D HAVE_SOCKETS '); MESSAGE("\tlibevent sockets support enabled"); } else { - if (typeof(PHP_SOCKETS) != "undefined" && PHP_SOCKETS != "no") { - WARNING("libevent sockets support enabled; sockets extension found"); - ADD_EXTENSION_DEP("libevent", "sockets"); - ADD_FLAG("CFLAGS_LIBEVENT", ' /D HAVE_SOCKETS '); - MESSAGE("\tlibevent sockets support enabled"); - } else { - MESSAGE("\tlibevent sockets support not enabled"); - } + MESSAGE("\tlibevent sockets support not enabled"); } } else { WARNING("libevent not enabled; libraries and headers not found"); From 9d3152b6348043669e647a59e5d546c96953fa85 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Mon, 13 Mar 2017 10:52:51 +0100 Subject: [PATCH 096/103] Update README.md --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 742fffb..9904f4c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,24 @@ This extension requires [» libevent](http://libevent.org/) library. Minimal req Information for installing this PECL extension may be found in the manual chapter titled [Installation of PECL extensions](http://php.net/manual/en/install.pecl.php). Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: » http://pecl.php.net/package/libevent. +## Compile from sources + +Pre-requisites (only for linux) : + +```sh +sudo apt-get install php7.0-dev gcc make libevent-dev +``` + +Compile sources : + +```sh +git clone https://github.com/expressif/pecl-event-libevent.git +cd pecl-event-libevent +phpize +./configure +make && sudo make install +``` + ## Predefined Constants The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime. From 995728b73b0a9eaf94900e80de6a195fc0768210 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Fri, 24 Mar 2017 21:43:31 +0100 Subject: [PATCH 097/103] https://github.com/expressif/pecl-event-libevent/issues/19 add test --- tests/libevent008.phpt | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/libevent008.phpt diff --git a/tests/libevent008.phpt b/tests/libevent008.phpt new file mode 100644 index 0000000..8c60a65 --- /dev/null +++ b/tests/libevent008.phpt @@ -0,0 +1,38 @@ +--TEST-- +pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/19 +--SKIPIF-- + +--EXPECTF-- +resource(5) of type (stream) +int(0) +string(0) "" +bool(true) +bool(true) +bool(true) +resource(5) of type (stream) +int(0) From 6841fa23ac583ac7d61af4a64e5668846ef0d4d0 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Fri, 24 Mar 2017 21:44:06 +0100 Subject: [PATCH 098/103] https://github.com/expressif/pecl-event-libevent/issues/19 fix into to resource cast --- libevent.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/libevent.c b/libevent.c index 940ea58..f6a64ed 100644 --- a/libevent.c +++ b/libevent.c @@ -677,6 +677,15 @@ static PHP_FUNCTION(event_set) RETURN_FALSE; } } else { + + if (Z_TYPE_P(fd) == IS_LONG) { + fd = zend_hash_index_find(&EG(regular_list), Z_RES_P(fd)); + if (!fd) { + php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); + RETURN_FALSE; + } + } + if (Z_TYPE_P(fd) == IS_RESOURCE) { stream = zend_fetch_resource2_ex(fd, NULL, php_file_le_stream(), php_file_le_pstream()); if (stream) { @@ -697,12 +706,6 @@ static PHP_FUNCTION(event_set) RETURN_FALSE; #endif } - } else if (Z_TYPE_P(fd) == IS_LONG) { - file_desc = Z_RES_P(fd); - if (!file_desc) { - php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); - RETURN_FALSE; - } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); @@ -907,6 +910,14 @@ static PHP_FUNCTION(event_buffer_new) if (zend_parse_parameters(ZEND_NUM_ARGS(), "zzzz|z", &zfd, &zreadcb, &zwritecb, &zerrorcb, &zarg) != SUCCESS) { return; } + + if (Z_TYPE_P(zfd) == IS_LONG) { + zfd = zend_hash_index_find(&EG(regular_list), Z_RES_P(zfd)); + if (!zfd) { + php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); + RETURN_FALSE; + } + } if (Z_TYPE_P(zfd) == IS_RESOURCE) { stream = zend_fetch_resource2_ex(zfd, NULL, php_file_le_stream(), php_file_le_pstream()); @@ -928,12 +939,6 @@ static PHP_FUNCTION(event_buffer_new) RETURN_FALSE; #endif } - } else if (Z_TYPE_P(zfd) == IS_LONG) { - fd = Z_RES_P(zfd); - if (!fd) { - php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); - RETURN_FALSE; - } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); @@ -1282,6 +1287,14 @@ static PHP_FUNCTION(event_buffer_fd_set) if(!(bevent= ZVAL_TO_BEVENT(zbevent))) RETURN_FALSE; + if (Z_TYPE_P(zfd) == IS_LONG) { + zfd = zend_hash_index_find(&EG(regular_list), Z_RES_P(zfd)); + if (!zfd) { + php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); + RETURN_FALSE; + } + } + if (Z_TYPE_P(zfd) == IS_RESOURCE) { stream = zend_fetch_resource2_ex(zfd, NULL, php_file_le_stream(), php_file_le_pstream()); if (stream) { @@ -1302,12 +1315,6 @@ static PHP_FUNCTION(event_buffer_fd_set) RETURN_FALSE; #endif } - } else if (Z_TYPE_P(zfd) == IS_LONG) { - fd = Z_RES_P(zfd); - if (!fd) { - php_error_docref(NULL, E_WARNING, "invalid file descriptor passed"); - RETURN_FALSE; - } } else { #ifdef LIBEVENT_SOCKETS_SUPPORT php_error_docref(NULL, E_WARNING, "fd argument must be valid PHP stream or socket resource or a file descriptor of type long"); From 74e5f5dcce39e7bbd698ae97bdbfdec8cea890fb Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Fri, 24 Mar 2017 21:47:41 +0100 Subject: [PATCH 099/103] fix test --- tests/libevent008.phpt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/libevent008.phpt b/tests/libevent008.phpt index 8c60a65..8641f41 100644 --- a/tests/libevent008.phpt +++ b/tests/libevent008.phpt @@ -4,6 +4,7 @@ pecl/libevent - bug https://github.com/expressif/pecl-event-libevent/issues/19 Date: Fri, 24 Mar 2017 22:17:23 +0100 Subject: [PATCH 100/103] https://github.com/expressif/pecl-event-libevent/issues/19 test with timeout flag --- tests/libevent007.phpt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/libevent007.phpt b/tests/libevent007.phpt index 0724154..0e49966 100644 --- a/tests/libevent007.phpt +++ b/tests/libevent007.phpt @@ -9,7 +9,7 @@ $base = event_base_new(); $event = event_new(); var_dump(@event_set($event, 0, EV_TIMEOUT, function() { -echo "function called"; + var_dump("TIMEOUT"); })); event_base_set($event, $base); @@ -17,7 +17,7 @@ var_dump(@event_add($event, 5000000)); var_dump(event_base_loop($base)); ?> --EXPECTF-- -bool(false) -bool(false) +bool(true) +bool(true) +string(7) "TIMEOUT" int(1) -[warn] event_del: event has no event_base set. From e42c9c5380008859d1f8cefaa120a79c35fa2405 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Fri, 24 Mar 2017 22:17:50 +0100 Subject: [PATCH 101/103] https://github.com/expressif/pecl-event-libevent/issues/19 handle timeout flag --- libevent.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libevent.c b/libevent.c index f6a64ed..f9b5702 100644 --- a/libevent.c +++ b/libevent.c @@ -668,7 +668,10 @@ static PHP_FUNCTION(event_set) if (!(event = ZVAL_TO_EVENT(zevent))) RETURN_FALSE; - if (events & EV_SIGNAL) { + if (events & EV_TIMEOUT) { + file_desc = -1; + fd = 0; + } else if (events & EV_SIGNAL) { /* signal support */ convert_to_long_ex(fd); file_desc = Z_LVAL_P(fd); @@ -735,7 +738,7 @@ static PHP_FUNCTION(event_set) event->callback = callback; if (events & EV_SIGNAL) { ZVAL_NULL(&event->stream_id); - } else { + } else if (fd) { ZVAL_COPY(&event->stream_id, fd); } From 5a726cee95ef9760fe81a60e595e87655f324594 Mon Sep 17 00:00:00 2001 From: Ioan CHIRIAC Date: Fri, 24 Mar 2017 22:52:11 +0100 Subject: [PATCH 102/103] handle exactly the same event_set as event_timer_set --- libevent.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/libevent.c b/libevent.c index f9b5702..8956e7d 100644 --- a/libevent.c +++ b/libevent.c @@ -671,6 +671,7 @@ static PHP_FUNCTION(event_set) if (events & EV_TIMEOUT) { file_desc = -1; fd = 0; + events = 0; } else if (events & EV_SIGNAL) { /* signal support */ convert_to_long_ex(fd); @@ -736,9 +737,15 @@ static PHP_FUNCTION(event_set) old_callback = event->callback; event->callback = callback; - if (events & EV_SIGNAL) { + + if (!fd) { + if (Z_TYPE_P(&event->stream_id) != IS_NULL) { + zend_list_close(Z_RES_P(&event->stream_id)); + ZVAL_NULL(&event->stream_id); + } + } else if (events & EV_SIGNAL) { ZVAL_NULL(&event->stream_id); - } else if (fd) { + } else { ZVAL_COPY(&event->stream_id, fd); } @@ -748,7 +755,7 @@ static PHP_FUNCTION(event_set) _php_event_callback_free(old_callback); } - if (event->base) { + if (fd && event->base) { ret = event_base_set(event->base->base, event->event); if (ret != 0) { RETURN_FALSE; From cca006613e5225df52163e8cb8855c634b3b6164 Mon Sep 17 00:00:00 2001 From: Tracy Date: Fri, 24 May 2019 10:11:54 +0000 Subject: [PATCH 103/103] fix the issue result in seg fault when the event_base_set() called --- libevent.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libevent.c b/libevent.c index 8956e7d..29d0337 100644 --- a/libevent.c +++ b/libevent.c @@ -355,8 +355,8 @@ static PHP_FUNCTION(event_base_new) base->events = 0; - base->rsrc_id = zend_list_insert(base, le_event_base); - ZVAL_COPY_VALUE(return_value, base->rsrc_id); + ZVAL_COPY_VALUE(return_value, zend_list_insert(base, le_event_base)); + base->rsrc_id = return_value; Z_ADDREF_P(base->rsrc_id); } /* }}} */ @@ -573,8 +573,8 @@ static PHP_FUNCTION(event_new) ZVAL_NULL(&event->stream_id); TSRMLS_SET_CTX(event->thread_ctx); - event->rsrc_id = zend_list_insert(event, le_event); - ZVAL_COPY_VALUE(return_value, event->rsrc_id); + ZVAL_COPY_VALUE(return_value, zend_list_insert(event, le_event)); + event->rsrc_id = return_value; } /* }}} */