Skip to content

Commit a0800a1

Browse files
committed
Implement q_free
Using list_for_each_entry_safe to enumerate every node in the queue, then using q_release_element to free the choosing node. Finally free the head. This commit ensures that all elements are freed for avoiding memory leak. Change-Id: I434a7d16dab3e8627c6c0016ff641520067a47d9
1 parent fc2f58c commit a0800a1

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

queue.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ struct list_head *q_new()
2222
}
2323

2424
/* Free all storage used by queue */
25-
void q_free(struct list_head *head) {}
25+
void q_free(struct list_head *head)
26+
{
27+
if (!head)
28+
return;
29+
element_t *cur, *safe;
30+
/* cppcheck-suppress unknownMacro */
31+
list_for_each_entry_safe (cur, safe, head, list)
32+
q_release_element(cur);
33+
free(head);
34+
}
2635

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

0 commit comments

Comments
 (0)