1 /*
2   This file is part of Deadbeef Player source code
3   http://deadbeef.sourceforge.net
4 
5   basic ring buffer implementation
6 
7   Copyright (C) 2009-2013 Alexey Yakovenko
8 
9   This software is provided 'as-is', without any express or implied
10   warranty.  In no event will the authors be held liable for any damages
11   arising from the use of this software.
12 
13   Permission is granted to anyone to use this software for any purpose,
14   including commercial applications, and to alter it and redistribute it
15   freely, subject to the following restrictions:
16 
17   1. The origin of this software must not be misrepresented; you must not
18      claim that you wrote the original software. If you use this software
19      in a product, an acknowledgment in the product documentation would be
20      appreciated but is not required.
21   2. Altered source versions must be plainly marked as such, and must not be
22      misrepresented as being the original software.
23   3. This notice may not be removed or altered from any source distribution.
24 
25   Alexey Yakovenko waker@users.sourceforge.net
26 */
27 
28 #include <string.h>
29 #include "ringbuf.h"
30 
31 void
ringbuf_init(ringbuf_t * p,char * buffer,size_t size)32 ringbuf_init (ringbuf_t *p, char *buffer, size_t size) {
33     memset (p, 0, sizeof (ringbuf_t));
34     p->bytes = buffer;
35     p->cursor = 0;
36     p->size = size;
37     p->remaining = 0;
38 }
39 
40 int
ringbuf_write(ringbuf_t * p,char * bytes,size_t size)41 ringbuf_write (ringbuf_t *p, char *bytes, size_t size) {
42     if (p->size - p->remaining < size) {
43         return -1;
44     }
45 
46     size_t cursor = p->cursor + p->remaining;
47     cursor %= p->size;
48 
49     if (p->size - cursor >= size) { // split
50         memcpy (p->bytes + cursor, bytes, size);
51         p->remaining += size;
52     }
53     else {
54         size_t n = p->size - cursor;
55         if (n > 0) {
56             memcpy (p->bytes + cursor, bytes, n);
57             p->remaining += n;
58             size -= n;
59             bytes += n;
60         }
61         memcpy (p->bytes, bytes, size);
62         p->remaining += size;
63     }
64     return 0;
65 }
66 
67 int
ringbuf_read(ringbuf_t * p,char * bytes,size_t size)68 ringbuf_read (ringbuf_t *p, char *bytes, size_t size) {
69     if (p->remaining < size) {
70         size = p->remaining;
71     }
72     int rb = size;
73 
74     if (p->size - p->cursor >= size) {
75         memcpy (bytes, p->bytes + p->cursor, size);
76         p->cursor += size;
77         p->remaining -= size;
78     }
79     else {
80         size_t n = p->size - p->cursor;
81         if (n > 0) {
82             memcpy (bytes, p->bytes + p->cursor, n);
83             p->cursor += n;
84             p->remaining -= n;
85             bytes += n;
86             size -= n;
87         }
88         memcpy (bytes, p->bytes, size);
89         p->cursor = size;
90         p->remaining -= size;
91     }
92     return rb;
93 }
94