chiark / gitweb /
Use %m instead of strerror(errno) where appropiate
[elogind.git] / src / shared / ring.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/uio.h>
29
30 struct ring {
31         uint8_t *buf;           /* buffer or NULL */
32         size_t size;            /* actual size of @buf */
33         size_t start;           /* start position of ring */
34         size_t used;            /* number of actually used bytes */
35 };
36
37 /* flush buffer so it is empty again */
38 void ring_flush(struct ring *r);
39
40 /* flush buffer, free allocated data and reset to initial state */
41 void ring_clear(struct ring *r);
42
43 /* get pointers to buffer data and their length */
44 size_t ring_peek(struct ring *r, struct iovec *vec);
45
46 /* copy data into external linear buffer */
47 size_t ring_copy(struct ring *r, void *buf, size_t size);
48
49 /* push data to the end of the buffer */
50 int ring_push(struct ring *r, const void *u8, size_t size);
51
52 /* pull data from the front of the buffer */
53 void ring_pull(struct ring *r, size_t size);
54
55 /* return size of occupied buffer in bytes */
56 static inline size_t ring_get_size(struct ring *r)
57 {
58         return r->used;
59 }