Skip to content

Commit b2ef9e6

Browse files
authored
Merge pull request #9 from gtkphp/backlog-project
Improve GList, fix leaks
2 parents 7d29d68 + c819ae9 commit b2ef9e6

14 files changed

+682
-128
lines changed

TODO.md

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

66
## Priority
77

8-
--with-gtk=all
9-
--with-gtk=4,3
10-
--with-gtk=3
11-
--with-gtk=2
8+
+TODO: refactor ghashtable, certenly leaks
9+
+TODO: Implement unset($list[0]);
10+
11+
12+
--with-gtk=shared
13+
--with-gtk=static
14+
--with-gtk=Gnome,4,shared => libgtk-4.so
15+
--with-gtk=Gtk3,3,shared
16+
--with-gtk=2,shared
17+
1218
<?php
13-
require_once("Gtk.php");// get latest
19+
require_once("Gtk.php");// Wrapper get latest
1420
// set_include_path "gtk+-4.0"
1521
require_once("gtk+-4.0/Gtk/Widget.php");
1622
require_once("gtk+-3.0/Gtk/Widget.php");
@@ -23,12 +29,9 @@ use Gtk\Window;// Wrapper --with-gtk=4
2329
Implemente g_hash_table, g_hash_table_iter, g_hash_func/g_equal_func
2430
Compatibility PHP5 -> PHP8
2531

26-
// maybe is better to Z_ADDREF_P();
27-
zval *val = emalloc(sizeof(zval));
28-
ZVAL_COPY(val, data);
29-
// efree();
3032

3133
+ REFACTOR: ZOBJ_GET_PHP_G_LIST by Z_OBJ_PHP_G_LIST_P ?( preferable work with pointer)
34+
+ REFACTOR: ZVAL_GET_PHP_G_LIST by Z_PHP_G_LIST_P ?
3235

3336

3437
create config.nice
@@ -69,6 +72,7 @@ Potential hacks
6972
Add tests
7073
- Unit
7174
- Fonctional
75+
- End-to-end
7276

7377

7478

config.m4

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ if test "$PHP_GTK" != "no"; then
124124
glib_sources="php_glib/glib.c "
125125

126126
g_sources="php_g/g-list.c \
127-
php_g/g-hash-table.c "
127+
php_g/g-hash-table.c"
128128

129129
sources="gtk.c"
130130

examples/g-list-new.php

+115-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
//if (!extension_loaded("Sdk")) die("Sdk not loaded");
1111
if (!extension_loaded("gtk")) die("Gtk+ not loaded");
1212

13+
ini_set('display_errors', 1);
14+
ini_set('display_startup_errors', 1);
15+
error_reporting(E_ALL);
1316

1417
//echo "Read: https://www.phpinternalsbook.com/php7/zvals/memory_management.html\n";
15-
18+
//confirm_gtk_compiled("");
1619

1720
class Key {
1821
public $value;
@@ -25,7 +28,9 @@ function __construct($val) {
2528
$key3 = new Key("c");
2629
$key4 = new Key("d");
2730

28-
$list = new GList($key2);
31+
/*
32+
//$list = new GList($key2);
33+
$list = g_list_append(NULL, $key2);
2934
$list = g_list_append($list, $key3);
3035
$list = g_list_prepend($list, $key1);
3136
$list = g_list_append($list, $key4);
@@ -37,9 +42,115 @@ function __construct($val) {
3742
3843
3944
40-
$list2 = g_list_append(NULL, "key2");
41-
//$list2 = new GList("key2");
45+
//$list2 = g_list_append(NULL, "key2");
46+
$list2 = new GList("key2");
4247
g_list_append($list2, "key3");
4348
$list2 = g_list_prepend($list2, "key1");
49+
4450
php_g_list_dump($list2);
51+
*/
52+
53+
54+
/*
55+
$list = new GList("key2");
56+
g_list_append($list, "key3");
57+
//g_list_prepend($list, "key1");
58+
59+
60+
$list->baz = "bar";
61+
$list["foo"] = "Too";
62+
63+
unset($list->baz);
64+
unset($list["foo"]);
65+
unset($list["next"]);// Notice: Forbidden to delete GList::$next
66+
//unset($list[0]);
67+
print_r($list);
68+
*/
69+
70+
/*
71+
$list = g_list_prepend(NULL, "key1");
72+
g_list_prepend($list, "key2");
73+
g_list_prepend($list, "key3");
74+
php_g_list_dump($list->prev->prev);
75+
76+
77+
$list = new GList("key1");
78+
g_list_prepend($list, "key2");
79+
g_list_prepend($list, "key3");
80+
php_g_list_dump($list->prev->prev);
81+
*/
82+
83+
/*
84+
$first = Null;
85+
$first = g_list_append($first, "key1");
86+
$first = g_list_append($first, "key2");
87+
$list = $first->next;//g_list_nth($first, 1);
88+
$list = g_list_append($list, "key3");
89+
$list = g_list_insert($list, "key", 0);
90+
php_g_list_dump($first);
91+
*/
92+
93+
/*
94+
$list = g_list_insert(NULL, "value1", 0);
95+
96+
var_dump($list);
97+
*/
98+
99+
//php_g_list_dump(new GList("value1"));// Ref: 1
100+
//php_g_list_dump(g_list_append(NULL, "value2"));// Ref: 1
101+
//$list = new GList("value1");
102+
//php_g_list_dump($list);// Ref: 2
103+
//$list = g_list_append(NULL, "value2");
104+
//php_g_list_dump($list);// Ref: 2
105+
106+
107+
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");
115+
116+
//var_dump($list);
117+
//$casted = (array)$list;
118+
//var_dump((array)$list);
119+
//print_r($list);
120+
//print_r((array)$list);
121+
//php_g_list_dump($list);
122+
123+
//$state = var_export($list, True);
124+
//eval('$c = ' . $state . ';');
125+
//var_export($list);
126+
127+
//$k = var_export($key1, True);
128+
//eval('$c = ' . $k . ';');
129+
130+
// TODO g_list_prepend
131+
// TODO g_list_insert
132+
133+
/*
134+
//$list = new GList("val1");
135+
$list = g_list_prepend(NULL, "val1");
136+
//php_g_list_dump($list);
137+
//$list = g_list_prepend($list, "val2");
138+
//php_g_list_dump($list);
139+
//$list = g_list_prepend($list, "val3");
140+
//php_g_list_dump($list);
141+
//$list = g_list_prepend($list, "val4");
142+
g_list_prepend($list, "val5");
143+
g_list_prepend($list, "val6");
144+
g_list_prepend($list, "val7");
145+
php_g_list_dump(g_list_first($list));
146+
//print_r((array)$list);
147+
*/
148+
149+
$list = g_list_insert(Null, "val1", 0);
150+
$list = g_list_insert($list, "val2", 0);
151+
$list = g_list_insert($list, "val3", 0);
152+
$list = g_list_insert($list, "val4", 0);
153+
//php_g_list_dump(g_list_first($list));
154+
php_g_list_dump($list);
45155

156+
confirm_gtk_compiled("");

gtk.c

+103-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ PHP_INI_END()
5858
so that your module can be compiled into PHP, it exists only for testing
5959
purposes. */
6060

61+
static print_g_list(GList *list) {
62+
GList *it;
63+
for(it = list/*g_list_first(list)*/; it; it = it->next) {
64+
g_print("GList{%s}\n", (char *)it->data);
65+
}
66+
}
6167
/* Every user-visible function in PHP should document itself in the source */
6268
/* {{{ proto string confirm_gtk_compiled(string arg)
6369
Return a string to confirm that the module is compiled in */
@@ -71,6 +77,94 @@ PHP_FUNCTION(confirm_gtk_compiled)
7177
return;
7278
}
7379

80+
//#define TEST_NUM_1 "04-g_list-0-insert.c"
81+
//#define TEST_NUM_2 "04-g_list-1-insert.c"
82+
//#define TEST_NUM_3 "04-g_list-2-insert.c"
83+
84+
//#define TEST_NUM TEST_NUM_1
85+
86+
#if 1
87+
// Check the bahavior of GList
88+
gchar *key1 = "key1";
89+
gchar *key2 = "key2";
90+
gchar *key3 = "key3";
91+
gchar *key4 = "key4";
92+
93+
typedef GList GList_CStr;
94+
GList_CStr *first=NULL, *list = NULL;
95+
96+
/*
97+
list = g_list_insert(list, key1, 0);
98+
list = g_list_insert(list, key2, 1);
99+
list = g_list_insert(list->next, key3, 0);
100+
//print_g_list(g_list_first(list));
101+
print_g_list(list);
102+
*/
103+
104+
/*
105+
list = g_list_insert(list, key1, 0);
106+
list = g_list_insert(list, key2, 1);
107+
list = g_list_insert(list, key3, 2);
108+
list = g_list_insert(list, key4, 3);
109+
print_g_list(list);//g_list_first()
110+
*/
111+
112+
/*
113+
list = g_list_insert(list, key1, 0);
114+
list = g_list_insert(list, key2, 0);
115+
list = g_list_insert(list, key3, 0);
116+
list = g_list_insert(list, key4, 0);
117+
print_g_list(g_list_first(list));
118+
//print_g_list(list);//g_list_first()
119+
*/
120+
121+
122+
/*
123+
first = g_list_append(first, key1);
124+
g_print("%p\n", first);// 600
125+
first = g_list_append(first, key2);
126+
g_print("%p\n", first);// 600
127+
list = g_list_nth(first, 1);
128+
g_print("%p\n", list);// 620
129+
list = g_list_append(list, key3);
130+
g_print("%p\n", list);// 620
131+
list = g_list_insert(list, "key", 0);
132+
g_print("%p\n", list);
133+
*/
134+
135+
/*
136+
first = list = g_list_append(list, key1);
137+
g_print("%p\n", list);
138+
list = g_list_append(list, key2);
139+
list = g_list_append(list, key3);
140+
list = g_list_nth(list, 2);
141+
g_print("%p\n", list);
142+
list = g_list_insert(list, "key", 0);
143+
g_print("%p\n", list);
144+
*/
145+
146+
147+
/*
148+
list = g_list_prepend(list, key2);
149+
g_print("%p\n", list);
150+
list = g_list_prepend(list, key3);
151+
g_print("%p\n", list);
152+
*/
153+
154+
/*
155+
list = g_list_append(list, key1);
156+
g_print("%p\n", list);
157+
list = g_list_append(list, key2);
158+
g_print("%p\n", list);
159+
list = g_list_nth(list, 1);
160+
list = g_list_append(list, key3);
161+
g_print("%p\n", list);
162+
*/
163+
164+
//print_g_list(first);
165+
166+
#endif
167+
74168
strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "gtk", arg);
75169

76170
RETURN_STR(strg);
@@ -112,15 +206,16 @@ static char* g_list_dump(zval *list, int tab){
112206
tmp_prev = g_list_dump_zval(&__list->prev);
113207
tmp_data = g_list_dump_zval(&__list->data);
114208
tmp_next = g_list_dump(&__list->next, tab+1);
115-
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){\n"
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"
116211
"%s prev: %s,\n"
117212
"%s data: %s,\n"
118213
"%s next: %s\n"
119214
"%s}}",
120215
list->value.counted->gc.refcount,
121216
list->value.obj->ce->name->val,
122217
Z_OBJ_HANDLE_P(list),
123-
list->value.obj->gc.refcount,
218+
list->value.obj->gc.refcount, list->value.obj,
124219
t, tmp_prev,
125220
t, tmp_data,
126221
t, tmp_next,
@@ -192,7 +287,7 @@ PHP_MINIT_FUNCTION(gtk)
192287
*/
193288

194289
zend_class_entry ce;
195-
zend_class_entry *g_hash_table_ce;
290+
zend_class_entry *g_hash_table_ce;
196291

197292
zend_object_handlers *handlers= php_glib_object_get_handlers();
198293

@@ -222,7 +317,11 @@ PHP_MSHUTDOWN_FUNCTION(gtk)
222317
/* uncomment this line if you have INI entries
223318
UNREGISTER_INI_ENTRIES();
224319
*/
225-
//zend_object_std_dtor();
320+
321+
zend_hash_destroy(&php_g_list_prop_handlers);
322+
zend_hash_destroy(&php_g_hash_table_prop_handlers);
323+
//zend_hash_destroy(&php_glib_object_handlers);
324+
zend_hash_destroy(&classes);
226325

227326
return SUCCESS;
228327
}

php_g/g-hash-table.c

+3-24
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,13 @@ void php_g_hash_table_free_storage(zend_object *object)
7474

7575
php_g_hash_table *intern = PHP_G_HASH_TABLE_FROM_STD(object);
7676
php_glib_object *parent = PHP_GLIB_OBJECT(&intern->parent_instance);
77-
#if defined(__GNUC__) && __GNUC__ >= 3
78-
int retcount __attribute__((unused)); /* keep compiler quiet */
79-
#else
80-
int retcount;
81-
#endif
82-
8377

8478
current_hash_table = intern;
8579
g_hash_table_unref((GHashTable*)parent->ptr);
8680
current_hash_table = NULL;
8781

8882
zend_object_std_dtor(&parent->std);// maybe use PHP_STD_FROM_G_HASH_TABLE()
89-
efree(intern);
90-
// FIXME
91-
#if 0
92-
if (intern->ptr != NULL ) {
93-
94-
/*
95-
if (((xmlNodePtr) ((php_libxml_node_ptr *)intern->ptr)->node)->type != XML_DOCUMENT_NODE && ((xmlNodePtr) ((php_libxml_node_ptr *)intern->ptr)->node)->type != XML_HTML_DOCUMENT_NODE) {
96-
php_libxml_node_decrement_resource((php_libxml_node_object *) intern);
97-
} else {
98-
php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
99-
retcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
100-
}
101-
*/
102-
intern->ptr = NULL;
103-
}
104-
#endif
83+
//efree(intern);
10584
}
10685
/* }}} */
10786

@@ -295,8 +274,8 @@ while (g_hash_table_iter_next (&iter, (gpointer)&key, (gpointer)&value))
295274

296275
//g_print("%s=>%s\n", (char *)k->value.str->val, (char *)val.value.str->val);
297276
zend_hash_add(debug_info, k.value.str, value);
298-
Z_TRY_DELREF(k);
299-
Z_TRY_DELREF_P(value);
277+
//Z_TRY_DELREF(k);
278+
//Z_TRY_DELREF_P(value);
300279

301280
}
302281
return debug_info;

0 commit comments

Comments
 (0)