3 * AT chat library with GLib integration
5 * Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "ringbuffer.h"
32 #define MAX_SIZE 262144
35 unsigned char *buffer;
42 struct ring_buffer *ring_buffer_new(unsigned int size)
44 unsigned int real_size = 1;
45 struct ring_buffer *buffer;
47 /* Find the next power of two for size */
48 while (real_size < size && real_size < MAX_SIZE)
49 real_size = real_size << 1;
51 if (real_size > MAX_SIZE)
54 buffer = g_slice_new(struct ring_buffer);
58 buffer->buffer = g_slice_alloc(real_size);
59 if (buffer->buffer == NULL) {
64 buffer->size = real_size;
65 buffer->mask = real_size - 1;
72 int ring_buffer_write(struct ring_buffer *buf, const void *data,
77 const unsigned char *d = data; /* Needed to satisfy non-gcc compilers */
79 /* Determine how much we can actually write */
80 len = MIN(len, buf->size - buf->in + buf->out);
82 /* Determine how much to write before wrapping */
83 offset = buf->in & buf->mask;
84 end = MIN(len, buf->size - offset);
85 memcpy(buf->buffer+offset, d, end);
87 /* Now put the remainder on the beginning of the buffer */
88 memcpy(buf->buffer, d + end, len - end);
95 unsigned char *ring_buffer_write_ptr(struct ring_buffer *buf,
98 return buf->buffer + ((buf->in + offset) & buf->mask);
101 int ring_buffer_avail_no_wrap(struct ring_buffer *buf)
103 unsigned int offset = buf->in & buf->mask;
104 unsigned int len = buf->size - buf->in + buf->out;
106 return MIN(len, buf->size - offset);
109 int ring_buffer_write_advance(struct ring_buffer *buf, unsigned int len)
111 len = MIN(len, buf->size - buf->in + buf->out);
117 int ring_buffer_read(struct ring_buffer *buf, void *data, unsigned int len)
121 unsigned char *d = data;
123 len = MIN(len, buf->in - buf->out);
125 /* Grab data from buffer starting at offset until the end */
126 offset = buf->out & buf->mask;
127 end = MIN(len, buf->size - offset);
128 memcpy(d, buf->buffer + offset, end);
130 /* Now grab remainder from the beginning */
131 memcpy(d + end, buf->buffer, len - end);
135 if (buf->out == buf->in)
136 buf->out = buf->in = 0;
141 int ring_buffer_drain(struct ring_buffer *buf, unsigned int len)
143 len = MIN(len, buf->in - buf->out);
147 if (buf->out == buf->in)
148 buf->out = buf->in = 0;
153 int ring_buffer_len_no_wrap(struct ring_buffer *buf)
155 unsigned int offset = buf->out & buf->mask;
156 unsigned int len = buf->in - buf->out;
158 return MIN(len, buf->size - offset);
161 unsigned char *ring_buffer_read_ptr(struct ring_buffer *buf,
164 return buf->buffer + ((buf->out + offset) & buf->mask);
167 int ring_buffer_len(struct ring_buffer *buf)
172 return buf->in - buf->out;
175 void ring_buffer_reset(struct ring_buffer *buf)
184 int ring_buffer_avail(struct ring_buffer *buf)
189 return buf->size - buf->in + buf->out;
192 int ring_buffer_capacity(struct ring_buffer *buf)
200 void ring_buffer_free(struct ring_buffer *buf)
205 g_slice_free1(buf->size, buf->buffer);
206 g_slice_free1(sizeof(struct ring_buffer), buf);