Implement gc handlers
authorMichael Wallner <mike@php.net>
Thu, 9 Jun 2016 09:33:02 +0000 (11:33 +0200)
committerMichael Wallner <mike@php.net>
Thu, 9 Jun 2016 09:33:02 +0000 (11:33 +0200)
JFC. Closes #44

src/php_http_client.c
src/php_http_client.h
src/php_http_client_curl.c
src/php_http_message.c
src/php_http_message.h
src/php_http_message_body.c
src/php_http_message_body.h

index 5de1eac6dd2311a22bb5b175292f2186839220cf..4b465c0dc2bb0db8f8474fe4184040706747414f 100644 (file)
@@ -330,6 +330,8 @@ void php_http_client_object_free(zend_object *object)
 {
        php_http_client_object_t *o = PHP_HTTP_OBJ(object, NULL);
 
+       PTR_FREE(o->gc);
+
        php_http_client_free(&o->client);
        php_http_object_method_dtor(&o->notify);
        php_http_object_method_free(&o->update);
@@ -356,6 +358,37 @@ zend_object *php_http_client_object_new(zend_class_entry *ce)
        return &php_http_client_object_new_ex(ce, NULL)->zo;
 }
 
+static HashTable *php_http_client_object_get_gc(zval *object, zval **table, int *n)
+{
+       php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, object);
+       zend_llist_element *el = NULL;
+       HashTable *props = Z_OBJPROP_P(object);
+       uint32_t count = zend_hash_num_elements(props) + zend_llist_count(&obj->client->responses) + zend_llist_count(&obj->client->requests);
+       zval *val;
+
+       *n = 0;
+       *table = obj->gc = erealloc(obj->gc, sizeof(zval) * count);
+
+       for (el = obj->client->responses.head; el; el = el->next) {
+               php_http_message_object_t *response_obj = *(php_http_message_object_t **) el->data;
+               ZVAL_OBJ(&obj->gc[(*n)++], &response_obj->zo);
+       }
+
+       for (el = obj->client->requests.head; el; el = el->next) {
+               php_http_client_enqueue_t *q = *(php_http_client_enqueue_t **) el->data;
+               php_http_message_object_t *request_obj = q->opaque; /* FIXME */
+               ZVAL_OBJ(&obj->gc[(*n)++], &request_obj->zo);
+       }
+
+       ZEND_HASH_FOREACH_VAL(props, val)
+       {
+               ZVAL_COPY_VALUE(&obj->gc[(*n)++], val);
+       }
+       ZEND_HASH_FOREACH_END();
+
+       return NULL;
+}
+
 static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response)
 {
        zval new_hist, old_hist_tmp, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0, &old_hist_tmp);
@@ -400,7 +433,7 @@ static ZEND_RESULT_CODE handle_response(void *arg, php_http_client_t *client, ph
                *response = NULL;
 
                msg_obj = php_http_message_object_new_ex(php_http_get_client_response_class_entry(), msg);
-               ZVAL_OBJ(&zresponse, &msg_obj->zo);
+               ZVAL_OBJECT(&zresponse, &msg_obj->zo, 1);
                ZVAL_OBJECT(&zrequest, &((php_http_message_object_t *) e->opaque)->zo, 1);
 
                php_http_message_object_prepend(&zresponse, &zrequest, 1);
@@ -411,7 +444,6 @@ static ZEND_RESULT_CODE handle_response(void *arg, php_http_client_t *client, ph
                zend_update_property(php_http_get_client_response_class_entry(), &zresponse, ZEND_STRL("transferInfo"), &info);
                zval_ptr_dtor(&info);
 
-               Z_ADDREF(zresponse);
                zend_llist_add_element(&client->responses, &msg_obj);
 
                if (e->closure.fci.size) {
@@ -478,7 +510,7 @@ static void response_dtor(void *data)
 {
        php_http_message_object_t *msg_obj = *(php_http_message_object_t **) data;
 
-       zend_objects_store_del(&msg_obj->zo);
+       zend_object_release(&msg_obj->zo);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_construct, 0, 0, 0)
@@ -579,14 +611,14 @@ static void msg_queue_dtor(php_http_client_enqueue_t *e)
 {
        php_http_message_object_t *msg_obj = e->opaque;
 
-       zend_objects_store_del(&msg_obj->zo);
+       zend_object_release(&msg_obj->zo);
        zend_hash_destroy(e->options);
        FREE_HASHTABLE(e->options);
 
        if (e->closure.fci.size) {
                zval_ptr_dtor(&e->closure.fci.function_name);
                if (e->closure.fci.object) {
-                       zend_objects_store_del(e->closure.fci.object);
+                       zend_object_release(e->closure.fci.object);
                }
        }
 }
@@ -1231,6 +1263,7 @@ PHP_MINIT_FUNCTION(http_client)
        php_http_client_object_handlers.offset = XtOffsetOf(php_http_client_object_t, zo);
        php_http_client_object_handlers.free_obj = php_http_client_object_free;
        php_http_client_object_handlers.clone_obj = NULL;
+       php_http_client_object_handlers.get_gc = php_http_client_object_get_gc;
        zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE);
        zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED);
        zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("history"), ZEND_ACC_PROTECTED);
index 6aa8abf282f6da9f0afe1a6d519ba22eb31afb4b..c206ccd75be97c841bf7528eccad8711f90a283a 100644 (file)
@@ -118,6 +118,7 @@ typedef struct php_http_client_object {
        php_http_object_method_t *update;
        php_http_object_method_t notify;
        long iterator;
+       zval *gc;
        zend_object zo;
 } php_http_client_object_t;
 
index e9fa1126e5c6cd72822002d15d64c5582657fab5..18eb944708b7d74b562966a9f4fed1b0e6552aac 100644 (file)
@@ -198,14 +198,10 @@ static php_resource_factory_ops_t php_http_curlm_resource_factory_ops = {
 
 static size_t php_http_curle_read_callback(void *data, size_t len, size_t n, void *ctx)
 {
-       php_http_message_body_t *body = ctx;
+       php_stream *s = php_http_message_body_stream(ctx);
 
-       if (body && body->res) {
-               php_stream *s = php_http_message_body_stream(body);
-
-               if (s) {
-                       return php_stream_read(s, data, len * n);
-               } else abort();
+       if (s) {
+               return php_stream_read(s, data, len * n);
        }
        return 0;
 }
index 56499bb8158d69db9f3e1f14c3d0cd849786fcea..cb1192e065ab30795c0bc5f15f9a49fc77414407 100644 (file)
@@ -461,7 +461,7 @@ php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_
 
                copy = temp = php_http_message_init(to, 0, php_http_message_body_copy(from->body, NULL));
                php_http_message_set_info(temp, &info);
-               zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref);
+               array_copy(&from->hdrs, &temp->hdrs);
 
                if (parents) while (from->parent) {
                        info.type = from->parent->type;
@@ -663,7 +663,7 @@ static void php_http_message_object_prophandler_set_parent_message(php_http_mess
                php_http_message_object_t *parent_obj = PHP_HTTP_OBJ(NULL, value);
 
                if (obj->message->parent) {
-                       zend_objects_store_del(&obj->parent->zo);
+                       zend_object_release(&obj->parent->zo);
                }
                Z_ADDREF_P(value);
                obj->parent = parent_obj;
@@ -797,7 +797,7 @@ ZEND_RESULT_CODE php_http_message_object_set_body(php_http_message_object_t *msg
                body_obj->body = php_http_message_body_init(NULL, NULL);
        }
        if (msg_obj->body) {
-               zend_objects_store_del(&msg_obj->body->zo);
+               zend_object_release(&msg_obj->body->zo);
        }
        if (msg_obj->message) {
                php_http_message_body_free(&msg_obj->message->body);
@@ -858,6 +858,8 @@ void php_http_message_object_free(zend_object *object)
 {
        php_http_message_object_t *o = PHP_HTTP_OBJ(object, NULL);
 
+       PTR_FREE(o->gc);
+
        if (!Z_ISUNDEF(o->iterator)) {
                zval_ptr_dtor(&o->iterator);
                ZVAL_UNDEF(&o->iterator);
@@ -869,17 +871,11 @@ void php_http_message_object_free(zend_object *object)
                o->message = NULL;
        }
        if (o->parent) {
-               if (GC_REFCOUNT(&o->parent->zo) == 1) {
-                       zend_objects_store_del(&o->parent->zo);
-               }
-               zend_objects_store_del(&o->parent->zo);
+               zend_object_release(&o->parent->zo);
                o->parent = NULL;
        }
        if (o->body) {
-               if (GC_REFCOUNT(&o->body->zo) == 1) {
-                       zend_objects_store_del(&o->body->zo);
-               }
-               zend_objects_store_del(&o->body->zo);
+               zend_object_release(&o->body->zo);
                o->body = NULL;
        }
        zend_object_std_dtor(object);
@@ -1014,6 +1010,32 @@ static HashTable *php_http_message_object_get_debug_info(zval *object, int *is_t
        return props;
 }
 
+static HashTable *php_http_message_object_get_gc(zval *object, zval **table, int *n)
+{
+       php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
+       HashTable *props = Z_OBJPROP_P(object);
+       uint32_t count = 2 + zend_hash_num_elements(props);
+       zval *val;
+
+       *n = 0;
+       *table = obj->gc = erealloc(obj->gc, count * sizeof(zval));
+
+       if (obj->body) {
+               ZVAL_OBJ(&obj->gc[(*n)++], &obj->body->zo);
+       }
+       if (obj->parent) {
+               ZVAL_OBJ(&obj->gc[(*n)++], &obj->parent->zo);
+       }
+
+       ZEND_HASH_FOREACH_VAL(props, val)
+       {
+               ZVAL_COPY_VALUE(&obj->gc[(*n)++], val);
+       }
+       ZEND_HASH_FOREACH_END();
+
+       return NULL;
+}
+
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___construct, 0, 0, 0)
        ZEND_ARG_INFO(0, message)
        ZEND_ARG_INFO(0, greedy)
@@ -2021,6 +2043,7 @@ PHP_MINIT_FUNCTION(http_message)
        php_http_message_object_handlers.write_property = php_http_message_object_write_prop;
        php_http_message_object_handlers.get_debug_info = php_http_message_object_get_debug_info;
        php_http_message_object_handlers.get_property_ptr_ptr = NULL;
+       php_http_message_object_handlers.get_gc = php_http_message_object_get_gc;
 
        zend_class_implements(php_http_message_class_entry, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
 
index eb5aa0d0df08bdb23323a08218f5897aa0cf3c8a..538cdb1730b4406b4ba93042f9a9731bd9e9141c 100644 (file)
@@ -88,7 +88,7 @@ typedef struct php_http_message_object {
        php_http_message_t *message;
        struct php_http_message_object *parent;
        php_http_message_body_object_t *body;
-       zval iterator;
+       zval iterator, *gc;
        zend_object zo;
 } php_http_message_object_t;
 
index 43aba7f4a8d2e731f31fae42e145ec28d2643142..c903624c703e890ce1d1a3f82e44ef15af0495c0 100644 (file)
@@ -48,10 +48,8 @@ php_http_message_body_t *php_http_message_body_init(php_http_message_body_t **bo
                body->res = stream->res;
                ++GC_REFCOUNT(body->res);
        } else {
-               stream = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff);
-               body->res = stream->res;
+               body->res = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff)->res;
        }
-       php_stream_auto_cleanup(stream);
 
        if (body_ptr) {
                *body_ptr = body;
@@ -93,7 +91,6 @@ void php_http_message_body_free(php_http_message_body_t **body_ptr)
                php_http_message_body_t *body = *body_ptr;
 
                if (!--body->refcount) {
-                       zend_list_delete(body->res);
                        PTR_FREE(body->boundary);
                        efree(body);
                }
@@ -567,8 +564,12 @@ php_http_message_body_object_t *php_http_message_body_object_new_ex(zend_class_e
        zend_object_std_init(&o->zo, php_http_message_body_class_entry);
        object_properties_init(&o->zo, ce);
 
+       o->gc = emalloc(sizeof(zval));
+
        if (body) {
                o->body = body;
+               php_stream_to_zval(php_http_message_body_stream(o->body), o->gc);
+
        }
 
        o->zo.handlers = &php_http_message_body_object_handlers;
@@ -588,10 +589,34 @@ zend_object *php_http_message_body_object_clone(zval *object)
        return &new_obj->zo;
 }
 
+static HashTable *php_http_message_body_object_get_gc(zval *object, zval **table, int *n)
+{
+       php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, object);
+       HashTable *props = Z_OBJPROP_P(object);
+       uint32_t count = zend_hash_num_elements(props);
+
+       *n = 1;
+       if (count) {
+               zval *val;
+
+               obj->gc = erealloc(obj->gc, (*n + count) * sizeof(zval));
+
+               ZEND_HASH_FOREACH_VAL(props, val)
+               {
+                       ZVAL_COPY_VALUE(&obj->gc[(*n)++], val);
+               }
+               ZEND_HASH_FOREACH_END();
+       }
+       *table = obj->gc;
+
+       return NULL;
+}
+
 void php_http_message_body_object_free(zend_object *object)
 {
        php_http_message_body_object_t *obj = PHP_HTTP_OBJ(object, NULL);
 
+       PTR_FREE(obj->gc);
        php_http_message_body_free(&obj->body);
        zend_object_std_dtor(object);
 }
@@ -600,6 +625,7 @@ void php_http_message_body_object_free(zend_object *object)
        do { \
                if (!obj->body) { \
                        obj->body = php_http_message_body_init(NULL, NULL); \
+                       php_stream_to_zval(php_http_message_body_stream(obj->body), obj->gc); \
                } \
        } while(0)
 
@@ -621,6 +647,7 @@ PHP_METHOD(HttpMessageBody, __construct)
                        php_http_message_body_free(&obj->body);
                }
                obj->body = php_http_message_body_init(NULL, stream);
+               php_stream_to_zval(stream, obj->gc);
        }
 }
 
@@ -655,6 +682,7 @@ PHP_METHOD(HttpMessageBody, unserialize)
                php_stream *s = php_stream_memory_open(0, us_str, us_len);
 
                obj->body = php_http_message_body_init(NULL, s);
+               php_stream_to_zval(s, obj->gc);
        }
 }
 
@@ -712,8 +740,8 @@ PHP_METHOD(HttpMessageBody, getResource)
 
                PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
 
-               ++GC_REFCOUNT(obj->body->res);
-               RETVAL_RES(obj->body->res);
+               php_stream_to_zval(php_http_message_body_stream(obj->body), return_value);
+               Z_ADDREF_P(return_value);
        }
 }
 
@@ -891,6 +919,7 @@ PHP_MINIT_FUNCTION(http_message_body)
        php_http_message_body_object_handlers.offset = XtOffsetOf(php_http_message_body_object_t, zo);
        php_http_message_body_object_handlers.clone_obj = php_http_message_body_object_clone;
        php_http_message_body_object_handlers.free_obj = php_http_message_body_object_free;
+       php_http_message_body_object_handlers.get_gc = php_http_message_body_object_get_gc;
        zend_class_implements(php_http_message_body_class_entry, 1, zend_ce_serializable);
 
        return SUCCESS;
index 4e224d00a35ef7e35a3b75d13d5a42ed9393375a..d4115f54a07450ebbe81ef8449e20ddfe29e123a 100644 (file)
@@ -52,11 +52,17 @@ static inline time_t php_http_message_body_mtime(php_http_message_body_t *b)
 
 static inline php_stream *php_http_message_body_stream(php_http_message_body_t *body)
 {
-       return body->res->ptr;
+       return body && body->res ? body->res->ptr : NULL;
+}
+
+static inline zend_resource *php_http_message_body_resource(php_http_message_body_t *body)
+{
+       return body ? body->res : NULL;
 }
 
 typedef struct php_http_message_body_object {
        php_http_message_body_t *body;
+       zval *gc;
        zend_object zo;
 } php_http_message_body_object_t;