]>
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_
37 * Creates a new ring buffer.
39 * @param size buffer size, in bytes
40 * @return the newly created buffer
42 struct ringbuf
*ringbuf_new(size_t size
);
45 * Deletes a ring buffer and frees all associated resources.
47 * @param buf the ring buffer to destroy
49 void ringbuf_del(struct ringbuf
*buf
);
52 * Get amount of data left to read from the buffer.
54 * @return number of readable bytes
56 size_t ringbuf_remain(struct ringbuf
*buf
);
59 * Get amount of space left to write to the buffer
61 * @return number of writeable bytes
63 size_t ringbuf_space(struct ringbuf
*buf
);
67 * Put data into the ring buffer.
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
74 size_t ringbuf_put(struct ringbuf
*buf
, const void *data
, size_t size
);
77 * Get data from the ring buffer.
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
84 size_t ringbuf_get(struct ringbuf
*buf
, void *data
, size_t size
);
87 * Peek data from the ring buffer.
89 * @param offset where to get the data from, in bytes offset from the
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
98 size_t ringbuf_peek(struct ringbuf
*buf
, size_t offset
, void *data
,
102 * Copy data from one ringbuf to another.
104 * @param to destination ringbuf
105 * @param from source ringbuf
106 * @param size how much data to copy
107 * @return amount of data copied
109 size_t ringbuf_copy(struct ringbuf
*to
, struct ringbuf
*from
, size_t size
);
112 * Reset buffer. Does not wipe.
116 void ringbuf_reset(struct ringbuf
*buf
);
119 * Reset buffer. Wipes.
123 void ringbuf_wipe(struct ringbuf
*buf
);
125 #endif /* _FRR_RINGBUF_H_ */