Skip to content

Commit 1b908ef

Browse files
authored
Merge pull request #10 from gtkphp/backlog-project
Fix memory leaks in GHashTable test
2 parents b2ef9e6 + 81cf175 commit 1b908ef

File tree

7 files changed

+285
-214
lines changed

7 files changed

+285
-214
lines changed

TODO.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ By continuously improving the design of code, we make it easier and easier to wo
55

66
## Priority
77

8+
Donner les liens
9+
- musique quelque chose viens de tomber
10+
- enfant de la lune( comic)
11+
12+
813
+TODO: refactor ghashtable, certenly leaks
14+
+TODO: gtk.c PHP_MINIT_FUNCTION_G_LIST
915
+TODO: Implement unset($list[0]);
1016

1117

examples/g-list-new.php

+42-8
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,6 @@ function __construct($val) {
105105

106106

107107

108-
//$list = new GList($key1);
109-
//$key1->value = "value1";
110-
//$list = g_list_append(NULL, "value1");
111-
//$list = g_list_append($list, "value2");
112-
//$list = g_list_append($list, "value3");
113-
//$list = g_list_append($list, "value4");
114-
//g_list_append($list, "value5");
115108

116109
//var_dump($list);
117110
//$casted = (array)$list;
@@ -146,11 +139,52 @@ function __construct($val) {
146139
//print_r((array)$list);
147140
*/
148141

142+
143+
149144
$list = g_list_insert(Null, "val1", 0);
150145
$list = g_list_insert($list, "val2", 0);
151146
$list = g_list_insert($list, "val3", 0);
152147
$list = g_list_insert($list, "val4", 0);
153148
//php_g_list_dump(g_list_first($list));
149+
//g_list_dump($list);
150+
//print_r((array)$list);
151+
$list->foo = "bar";
152+
$list->Foo = "baz";
153+
echo $list->foo, PHP_EOL;
154+
echo $list->Foo, PHP_EOL;
155+
echo $list->nulle, PHP_EOL;
156+
157+
158+
/*
159+
$list = g_list_append(NULL, "value1");
160+
g_list_append($list, "value2");
161+
g_list_append($list, "value3");
162+
g_list_append($list, "value4");
163+
g_list_append($list, "value5");
154164
php_g_list_dump($list);
165+
*/
166+
167+
/*
168+
$list = g_list_prepend(NULL, "value1");
169+
$list = g_list_prepend($list, "value2");
170+
$list = g_list_prepend($list, "value3");
171+
$list = g_list_prepend($list, "value4");
172+
$list = g_list_prepend(g_list_first($list), "value5");
173+
php_g_list_dump($list);
174+
*/
175+
176+
/*
177+
List#1 (object){
178+
'prev'=> NULL,
179+
'data'=> 'val1',
180+
'next'=>List#2 (object){
181+
'prev'=> List#1 (object){*RECURSION*},
182+
'data'=> 'val2',
183+
'next'=>NULL,
184+
},
185+
'properties'=> ,
186+
}
187+
*/
188+
189+
155190

156-
confirm_gtk_compiled("");

gtk.c

+15-103
Original file line numberDiff line numberDiff line change
@@ -172,87 +172,6 @@ PHP_FUNCTION(confirm_gtk_compiled)
172172
/* }}} */
173173

174174

175-
static char*
176-
g_list_dump_zval(zval *data) {
177-
char *str = NULL;
178-
if (ZVAL_IS_NULL(data)) {
179-
str = g_strdup_printf("NULL");
180-
} else if (Z_TYPE_P(data)==IS_STRING) {
181-
str = g_strdup_printf("\e[1;31m\"%s\"\e[0;m", data->value.str->val);
182-
} else if (Z_TYPE_P(data)==IS_LONG) {
183-
str = g_strdup_printf("%ld", data->value.lval);
184-
} else if (Z_TYPE_P(data)==IS_OBJECT) {
185-
str = g_strdup_printf("\e[2;34m%s\e[0;m\e[2;31m#%d\e[0;m(\e[2;35m%d\e[0;m){}",
186-
data->value.obj->ce->name->val,
187-
Z_OBJ_HANDLE_P(data),
188-
data->value.obj->gc.refcount);
189-
} else {
190-
str = g_strdup_printf("%ld", data->value.lval);
191-
}
192-
return str;
193-
}
194-
static char* g_list_dump(zval *list, int tab){
195-
char *str;
196-
char *tmp_prev;
197-
char *tmp_data;
198-
char *tmp_next;
199-
200-
php_g_list *__list = ZVAL_GET_PHP_G_LIST(list);
201-
char *t = g_strdup_printf("%*.s", tab*4, "");
202-
203-
if (ZVAL_IS_NULL(list)) {
204-
str = g_strdup_printf("NULL");
205-
} else {
206-
tmp_prev = g_list_dump_zval(&__list->prev);
207-
tmp_data = g_list_dump_zval(&__list->data);
208-
tmp_next = g_list_dump(&__list->next, tab+1);
209-
210-
str = g_strdup_printf("\e[2;34mzval\e[0;m(\e[2;35m%d\e[0;m){ value: \e[1;34m%s\e[0;m\e[1;31m#%d\e[0;m(\e[2;35m%d\e[0;m)%p{\n"
211-
"%s prev: %s,\n"
212-
"%s data: %s,\n"
213-
"%s next: %s\n"
214-
"%s}}",
215-
list->value.counted->gc.refcount,
216-
list->value.obj->ce->name->val,
217-
Z_OBJ_HANDLE_P(list),
218-
list->value.obj->gc.refcount, list->value.obj,
219-
t, tmp_prev,
220-
t, tmp_data,
221-
t, tmp_next,
222-
t);
223-
g_free(tmp_prev);
224-
g_free(tmp_data);
225-
g_free(tmp_next);
226-
}
227-
g_free(t);
228-
229-
230-
return str;
231-
}
232-
233-
ZEND_BEGIN_ARG_INFO_EX(arginfo_php_g_list_dump, 0, 0, 0)
234-
ZEND_ARG_INFO(0, list)
235-
ZEND_END_ARG_INFO()
236-
237-
/* {{{ proto string php_g_list_dump(GList list)
238-
Return a string to confirm that the module is compiled in */
239-
PHP_FUNCTION(php_g_list_dump)
240-
{
241-
zval *list = NULL;
242-
243-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &list) == FAILURE) {
244-
return;
245-
}
246-
247-
//php_g_list_first();
248-
char *str = g_list_dump(list, 0);
249-
g_print("%s\n", str);
250-
g_free(str);
251-
252-
RETURN_NULL();
253-
}
254-
/* }}} */
255-
256175

257176
/* The previous line is meant for vim and emacs, so it can correctly fold and
258177
unfold functions in source code. See the corresponding marks just before
@@ -288,21 +207,14 @@ PHP_MINIT_FUNCTION(gtk)
288207

289208
zend_class_entry ce;
290209
zend_class_entry *g_hash_table_ce;
291-
210+
zend_class_entry *g_list_ce;
211+
292212
zend_object_handlers *handlers= php_glib_object_get_handlers();
293213

294214
zend_hash_init(&classes, 0, NULL, NULL, 1);
295215

296-
//PHP_MINIT(glib)(INIT_FUNC_ARGS_PASSTHRU);
297-
//PHP_MINIT(g)(INIT_FUNC_ARGS_PASSTHRU);
298-
g_hash_table_ce = php_g_hash_table_class_init(&ce);
299-
//PHP_MINIT(gobject)(INIT_FUNC_ARGS_PASSTHRU);
300-
//PHP_MINIT(gio)(INIT_FUNC_ARGS_PASSTHRU);
301-
//PHP_MINIT(cairo)(INIT_FUNC_ARGS_PASSTHRU);
302-
//PHP_MINIT(pango)(INIT_FUNC_ARGS_PASSTHRU);
303-
//PHP_MINIT(gtk)(INIT_FUNC_ARGS_PASSTHRU);
304-
//...
305-
php_g_list_class_init(&ce);
216+
g_hash_table_ce = PHP_G_HASH_TABLE_MINIT_FUNCTION(&ce);
217+
g_list_ce = PHP_G_LIST_MINIT_FUNCTION(&ce);
306218

307219
return SUCCESS;
308220
}
@@ -318,8 +230,10 @@ PHP_MSHUTDOWN_FUNCTION(gtk)
318230
UNREGISTER_INI_ENTRIES();
319231
*/
320232

321-
zend_hash_destroy(&php_g_list_prop_handlers);
322-
zend_hash_destroy(&php_g_hash_table_prop_handlers);
233+
PHP_G_LIST_MSHUTDOWN_FUNCTION();
234+
235+
PHP_G_HASH_TABLE_MSHUTDOWN_FUNCTION();
236+
323237
//zend_hash_destroy(&php_glib_object_handlers);
324238
zend_hash_destroy(&classes);
325239

@@ -335,6 +249,8 @@ PHP_RINIT_FUNCTION(gtk)
335249
#if defined(COMPILE_DL_GTK) && defined(ZTS)
336250
ZEND_TSRMLS_CACHE_UPDATE();
337251
#endif
252+
253+
338254
return SUCCESS;
339255
}
340256
/* }}} */
@@ -344,7 +260,10 @@ PHP_RINIT_FUNCTION(gtk)
344260
*/
345261
PHP_RSHUTDOWN_FUNCTION(gtk)
346262
{
347-
return SUCCESS;
263+
PHP_G_HASH_TABLE_RSHUTDOWN_FUNCTION();
264+
PHP_G_LIST_RSHUTDOWN_FUNCTION();
265+
266+
return SUCCESS;
348267
}
349268
/* }}} */
350269

@@ -369,15 +288,8 @@ PHP_MINFO_FUNCTION(gtk)
369288
*/
370289
const zend_function_entry gtk_functions[] = {
371290
PHP_FE(confirm_gtk_compiled, NULL) /* For testing, remove later. */
372-
PHP_FE(php_g_list_dump, arginfo_php_g_list_dump) /* For debuging, remove later. */
373-
/* from g-list.h */
374291
PHP_G_LIST_FE()
375-
/* from g-hash-table.h */
376-
PHP_FE(g_str_hash, arginfo_g_str_hash)
377-
PHP_FE(g_str_equal, arginfo_g_str_equal)
378-
PHP_FE(g_hash_table_new, arginfo_g_hash_table_new)
379-
PHP_FE(g_hash_table_add, arginfo_g_hash_table_add)
380-
PHP_FE(g_hash_table_insert, arginfo_g_hash_table_insert)
292+
PHP_G_HASH_TABLE_FE()
381293
PHP_FE_END /* Must be the last line in gtk_functions[] */
382294
};
383295
/* }}} */

php_g/g-hash-table.c

+15-24
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void php_g_hash_table_free_storage(zend_object *object)
7272
{
7373
TRACE();
7474

75-
php_g_hash_table *intern = PHP_G_HASH_TABLE_FROM_STD(object);
75+
php_g_hash_table *intern = ZOBJ_TO_PHP_G_HASH_TABLE(object);
7676
php_glib_object *parent = PHP_GLIB_OBJECT(&intern->parent_instance);
7777

7878
current_hash_table = intern;
@@ -87,7 +87,7 @@ void php_g_hash_table_free_storage(zend_object *object)
8787
zval *php_g_hash_table_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
8888
{
8989
TRACE();
90-
php_g_hash_table *obj = PHP_G_HASH_TABLE_FROM_STD(Z_OBJ_P(object));
90+
php_g_hash_table *obj = ZVAL_GET_PHP_G_HASH_TABLE(object);
9191
php_glib_object *parent = &obj->parent_instance;
9292
zend_string *member_str = zval_get_string(member);
9393
zval *retval = NULL;
@@ -107,7 +107,7 @@ zval *php_g_hash_table_get_property_ptr_ptr(zval *object, zval *member, int type
107107
zval *php_g_hash_table_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv)
108108
{
109109
TRACE();
110-
php_g_hash_table *obj = PHP_G_HASH_TABLE_FROM_STD(Z_OBJ_P(object));
110+
php_g_hash_table *obj = ZVAL_GET_PHP_G_HASH_TABLE(object);
111111
php_glib_object *parent = &obj->parent_instance;
112112
zend_string *member_str = zval_get_string(member);
113113
zval *retval;
@@ -141,7 +141,7 @@ zval *php_g_hash_table_read_property(zval *object, zval *member, int type, void
141141
void php_g_hash_table_write_property(zval *object, zval *member, zval *value, void **cache_slot)
142142
{
143143
TRACE();
144-
php_g_hash_table *obj = PHP_G_HASH_TABLE_FROM_STD(Z_OBJ_P(object));
144+
php_g_hash_table *obj = ZVAL_GET_PHP_G_HASH_TABLE(object);
145145
php_glib_object *parent = &obj->parent_instance;
146146
zend_string *member_str = zval_get_string(member);
147147
php_g_hash_table_prop_handler *hnd = NULL;
@@ -200,7 +200,7 @@ php_g_hash_table_get_debug_info_helper(zval *object, int *is_temp) /* {{{ */
200200
{
201201
TRACE();
202202

203-
php_g_hash_table *obj = PHP_G_HASH_TABLE_FROM_STD(Z_OBJ_P(object));
203+
php_g_hash_table *obj = ZVAL_GET_PHP_G_HASH_TABLE(object);
204204
php_glib_object *parent = &obj->parent_instance;
205205
HashTable *debug_info,
206206
*prop_handlers = obj->prop_handler,
@@ -262,20 +262,11 @@ while (g_hash_table_iter_next (&iter, (gpointer)&key, (gpointer)&value))
262262
} else {
263263
g_print("Unexpected 21 : php_g_hash_table_get_debug_info_helper\n");
264264
}
265-
/*
266-
if (Z_TYPE_P(value)==IS_STRING) {
267-
ZVAL_COPY(&val, value);
268-
} else if (Z_TYPE_P(value)==IS_OBJECT ) {
269-
ZVAL_COPY(&val, value);
270-
} else {
271-
g_print("Unexpected 21 : php_g_hash_table_get_debug_info_helper\n");
272-
}
273-
*/
274265

275266
//g_print("%s=>%s\n", (char *)k->value.str->val, (char *)val.value.str->val);
276267
zend_hash_add(debug_info, k.value.str, value);
277-
//Z_TRY_DELREF(k);
278-
//Z_TRY_DELREF_P(value);
268+
Z_TRY_DELREF(k);
269+
Z_TRY_ADDREF_P(value);
279270

280271
}
281272
return debug_info;
@@ -303,7 +294,7 @@ php_g_hash_table_has_dimension(zval *object, zval *member, int check_empty)
303294
//zval *length = zend_read_property(Z_OBJCE_P(object), object, "length", sizeof("length") - 1, 0, &rv);
304295

305296
//return length && offset < Z_LVAL_P(length);
306-
php_g_hash_table *intern = PHP_G_HASH_TABLE_FROM_STD(Z_OBJCE_P(object));
297+
php_g_hash_table *intern = ZVAL_GET_PHP_G_HASH_TABLE(object);
307298
GHashTable *hash_table = intern->parent_instance.ptr;
308299
guint size = g_hash_table_size(hash_table);
309300
return size>0;
@@ -322,7 +313,7 @@ php_g_hash_table_read_dimension(zval *object, zval *offset, int type, zval *rv)
322313

323314

324315
//ZVAL_LONG(&offset_copy, zval_get_long(offset));
325-
php_g_hash_table *intern = PHP_G_HASH_TABLE_FROM_STD(Z_OBJ_P(object));
316+
php_g_hash_table *intern = ZVAL_GET_PHP_G_HASH_TABLE(object);
326317
GHashTable *hash_table = intern->parent_instance.ptr;
327318
zval *value = NULL;
328319
if (hash_table!=NULL) {
@@ -434,8 +425,8 @@ php_g_hash_table_hash_func(gconstpointer v) {
434425
ZVAL_STRING(&params[0], name);
435426
g_free(name);
436427
}
437-
g_print("!!!%p\n", hash_func);
438-
g_print("%d\n", Z_TYPE_P(hash_func));
428+
//g_print("!!!%p\n", hash_func);
429+
//g_print("%d\n", Z_TYPE_P(hash_func));
439430
result = call_user_function(NULL, NULL, hash_func, &retval, param_count, params);
440431
if (result==FAILURE) {
441432
g_print("Unexpected 22 : php_g_hash_table_hash_func\n");
@@ -548,7 +539,7 @@ php_g_hash_table_new(zval *hash_func, zval *key_equal_func)
548539
{
549540
TRACE();
550541
zend_object *object = php_g_hash_table_create_object(php_g_hash_table_class_entry);
551-
php_g_hash_table *hash_table = PHP_G_HASH_TABLE_FROM_STD(object);
542+
php_g_hash_table *hash_table = ZOBJ_TO_PHP_G_HASH_TABLE(object);
552543
php_glib_object *obj = PHP_GLIB_OBJECT(&hash_table->parent_instance);
553544

554545

@@ -572,7 +563,7 @@ php_g_hash_table_new(zval *hash_func, zval *key_equal_func)
572563
hash_table->key_equal_func = NULL;
573564
}
574565

575-
return PHP_G_HASH_TABLE_FROM_STD(object);
566+
return hash_table;
576567
}
577568

578569
zend_bool
@@ -660,7 +651,7 @@ PHP_FUNCTION(g_hash_table_insert)
660651
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ozz", &hash_table, &key, &value) == FAILURE) {
661652
return;
662653
}
663-
php_g_hash_table *obj = PHP_G_HASH_TABLE_FROM_STD(hash_table->value.obj);
654+
php_g_hash_table *obj = ZVAL_GET_PHP_G_HASH_TABLE(hash_table);
664655
zend_bool ret = php_g_hash_table_insert(obj, key, value);
665656
RETURN_BOOL(ret);
666657
}
@@ -679,7 +670,7 @@ PHP_FUNCTION(g_hash_table_add)
679670
Z_PARAM_ZVAL(key);
680671
ZEND_PARSE_PARAMETERS_END();
681672

682-
php_g_hash_table *__self = PHP_G_HASH_TABLE_FROM_STD(hash_table->value.obj);
673+
php_g_hash_table *__self = ZVAL_GET_PHP_G_HASH_TABLE(hash_table);
683674
zend_bool __ret = php_g_hash_table_add(__self, key);
684675

685676
//Z_TRY_DELREF(*hash_table);

0 commit comments

Comments
 (0)