1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #define RING_MASK(_r, _v) ((_v) & ((_r)->size - 1))
32 void ring_flush(Ring *r) {
39 void ring_clear(Ring *r) {
47 * Get data pointers for current ring-buffer data. @vec must be an array of 2
48 * iovec objects. They are filled according to the data available in the
49 * ring-buffer. 0, 1 or 2 is returned according to the number of iovec objects
50 * that were filled (0 meaning buffer is empty).
52 * Hint: "struct iovec" is defined in <sys/uio.h> and looks like this:
58 size_t ring_peek(Ring *r, struct iovec *vec) {
63 } else if (r->start + r->used <= r->size) {
65 vec[0].iov_base = &r->buf[r->start];
66 vec[0].iov_len = r->used;
71 vec[0].iov_base = &r->buf[r->start];
72 vec[0].iov_len = r->size - r->start;
73 vec[1].iov_base = r->buf;
74 vec[1].iov_len = r->used - (r->size - r->start);
81 * Copy data from the ring buffer into the linear external buffer @buf. Copy
82 * at most @size bytes. If the ring buffer size is smaller, copy less bytes and
83 * return the number of bytes copied.
85 size_t ring_copy(Ring *r, void *buf, size_t size) {
95 l = r->size - r->start;
97 memcpy(buf, &r->buf[r->start], size);
99 memcpy(buf, &r->buf[r->start], l);
100 memcpy((uint8_t*)buf + l, r->buf, size - l);
108 * Resize ring-buffer to size @nsize. @nsize must be a power-of-2, otherwise
109 * ring operations will behave incorrectly.
111 static int ring_resize(Ring *r, size_t nsize) {
123 l = r->size - r->start;
125 memcpy(buf, &r->buf[r->start], r->used);
127 memcpy(buf, &r->buf[r->start], l);
128 memcpy(&buf[l], r->buf, r->used - l);
141 * Resize ring-buffer to provide enough room for @add bytes of new data. This
142 * resizes the buffer if it is too small. It returns -ENOMEM on OOM and 0 on
145 static int ring_grow(Ring *r, size_t add) {
150 if (r->size - r->used >= add)
153 need = r->used + add;
156 else if (need < 4096)
159 need = ALIGN_POWER2(need);
163 return ring_resize(r, need);
167 * Push @len bytes from @u8 into the ring buffer. The buffer is resized if it
168 * is too small. -ENOMEM is returned on OOM, 0 on success.
170 int ring_push(Ring *r, const void *u8, size_t size) {
180 err = ring_grow(r, size);
184 pos = RING_MASK(r, r->start + r->used);
187 memcpy(&r->buf[pos], u8, size);
189 memcpy(&r->buf[pos], u8, l);
190 memcpy(r->buf, (const uint8_t*)u8 + l, size - l);
199 * Remove @len bytes from the start of the ring-buffer. Note that we protect
200 * against overflows so removing more bytes than available is safe.
202 void ring_pull(Ring *r, size_t size) {
208 r->start = RING_MASK(r, r->start + size);