gpt4 book ai didi

c - 在 C 中实现队列

转载 作者:行者123 更新时间:2023-12-02 07:04:53 25 4
gpt4 key购买 nike

<分区>

我正在尝试为 Gstreamer 缓冲区实现自定义队列。问题是,当我尝试出队时,似乎我失去了队头。每当我尝试两次出队时,都会出现段错误。我还注意到 head 总是等于 head->next。现在我不确定入队或出队是否有问题。请帮帮我。谢谢。

typedef struct _GstBUFFERQUEUE GstBufferQueue;

struct _GstBUFFERQUEUE {
GstBuffer *buf;
guint buf_size;
struct _GstBUFFERQUEUE *next;
};

void enqueue_gstbuffer(GstBufferQueue **head, GstBufferQueue **tail, guint *queue_size, GstBuffer *buf)
{
if (*queue_size == 0)
{
*head = malloc(sizeof(GstBufferQueue));
(*head)->buf = gst_buffer_try_new_and_alloc (GST_BUFFER_SIZE(buf));
(*head)->buf = gst_buffer_copy(buf);
*tail = *head;
}
else
{
if ((*tail)->next = malloc(sizeof(GstBufferQueue))) {
(*tail)->next->buf = gst_buffer_try_new_and_alloc (GST_BUFFER_SIZE(buf));
(*tail)->next->buf = gst_buffer_copy(buf);
(*tail) = (*tail)->next;
}
else {
GST_WARNING("Error allocating memory for new buffer in queue");
}
}
(*tail)->next = NULL;
(*queue_size)++;

}

void dequeue_gstbuffer(GstBufferQueue **head, GstBufferQueue **tail, guint *queue_size, GstBuffer **buf)
{
GstBufferQueue **tmpPtr = head;
GstBufferQueue **nextPtr;
*nextPtr = (*head)->next;
*buf = gst_buffer_try_new_and_alloc (GST_BUFFER_SIZE((*tmpPtr)->buf));
*buf = gst_buffer_copy((*tmpPtr)->buf);
gst_buffer_unref((*tmpPtr)->buf);
free((*tmpPtr));
*head = *nextPtr;

if ((*head) == NULL)
(*tail) = NULL;

(*queue_size)--;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com