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_LIBRARIES_RDT_CTR_RDT_RING_BUFFER_H_ 19 #define NN_LIBRARIES_RDT_CTR_RDT_RING_BUFFER_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 series. 59 // If a value larger than the length of the data series is specified, all of the data is deleted. 60 void Pop(size_t n); 61 62 // A data string of n bytes indicated by pBuf is appended at the end of the existing data series. 63 // If the buffer is insufficient or if the passed data series cannot be written, FALSE is returned. 64 bool Push(const void *pBuf, size_t n); 65 66 // A maximum of n bytes of data (from the start of the maintained data series) is read out. 67 // The data series is not deleted from the buffer at this time. 68 // The return value is the byte count that could actually be read out. 69 // If there is no data to read out, zero is returned. 70 size_t Read(void *pBuf, size_t n) const; 71 72 // Reads out a maximum of n bytes from a position shifted offset bytes from the start of the data series. 73 // The data series is not deleted from the buffer at this time. 74 // The return value is the byte count that could actually be read out. 75 // If there is no data to read out, zero is returned. 76 size_t Read(void *pBuf, size_t n, size_t offset) const; 77 78 // Determines whether the data series is empty. IsEmpty(void)79 bool IsEmpty(void) const { ASSERT(m_initialized); return m_dataLength==0; } 80 81 // Empties the data series. Clear(void)82 void Clear(void) { ASSERT(m_initialized); m_pHeadData = getHeadOfBuffer(); m_dataLength = 0; } 83 84 // Returns the length, in bytes, of the data series currently maintained. GetDataSize(void)85 size_t GetDataSize(void) const { ASSERT(m_initialized); return m_dataLength; } 86 87 // Returns the size, in bytes, of this buffer. 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 of the buffer, +1. getEndOfBuffer(void)114 u8* getEndOfBuffer(void) const { return m_pHeadBuffer + m_bufLength; } 115 116 // Returns the address where the next data should be written. 117 u8* getEndOfData(void) const; 118 119 // Subcontracting function for the Read function. 120 size_t read(void *pBuf, size_t n, size_t offset) const; 121 122 // For debugging. Does the passed pointer point to inside 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 to the start of the (meaningful) data. 128 // Data can be read out from this position. 129 size_t m_dataLength; // The length of the data series currently being maintained. 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_LIBRARIES_RDT_CTR_RDT_RING_BUFFER_H_ 138