Added ring_buffer_get_vecs() that returns iovecs describing the contents of

the buffer.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35880 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2010-03-16 17:07:40 +00:00
parent 46fe29acd8
commit fac2ce3d92
2 changed files with 39 additions and 0 deletions

View File

@ -9,6 +9,9 @@
#include <OS.h> #include <OS.h>
struct iovec;
struct ring_buffer { struct ring_buffer {
int32 first; int32 first;
int32 in; int32 in;
@ -40,6 +43,7 @@ ssize_t ring_buffer_user_read(struct ring_buffer *buffer, uint8 *data, ssize_t l
ssize_t ring_buffer_user_write(struct ring_buffer *buffer, const uint8 *data, ssize_t length); ssize_t ring_buffer_user_write(struct ring_buffer *buffer, const uint8 *data, ssize_t length);
size_t ring_buffer_peek(struct ring_buffer *buffer, size_t offset, void *data, size_t ring_buffer_peek(struct ring_buffer *buffer, size_t offset, void *data,
size_t length); size_t length);
int32 ring_buffer_get_vecs(struct ring_buffer *buffer, struct iovec *vecs);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -13,6 +13,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/uio.h>
#ifndef HAIKU_TARGET_PLATFORM_HAIKU #ifndef HAIKU_TARGET_PLATFORM_HAIKU
#define user_memcpy(x...) (memcpy(x), B_OK) #define user_memcpy(x...) (memcpy(x), B_OK)
@ -267,6 +268,40 @@ ring_buffer_peek(struct ring_buffer* buffer, size_t offset, void* data,
} }
/*! Returns iovecs describing the contents of the ring buffer.
\param buffer The ring buffer.
\param vecs Pointer to an iovec array with at least 2 elements to be filled
in by the function.
\return The number of iovecs the function has filled in to describe the
contents of the ring buffer. \c 0, if empty, \c 2 at maximum.
*/
int32
ring_buffer_get_vecs(struct ring_buffer* buffer, struct iovec* vecs)
{
if (buffer->in == 0)
return 0;
if (buffer->first + buffer->in <= buffer->size) {
// one element
vecs[0].iov_base = buffer->buffer + buffer->first;
vecs[0].iov_len = buffer->in;
return 1;
}
// two elements
size_t upper = buffer->size - buffer->first;
size_t lower = buffer->in - upper;
vecs[0].iov_base = buffer->buffer + buffer->first;
vecs[0].iov_len = upper;
vecs[1].iov_base = buffer->buffer;
vecs[1].iov_len = lower;
return 2;
}
#if 0 #if 0
/** Sends the contents of the ring buffer to a port. /** Sends the contents of the ring buffer to a port.
* The buffer will be empty afterwards only if sending the data actually works. * The buffer will be empty afterwards only if sending the data actually works.