본문 바로가기

예전글 목록

초간단 링버퍼 작성

8Bit MCU에서 프로젝트를 하나 하고 있다.. 그런데 간단히 RingBuffer를 작성할 필요가 생겨서 인터넷을 참조로 하여서 간단히 구현을 해보았다.

http://elonen.iki.fi/code/misc-notes/ringbuffer/

실제 사용을 할려면 약간의 수정이 필요할듯.. 뭐 간단확인 코드이니.. 쩝

#include <stdio.h>
#include <stdlib.h>

unsigned char *m_load_ptr, *m_consume_ptr;
unsigned char *m_buff_end;
unsigned char *m_buff;

unsigned char m_max_load, m_max_consume, m_data_in_buffer;


void Init_RingBuffer(void *buffer, unsigned char buffer_size);
void Add_Buffer_Data(const void *inData, unsigned char len);
void Get_Buffer_Data(void *outData, unsigned char len);
void Skip_Buffer_Data(unsigned char len);
void Update_State(void);
void Dump_RingBuffer(void);

unsigned char buffer[100];
int main(void)
{
        unsigned char rBuffer[10];
        Init_RingBuffer(buffer, 90);
        Add_Buffer_Data("AAAAA", 5);
        Add_Buffer_Data("BBBBB", 5);
        Dump_RingBuffer();
        Get_Buffer_Data(rBuffer, 5);
        Dump_RingBuffer();
        Add_Buffer_Data("CCCCC", 5);
        Dump_RingBuffer();
        return 0;
}

void Init_RingBuffer(void *buffer, unsigned char buffer_size)
{
        unsigned char m_buffer_size = buffer_size;

        m_buff = (unsigned char *)buffer;
        m_buff_end = m_buff + m_buffer_size;
        m_load_ptr = m_consume_ptr = m_buff;
        m_max_load = m_buffer_size;
        m_max_consume = m_data_in_buffer = 0;
        Update_State();
}

void Add_Buffer_Data(const void *inData, unsigned char len)
{
        if(len > (unsigned char)m_max_load)
                len = (unsigned char)m_max_load;

        memcpy(m_load_ptr, inData, len);
        m_load_ptr += len;
        m_data_in_buffer += len;
        Update_State();
}

void Get_Buffer_Data(void *outData, unsigned char len)
{
        if(len > (unsigned char)m_max_consume)
                len = (unsigned char)m_max_consume;

        memcpy(outData, m_consume_ptr, len);
        m_consume_ptr += len;
        m_data_in_buffer -= len;
        Update_State();
}

void Skip_Buffer_Data(unsigned char len)
{
        unsigned char i, skip;
        unsigned char requestedSkip = len;
        for(i=0; i<2; i++) {
                skip = (unsigned char)len;
                if(skip > m_max_consume)
                        skip = m_max_consume;

                m_consume_ptr += skip;
                m_data_in_buffer -= skip;
                len -= skip;
                Update_State();
        }
        len = requestedSkip - len;
}

void Update_State(void)
{
        if(m_consume_ptr == m_buff_end) m_consume_ptr = m_buff;
        if(m_load_ptr == m_buff_end)    m_load_ptr = m_buff;

        if(m_load_ptr == m_consume_ptr) {
                if(m_data_in_buffer > 0) {
                        m_max_load = 0;
                        m_max_consume = m_buff_end - m_consume_ptr;
                }
                else {
                        m_max_load = m_buff_end - m_load_ptr;
                        m_max_consume = 0;
                }
        }
        else if(m_load_ptr > m_consume_ptr) {
                m_max_load = m_buff_end - m_load_ptr;
                m_max_consume = m_load_ptr - m_consume_ptr;
        }
        else {
                m_max_load = m_buff_end - m_load_ptr;
                m_max_consume = m_buff_end - m_consume_ptr;
        }
}

void Dump_RingBuffer(void)
{
        unsigned char *buffStart = m_buff;
        unsigned char *buffEnd = m_buff_end;
        printf("\nDump Start===>\n");
        for(buffStart; buffStart <= buffEnd; buffStart+=1) {
                printf("%c", *buffStart);
        }
        printf("\nDump End===>\n");
}


'예전글 목록' 카테고리의 다른 글

ZigBee Chipset & Stack #2 [Ember]  (0) 2007.08.18
uC/OS 분석  (0) 2007.08.15
무선 센서네트워크를 위한 OS  (0) 2007.08.12
노트북 LCD 수리  (0) 2007.08.07
6lowpan WG  (0) 2007.08.06