configure.ac: Add subheaders to Input/Sticker/Autodiscovery/Metadata and Misc.
[mpd-mk.git] / src / fifo_buffer.c
blob9ac7270bb0be00fdd1349c098d4f7de5397b67d3
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the
15 * distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "config.h"
32 #include "fifo_buffer.h"
34 #include <glib.h>
36 #include <assert.h>
37 #include <string.h>
39 struct fifo_buffer {
40 size_t size, start, end;
41 unsigned char buffer[sizeof(size_t)];
44 struct fifo_buffer *
45 fifo_buffer_new(size_t size)
47 struct fifo_buffer *buffer;
49 assert(size > 0);
51 buffer = (struct fifo_buffer *)g_malloc(sizeof(*buffer) -
52 sizeof(buffer->buffer) + size);
54 buffer->size = size;
55 buffer->start = 0;
56 buffer->end = 0;
58 return buffer;
61 void
62 fifo_buffer_free(struct fifo_buffer *buffer)
64 assert(buffer != NULL);
66 g_free(buffer);
69 void
70 fifo_buffer_clear(struct fifo_buffer *buffer)
72 assert(buffer != NULL);
74 buffer->start = 0;
75 buffer->end = 0;
78 const void *
79 fifo_buffer_read(const struct fifo_buffer *buffer, size_t *length_r)
81 assert(buffer != NULL);
82 assert(buffer->end >= buffer->start);
83 assert(length_r != NULL);
85 if (buffer->start == buffer->end)
86 /* the buffer is empty */
87 return NULL;
89 *length_r = buffer->end - buffer->start;
90 return buffer->buffer + buffer->start;
93 void
94 fifo_buffer_consume(struct fifo_buffer *buffer, size_t length)
96 assert(buffer != NULL);
97 assert(buffer->end >= buffer->start);
98 assert(buffer->start + length <= buffer->end);
100 buffer->start += length;
104 * Move data to the beginning of the buffer, to make room at the end.
106 static void
107 fifo_buffer_move(struct fifo_buffer *buffer)
109 if (buffer->start == 0)
110 return;
112 if (buffer->end > buffer->start)
113 memmove(buffer->buffer,
114 buffer->buffer + buffer->start,
115 buffer->end - buffer->start);
117 buffer->end -= buffer->start;
118 buffer->start = 0;
121 void *
122 fifo_buffer_write(struct fifo_buffer *buffer, size_t *max_length_r)
124 assert(buffer != NULL);
125 assert(buffer->end <= buffer->size);
126 assert(max_length_r != NULL);
128 if (buffer->end == buffer->size) {
129 fifo_buffer_move(buffer);
130 if (buffer->end == buffer->size)
131 return NULL;
132 } else if (buffer->start > 0 && buffer->start == buffer->end) {
133 buffer->start = 0;
134 buffer->end = 0;
137 *max_length_r = buffer->size - buffer->end;
138 return buffer->buffer + buffer->end;
141 void
142 fifo_buffer_append(struct fifo_buffer *buffer, size_t length)
144 assert(buffer != NULL);
145 assert(buffer->end >= buffer->start);
146 assert(buffer->end + length <= buffer->size);
148 buffer->end += length;
151 bool
152 fifo_buffer_is_empty(struct fifo_buffer *buffer)
154 return buffer->start == buffer->end;
157 bool
158 fifo_buffer_is_full(struct fifo_buffer *buffer)
160 return buffer->start == 0 && buffer->end == buffer->size;