]>
Commit | Line | Data |
---|---|---|
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 | ||
28 | struct ringbuf { | |
29 | size_t size; | |
30 | ssize_t start; | |
31 | ssize_t end; | |
32 | bool empty; | |
33 | uint8_t *data; | |
34 | }; | |
35 | ||
36 | /* | |
37 | * Creates a new ring buffer. | |
38 | * | |
39 | * @param size buffer size, in bytes | |
40 | * @return the newly created buffer | |
41 | */ | |
42 | struct ringbuf *ringbuf_new(size_t size); | |
43 | ||
44 | /* | |
45 | * Deletes a ring buffer and frees all associated resources. | |
46 | * | |
47 | * @param buf the ring buffer to destroy | |
48 | */ | |
49 | void ringbuf_del(struct ringbuf *buf); | |
50 | ||
51 | /* | |
52 | * Get amount of data left to read from the buffer. | |
53 | * | |
54 | * @return number of readable bytes | |
55 | */ | |
56 | size_t ringbuf_remain(struct ringbuf *buf); | |
57 | ||
58 | /* | |
59 | * Get amount of space left to write to the buffer | |
60 | * | |
61 | * @return number of writeable bytes | |
62 | */ | |
63 | size_t ringbuf_space(struct ringbuf *buf); | |
64 | ||
65 | ||
66 | /* | |
67 | * Put data into the ring buffer. | |
68 | * | |
69 | * @param data the data to put in the buffer | |
70 | * @param size how much of data to put in | |
71 | * @return number of bytes written; will be less than size if there was not | |
72 | * enough space | |
73 | */ | |
74 | size_t ringbuf_put(struct ringbuf *buf, const void *data, size_t size); | |
75 | ||
76 | /* | |
77 | * Get data from the ring buffer. | |
78 | * | |
79 | * @param data where to put the data | |
80 | * @param size how much of data to get | |
81 | * @return number of bytes read into data; will be less than size if there was | |
82 | * not enough data to read | |
83 | */ | |
84 | size_t ringbuf_get(struct ringbuf *buf, void *data, size_t size); | |
85 | ||
a5080622 QY |
86 | /* |
87 | * Peek data from the ring buffer. | |
88 | * | |
89 | * @param offset where to get the data from, in bytes offset from the | |
90 | * start of the data | |
91 | * @param data where to put the data | |
92 | * @param size how much data to get | |
93 | * @return number of bytes read into data; will be less than size | |
94 | * if there was not enough data to read; will be -1 if the | |
95 | * offset exceeds the amount of data left in the ring | |
96 | * buffer | |
97 | */ | |
98 | size_t ringbuf_peek(struct ringbuf *buf, size_t offset, void *data, | |
99 | size_t size); | |
100 | ||
cb94eaeb QY |
101 | /* |
102 | * Copy data from one ringbuf to another. | |
103 | * | |
104 | * @param to destination ringbuf | |
105 | * @param from source ringbuf | |
106 | * @param size how much data to copy | |
107 | * @return amount of data copied | |
108 | */ | |
109 | size_t ringbuf_copy(struct ringbuf *to, struct ringbuf *from, size_t size); | |
110 | ||
5318d896 QY |
111 | /* |
112 | * Reset buffer. Does not wipe. | |
113 | * | |
114 | * @param buf | |
115 | */ | |
116 | void ringbuf_reset(struct ringbuf *buf); | |
117 | ||
118 | /* | |
119 | * Reset buffer. Wipes. | |
120 | * | |
121 | * @param buf | |
122 | */ | |
123 | void ringbuf_wipe(struct ringbuf *buf); | |
124 | ||
125 | #endif /* _FRR_RINGBUF_H_ */ |