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
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
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.
32 #include "fifo_buffer.h"
40 size_t size
, start
, end
;
41 unsigned char buffer
[sizeof(size_t)];
45 fifo_buffer_new(size_t size
)
47 struct fifo_buffer
*buffer
;
51 buffer
= (struct fifo_buffer
*)g_malloc(sizeof(*buffer
) -
52 sizeof(buffer
->buffer
) + size
);
62 fifo_buffer_free(struct fifo_buffer
*buffer
)
64 assert(buffer
!= NULL
);
70 fifo_buffer_clear(struct fifo_buffer
*buffer
)
72 assert(buffer
!= NULL
);
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 */
89 *length_r
= buffer
->end
- buffer
->start
;
90 return buffer
->buffer
+ buffer
->start
;
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.
107 fifo_buffer_move(struct fifo_buffer
*buffer
)
109 if (buffer
->start
== 0)
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
;
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
)
132 } else if (buffer
->start
> 0 && buffer
->start
== buffer
->end
) {
137 *max_length_r
= buffer
->size
- buffer
->end
;
138 return buffer
->buffer
+ buffer
->end
;
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
;
152 fifo_buffer_is_empty(struct fifo_buffer
*buffer
)
154 return buffer
->start
== buffer
->end
;
158 fifo_buffer_is_full(struct fifo_buffer
*buffer
)
160 return buffer
->start
== 0 && buffer
->end
== buffer
->size
;