]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
5318d896 QY |
2 | /* |
3 | * Circular buffer implementation. | |
4 | * Copyright (C) 2017 Cumulus Networks | |
5 | * Quentin Young | |
5318d896 QY |
6 | */ |
7 | #ifndef _FRR_RINGBUF_H_ | |
8 | #define _FRR_RINGBUF_H_ | |
9 | ||
10 | #include <zebra.h> | |
11 | #include <stdint.h> | |
12 | ||
13 | #include "memory.h" | |
14 | ||
5e244469 RW |
15 | #ifdef __cplusplus |
16 | extern "C" { | |
17 | #endif | |
18 | ||
5318d896 QY |
19 | struct ringbuf { |
20 | size_t size; | |
21 | ssize_t start; | |
22 | ssize_t end; | |
23 | bool empty; | |
24 | uint8_t *data; | |
25 | }; | |
26 | ||
27 | /* | |
28 | * Creates a new ring buffer. | |
29 | * | |
30 | * @param size buffer size, in bytes | |
31 | * @return the newly created buffer | |
32 | */ | |
33 | struct ringbuf *ringbuf_new(size_t size); | |
34 | ||
35 | /* | |
36 | * Deletes a ring buffer and frees all associated resources. | |
37 | * | |
38 | * @param buf the ring buffer to destroy | |
39 | */ | |
40 | void ringbuf_del(struct ringbuf *buf); | |
41 | ||
42 | /* | |
43 | * Get amount of data left to read from the buffer. | |
44 | * | |
45 | * @return number of readable bytes | |
46 | */ | |
47 | size_t ringbuf_remain(struct ringbuf *buf); | |
48 | ||
49 | /* | |
50 | * Get amount of space left to write to the buffer | |
51 | * | |
52 | * @return number of writeable bytes | |
53 | */ | |
54 | size_t ringbuf_space(struct ringbuf *buf); | |
55 | ||
56 | ||
57 | /* | |
58 | * Put data into the ring buffer. | |
59 | * | |
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 | |
63 | * enough space | |
64 | */ | |
65 | size_t ringbuf_put(struct ringbuf *buf, const void *data, size_t size); | |
66 | ||
67 | /* | |
68 | * Get data from the ring buffer. | |
69 | * | |
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 | |
74 | */ | |
75 | size_t ringbuf_get(struct ringbuf *buf, void *data, size_t size); | |
76 | ||
a5080622 QY |
77 | /* |
78 | * Peek data from the ring buffer. | |
79 | * | |
80 | * @param offset where to get the data from, in bytes offset from the | |
81 | * start of the data | |
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 | |
87 | * buffer | |
88 | */ | |
89 | size_t ringbuf_peek(struct ringbuf *buf, size_t offset, void *data, | |
90 | size_t size); | |
91 | ||
cb94eaeb QY |
92 | /* |
93 | * Copy data from one ringbuf to another. | |
94 | * | |
95 | * @param to destination ringbuf | |
96 | * @param from source ringbuf | |
97 | * @param size how much data to copy | |
98 | * @return amount of data copied | |
99 | */ | |
100 | size_t ringbuf_copy(struct ringbuf *to, struct ringbuf *from, size_t size); | |
101 | ||
5318d896 QY |
102 | /* |
103 | * Reset buffer. Does not wipe. | |
104 | * | |
105 | * @param buf | |
106 | */ | |
107 | void ringbuf_reset(struct ringbuf *buf); | |
108 | ||
109 | /* | |
110 | * Reset buffer. Wipes. | |
111 | * | |
112 | * @param buf | |
113 | */ | |
114 | void ringbuf_wipe(struct ringbuf *buf); | |
115 | ||
5e244469 RW |
116 | #ifdef __cplusplus |
117 | } | |
118 | #endif | |
119 | ||
5318d896 | 120 | #endif /* _FRR_RINGBUF_H_ */ |