@@ -219,16 +219,18 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
219219RTM_EXPORT (rt_ringbuffer_get );
220220
221221/**
222- * @brief Get the first readable byte of the ring buffer.
222+ * @brief Get data from the ring buffer in zero-copy mode .
223223 *
224224 * @param rb A pointer to the ringbuffer.
225225 * @param ptr When this function return, *ptr is a pointer to the first readable byte of the ring buffer.
226226 *
227- * @note It is recommended to read only one byte, otherwise it may cause buffer overflow.
227+ * @note This function returns a direct pointer to the internal buffer and consumes the data
228+ * (advances read_index). It returns the contiguous readable data length. If data wraps
229+ * around the buffer end, call this function again to get the remaining segment.
228230 *
229- * @return Return the size of the ring buffer.
231+ * @return Return the contiguous readable data size we consumed from the ring buffer.
230232 */
231- rt_size_t rt_ringbuffer_peek (struct rt_ringbuffer * rb , rt_uint8_t * * ptr )
233+ rt_size_t rt_ringbuffer_get_direct (struct rt_ringbuffer * rb , rt_uint8_t * * ptr )
232234{
233235 rt_size_t size ;
234236
@@ -259,6 +261,44 @@ rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
259261
260262 return size ;
261263}
264+ RTM_EXPORT (rt_ringbuffer_get_direct );
265+
266+ /**
267+ * @brief Get the first readable byte of the ring buffer.
268+ *
269+ * @param rb A pointer to the ringbuffer.
270+ * @param ptr When this function return, *ptr is a pointer to the first readable byte of the ring buffer.
271+ *
272+ * @note It is recommended to read only one byte, otherwise it may cause buffer overflow.
273+ *
274+ * @return Return the size of the ring buffer.
275+ */
276+ rt_size_t rt_ringbuffer_peek (struct rt_ringbuffer * rb , rt_uint8_t * * ptr )
277+ {
278+ rt_size_t size ;
279+
280+ RT_ASSERT (rb != RT_NULL );
281+
282+ * ptr = RT_NULL ;
283+
284+ /* whether has enough data */
285+ size = rt_ringbuffer_data_len (rb );
286+
287+ /* no data */
288+ if (size == 0 )
289+ return 0 ;
290+
291+ * ptr = & rb -> buffer_ptr [rb -> read_index ];
292+
293+ if ((rt_size_t )(rb -> buffer_size - rb -> read_index ) > size )
294+ {
295+ return size ;
296+ }
297+
298+ size = rb -> buffer_size - rb -> read_index ;
299+
300+ return size ;
301+ }
262302RTM_EXPORT (rt_ringbuffer_peek );
263303
264304/**
0 commit comments