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(struct ring *r) {
39 void ring_clear(struct ring *r) {
45 * Get data pointers for current ring-buffer data. @vec must be an array of 2
46 * iovec objects. They are filled according to the data available in the
47 * ring-buffer. 0, 1 or 2 is returned according to the number of iovec objects
48 * that were filled (0 meaning buffer is empty).
50 * Hint: "struct iovec" is defined in <sys/uio.h> and looks like this:
56 size_t ring_peek(struct ring *r, struct iovec *vec) {
61 } else if (r->start + r->used <= r->size) {
63 vec[0].iov_base = &r->buf[r->start];
64 vec[0].iov_len = r->used;
69 vec[0].iov_base = &r->buf[r->start];
70 vec[0].iov_len = r->size - r->start;
71 vec[1].iov_base = r->buf;
72 vec[1].iov_len = r->used - (r->size - r->start);
79 * Copy data from the ring buffer into the linear external buffer @buf. Copy
80 * at most @size bytes. If the ring buffer size is smaller, copy less bytes and
81 * return the number of bytes copied.
83 size_t ring_copy(struct ring *r, void *buf, size_t size) {
93 l = r->size - r->start;
95 memcpy(buf, &r->buf[r->start], size);
97 memcpy(buf, &r->buf[r->start], l);
98 memcpy((uint8_t*)buf + l, r->buf, size - l);
106 * Resize ring-buffer to size @nsize. @nsize must be a power-of-2, otherwise
107 * ring operations will behave incorrectly.
109 static int ring_resize(struct ring *r, size_t nsize) {
121 l = r->size - r->start;
123 memcpy(buf, &r->buf[r->start], r->used);
125 memcpy(buf, &r->buf[r->start], l);
126 memcpy(&buf[l], r->buf, r->used - l);
139 * Resize ring-buffer to provide enough room for @add bytes of new data. This
140 * resizes the buffer if it is too small. It returns -ENOMEM on OOM and 0 on
143 static int ring_grow(struct ring *r, size_t add) {
148 if (r->size - r->used >= add)
151 need = r->used + add;
154 else if (need < 4096)
157 need = ALIGN_POWER2(need);
161 return ring_resize(r, need);
165 * Push @len bytes from @u8 into the ring buffer. The buffer is resized if it
166 * is too small. -ENOMEM is returned on OOM, 0 on success.
168 int ring_push(struct ring *r, const void *u8, size_t size) {
178 err = ring_grow(r, size);
182 pos = RING_MASK(r, r->start + r->used);
185 memcpy(&r->buf[pos], u8, size);
187 memcpy(&r->buf[pos], u8, l);
188 memcpy(r->buf, (const uint8_t*)u8 + l, size - l);
197 * Remove @len bytes from the start of the ring-buffer. Note that we protect
198 * against overflows so removing more bytes than available is safe.
200 void ring_pull(struct ring *r, size_t size) {
206 r->start = RING_MASK(r, r->start + size);