]>
git.proxmox.com Git - mirror_frr.git/blob - lib/ringbuf.h
2 * Circular buffer implementation.
3 * Copyright (C) 2017 Cumulus Networks
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)
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
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
20 #ifndef _FRR_RINGBUF_H_
21 #define _FRR_RINGBUF_H_
41 * Creates a new ring buffer.
43 * @param size buffer size, in bytes
44 * @return the newly created buffer
46 struct ringbuf
*ringbuf_new(size_t size
);
49 * Deletes a ring buffer and frees all associated resources.
51 * @param buf the ring buffer to destroy
53 void ringbuf_del(struct ringbuf
*buf
);
56 * Get amount of data left to read from the buffer.
58 * @return number of readable bytes
60 size_t ringbuf_remain(struct ringbuf
*buf
);
63 * Get amount of space left to write to the buffer
65 * @return number of writeable bytes
67 size_t ringbuf_space(struct ringbuf
*buf
);
71 * Put data into the ring buffer.
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
78 size_t ringbuf_put(struct ringbuf
*buf
, const void *data
, size_t size
);
81 * Get data from the ring buffer.
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
88 size_t ringbuf_get(struct ringbuf
*buf
, void *data
, size_t size
);
91 * Peek data from the ring buffer.
93 * @param offset where to get the data from, in bytes offset from the
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
102 size_t ringbuf_peek(struct ringbuf
*buf
, size_t offset
, void *data
,
106 * Copy data from one ringbuf to another.
108 * @param to destination ringbuf
109 * @param from source ringbuf
110 * @param size how much data to copy
111 * @return amount of data copied
113 size_t ringbuf_copy(struct ringbuf
*to
, struct ringbuf
*from
, size_t size
);
116 * Reset buffer. Does not wipe.
120 void ringbuf_reset(struct ringbuf
*buf
);
123 * Reset buffer. Wipes.
127 void ringbuf_wipe(struct ringbuf
*buf
);
133 #endif /* _FRR_RINGBUF_H_ */