/*---------------------------------------------------------------------------* Project: Horizon File: rdt_RingBuffer.h Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 46347 $ *---------------------------------------------------------------------------*/ #include "stdafx.h" #ifndef NN_LIBRARIES_RDT_CTR_RDT_RING_BUFFER_H_ #define NN_LIBRARIES_RDT_CTR_RDT_RING_BUFFER_H_ #include "rdt_Utility.h" namespace nn { namespace rdt { namespace CTR { /* Please see man pages for details */ class RingBuffer{ public: /* Please see man pages for details */ RingBuffer(void); /* Please see man pages for details */ RingBuffer(void *pBuf, size_t len); /* Please see man pages for details */ ~RingBuffer(void); /* Please see man pages for details */ void Initialize(void *pBuf, size_t len); /* Please see man pages for details */ void Finalize(void); // Removes n bytes from the start of the data series. // If a value larger than the length of the data series is specified, all of the data is deleted. void Pop(size_t n); // A data string of n bytes indicated by pBuf is appended at the end of the existing data series. // If the buffer is insufficient or if the passed data series cannot be written, FALSE is returned. bool Push(const void *pBuf, size_t n); // A maximum of n bytes of data (from the start of the maintained data series) is read out. // The data series is not deleted from the buffer at this time. // The return value is the byte count that could actually be read out. // If there is no data to read out, zero is returned. size_t Read(void *pBuf, size_t n) const; // Reads out a maximum of n bytes from a position shifted offset bytes from the start of the data series. // The data series is not deleted from the buffer at this time. // The return value is the byte count that could actually be read out. // If there is no data to read out, zero is returned. size_t Read(void *pBuf, size_t n, size_t offset) const; // Determines whether the data series is empty. bool IsEmpty(void) const { ASSERT(m_initialized); return m_dataLength==0; } // Empties the data series. void Clear(void) { ASSERT(m_initialized); m_pHeadData = getHeadOfBuffer(); m_dataLength = 0; } // Returns the length, in bytes, of the data series currently maintained. size_t GetDataSize(void) const { ASSERT(m_initialized); return m_dataLength; } // Returns the size, in bytes, of this buffer. size_t GetBufferSize(void) const { ASSERT(m_initialized); return m_bufLength; } // Returns the number of bytes that can currently be written (the free size, in other words). size_t GetRestSize(void) const { ASSERT(m_initialized); return GetBufferSize() - GetDataSize(); } /* Please see man pages for details */ static void Test(void); private: /* Please see man pages for details */ RingBuffer (const RingBuffer&); /* Please see man pages for details */ RingBuffer& operator=(const RingBuffer&); // Returns the start address of the buffer. u8* getHeadOfBuffer(void) const { return m_pHeadBuffer; } // Returns the end address of the buffer, +1. u8* getEndOfBuffer(void) const { return m_pHeadBuffer + m_bufLength; } // Returns the address where the next data should be written. u8* getEndOfData(void) const; // Subcontracting function for the Read function. size_t read(void *pBuf, size_t n, size_t offset) const; // For debugging. Does the passed pointer point to inside the buffer? bool isValidAddress(u8 *p) const { return (getHeadOfBuffer() <= p) && (p < getEndOfBuffer()); } u8 *m_pHeadBuffer; // Indicates the start of the buffer. size_t m_bufLength; // Buffer length u8 *m_pHeadData; // Pointer to the start of the (meaningful) data. // Data can be read out from this position. size_t m_dataLength; // The length of the data series currently being maintained. bool m_initialized; // Has the object been initialized? u8 m_padding[3]; // Padding }; }}} // namespace nn::rdt::CTR #endif // end of NN_LIBRARIES_RDT_CTR_RDT_RING_BUFFER_H_