Skip to content

Commit dfb08dd

Browse files
committed
Implement q_free to properly free queue elements
Previously, q_free() was defined but had no functionality. Now it correctly frees all elements in the queue and releases allocated memory. Change-Id: Id0d688bc368e94281885f6e5fca0d69aefd417f3
1 parent bda78c1 commit dfb08dd

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

queue.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,21 @@ struct list_head *q_new()
2121
}
2222

2323
/* Free all storage used by queue */
24-
void q_free(struct list_head *head) {}
24+
void q_free(struct list_head *head)
25+
{
26+
if (!head)
27+
return;
28+
if (list_empty(head)) {
29+
free(head);
30+
return;
31+
}
32+
struct list_head *node, *safe;
33+
list_for_each_safe (node, safe, head) {
34+
element_t *current = list_entry(node, element_t, list);
35+
q_release_element(current);
36+
}
37+
free(head);
38+
}
2539

2640
/* Insert an element at head of queue */
2741
bool q_insert_head(struct list_head *head, char *s)

0 commit comments

Comments
 (0)