无锁的环形队列

#ifndef _RingQueue_H_
#define _RingQueue_H_
#include <memory.h>
template<class T, unsigned int MAX_LEN = 1024>
class RingQueue
{
public:

    //-----------------------------------------------------
    // 构造
    //-----------------------------------------------------
    RingQueue()
    {
        m_nHead = 0;
        m_nTail = 0;
    }

    //-----------------------------------------------------
    // 析构
    //-----------------------------------------------------
    ~RingQueue()
    {

    }

    //-----------------------------------------------------
    // 重置,清空队列
    //-----------------------------------------------------
    void    Reset()
    {
        m_nHead = 0;
        m_nTail = 0;
    }

    //-----------------------------------------------------
    // 取得最多读取个数
    //-----------------------------------------------------
    unsigned int        GetMaxReadSize()
    {
        int size = (m_nTail - m_nHead + MAX_LEN) % MAX_LEN;
        return size;
    }

    //-----------------------------------------------------
    // 取得最多写入个数
    //-----------------------------------------------------
    unsigned int        GetMaxWriteSize()
    {
        int size = (m_nHead - m_nTail + MAX_LEN - 1) % MAX_LEN; 
        return size;
    }

    //-----------------------------------------------------
    // 添加数据
    //-----------------------------------------------------
    bool    PushData(const T* pData, unsigned int nLength = 1)
    {
        if (pData == nullptr || nLength > GetMaxWriteSize())
        {
            return false;
        }

        if (m_nTail + nLength <= MAX_LEN)
        {
            memcpy(m_Buffer + m_nTail, pData, nLength * sizeof(T));
            m_nTail = (m_nTail + nLength) % MAX_LEN;
        }
        else
        {
            unsigned int size1 = MAX_LEN - m_nTail;
            unsigned int size2 = nLength - size1;

            memcpy(m_Buffer + m_nTail, pData, size1 * sizeof(T));
            memcpy(m_Buffer, pData+size1, size2 * sizeof(T));

            m_nTail = size2;
        }

        return true;
    }

    //-----------------------------------------------------
    // 取出并删除数据
    //-----------------------------------------------------
    bool    PopData(T* pData, unsigned int nLength = 1)
    {
        if (pData == nullptr || nLength > GetMaxReadSize() || nLength <= 0)
        {
            return false;
        }

        if (m_nHead + nLength <= MAX_LEN)
        {
            memcpy(pData, m_Buffer + m_nHead, nLength * sizeof(T));
            m_nHead    = (m_nHead + nLength) % MAX_LEN;
        }
        else
        {
            unsigned int size1 = MAX_LEN - m_nHead;
            unsigned int size2 = nLength - size1;

            memcpy(pData, m_Buffer + m_nHead, size1 * sizeof(T));
            memcpy(pData + size1, m_Buffer, size2 * sizeof(T));

            m_nHead = size2;
        }

        return true;
    }

    //-----------------------------------------------------
    // 查看数据 不删除
    //-----------------------------------------------------
    bool    PeekData(T* pData, unsigned int nLen)
    {
        if (nLen > GetMaxReadSize() || nLen <= 0)
        {
            return false;
        }

        if (m_nHead + nLen <= MAX_LEN)
        {
            memcpy(pData, m_Buffer + m_nHead, nLen * sizeof(T));
        }
        else
        {
            unsigned int size1 = MAX_LEN - m_nHead;
            unsigned int size2 = nLen - size1;

            memcpy(pData, m_Buffer + m_nHead, size1 * sizeof(T));
            memcpy(pData + size1, m_Buffer, size2 * sizeof(T));
        }

        return true;
    }

    //-----------------------------------------------------
    // 删除数据
    //-----------------------------------------------------
    bool    DeleteData(unsigned int nLen)
    {
        if (nLen > GetMaxReadSize() || nLen <= 0)
        {
            return false;
        }

        if (m_nHead + nLen <= MAX_LEN )
        {
            m_nHead    = (m_nHead + nLen) % MAX_LEN;
        }
        else
        {
            m_nHead = nLen - (MAX_LEN - m_nHead);
        }

        return true;
    }

public:
    T    m_Buffer[MAX_LEN];    // 数据区
private:

    unsigned int    m_nHead;                        // 队头
    unsigned int    m_nTail;                        // 队尾

};

#endif

 

posted @ 2015-01-11 14:21  zzyoucan  阅读(212)  评论(0编辑  收藏  举报