4 * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5 * Copyright (c) 2011 University of Tuebingen
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * A circular memory ring implementation
39 #define SHL_RING_SIZE 512
41 struct shl_ring_entry {
42 struct shl_ring_entry *next;
48 struct shl_ring_entry *first;
49 struct shl_ring_entry *last;
52 static inline int shl_ring_new(struct shl_ring **out)
54 struct shl_ring *ring;
59 ring = malloc(sizeof(*ring));
63 memset(ring, 0, sizeof(*ring));
69 static inline void shl_ring_free(struct shl_ring *ring)
71 struct shl_ring_entry *tmp;
78 ring->first = tmp->next;
84 static inline bool shl_ring_is_empty(struct shl_ring *ring)
89 return ring->first == NULL;
92 static inline int shl_ring_write(struct shl_ring *ring, const char *val,
95 struct shl_ring_entry *ent;
98 if (!ring || !val || !len)
103 if (!ent || ent->len >= SHL_RING_SIZE) {
104 ent = malloc(sizeof(*ent) + SHL_RING_SIZE);
111 ring->last->next = ent;
117 space = SHL_RING_SIZE - ent->len;
123 memcpy(&ent->buf[ent->len], val, cp);
134 static inline const char *shl_ring_peek(struct shl_ring *ring, size_t *len,
137 struct shl_ring_entry *iter;
139 if (!ring || !ring->first || !len) {
146 while (iter->len <= offset) {
156 *len = ring->first->len - offset;
157 return &ring->first->buf[offset];
160 static inline void shl_ring_drop(struct shl_ring *ring, size_t len)
162 struct shl_ring_entry *ent;
172 if (len >= ent->len) {
174 ring->first = ent->next;
182 memmove(ent->buf, &ent->buf[len], ent->len - len);
187 static inline void shl_ring_flush(struct shl_ring *ring)
189 struct shl_ring_entry *tmp;
194 while (ring->first) {
196 ring->first = tmp->next;
202 #endif /* SHL_RING_H */