]> git.proxmox.com Git - mirror_frr.git/blame - lib/ringbuf.h
lib: Fix missing __be16 typedef on CentOS6
[mirror_frr.git] / lib / ringbuf.h
CommitLineData
5318d896
QY
1/*
2 * Circular buffer implementation.
3 * Copyright (C) 2017 Cumulus Networks
4 * Quentin Young
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20#ifndef _FRR_RINGBUF_H_
21#define _FRR_RINGBUF_H_
22
23#include <zebra.h>
24#include <stdint.h>
25
26#include "memory.h"
27
5e244469
RW
28#ifdef __cplusplus
29extern "C" {
30#endif
31
5318d896
QY
32struct ringbuf {
33 size_t size;
34 ssize_t start;
35 ssize_t end;
36 bool empty;
37 uint8_t *data;
38};
39
40/*
41 * Creates a new ring buffer.
42 *
43 * @param size buffer size, in bytes
44 * @return the newly created buffer
45 */
46struct ringbuf *ringbuf_new(size_t size);
47
48/*
49 * Deletes a ring buffer and frees all associated resources.
50 *
51 * @param buf the ring buffer to destroy
52 */
53void ringbuf_del(struct ringbuf *buf);
54
55/*
56 * Get amount of data left to read from the buffer.
57 *
58 * @return number of readable bytes
59 */
60size_t ringbuf_remain(struct ringbuf *buf);
61
62/*
63 * Get amount of space left to write to the buffer
64 *
65 * @return number of writeable bytes
66 */
67size_t ringbuf_space(struct ringbuf *buf);
68
69
70/*
71 * Put data into the ring buffer.
72 *
73 * @param data the data to put in the buffer
74 * @param size how much of data to put in
75 * @return number of bytes written; will be less than size if there was not
76 * enough space
77 */
78size_t ringbuf_put(struct ringbuf *buf, const void *data, size_t size);
79
80/*
81 * Get data from the ring buffer.
82 *
83 * @param data where to put the data
84 * @param size how much of data to get
85 * @return number of bytes read into data; will be less than size if there was
86 * not enough data to read
87 */
88size_t ringbuf_get(struct ringbuf *buf, void *data, size_t size);
89
a5080622
QY
90/*
91 * Peek data from the ring buffer.
92 *
93 * @param offset where to get the data from, in bytes offset from the
94 * start of the data
95 * @param data where to put the data
96 * @param size how much data to get
97 * @return number of bytes read into data; will be less than size
98 * if there was not enough data to read; will be -1 if the
99 * offset exceeds the amount of data left in the ring
100 * buffer
101 */
102size_t ringbuf_peek(struct ringbuf *buf, size_t offset, void *data,
103 size_t size);
104
cb94eaeb
QY
105/*
106 * Copy data from one ringbuf to another.
107 *
108 * @param to destination ringbuf
109 * @param from source ringbuf
110 * @param size how much data to copy
111 * @return amount of data copied
112 */
113size_t ringbuf_copy(struct ringbuf *to, struct ringbuf *from, size_t size);
114
5318d896
QY
115/*
116 * Reset buffer. Does not wipe.
117 *
118 * @param buf
119 */
120void ringbuf_reset(struct ringbuf *buf);
121
122/*
123 * Reset buffer. Wipes.
124 *
125 * @param buf
126 */
127void ringbuf_wipe(struct ringbuf *buf);
128
5e244469
RW
129#ifdef __cplusplus
130}
131#endif
132
5318d896 133#endif /* _FRR_RINGBUF_H_ */