AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
ringbuf.h File Reference
#include <core/system.h>
#include <mm/mm.h>
#include <fs/iobuf.h>

Go to the source code of this file.

Data Structures

struct  ringbuf
 A ring buffer. More...

Macros

#define RING_INDEX(ring, i)
#define RINGBUF_NEW(sz)

Functions

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.
static void ringbuf_free (struct ringbuf *r)
 Frees a ring buffer that ringbuf_new() made (NULL does nothing).
static size_t ringbuf_available (struct ringbuf *ring)
 Returns how many bytes the ring holds.
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.
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 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_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_read_iobuf (struct ringbuf *ring, size_t n, struct iobuf *io)
 Reads up to n bytes from the ring into an iobuf.
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 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).

Macro Definition Documentation

◆ RING_INDEX

#define RING_INDEX ( ring,
i )

◆ RINGBUF_NEW

#define RINGBUF_NEW ( sz)
Value:
(&(struct ringbuf){.buf = (char[sz]){0}, .size = sz, .head = 0, .tail = 0})
A ring buffer.
Definition ringbuf.h:18

Definition at line 26 of file ringbuf.h.

Function Documentation

◆ ringbuf_available()

size_t ringbuf_available ( struct ringbuf * ring)
inlinestatic

Returns how many bytes the ring holds.

Definition at line 68 of file ringbuf.h.

References ringbuf::head, RING_INDEX, ringbuf::size, and ringbuf::tail.

Referenced by ringbuf_discard(), and ringbuf_read_noconsume().

◆ ringbuf_discard()

void ringbuf_discard ( struct ringbuf * ring,
size_t n )
inlinestatic

Throws away the first n bytes of the ring, unread (at most what it holds).

Definition at line 113 of file ringbuf.h.

References ringbuf::head, MIN, RING_INDEX, ringbuf_available(), and ringbuf_discard().

Referenced by ack_input(), ring_to_iobuf(), and ringbuf_discard().

◆ ringbuf_free()

void ringbuf_free ( struct ringbuf * r)
inlinestatic

Frees a ring buffer that ringbuf_new() made (NULL does nothing).

Definition at line 52 of file ringbuf.h.

References ringbuf::buf, and kfree().

◆ ringbuf_new()

struct ringbuf * ringbuf_new ( size_t size)
inlinestatic

Returns a new ring buffer of size bytes, from kmalloc(), or NULL if there was no memory for it.

Definition at line 30 of file ringbuf.h.

References ringbuf::buf, ringbuf::head, kfree(), kmalloc(), NULL, ringbuf::size, and ringbuf::tail.

◆ ringbuf_read()

size_t ringbuf_read ( struct ringbuf * ring,
size_t n,
char * buf )
inlinestatic

Reads up to n bytes from the ring into buf, and returns how many there were.

Definition at line 75 of file ringbuf.h.

References ringbuf::buf, ringbuf::head, RING_INDEX, ringbuf::size, and ringbuf::tail.

◆ ringbuf_read_iobuf()

ssize_t ringbuf_read_iobuf ( struct ringbuf * ring,
size_t n,
struct iobuf * io )
inlinestatic

Reads up to n bytes from the ring into an iobuf.

Definition at line 146 of file ringbuf.h.

References ringbuf::buf, ringbuf::head, iobuf_out(), MIN, iobuf::resid, RING_INDEX, ringbuf::size, and ringbuf::tail.

◆ ringbuf_read_noconsume()

size_t ringbuf_read_noconsume ( struct ringbuf * ring,
off_t off,
size_t n,
char * buf )
inlinestatic

Copies up to n bytes from the ring, starting off bytes from its head, without taking them off it.

Returns how many were copied.

Definition at line 92 of file ringbuf.h.

References ringbuf::buf, ringbuf::head, MIN, RING_INDEX, and ringbuf_available().

◆ ringbuf_write()

size_t ringbuf_write ( struct ringbuf * ring,
size_t n,
char * buf )
inlinestatic

Writes up to n bytes from buf into the ring, as many as there is room for, and returns how many.

Definition at line 121 of file ringbuf.h.

References ringbuf::buf, ringbuf::head, RING_INDEX, ringbuf::size, and ringbuf::tail.

◆ ringbuf_write_iobuf()

ssize_t ringbuf_write_iobuf ( struct ringbuf * ring,
size_t n,
struct iobuf * io )
inlinestatic

Writes up to n bytes from an iobuf into the ring.

Definition at line 168 of file ringbuf.h.

References ringbuf::buf, ringbuf::head, iobuf_in(), MIN, iobuf::resid, RING_INDEX, ringbuf::size, and ringbuf::tail.

◆ ringbuf_write_overwrite()

size_t ringbuf_write_overwrite ( struct ringbuf * ring,
size_t n,
char * buf )
inlinestatic

Writes n bytes into the ring, throwing away its oldest bytes if it is full (the kernel's messages).

Returns n.

Definition at line 192 of file ringbuf.h.

References ringbuf::buf, ringbuf::head, RING_INDEX, ringbuf::size, and ringbuf::tail.