15#define RING_INDEX(ring, i) ((i) % ((ring)->size))
26#define RINGBUF_NEW(sz) (&(struct ringbuf){.buf = (char[sz]){0}, .size = sz, .head = 0, .tail = 0})
96 if (off < 0 || (
size_t) off >= available)
99 n =
MIN(n, available - (
size_t) off);
103 for (
size_t i = 0; i < n; ++i) {
152 while (done < n && ring->
head != ring->
tail) {
158 return done ? (
ssize_t) done : r;
181 return done ? (
ssize_t) done : r;
ssize_t iobuf_out(struct iobuf *io, const void *src, size_t n)
Copies bytes from the kernel into the iobuf's buffer, and moves past them.
ssize_t iobuf_in(struct iobuf *io, void *dst, size_t n)
Copies bytes from the iobuf's buffer into the kernel, and moves past them.
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
void kfree(void *)
Frees an object that kmalloc() gave.
static size_t ringbuf_read(struct ringbuf *ring, size_t n, char *buf)
Reads up to n bytes from the ring into buf, and returns how many there were.
#define RING_INDEX(ring, i)
static size_t ringbuf_write_overwrite(struct ringbuf *ring, size_t n, char *buf)
Writes n bytes into the ring, throwing away its oldest bytes if it is full (the kernel's messages).
static size_t ringbuf_available(struct ringbuf *ring)
Returns how many bytes the ring holds.
static void ringbuf_discard(struct ringbuf *ring, size_t n)
Throws away the first n bytes of the ring, unread (at most what it holds).
static size_t ringbuf_read_noconsume(struct ringbuf *ring, off_t off, size_t n, char *buf)
Copies up to n bytes from the ring, starting off bytes from its head, without taking them off it.
static size_t ringbuf_write(struct ringbuf *ring, size_t n, char *buf)
Writes up to n bytes from buf into the ring, as many as there is room for, and returns how many.
static ssize_t ringbuf_write_iobuf(struct ringbuf *ring, size_t n, struct iobuf *io)
Writes up to n bytes from an iobuf into the ring.
static void ringbuf_free(struct ringbuf *r)
Frees a ring buffer that ringbuf_new() made (NULL does nothing).
static ssize_t ringbuf_read_iobuf(struct ringbuf *ring, size_t n, struct iobuf *io)
Reads up to n bytes from the ring into an iobuf.
static struct ringbuf * ringbuf_new(size_t size)
Returns a new ring buffer of size bytes, from kmalloc(), or NULL if there was no memory for it.