aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-02-10 15:41:55 +0100
committerHugo Hörnquist <hugo@hornquist.se>2019-02-10 15:41:55 +0100
commit1e0c225b5728990bb0c96cd1297523e35e6646b4 (patch)
tree1e14b3804b3d4108c6687bdc6d01e13dc2705f1d
parentClean up vcal, add key_val type. (diff)
downloadcalp-1e0c225b5728990bb0c96cd1297523e35e6646b4.tar.gz
calp-1e0c225b5728990bb0c96cd1297523e35e6646b4.tar.xz
Fix linked_list RESET.
-rw-r--r--linked_list.h4
-rw-r--r--linked_list.inc.h23
2 files changed, 16 insertions, 11 deletions
diff --git a/linked_list.h b/linked_list.h
index 35e90d9e..4e9ca042 100644
--- a/linked_list.h
+++ b/linked_list.h
@@ -55,6 +55,10 @@ int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new );
int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist );
int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist );
+/*
+ * Resets a linked list by removing all it's objects.
+ * FREE's all elements stored in the list.
+ */
int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist );
LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new);
diff --git a/linked_list.inc.h b/linked_list.inc.h
index 6bf3d158..8de8d06b 100644
--- a/linked_list.inc.h
+++ b/linked_list.inc.h
@@ -148,19 +148,20 @@ LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new) {
}
int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {
- INFO_F("|llist| = %i", SIZE(LLIST(TYPE))(llist));
-#if 1
- FOR(LLIST(TYPE), link, llist) {
- // INFO_F("Freeing link '%p' → '%p'", link, link->value);
- INFO("leaking memmory, or something");
+
+ LINK(TYPE) *link = FIRST(llist), *next;
+ /*
+ * Manual looping rather than itterators since we destroyed the
+ * loop variable.
+ */
+ while (link != llist->tail) {
+ next = link->after;
FFREE(LINK(TYPE), link);
- // UNLINK(LINK(TYPE))(link);
+ link = next;
}
-#else
- // TODO This is a memmory leak
- FIRST(llist) = llist->tail;
- LAST(llist) = llist->head;
-#endif
+
+ llist->cur = llist->head;
+
return 0;
}