summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkira TAGOH <akira@tagoh.org>2006-11-12 14:10:49 +0000
committerAkira TAGOH <akira@tagoh.org>2006-11-12 14:10:49 +0000
commitefc5c92b3698f17f31cbc225ee06d9729e683600 (patch)
treef1444df2cfec61bc7e6121bcc6cec1aeca5a7b99
parent831c136b77ecf72c93e0fae2ee91676ee7dc7e42 (diff)
2006-11-12 Akira TAGOH <at@gclab.org>
* hieroglyph/hglist.c (hg_list_length): fixed an infinite loop issue. (hg_list_remove): reimplement with iterator to be simplified. * hieroglyph/ilist.c (_hg_list_length): fixed an infinite loop issue. (_hg_list_remove): reimplement with iterator to be simplified.
-rw-r--r--ChangeLog8
-rw-r--r--hieroglyph/hglist.c47
-rw-r--r--hieroglyph/ilist.c47
-rw-r--r--hieroglyph/version.h.in2
-rw-r--r--tests/hglist.c79
5 files changed, 104 insertions, 79 deletions
diff --git a/ChangeLog b/ChangeLog
index 11045c1..68a1515 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2006-11-12 Akira TAGOH <at@gclab.org>
+
+ * hieroglyph/hglist.c (hg_list_length): fixed an infinite loop issue.
+ (hg_list_remove): reimplement with iterator to be simplified.
+
+ * hieroglyph/ilist.c (_hg_list_length): fixed an infinite loop issue.
+ (_hg_list_remove): reimplement with iterator to be simplified.
+
2006-11-04 Akira TAGOH <at@gclab.org>
* hieroglyph/vm.c (hg_vm_finalize): change the order of fnalization.
diff --git a/hieroglyph/hglist.c b/hieroglyph/hglist.c
index 2bb4dfe..594b6f3 100644
--- a/hieroglyph/hglist.c
+++ b/hieroglyph/hglist.c
@@ -455,7 +455,7 @@ hg_list_length(HgList *list)
g_return_val_if_fail (list != NULL, 0);
do {
- l = hg_list_next(list);
+ l = hg_list_next(l);
retval++;
} while (l && l != list);
@@ -471,51 +471,20 @@ HgList *
hg_list_remove(HgList *list,
gpointer data)
{
- HgList *l = list, *top = NULL, *prev, *next;
+ HgListIter iter;
g_return_val_if_fail (list != NULL, NULL);
+ iter = hg_list_iter_new(list);
do {
- if (top == NULL) {
- if (HG_LIST_IS_LAST_NODE (l)) {
- top = hg_list_next(l);
- } else {
- prev = hg_list_previous(l);
- if (prev && HG_LIST_IS_LAST_NODE (prev)) {
- top = l;
- }
- }
- }
- if (l->data == data) {
- prev = hg_list_previous(l);
- next = hg_list_next(l);
- hg_list_next(prev) = next;
- hg_list_previous(next) = prev;
- if (HG_LIST_IS_LAST_NODE (l)) {
- HG_LIST_SET_LAST_NODE (l, FALSE);
- HG_LIST_SET_LAST_NODE (prev, TRUE);
- top = next;
- }
- HG_LIST_SET_UNUSED (l, TRUE);
- hg_list_next(l) = NULL;
- hg_list_previous(l) = NULL;
- /* relying on GC to be really freed. */
+ if (hg_list_iter_get_data(iter) == data) {
+ list = hg_list_iter_delete_link(iter);
break;
}
- l = hg_list_next(l);
- } while (l && l != list);
+ } while (hg_list_get_iter_next(list, iter));
+ hg_list_iter_free(iter);
- /* validate node */
- if (l == NULL) {
- hg_log_warning("[BUG] no loop detected in HgList %p during removing %p",
- list, data);
- top = list;
- } else {
- if (!top)
- top = _hg_list_get_top_node(l);
- }
-
- return top;
+ return list;
}
HgList *
diff --git a/hieroglyph/ilist.c b/hieroglyph/ilist.c
index 95fd76e..d810aba 100644
--- a/hieroglyph/ilist.c
+++ b/hieroglyph/ilist.c
@@ -268,7 +268,7 @@ _hg_list_length(HgList *list)
g_return_val_if_fail (list != NULL, 0);
do {
- l = hg_list_next(list);
+ l = hg_list_next(l);
retval++;
} while (l && l != list);
@@ -284,51 +284,20 @@ HgList *
_hg_list_remove(HgList *list,
gpointer data)
{
- HgList *l = list, *top = NULL, *prev, *next;
+ HgListIter iter;
g_return_val_if_fail (list != NULL, NULL);
+ iter = hg_list_iter_new(list);
do {
- if (top == NULL) {
- if (HG_LIST_IS_LAST_NODE (l)) {
- top = hg_list_next(l);
- } else {
- prev = hg_list_previous(l);
- if (prev && HG_LIST_IS_LAST_NODE (prev)) {
- top = l;
- }
- }
- }
- if (l->data == data) {
- prev = hg_list_previous(l);
- next = hg_list_next(l);
- hg_list_next(prev) = next;
- hg_list_previous(next) = prev;
- if (HG_LIST_IS_LAST_NODE (l)) {
- HG_LIST_SET_LAST_NODE (l, FALSE);
- HG_LIST_SET_LAST_NODE (prev, TRUE);
- top = next;
- }
- HG_LIST_SET_UNUSED (l, TRUE);
- hg_list_next(l) = NULL;
- hg_list_previous(l) = NULL;
- /* relying on GC to be really freed. */
+ if (hg_list_iter_get_data(iter) == data) {
+ list = hg_list_iter_delete_link(iter);
break;
}
- l = hg_list_next(l);
- } while (l && l != list);
+ } while (hg_list_get_iter_next(list, iter));
+ hg_list_iter_free(iter);
- /* validate node */
- if (l == NULL) {
- hg_log_warning("[BUG] no loop detected in HgList %p during removing %p",
- list, data);
- top = list;
- } else {
- if (!top)
- top = _hg_list_get_top_node(l);
- }
-
- return top;
+ return list;
}
HgList *
diff --git a/hieroglyph/version.h.in b/hieroglyph/version.h.in
index b3caa83..db1f2ba 100644
--- a/hieroglyph/version.h.in
+++ b/hieroglyph/version.h.in
@@ -29,7 +29,7 @@
G_BEGIN_DECLS
#define HIEROGLYPH_VERSION "@VERSION@"
-#define HIEROGLYPH_UUID "69f65847-4e4c-4073-a94a-8bd15280477b"
+#define HIEROGLYPH_UUID "be34594e-1d16-42e7-81fa-738241b37d81"
const char *__hg_rcsid G_GNUC_UNUSED = "$Rev$";
diff --git a/tests/hglist.c b/tests/hglist.c
index b6bea30..a76b925 100644
--- a/tests/hglist.c
+++ b/tests/hglist.c
@@ -13,6 +13,8 @@ main(void)
HgListIter iter;
gint tc1[] = {1, 2, 3, 0};
gint tc2[] = {1, 3, 0};
+ gint tc3[] = {2, 3, 0};
+ gint tc4[] = {1, 2, 0};
gint i;
allocator = hg_allocator_new(hg_allocator_bfit_get_vtable());
@@ -66,6 +68,83 @@ main(void)
return 1;
}
+ /* testcase 2 */
+ list = hg_list_new(pool);
+ if (list == NULL) {
+ g_print("Failed to create an list.\n");
+ return 1;
+ }
+
+ list = hg_list_append(list, GINT_TO_POINTER (1));
+ list = hg_list_append(list, GINT_TO_POINTER (2));
+ list = hg_list_append(list, GINT_TO_POINTER (3));
+
+ list = hg_list_remove(list, GINT_TO_POINTER (1));
+ iter = hg_list_iter_new(list);
+ if (iter == NULL) {
+ g_print("Failed to create an iter.\n");
+ return 1;
+ }
+ for (i = 0; ; i++) {
+ if (tc3[i] != GPOINTER_TO_INT (hg_list_iter_get_data(iter))) {
+ g_print("Failed to compare: expected %d, but actually %d",
+ tc3[i], GPOINTER_TO_INT (hg_list_iter_get_data(iter)));
+ return 1;
+ }
+ if (!hg_list_get_iter_next(list, iter))
+ break;
+ }
+ if (i != 1) {
+ g_print("Expected list size 2 but actually was %d\n", i + 1);
+ return 1;
+ }
+
+ /* testcase 3 */
+ list = hg_list_new(pool);
+ if (list == NULL) {
+ g_print("Failed to create an list.\n");
+ return 1;
+ }
+
+ list = hg_list_append(list, GINT_TO_POINTER (1));
+ list = hg_list_append(list, GINT_TO_POINTER (2));
+ list = hg_list_append(list, GINT_TO_POINTER (3));
+
+ list = hg_list_remove(list, GINT_TO_POINTER (3));
+ iter = hg_list_iter_new(list);
+ if (iter == NULL) {
+ g_print("Failed to create an iter.\n");
+ return 1;
+ }
+ for (i = 0; ; i++) {
+ if (tc4[i] != GPOINTER_TO_INT (hg_list_iter_get_data(iter))) {
+ g_print("Failed to compare: expected %d, but actually %d",
+ tc4[i], GPOINTER_TO_INT (hg_list_iter_get_data(iter)));
+ return 1;
+ }
+ if (!hg_list_get_iter_next(list, iter))
+ break;
+ }
+ if (i != 1) {
+ g_print("Expected list size 2 but actually was %d\n", i + 1);
+ return 1;
+ }
+
+ /* testcase 4 */
+ list = hg_list_new(pool);
+ if (list == NULL) {
+ g_print("Failed to create an list.\n");
+ return 1;
+ }
+
+ list = hg_list_append(list, GINT_TO_POINTER (1));
+
+ list = hg_list_remove(list, GINT_TO_POINTER (1));
+ if (list != NULL) {
+ g_print("Expected list size 0 but actually was %d\n", hg_list_length(list));
+ return 1;
+ }
+
hg_mem_pool_destroy(pool);
hg_allocator_destroy(allocator);
hg_mem_finalize();