]>
git.proxmox.com Git - mirror_frr.git/blob - lib/ringbuf.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Circular buffer implementation.
4 * Copyright (C) 2017 Cumulus Networks
7 #ifndef _FRR_RINGBUF_H_
8 #define _FRR_RINGBUF_H_
28 * Creates a new ring buffer.
30 * @param size buffer size, in bytes
31 * @return the newly created buffer
33 struct ringbuf
*ringbuf_new(size_t size
);
36 * Deletes a ring buffer and frees all associated resources.
38 * @param buf the ring buffer to destroy
40 void ringbuf_del(struct ringbuf
*buf
);
43 * Get amount of data left to read from the buffer.
45 * @return number of readable bytes
47 size_t ringbuf_remain(struct ringbuf
*buf
);
50 * Get amount of space left to write to the buffer
52 * @return number of writeable bytes
54 size_t ringbuf_space(struct ringbuf
*buf
);
58 * Put data into the ring buffer.
60 * @param data the data to put in the buffer
61 * @param size how much of data to put in
62 * @return number of bytes written; will be less than size if there was not
65 size_t ringbuf_put(struct ringbuf
*buf
, const void *data
, size_t size
);
68 * Get data from the ring buffer.
70 * @param data where to put the data
71 * @param size how much of data to get
72 * @return number of bytes read into data; will be less than size if there was
73 * not enough data to read
75 size_t ringbuf_get(struct ringbuf
*buf
, void *data
, size_t size
);
78 * Peek data from the ring buffer.
80 * @param offset where to get the data from, in bytes offset from the
82 * @param data where to put the data
83 * @param size how much data to get
84 * @return number of bytes read into data; will be less than size
85 * if there was not enough data to read; will be -1 if the
86 * offset exceeds the amount of data left in the ring
89 size_t ringbuf_peek(struct ringbuf
*buf
, size_t offset
, void *data
,
93 * Copy data from one ringbuf to another.
95 * @param to destination ringbuf
96 * @param from source ringbuf
97 * @param size how much data to copy
98 * @return amount of data copied
100 size_t ringbuf_copy(struct ringbuf
*to
, struct ringbuf
*from
, size_t size
);
103 * Reset buffer. Does not wipe.
107 void ringbuf_reset(struct ringbuf
*buf
);
110 * Reset buffer. Wipes.
114 void ringbuf_wipe(struct ringbuf
*buf
);
120 #endif /* _FRR_RINGBUF_H_ */