]> git.proxmox.com Git - mirror_frr.git/blob - lib/ringbuf.h
Merge pull request #5410 from ton31337/feature/bgp_default-route_with_route-map_set
[mirror_frr.git] / lib / ringbuf.h
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
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 struct 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 */
46 struct 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 */
53 void 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 */
60 size_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 */
67 size_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 */
78 size_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 */
88 size_t ringbuf_get(struct ringbuf *buf, void *data, size_t size);
89
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 */
102 size_t ringbuf_peek(struct ringbuf *buf, size_t offset, void *data,
103 size_t size);
104
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 */
113 size_t ringbuf_copy(struct ringbuf *to, struct ringbuf *from, size_t size);
114
115 /*
116 * Reset buffer. Does not wipe.
117 *
118 * @param buf
119 */
120 void ringbuf_reset(struct ringbuf *buf);
121
122 /*
123 * Reset buffer. Wipes.
124 *
125 * @param buf
126 */
127 void ringbuf_wipe(struct ringbuf *buf);
128
129 #ifdef __cplusplus
130 }
131 #endif
132
133 #endif /* _FRR_RINGBUF_H_ */