mirror of
https://gitee.com/moluo-tech/CodeBrick.git
synced 2025-01-30 04:42:53 +08:00
90 lines
2.3 KiB
C
90 lines
2.3 KiB
C
/******************************************************************************
|
||
* @brief <20><><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28>ο<EFBFBD>linux/kfifo)
|
||
*
|
||
* Copyright (c) 2016~2020, <morro_luo@163.com>
|
||
*
|
||
* SPDX-License-Identifier: Apache-2.0
|
||
*
|
||
* Change Logs:
|
||
* Date Author Notes
|
||
* 2016-05-30 Morro <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
******************************************************************************/
|
||
#include "ringbuffer.h"
|
||
#include <string.h>
|
||
#include <stddef.h>
|
||
|
||
#define min(a,b) ( (a) < (b) )? (a):(b)
|
||
|
||
/*
|
||
*@brief <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ջ<EFBFBD><D5BB>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>
|
||
*@param[in] r - <20><><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*@param[in] buf - <20><><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD>
|
||
*@param[in] len - buf<75><66><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><32>N<EFBFBD><4E><EFBFBD><EFBFBD>)
|
||
*@retval bool
|
||
*/
|
||
bool ring_buf_init(ring_buf_t *r,unsigned char *buf, unsigned int len)
|
||
{
|
||
r->buf = buf;
|
||
r->size = len;
|
||
r->front = r->rear = 0;
|
||
return buf != NULL && (len & len -1) == 0;
|
||
}
|
||
|
||
/*
|
||
*@brief <20><><EFBFBD>ջ<EFBFBD><D5BB>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>
|
||
*@param[in] r - <20><><EFBFBD><EFBFBD><EFBFBD>յĻ<D5B5><C4BB>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>
|
||
*@retval none
|
||
*/
|
||
void ring_buf_clr(ring_buf_t *r)
|
||
{
|
||
r->front = r->rear = 0;
|
||
}
|
||
|
||
/*
|
||
*@brief <20><>ȡ<EFBFBD><C8A1><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||
*@retval <20><><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
|
||
*/
|
||
unsigned int ring_buf_len(ring_buf_t *r)
|
||
{
|
||
return r->rear - r->front;
|
||
}
|
||
|
||
/*
|
||
*@brief <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD>ݷŵ<DDB7><C5B5><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*@param[in] buf - <20><><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD>
|
||
* len - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*@retval ʵ<>ʷŵ<CAB7><C5B5>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
||
*/
|
||
unsigned int ring_buf_put(ring_buf_t *r,unsigned char *buf,unsigned int len)
|
||
{
|
||
unsigned int i;
|
||
unsigned int left;
|
||
left = r->size + r->front - r->rear;
|
||
len = min(len , left);
|
||
i = min(len, r->size - (r->rear & r->size - 1));
|
||
memcpy(r->buf + (r->rear & r->size - 1), buf, i);
|
||
memcpy(r->buf, buf + i, len - i);
|
||
r->rear += len;
|
||
return len;
|
||
|
||
}
|
||
|
||
/*
|
||
*@brief <20>ӻ<EFBFBD><D3BB>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ȡָ<C8A1><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>
|
||
*@param[in] len - <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
||
*@param[out] buf - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD>
|
||
*@retval ʵ<>ʶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
||
*/
|
||
unsigned int ring_buf_get(ring_buf_t *r,unsigned char *buf,unsigned int len)
|
||
{
|
||
unsigned int i;
|
||
unsigned int left;
|
||
left = r->rear - r->front;
|
||
len = min(len , left);
|
||
i = min(len, r->size - (r->front & r->size - 1));
|
||
memcpy(buf, r->buf + (r->front & r->size - 1), i);
|
||
memcpy(buf + i, r->buf, len - i);
|
||
r->front += len;
|
||
return len;
|
||
}
|