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