Appiko
CBUF.h
Go to the documentation of this file.
1 /****************************************************************************
2 *
3 * Since this code originated from code which is public domain, I
4 * hereby declare this code to be public domain as well.
5 *
6 ****************************************************************************/
79 #if !defined( CBUF_H )
80 #define CBUF_H
82 /* ---- Include Files ---------------------------------------------------- */
83 
84 /* ---- Constants and Types ---------------------------------------------- */
85 
90 #define CBUF_Init( cbuf ) cbuf.m_getIdx = cbuf.m_putIdx = 0
91 
97 #define CBUF_Len( cbuf ) ((typeof( cbuf.m_putIdx ))(( cbuf.m_putIdx ) - ( cbuf.m_getIdx )))
98 
103 #define CBUF_Push( cbuf, elem ) (cbuf.m_entry)[ cbuf.m_putIdx++ & (( cbuf##_SIZE ) - 1 )] = (elem)
104 
109 #define CBUF_Pop( cbuf ) (cbuf.m_entry)[ cbuf.m_getIdx++ & (( cbuf##_SIZE ) - 1 )]
110 
115 #define CBUF_Get( cbuf, idx ) (cbuf.m_entry)[( cbuf.m_getIdx + idx ) & (( cbuf##_SIZE ) - 1 )]
116 
121 #define CBUF_GetEnd( cbuf, idx ) (cbuf.m_entry)[( cbuf.m_putIdx - idx - 1 ) & (( cbuf##_SIZE ) - 1 )]
122 
127 #define CBUF_IsEmpty( cbuf ) ( CBUF_Len( cbuf ) == 0 )
128 
133 #define CBUF_IsFull( cbuf ) ( CBUF_Len( cbuf ) == ( cbuf##_SIZE ))
134 
139 #define CBUF_Error( cbuf ) ( CBUF_Len( cbuf ) > cbuf##_SIZE )
140 
145 #define CBUF_GetPtr( cbuf, idx ) ((typeof( &cbuf.m_entry[0]))( cbuf.m_entry + (( cbuf.m_getIdx + idx ) & (( cbuf##_SIZE ) - 1 ))))
146 
151 #define CBUF_Remove( cbuf, idx ) ({cbuf.m_getIdx += ( idx );})
152 
153 #if defined( __cplusplus )
154 
155 template < class IndexType, unsigned Size, class EntryType >
156 class CBUF
157 {
158 public:
159 
160  CBUF()
161  {
162  m_getIdx = m_putIdx = 0;
163  }
164 
165  IndexType Len() const { return m_putIdx - m_getIdx; }
166 
167  bool IsEmpty() const { return Len() == 0; }
168  bool IsFull() const { return Len() == Size; }
169  bool Error() const { return Len() > Size; }
170 
171  void Push( EntryType val )
172  {
173  m_entry[ m_putIdx++ & ( Size - 1 )] = val;
174  }
175 
176  EntryType Pop()
177  {
178  return m_entry[ m_getIdx++ & ( Size - 1 )];
179  }
180 
181 private:
182 
183  volatile IndexType m_getIdx;
184  volatile IndexType m_putIdx;
185  EntryType m_entry[ Size ];
186 
187 };
188 
189 #endif // __cplusplus
190 
191 /* ---- Variable Externs ------------------------------------------------- */
192 /* ---- Function Prototypes ---------------------------------------------- */
193 
196 #endif // CBUF_H