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/>.
29 #define RING_MASK(_r, _v) ((_v) & ((_r)->size - 1))
31 void ring_flush(Ring *r) {
38 void ring_clear(Ring *r) {
46 * Get data pointers for current ring-buffer data. @vec must be an array of 2
47 * iovec objects. They are filled according to the data available in the
48 * ring-buffer. 0, 1 or 2 is returned according to the number of iovec objects
49 * that were filled (0 meaning buffer is empty).
51 * Hint: "struct iovec" is defined in <sys/uio.h> and looks like this:
57 size_t ring_peek(Ring *r, struct iovec *vec) {
62 } else if (r->start + r->used <= r->size) {
64 vec[0].iov_base = &r->buf[r->start];
65 vec[0].iov_len = r->used;
70 vec[0].iov_base = &r->buf[r->start];
71 vec[0].iov_len = r->size - r->start;
72 vec[1].iov_base = r->buf;
73 vec[1].iov_len = r->used - (r->size - r->start);
80 * Copy data from the ring buffer into the linear external buffer @buf. Copy
81 * at most @size bytes. If the ring buffer size is smaller, copy less bytes and
82 * return the number of bytes copied.
84 size_t ring_copy(Ring *r, void *buf, size_t size) {
94 l = r->size - r->start;
96 memcpy(buf, &r->buf[r->start], size);
98 memcpy(buf, &r->buf[r->start], l);
99 memcpy((uint8_t*)buf + l, r->buf, size - l);
107 * Resize ring-buffer to size @nsize. @nsize must be a power-of-2, otherwise
108 * ring operations will behave incorrectly.
110 static int ring_resize(Ring *r, size_t nsize) {
122 l = r->size - r->start;
124 memcpy(buf, &r->buf[r->start], r->used);
126 memcpy(buf, &r->buf[r->start], l);
127 memcpy(&buf[l], r->buf, r->used - l);
140 * Resize ring-buffer to provide enough room for @add bytes of new data. This
141 * resizes the buffer if it is too small. It returns -ENOMEM on OOM and 0 on
144 static int ring_grow(Ring *r, size_t add) {
149 if (r->size - r->used >= add)
152 need = r->used + add;
155 else if (need < 4096)
158 need = ALIGN_POWER2(need);
162 return ring_resize(r, need);
166 * Push @len bytes from @u8 into the ring buffer. The buffer is resized if it
167 * is too small. -ENOMEM is returned on OOM, 0 on success.
169 int ring_push(Ring *r, const void *u8, size_t size) {
179 err = ring_grow(r, size);
183 pos = RING_MASK(r, r->start + r->used);
186 memcpy(&r->buf[pos], u8, size);
188 memcpy(&r->buf[pos], u8, l);
189 memcpy(r->buf, (const uint8_t*)u8 + l, size - l);
198 * Remove @len bytes from the start of the ring-buffer. Note that we protect
199 * against overflows so removing more bytes than available is safe.
201 void ring_pull(Ring *r, size_t size) {
207 r->start = RING_MASK(r, r->start + size);