硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1550|回复: 4
收起左侧

求助贴!自己实现标准环形队列

[复制链接]

7

主题

31

回帖

52

积分

初级会员

积分
52
发表于 2024-10-16 18:08:39 | 显示全部楼层 |阅读模式
本帖最后由 LY_Bear 于 2024-10-16 22:28 编辑

背景:

在实际生产中,往往不想让别人动我的.c文件,只给其他人一个头文件和对应的库供别人使用,
因此,就要对.c/h文件整体重新设计,隐藏实现细节。

实践:
下面是我根据这种思想写的一个典型的环形队列代码,供大家参考及验证。
由于本人新手上路,所以代码中定有考虑不周之处,希望各位大神不吝赐教

circular_queue.c (4.01 KB, 下载次数: 7)


circular_queue.h (1.61 KB, 下载次数: 3)



回复

使用道具 举报

7

主题

31

回帖

52

积分

初级会员

积分
52
 楼主| 发表于 2024-10-16 22:34:29 | 显示全部楼层
本帖最后由 LY_Bear 于 2024-10-17 11:37 编辑


2024/10/16 18:21
开始写的部分代码,有些地方错了,我更新了一下,
下面在CubeIDE编译测试已通过。
更详细的测试,还有待测试。
1729089062114.png

2024/10/16 22:21
在调试的过程中发现,变量表中接口函数有地址外,经过接口函数的强制转换处理后
interface部分地址就发生变化,直接变成硬件错误了。问题还在找。

2024/10/16 23:41
每次函数内的强制类型转换会改变interface的指针值,因此,每次执行完函数后要重新刷写interface值,
使其指向接口组。
害~,感觉还是没有实现好,这样还不如数据加函数的组合。

2024/10/17 12:00
利用昨晚时间和午休的时间,重新改动了代码,个人Demo测试已经通过。
测试代码放在头文件中,以供感兴趣的哥们儿验证和检查。
现在可以实现只看头文件,调用统一接口即可实现所有操作;
不必知道也不用看.c文件中的细节部分和结构中的数据部分。

为了各位更方便阅读,下面依次粘贴 .h和.c文件内容。
内容以最新时间戳更新为准。


[C] 纯文本查看 复制代码
#ifndef __CIRCULAR_QUEUE_H__
#define __CIRCULAR_QUEUE_H__

/*---------------------------------Inc----------------------------------*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/*---------------------------------Define--------------------------------*/

/*---------------------------------Typedef--------------------------------*/

typedef uint16_t cqcType_t; // circular queue control member type
typedef void* circularQueue_t;

/* circular queue interface */
typedef struct circularQueueInterface {
    void* (*init)(cqcType_t n);
    bool (*push)(void* cqueue, uint8_t value);
    bool (*pop)(void* cqueue, uint8_t* value);
    bool (*isEmpty)(const void* cqueue);
    bool (*isFull)(const void* cqueue);
    bool (*peekHead)(const void* cqueue, uint8_t* value);
    bool (*peekTail)(const void* cqueue, uint8_t* value);
    cqcType_t (*getSize)(const void* cqueue);
    cqcType_t (*getCount)(const void* cqueue);
    void (*clear)(void* cqueue);
    void (*deInit)(void* cqueue);
} circularQueueInterface_t;

/*---------------------------------Interface--------------------------------*/

/*  向外提供接口组件,circular_queue.c内部已实现 */
extern const circularQueueInterface_t cqInterface;

/*-----------------------------------Macro----------------------------------*/

/* 安全释放宏 */
#define cqSafeDeInit(cqueue)    \
    cqInterface.deInit(cqueue); \
    cqueue = NULL

/*-----------------------------------Test----------------------------------*/

/* 测试用例 */
// void testCircularQueue(void)
// {
//     circularQueue_t uart1_cq = cqInterface.init(128);
//     for (int i = 0; i < 127; i++) {
//         cqInterface.push(uart1_cq, i);
//     }
//     cqcType_t temp = cqInterface.getSize(uart1_cq);
//     temp = cqInterface.getCount(uart1_cq);
//     uint8_t value = 0;
//     cqInterface.peekHead(uart1_cq, &value);
//     cqInterface.peekTail(uart1_cq, &value);
//     for (int i = 0; i < 127; i++) {
//         cqInterface.pop(uart1_cq, &value);
//     }
//     cqInterface.clear(uart1_cq);
//     cqSafeDeInit(uart1_cq);
// }

#endif // !__CIRCULAR_QUEUE_H__

-------------------------------------------------------------------------
[C] 纯文本查看 复制代码
/*---------------------------------Inc----------------------------------*/
#include "circular_queue.h"

/*---------------------------------Define--------------------------------*/

/*---------------------------------Typedef--------------------------------*/
typedef struct circularQueueData {
    uint8_t* _queue; // data pointer
    cqcType_t head; // head index
    cqcType_t tail; // tail index
    cqcType_t size; // queue size
    cqcType_t count; // queue counter
} circularQueueData_t;

/*---------------------------------Declaration--------------------------------*/
void* cqInit(cqcType_t n);
bool cqPush(void* cqueue, uint8_t value);
bool cqPop(void* cqueue, uint8_t* value);
bool cqIsEmpty(const void* cqueue);
bool cqIsFull(const void* cqueue);
bool cqPeekHead(const void* cqueue, uint8_t* value);
bool cqPeekTail(const void* cqueue, uint8_t* value);
cqcType_t cqGetSize(const void* cqueue);
cqcType_t cqGetCount(const void* cqueue);
void cqClear(void* cqueue);
void cqDeInit(void* cqueue);

/*---------------------------------Global--------------------------------*/
const circularQueueInterface_t cqInterface = {
    .init = cqInit,
    .push = cqPush,
    .pop = cqPop,
    .isEmpty = cqIsEmpty,
    .isFull = cqIsFull,
    .peekHead = cqPeekHead,
    .peekTail = cqPeekTail,
    .getSize = cqGetSize,
    .getCount = cqGetCount,
    .clear = cqClear,
    .deInit = cqDeInit
};

/*---------------------------------Static--------------------------------*/

/*---------------------------------Function--------------------------------*/
void* cqInit(cqcType_t n)
{
    if (n == 0) {
        return NULL;
    }
    circularQueueData_t* cq = (circularQueueData_t*)malloc(sizeof(circularQueueData_t));
    if (!cq) {
        return NULL;
    }
    cq->_queue = (uint8_t*)malloc(n * sizeof(uint8_t));
    if (!cq->_queue) {
        free(cq);
        cq = NULL;
        return NULL;
    }
    cq->head = 0;
    cq->tail = 0;
    cq->size = n;
    cq->count = 0;
    return cq;
}

// 入队
bool cqPush(void* cqueue, uint8_t value)
{
    if (NULL == cqueue || cqIsFull(cqueue)) {
        return false; // Queue is full
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    cq->_queue[cq->tail] = value;
    cq->tail = (cq->tail + 1) % cq->size;
    cq->count++;
    return true;
}

// 出队
bool cqPop(void* cqueue, uint8_t* value)
{
    if (NULL == cqueue || cqIsEmpty(cqueue)) {
        return false; // Queue is empty
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    *value = cq->_queue[cq->head];
    cq->head = (cq->head + 1) % cq->size;
    cq->count--;
    return true;
}

bool cqIsEmpty(const void* cqueue)
{
    if (NULL == cqueue) {
        return false;
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    return (0 == cq->count);
}

bool cqIsFull(const void* cqueue)
{
    if (NULL == cqueue) {
        return false;
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    return (cq->size == cq->count);
}

bool cqPeekHead(const void* cqueue, uint8_t* value)
{
    if (NULL == cqueue || cqIsEmpty(cqueue)) {
        return false; // Queue is empty
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    *value = cq->_queue[cq->head];
    return true;
}

bool cqPeekTail(const void* cqueue, uint8_t* value)
{
    if (NULL == cqueue || cqIsEmpty(cqueue)) {
        return false; // Queue is empty
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    *value = cq->_queue[(cq->tail - 1 + cq->size) % cq->size];
    return true;
}

cqcType_t cqGetSize(const void* cqueue)
{
    if (NULL == cqueue) {
        return 0;
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    return cq->size;
}

cqcType_t cqGetCount(const void* cqueue)
{
    if (NULL == cqueue) {
        return 0;
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    return cq->count;
}

void cqClear(void* cqueue)
{
    if (NULL == cqueue) {
        return;
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    cq->head = 0;
    cq->tail = 0;
    cq->count = 0;
    memset(cq->_queue, 0, (cq->size * sizeof(uint8_t)));
}

void cqDeInit(void* cqueue)
{
    if (NULL == cqueue) {
        return;
    }
    circularQueueData_t* cq = (circularQueueData_t*)cqueue;
    cq->count = 0;
    cq->head = 0;
    cq->tail = 0;
    cq->size = 0;
    free(cq->_queue);
    cq->_queue = NULL;
    free(cq);
    cq = NULL;
}


回复

使用道具 举报

6

主题

84

回帖

102

积分

初级会员

积分
102
发表于 2024-10-18 09:57:36 | 显示全部楼层
大佬太谦虚了,这个有点像是模仿C++的写法是吗?这个后面你打算在实际应用中封装成一个lib文件吗?
回复

使用道具 举报

7

主题

31

回帖

52

积分

初级会员

积分
52
 楼主| 发表于 2024-10-24 22:53:49 | 显示全部楼层
lizhaoming 发表于 2024-10-18 09:57
大佬太谦虚了,这个有点像是模仿C++的写法是吗?这个后面你打算在实际应用中封装成一个lib文件吗?

我也刚开始学着这种写法,算初学者吧。你可以这么做,但是我准备完善一下,公开出来给大伙用,看看可有没考虑到的地方。
一起学习一起进步呀!
回复

使用道具 举报

7

主题

31

回帖

52

积分

初级会员

积分
52
 楼主| 发表于 2024-10-24 23:18:54 | 显示全部楼层
本帖最后由 LY_Bear 于 2024-10-26 12:30 编辑
LY_Bear 发表于 2024-10-16 22:34
2024/10/16 18:21
开始写的部分代码,有些地方错了,我更新了一下,
下面在CubeIDE编译测试已通过。

今天把完善后的代码公布出来,以供审阅与使用。

---------------------------------------------------------------更新---------------------------------------------------------------
1、增加在3个注释标记为 (DMA use) 的接口,用于DMA对队列的连续写入和空闲中断回调函数中对于头尾索引的维护。
2、移除Pop和Push接口,增加PopN和PushN接口,减少函数调用次数,并优化了入口错误判断。

-----------------------------------------------------以下分别为.h与.c文件-----------------------------------------------------
.H文件
#ifndef __RING_QUEUE_H__
#define __RING_QUEUE_H__

#ifdef __cplusplus
extern "C"
{
#endif
/*---------------------------------Inc----------------------------------*/
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
    /*---------------------------------Define--------------------------------*/

    /*---------------------------------Typedef--------------------------------*/

    typedef uint16_t rqcType_t; // ring queue control member type
    typedef void *ringQueue_t;  // ring queue type, private data type

    /* ring queue interface */
    typedef struct _RING_QUEUE_INTERFACE
    {
        void *(*init)(rqcType_t n);                                     // initialize the ring queue
        rqcType_t (*pushN)(void *rqueue, uint8_t *buffer, rqcType_t n); // push an element into the queue
        rqcType_t (*popN)(void *rqueue, uint8_t *buffer, rqcType_t n);  // pop an element from the queue
        bool (*updateHead)(void *rqueue, rqcType_t num);                // (DMA use) update the head index
        bool (*updateTail)(void *rqueue, rqcType_t num);                // (DMA use) update the tail index
        uint8_t *(*getHeadPointer)(const void *rqueue);                 // (DMA use) get the head pointer
        bool (*isEmpty)(const void *rqueue);                            // check if the queue is empty
        bool (*isFull)(const void *rqueue);                             // check if the queue is full
        bool (*peekHead)(const void *rqueue, uint8_t *value);           // peek the head element
        bool (*peekTail)(const void *rqueue, uint8_t *value);           // peek the tail element
        rqcType_t (*getSize)(const void *rqueue);                       // get the size of the queue
        rqcType_t (*getCount)(const void *rqueue);                      // gets the number of existing members of the queue.
        void (*clear)(void *rqueue);                                    // clear the queue
        void (*deInit)(void *rqueue);                                   // de-initialize the ring queue
    } ringQueueInterface_ts;

    /*---------------------------------Interface--------------------------------*/

    /*  向外提供接口组件,.c文件中已全部实现 */
    extern const ringQueueInterface_ts rqInterface;

/*-----------------------------------Macro----------------------------------*/

/* 安全释放宏 */
#define rqSafeDeInit(rqueue)        \
    do                              \
    {                               \
        rqInterface.deInit(rqueue); \
        rqueue = NULL;              \
    } while (0)

    /*-----------------------------------Test----------------------------------*/

    /* 测试用例 */
    // void testringQueue(void) {
    //  ringQueue_t uart1_rq = rqInterface.init(128);
    //  for (int i = 0; i < 127; i++) {
    //      rqInterface.pushN(uart1_rq, &i, 1);
    //  }
    //  rqcType_t temp = rqInterface.getSize(uart1_rq);
    //  temp = rqInterface.getCount(uart1_rq);
    //  uint8_t *rq_ptr = rqInterface.getHeadPointer(uart1_rq);
    //  uint8_t value = 0;
    //  value = rq_ptr[2];
    //  rq_ptr = NULL;
    //  rqInterface.peekHead(uart1_rq, &value);
    //  rqInterface.peekTail(uart1_rq, &value);
    //  for (int i = 0; i < 127; i++) {
    //      rqInterface.popN(uart1_rq, &value, 1);
    //  }
    //  rqInterface.clear(uart1_rq);
    //  rqSafeDeInit(uart1_rq);
    // }

#ifdef __cplusplus
}
#endif

#endif // !__ring_QUEUE_H__



C文件
/*---------------------------------Inc----------------------------------*/
#include "ringqueue.h"

/*---------------------------------Define--------------------------------*/

/*---------------------------------Typedef--------------------------------*/
typedef struct _RING_QUEUE_DATA
{
    uint8_t *queue;  // data pointer
    rqcType_t head;  // head index
    rqcType_t tail;  // tail index
    rqcType_t size;  // queue size
    rqcType_t count; // queue counter
} ringQueueData_ts;

/*---------------------------------Declaration--------------------------------*/
void *rqInit(rqcType_t n);
rqcType_t rqPushN(void *rqueue, uint8_t *buffer, rqcType_t n);
rqcType_t rqPopN(void *rqueue, uint8_t *buffer, rqcType_t n);
bool rqUpdateHead(void *rqueue, rqcType_t num);
bool rqUpdateTail(void *rqueue, rqcType_t num);
uint8_t *rqGetHeadPointer(const void *rqueue); // get the head pointer
bool rqIsEmpty(const void *rqueue);
bool rqIsFull(const void *rqueue);
bool rqPeekHead(const void *rqueue, uint8_t *value);
bool rqPeekTail(const void *rqueue, uint8_t *value);
rqcType_t rqGetSize(const void *rqueue);
rqcType_t rqGetCount(const void *rqueue);
void rqClear(void *rqueue);
void rqDeInit(void *rqueue);

/*---------------------------------Global--------------------------------*/
const ringQueueInterface_ts rqInterface = {.init = rqInit,
                                           .pushN = rqPushN,
                                           .popN = rqPopN,
                                           .getHeadPointer = rqGetHeadPointer,
                                           .updateHead = rqUpdateHead,
                                           .updateTail = rqUpdateTail,
                                           .isEmpty = rqIsEmpty,
                                           .isFull = rqIsFull,
                                           .peekHead = rqPeekHead,
                                           .peekTail = rqPeekTail,
                                           .getSize = rqGetSize,
                                           .getCount = rqGetCount,
                                           .clear = rqClear,
                                           .deInit = rqDeInit};

/*---------------------------------Static--------------------------------*/

/*---------------------------------Function--------------------------------*/

/**
* @brief   initialize the ring queue
*
* @param n         the size of the queue
* @return void*    the pointer to the ring queue
*/
void *rqInit(rqcType_t n)
{
    if (n == 0)
    {
        return NULL;
    }
    ringQueueData_ts *rq = (ringQueueData_ts *)malloc(
        sizeof(ringQueueData_ts));
    if (!rq)
    {
        return NULL;
    }
    rq->queue = (uint8_t *)malloc(n * sizeof(uint8_t));
    if (!rq->queue)
    {
        free(rq);
        rq = NULL;
        return NULL;
    }
    rq->head = 0;
    rq->tail = 0;
    rq->size = n;
    rq->count = 0;
    return rq;
}

/**
* @brief   Push N elements into the queue
*
* @param rqueue        Pointer to the queue
* @param buffer        Pointer to the buffer
* @param n             Number of elements to push
* @return rqcType_t    Number of elements pushed
*/
rqcType_t rqPushN(void *rqueue, uint8_t *buffer, rqcType_t n)
{
    static rqcType_t i = 0;
    if (NULL == rqueue || 0 == n)
    {
        return 0; // Queue is invalid parameters
    }
    ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    if (rq->size == rq->count)
    {
        return 0; // Queue is full
    }

    // to push n elements into the queue
    for (i = 0; i < n; i++)
    {
        rq->queue[rq->tail] = buffer[i];
        rq->tail = (rq->tail + 1) % rq->size;
        rq->count++;
        if (rq->size == rq->count)
        {
            break;
        }
    }
    return i; // Number of elements pushed
}

/**
* @brief   Pop N elements from the queue
*
* @param rqueue        Pointer to the queue
* @param buffer        Pointer to the buffer
* @param n             Number of elements to pop
* @return rqcType_t    Number of elements popped
*/
rqcType_t rqPopN(void *rqueue, uint8_t *buffer, rqcType_t n)
{
    static rqcType_t i = 0;
    if (NULL == rqueue || 0 == n)
    {
        return 0; // Queue is empty or invalid parameters
    }
    ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    if (0 == rq->count)
    {
        return 0; // Queue is empty
    }

    // to pop n elements from the queue
    for (i = 0; i < n; i++)
    {
        buffer[i] = rq->queue[rq->head];
        rq->head = (rq->head + 1) % rq->size;
        rq->count--;
        if (0 == rq->count)
        {
            break; // Queue is empty
        }
    }
    return i; // Number of elements popped
}

/**
* @brief   This is a function used to get the first address pointer of the data part in the ring queue structure.
*
* @param rqueue        Pointer to the ring queue structure.
* @return uint8_t*     The first address pointer of the data part in the ring queue structure
* @return NULL         If the ring queue pointer is NULL
*/
uint8_t *rqGetHeadPointer(const void *rqueue)
{
    if (NULL == rqueue)
    {
        return NULL;
    }
    const ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    return rq->queue;
}

/**
* @brief This is a function used to update the Head index in the ring queue structure.
*  It has two parameters, the ring queue pointer rqueue and the number of bytes that have been dequeued num.
*
* @param rqueue    Pointer to the ring queue structure.
* @param num       Number of bytes that have been dequeued.
* @return true     If the head index is updated successfully.
* @return false    If the head index is not updated.
*/
bool rqUpdateHead(void *rqueue, rqcType_t num)
{
    if (NULL == rqueue)
    {
        return false;
    }
    ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    if (rq->count < num)
    {
        return false;
    }

    // to update the head index
    if (0 == num)
    {
        return true;
    }
    rq->head = (rq->head + num) % rq->size;
    rq->count -= num;
    return true;
}

/**
* @brief This is a function used to update the Tail index in the ring queue structure.
*  It has two parameters, the ring queue pointer rqueue and the number of bytes written to the queue num.
*
* @param rqueue    Pointer to the ring queue structure.
* @param num       Number of bytes written to the queue.
* @return true     If the tail index is updated successfully.
* @return false    If the tail index is not updated.
*/
bool rqUpdateTail(void *rqueue, rqcType_t num)
{
    if (NULL == rqueue)
    {
        return false;
    }
    ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    if ((rq->count + num) > rq->size)
    {
        return false;
    }

    // to update the tail index
    if (0 == num)
    {
        return true;
    }
    rq->tail = (rq->tail + num) % rq->size;
    rq->count += num;
    return true;
}

/**
* @brief   This is a function used to check if the ring queue is empty.
*
* @param rqueue    Pointer to the ring queue structure.
* @return true     If the ring queue is empty.
* @return false    If the ring queue is not empty.
*/
bool rqIsEmpty(const void *rqueue)
{
    if (NULL == rqueue)
    {
        return false;
    }
    const ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    return ((0 == rq->count) ? true : false);
}

/**
* @brief   This is a function used to check if the ring queue is full.
*
* @param rqueue    Pointer to the ring queue structure.
* @return true     If the ring queue is full.
* @return false    If the ring queue is not full.
*/
bool rqIsFull(const void *rqueue)
{
    if (NULL == rqueue)
    {
        return false;
    }
    const ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    return ((rq->size == rq->count) ? true : false);
}

/**
* @brief   This is a function used to peek the element at the head of the ring queue.
*
* @param rqueue    Pointer to the ring queue structure.
* @param value     Pointer to the variable where the value will be stored.
* @return true     If the element was peeked successfully.
* @return false    If the ring queue is empty or the pointer to the ring queue is NULL.
*/
bool rqPeekHead(const void *rqueue, uint8_t *value)
{
    if (NULL == rqueue)
    {
        return false; // rqueue is NULL
    }
    const ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    if (0 == rq->count)
    {
        return false; // Queue is empty
    }
    *value = rq->queue[rq->head];
    return true;
}

/**
* @brief   This is a function used to peek the element at the tail of the ring queue.
*
* @param rqueue    Pointer to the ring queue structure.
* @param value     Pointer to the variable where the value will be stored.
* @return true     If the element was peeked successfully.
* @return false    If the ring queue is empty or the pointer to the ring queue is NULL.
*/
bool rqPeekTail(const void *rqueue, uint8_t *value)
{
    if (NULL == rqueue)
    {
        return false; // rqueue is NULL
    }
    const ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    if (0 == rq->count)
    {
        return false; // Queue is empty
    }
    *value = rq->queue[(rq->tail - 1 + rq->size) % rq->size];
    return true;
}

/**
* @brief   This is a function used to get the size of the ring queue.
*
* @param rqueue        Pointer to the ring queue structure.
* @return rqcType_t    The size of the ring queue.
*/
rqcType_t rqGetSize(const void *rqueue)
{
    if (NULL == rqueue)
    {
        return 0;
    }
    const ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    return rq->size;
}

/**
* @brief   This is a function used to get the number of elements in the ring queue.
*
* @param rqueue        Pointer to the ring queue structure.
* @return rqcType_t    The number of elements in the ring queue.
*/
rqcType_t rqGetCount(const void *rqueue)
{
    if (NULL == rqueue)
    {
        return 0;
    }
    const ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    return rq->count;
}

/**
* @brief   This is a function used to clear the ring queue.
*
* @param rqueue    Pointer to the ring queue structure.
*/
void rqClear(void *rqueue)
{
    if (NULL == rqueue)
    {
        return;
    }
    ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    rq->head = 0;
    rq->tail = 0;
    rq->count = 0;
    memset(rq->queue, 0, (rq->size * sizeof(uint8_t)));
}

/**
* @brief   This is a function used to deinitialize the ring queue.
*
* @param rqueue    Pointer to the ring queue structure.
* @attention   This function will free the memory allocated for the ring queue.
*/
void rqDeInit(void *rqueue)
{
    if (NULL == rqueue)
    {
        return;
    }
    ringQueueData_ts *rq = (ringQueueData_ts *)rqueue;
    rq->count = 0;
    rq->head = 0;
    rq->tail = 0;
    rq->size = 0;
    free(rq->queue);
    rq->queue = NULL;
    free(rq);
    rq = NULL;
}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-6-10 23:05 , Processed in 0.269167 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表