00001 00013 #ifndef _CO_RING_H_ 00014 #define _CO_RING_H_ 00015 00028 /* 00029 * INCLUDE FILES 00030 **************************************************************************************** 00031 */ 00032 #include "co_int.h" 00033 #include "co_bool.h" 00034 // for NULL 00035 #include <stddef.h> 00036 00037 // for __INLINE 00038 #include "compiler.h" 00039 00040 // for assertions 00041 #include "dbg_assert.h" 00042 00043 /* 00044 * STRUCTURE DECLARATIONS 00045 **************************************************************************************** 00046 */ 00048 struct co_ring 00049 { 00051 uint8_t free_idx; 00053 uint8_t used_idx; 00055 uint8_t used_count; 00057 uint8_t max_count; 00058 }; 00059 00060 /* 00061 * FUNCTION DECLARATIONS 00062 **************************************************************************************** 00063 */ 00064 00073 __INLINE void co_ring_init(struct co_ring *ring, uint8_t max_count) 00074 { 00075 // sanity check 00076 ASSERT_ERR(ring != NULL); 00077 ASSERT_ERR(max_count != 0); 00078 00079 // initialize the ring 00080 ring->free_idx = 0; 00081 ring->used_idx = 0; 00082 ring->used_count = 0; 00083 ring->max_count = max_count; 00084 } 00085 00092 __INLINE void co_ring_write(struct co_ring *ring) 00093 { 00094 // sanity check 00095 ASSERT_ERR(ring != NULL); 00096 ASSERT_ERR(ring->used_count < ring->max_count); 00097 00098 // increment the index (roll over if necessary) 00099 ring->free_idx++; 00100 if (ring->free_idx == ring->max_count) 00101 { 00102 ring->free_idx = 0; 00103 } 00104 00105 // increment the number of used elements 00106 ring->used_count++; 00107 } 00108 00115 __INLINE void co_ring_read(struct co_ring *ring) 00116 { 00117 // sanity check 00118 ASSERT_ERR(ring != NULL); 00119 ASSERT_ERR(ring->used_count > 0); 00120 ASSERT_ERR(ring->used_count <= ring->max_count); 00121 00122 00123 // increment the index (roll over if necessary) 00124 ring->used_idx++; 00125 if (ring->used_idx == ring->max_count) 00126 { 00127 ring->used_idx = 0; 00128 } 00129 00130 // decrement the number of used elements 00131 ring->used_count--; 00132 } 00133 00141 __INLINE bool co_ring_empty(struct co_ring *ring) 00142 { 00143 // sanity check 00144 ASSERT_ERR(ring != NULL); 00145 ASSERT_ERR(ring->used_count <= ring->max_count); 00146 00147 return (ring->used_count == 0); 00148 } 00149 00157 __INLINE bool co_ring_full(struct co_ring *ring) 00158 { 00159 // sanity check 00160 ASSERT_ERR(ring != NULL); 00161 ASSERT_ERR(ring->used_count <= ring->max_count); 00162 00163 return (ring->used_count == ring->max_count); 00164 } 00165 00167 00168 #endif // _CO_RING_H_ 00169
1.6.1