1 /* Copyright  (C) 2010-2018 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (fifo_queue.c).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <queues/fifo_queue.h>
27 
fifo_new(size_t size)28 fifo_buffer_t *fifo_new(size_t size)
29 {
30    uint8_t    *buffer = NULL;
31    fifo_buffer_t *buf = (fifo_buffer_t*)calloc(1, sizeof(*buf));
32 
33    if (!buf)
34       return NULL;
35 
36    buffer = (uint8_t*)calloc(1, size + 1);
37 
38    if (!buffer)
39    {
40       free(buf);
41       return NULL;
42    }
43 
44    buf->buffer = buffer;
45    buf->size   = size + 1;
46 
47    return buf;
48 }
49 
fifo_write(fifo_buffer_t * buffer,const void * in_buf,size_t size)50 void fifo_write(fifo_buffer_t *buffer, const void *in_buf, size_t size)
51 {
52    size_t first_write = size;
53    size_t rest_write  = 0;
54 
55    if (buffer->end + size > buffer->size)
56    {
57       first_write = buffer->size - buffer->end;
58       rest_write  = size - first_write;
59    }
60 
61    memcpy(buffer->buffer + buffer->end, in_buf, first_write);
62    memcpy(buffer->buffer, (const uint8_t*)in_buf + first_write, rest_write);
63 
64    buffer->end = (buffer->end + size) % buffer->size;
65 }
66 
fifo_read(fifo_buffer_t * buffer,void * in_buf,size_t size)67 void fifo_read(fifo_buffer_t *buffer, void *in_buf, size_t size)
68 {
69    size_t first_read = size;
70    size_t rest_read  = 0;
71 
72    if (buffer->first + size > buffer->size)
73    {
74       first_read = buffer->size - buffer->first;
75       rest_read  = size - first_read;
76    }
77 
78    memcpy(in_buf, (const uint8_t*)buffer->buffer + buffer->first, first_read);
79    memcpy((uint8_t*)in_buf + first_read, buffer->buffer, rest_read);
80 
81    buffer->first = (buffer->first + size) % buffer->size;
82 }
83