循环缓冲区

2020-04-08  本文已影响0人  幕枫楚雨

参考

圆形缓冲区(循环buffer)实现
35.Linux-分析并制作环形缓冲区

代码

#ifndef __PRODUCT_CIRCULAR_BUFFER_H__
#define __PRODUCT_CIRCULAR_BUFFER_H__

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* End of #ifdef __cplusplus */

/********************************** include **********************************/
#include "hi_type.h"

/***************************** Macro Definition ******************************/
//#define 

/*************************** Structure Definition ****************************/
//typedef struct 
typedef struct {
    HI_U64 videoPts;
    HI_U32 videoLen;
    HI_U32 pVideoAddr;
    HI_U32 videoFrame;
} ElemType;
/* Circular buffer object */
typedef struct {
    int         size;   /* maximum number of elements           */
    int         start;  /* index of oldest element              */
    int         end;    /* index at which to write new element  */
    ElemType   *elems;  /* vector of elements                   */
} CircularBuffer;

/****************************************function****************************************/

/**function: CicbufInit()
 * description: cicbuf Init
 */
void CicbufInit(CircularBuffer *cb, int size);
/**function: CicbufDeInit()
 * description: cicbuf DeInit
 *  if deinit, can not used cicbuf 
 */
void CicbufDeInit(CircularBuffer *cb);
/**function: CicbufIsFull()
 * description: judge cicbuf full
 * return: [1]-full, [0]-not full;
 */
int CicbufIsFull(CircularBuffer *cb);
/**function: CicbufIsEmpty()
 * description: judge cicbuf empty
 * return: [1]-empty, [0]-not empty;
 */
int CicbufIsEmpty(CircularBuffer *cb);

/**function: CicbufIsInvalid()
 * description: judge cicbuf invalid
 * return: [0]-invalid, [other]-valid;
 */
int CicbufIsInvalid(CircularBuffer *cb);
/**function: CicbufSetValid()
 * description: set cicbuf invalid
 */
void CicbufSetValid(CircularBuffer *cb);

/**function: CicbufWrite()
 * description: write cicbuf 
 */
void CicbufWrite(CircularBuffer *cb, ElemType *elem);
/**function: CicbufRead()
 * description: read cicbuf 
 */
void CicbufRead(CircularBuffer *cb, ElemType *elem);
/**function: CicbufGetData()
 * description: get cicbuf data
 */
void CicbufGetData(CircularBuffer *cb, ElemType *elem);
/**function: CicbufRemoveData()
 * description: remove cicbuf data
 */
void CicbufRemoveData(CircularBuffer *cb);

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */

#endif /* End of #ifndef __PRODUCT_CIRCULAR_BUFFER_H__ */
/********************************** include **********************************/
#include <stdlib.h>
#include <unistd.h>
#include "ProductCircularBuffer.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* End of #ifdef __cplusplus */
/***************************** Macro Definition ******************************/
//#define 

/*************************** Structure Definition ****************************/
//typedef struct 

/***************************** Global Definition *****************************/

/***************************** Static Definition *****************************/
//static

/**********************************function*********************************/
/**function: CicbufInit()
 * description: cicbuf Init
 */
void CicbufInit(CircularBuffer *cb, int size)
{
    cb->size  = size;
    cb->start = 0;
    cb->end   = 0;
    cb->elems = (ElemType *)calloc(cb->size, sizeof(ElemType));
}
/**function: CicbufDeInit()
 * description: cicbuf DeInit
 *  if deinit, can not used cicbuf 
 */
void CicbufDeInit(CircularBuffer *cb)
{
    cb->size  = 0;
    cb->start = 0;
    cb->end   = 0;
    free(cb->elems);
}

/**function: CicbufPrint()
 * description: cicbuf print info
 */
static void CicbufPrint(CircularBuffer *cb)
{
    printf("size=0x%x, start=%d, end=%d\n", cb->size, cb->start, cb->end);
}

/**function: CicbufIsFull()
 * description: judge cicbuf full
 * return: [1]-full, [0]-not full;
 */
int CicbufIsFull(CircularBuffer *cb)
{
    return cb->end == (cb->start ^ cb->size); /* This inverts the most significant bit of start before comparison */ 
}
/**function: CicbufIsEmpty()
 * description: judge cicbuf empty
 * return: [1]-empty, [0]-not empty;
 */
int CicbufIsEmpty(CircularBuffer *cb)
{
    //cbPrint(cb);
    return cb->end == cb->start; 
}

/**function: CicbufIsInvalid()
 * description: judge cicbuf invalid
 * return: [0]-invalid, [other]-valid;
 */
int CicbufIsInvalid(CircularBuffer *cb)
{
    return cb->size; 
}
/**function: CicbufSetValid()
 * description: set cicbuf invalid
 */
void CicbufSetValid(CircularBuffer *cb)
{
    cb->size = 0;
}

/**function: CicbufIncr()
 * description:  cicbuf 
 */
static int CicbufIncr(CircularBuffer *cb, int p) 
{
    return (p + 1)&(2*cb->size-1); /* start and end pointers incrementation is done modulo 2*size */
}

/**function: CicbufWrite()
 * description: write cicbuf 
 */
void CicbufWrite(CircularBuffer *cb, ElemType *elem)
{
    cb->elems[cb->end&(cb->size-1)] = *elem;
    if (CicbufIsFull(cb)) /* full, overwrite moves start pointer */
    {
        cb->start = CicbufIncr(cb, cb->start);
    }
    cb->end = CicbufIncr(cb, cb->end);
}

/**function: CicbufRead()
 * description: read cicbuf 
 */
void CicbufRead(CircularBuffer *cb, ElemType *elem)
{
    *elem = cb->elems[cb->start&(cb->size-1)];
    cb->start = CicbufIncr(cb, cb->start);
}
/**function: CicbufGetData()
 * description: get cicbuf data
 */
void CicbufGetData(CircularBuffer *cb, ElemType *elem)
{
    *elem = cb->elems[cb->start&(cb->size-1)];
}
/**function: CicbufRemoveData()
 * description: remove cicbuf data
 */
void CicbufRemoveData(CircularBuffer *cb)
{
    cb->start = CicbufIncr(cb, cb->start);
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */
#define CIC_BUF_LEN 64
...
    //init circularbuf
    CicbufInit(&cicbuf, CIC_BUF_LEN);
    if(CicbufIsInvalid(&cicbuf) != 0)
    {
        CicbufWrite(&cicbuf, (ElemType *)elemtype);
    }
    if(CicbufIsEmpty(&cicbuf))
    {
        return 0;
    }
    CicbufRead(&cicbuf, (ElemType*)elemtype);
    CicbufSetValid(&cicbuf);
    //deinit circularbuf, need not used cicbuf, otherwise result in signal 6 & 11???
    CicbufDeInit(&cicbuf);
上一篇下一篇

猜你喜欢

热点阅读