118 lines
3.0 KiB
C
Raw Normal View History

2020-07-11 10:18:03 +08:00
/******************************************************************************
* @brief ͨ<EFBFBD>ö<EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>
*
* Copyright (c) 2017~2020, <master_roger@sina.com>
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-03-19 Morro Initial version.
******************************************************************************/
#include "queue.h"
#include <stddef.h>
#include <string.h>
/* @brief <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] q - <EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] base - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @param[in] size - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD>
* @param[in] element_size - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ش<EFBFBD>С(sizeof(type))
* @return true - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>, false - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
*/
bool queue_create(queue_t *q, void *base, int size, int element_size)
{
if (q == NULL || base == NULL || size== 0 || element_size == 0)
return false;
q->base = base;
q->size = size;
q->element_size = element_size;
q->front = q->rear = 0;
return true;
}
/* @brief <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
* @return true - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>, false - <EFBFBD><EFBFBD><EFBFBD>зǿ<EFBFBD>
*/
bool queue_is_empty(queue_t *q)
{
return q->front == q->rear;
}
/* @brief <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>
* @return true - <EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD>, false - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD>
*/
bool queue_is_full(queue_t *q)
{
return (q->rear + 1) % q->size == q->front;
}
/* @brief <20><><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β)
* @param[in] element - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
* @return true - <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, false - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
bool queue_put(queue_t *q, void *element)
{
if (queue_is_full(q))
return false;
memcpy((unsigned char *)q->base + q->rear * q->element_size, element,
q->element_size);
q->rear = (q->rear + 1) % q->size;
return true;
}
/* @brief <20><><EFBFBD><EFBFBD>(<28>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>)
* @param[in] element - ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>صĵ<EFBFBD>ַ
* @return true - <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, false - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
*/
bool queue_get(queue_t *q, void *element)
{
if (queue_is_empty(q))
return false;
memcpy(element, (unsigned char *)q->base + q->front * q->element_size,
q->element_size);
q->front = (q->front + 1) % q->size;
return true;
}
/* @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>ַ(Ԥ<><D4A4>ȡ)
* @param[in] element - ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>صĵ<EFBFBD>ַ
* @return true - <EFBFBD><EFBFBD>ȡ<EFBFBD>ɹ<EFBFBD>, false - <EFBFBD><EFBFBD>ȡʧ<EFBFBD>ܶ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
*/
bool queue_peek(queue_t *q, void **element)
{
if (queue_is_empty(q))
return false;
*element = (void *)((unsigned char *)q->base + q->front *
q->element_size);
return true;
}
/* @brief ɾ<><C9BE><EFBFBD><EFBFBD>βԪ<CEB2><D4AA>
* @return true - <EFBFBD>ɹ<EFBFBD><EFBFBD>Ƴ<EFBFBD>, false - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>,
*/
bool queue_del(queue_t *q)
{
if (queue_is_empty(q))
return false;
q->front = (q->front + 1) % q->size;
return true;
}
/* @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD>
*/
int queue_size(queue_t *q)
{
return (q->rear + q->size - q->front ) % q->size;
}
/* @brief <20><><EFBFBD>ն<EFBFBD><D5B6><EFBFBD>
* @return none
*/
void queue_clr(queue_t *q)
{
q->front = q->rear = 0;
}