1 /*-------------------------------------------------------------------------- 2 Project: HorizonSDK 3 File: rdt_RingBuffer.h 4 5 Copyright 2009 Nintendo. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Date:: 2010-04-30#$ 14 $Rev: 15289 $ 15 $Author: hiratsu_daisuke $ 16 *-------------------------------------------------------------------------*/ 17 18 #include "stdafx.h" 19 20 #ifndef NN_RDT_RINGBUFFER_H_ 21 #define NN_RDT_RINGBUFFER_H_ 22 23 #include "rdt_Utility.h" 24 25 26 namespace nn { namespace rdt { namespace CTR { 27 28 /* Please see man pages for details 29 30 */ 31 class RingBuffer{ 32 public: 33 /* Please see man pages for details 34 35 */ 36 RingBuffer(void); 37 38 /* Please see man pages for details 39 40 */ 41 RingBuffer(void *pBuf, size_t len); 42 43 /* Please see man pages for details 44 45 */ 46 ~RingBuffer(void); 47 48 /* Please see man pages for details 49 50 51 */ 52 void Initialize(void *pBuf, size_t len); 53 54 /* Please see man pages for details 55 56 */ 57 void Finalize(void); 58 59 60 // Removes n bytes from the start of the data string. 61 // When a numerical value larger than the length of the data string is specified, all data is removed. 62 void Pop(size_t n); 63 64 // n bytes of data indicated by pBuf is appended to the end of the existing data string. 65 // Returns false when the buffer is insufficient and the given data string cannot be written. 66 bool Push(const void *pBuf, size_t n); 67 68 // Load a maximum of n bytes of data (from the beginning of the maintained data string). 69 // At this time, the data string is not removed from the buffer. 70 // The returned value is the actual number of bytes that could be read. 71 // Returns zero if no data to read exists. 72 size_t Read(void *pBuf, size_t n) const; 73 74 // Load a maximum of n bytes of data from the position shifted offset bytes from the beginning of the data string. 75 // At this time, the data string is not removed from the buffer. 76 // The returned value is the actual number of bytes that could be read. 77 // Returns zero if no data to read exists. 78 size_t Read(void *pBuf, size_t n, size_t offset) const; 79 80 // Determines whether the data string is empty. IsEmpty(void)81 bool IsEmpty(void) const { ASSERT(m_initialized); return m_dataLength==0; } 82 83 // Empties the data string. Clear(void)84 void Clear(void) { ASSERT(m_initialized); m_pHeadData = getHeadOfBuffer(); m_dataLength = 0; } 85 86 // Returns the length (in bytes) of the currently maintained data string. GetDataSize(void)87 size_t GetDataSize(void) const { ASSERT(m_initialized); return m_dataLength; } 88 89 // Returns this buffer size (in bytes). GetBufferSize(void)90 size_t GetBufferSize(void) const { ASSERT(m_initialized); return m_bufLength; } 91 92 // Returns the number of bytes that can currently be written (the free size, in other words). GetRestSize(void)93 size_t GetRestSize(void) const { ASSERT(m_initialized); return GetBufferSize() - GetDataSize(); } 94 95 /* Please see man pages for details 96 97 */ 98 static void Test(void); 99 100 101 private: 102 /* Please see man pages for details 103 104 */ 105 RingBuffer (const RingBuffer&); 106 107 /* Please see man pages for details 108 109 */ 110 RingBuffer& operator=(const RingBuffer&); 111 112 // Returns the start address of the buffer. getHeadOfBuffer(void)113 u8* getHeadOfBuffer(void) const { return m_pHeadBuffer; } 114 115 // Returns the end address +1 for the buffer. getEndOfBuffer(void)116 u8* getEndOfBuffer(void) const { return m_pHeadBuffer + m_bufLength; } 117 118 // Returns the address of the location where the data is to be written next. 119 u8* getEndOfData(void) const; 120 121 // Subcontracting function for Read function 122 size_t read(void *pBuf, size_t n, size_t offset) const; 123 124 // For debugging. Is the passed pointer indicating in the buffer? isValidAddress(u8 * p)125 bool isValidAddress(u8 *p) const { return (getHeadOfBuffer() <= p) && (p < getEndOfBuffer()); } 126 127 u8 *m_pHeadBuffer; // Indicates the start of the buffer. 128 size_t m_bufLength; // Buffer length. 129 u8 *m_pHeadData; // Pointer indicating start of (meaningful) data. 130 // Data can be read from this position. 131 size_t m_dataLength; // Length of currently maintained data string. 132 bool m_initialized; // Has the object been initialized? 133 134 u8 m_padding[3]; // Padding 135 }; 136 137 }}} // namespace nn::rdt::CTR 138 139 #endif // end of NN_RDT_RINGBUFFER_H_ 140