Skip to content

Commit a4e6553

Browse files
committed
Tear down list and allocated spaces
1 parent 5f1aa0d commit a4e6553

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

queue.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static inline void queue_push(struct queue_head *node, struct queue_head *head)
3131
head->prev = node;
3232
}
3333

34-
static inline int head_empty(const struct queue_head *head)
34+
static inline int queue_empty(const struct queue_head *head)
3535
{
3636
return (head->next == head);
3737
}
@@ -85,4 +85,14 @@ static inline void queue_del_init(struct queue_head *node)
8585
entry = queue_entry(entry->member.next, __typeof__(*entry), member))
8686
#endif
8787

88+
#define queue_for_each_safe(node, safe, head) \
89+
for (node = (head)->next, safe = node->next; node != (head); \
90+
node = safe, safe = node->next)
91+
92+
#define queue_for_each_entry_safe(entry, safe, head, member) \
93+
for (entry = queue_entry((head)->next, __typeof__(*entry), member), \
94+
safe = queue_entry(entry->member.next, __typeof__(*entry), member); \
95+
&entry->member != (head); entry = safe, \
96+
safe = queue_entry(safe->member.next, __typeof__(*entry), member))
97+
8898
#endif

virtio-snd.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,13 +739,16 @@ static int virtio_snd_tx_desc_handler(virtio_snd_state_t *vsnd,
739739
{
740740
uint32_t addr = node->vq_desc.addr;
741741
uint32_t len = node->vq_desc.len;
742-
fprintf(stderr, "idx %d addr %" PRIu32 " len %" PRIu32 "\n", idx, addr, len);
742+
fprintf(stderr, "idx %d addr %" PRIu32 " len %" PRIu32 "\n", idx, addr,
743+
len);
743744
if (idx == 0) { // the first descriptor
744-
const virtio_snd_pcm_xfer_t *request = (virtio_snd_pcm_xfer_t *)(base + addr);
745+
const virtio_snd_pcm_xfer_t *request =
746+
(virtio_snd_pcm_xfer_t *) (base + addr);
745747
stream_id = request->stream_id;
746748
fprintf(stderr, "stream_id %" PRIu32 "\n", stream_id);
747749
} else if (idx == cnt - 1) { // the last descriptor
748-
virtio_snd_pcm_status_t *response = (virtio_snd_pcm_status_t *)(base + addr);
750+
virtio_snd_pcm_status_t *response =
751+
(virtio_snd_pcm_status_t *) (base + addr);
749752
response->status = VIRTIO_SND_S_OK;
750753
response->latency_bytes =
751754
0; /* TODO: show the actual latency bytes */
@@ -757,6 +760,18 @@ static int virtio_snd_tx_desc_handler(virtio_snd_state_t *vsnd,
757760
idx++;
758761
}
759762

763+
/* Tear down the list and free space */
764+
idx = 0;
765+
virtq_desc_queue_node_t *tmp = NULL;
766+
queue_for_each_entry_safe(node, tmp, &q, q)
767+
{
768+
queue_del(&node->q);
769+
free(node);
770+
idx++;
771+
}
772+
assert(idx == cnt);
773+
assert(queue_empty(&q));
774+
760775
return 0;
761776
}
762777

0 commit comments

Comments
 (0)