1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*
* Copyright (c) 2002-2014 Balabit
* Copyright (c) 2014 Laszlo Budai
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "ringbuffer.h"
void
ring_buffer_init(RingBuffer *self)
{
self->head = 0;
self->tail = 0;
self->count = 0;
self->capacity = 0;
self->element_size = 0;
self->buffer = NULL;
}
void
ring_buffer_alloc(RingBuffer *self, guint32 element_size, guint32 capacity)
{
g_assert(capacity > 0);
self->head = 0;
self->tail = 0;
self->count = 0;
self->capacity = capacity;
self->element_size = element_size;
self->buffer = g_malloc0((gsize) element_size * self->capacity);
}
gboolean
ring_buffer_is_allocated(RingBuffer *self)
{
return (self->buffer != NULL);
}
void
ring_buffer_free(RingBuffer *self)
{
g_free(self->buffer);
self->head = 0;
self->tail = 0;
self->count = 0;
self->capacity = 0;
self->element_size = 0;
self->buffer = NULL;
}
gboolean
ring_buffer_is_full(RingBuffer *self)
{
return self->capacity == self->count ? TRUE : FALSE;
}
gboolean
ring_buffer_is_empty(RingBuffer *self)
{
return (self->count == 0) ? TRUE : FALSE;
}
gpointer
ring_buffer_push(RingBuffer *self)
{
gpointer r = ring_buffer_tail(self);
if (!r)
return NULL;
++self->count;
self->tail = (self->tail + 1) % self->capacity;
return r;
}
gpointer
ring_buffer_tail (RingBuffer *self)
{
g_assert(self->buffer != NULL);
if (ring_buffer_is_full(self))
return NULL;
gpointer r = (guint8 *) (self->buffer) + self->tail * self->element_size;
return r;
}
gpointer
ring_buffer_pop(RingBuffer *self)
{
g_assert(self->buffer != NULL);
if (ring_buffer_is_empty(self))
return NULL;
gpointer r = (guint8 *) (self->buffer) + self->head * self->element_size;
--self->count;
self->head = (self->head + 1) % self->capacity;
return r;
}
gboolean
ring_buffer_drop(RingBuffer *self, guint32 n)
{
g_assert(self->buffer != NULL);
if (ring_buffer_count(self) < n)
return FALSE;
self->count -= n;
self->head = (self->head + n) % self->capacity;
return TRUE;
}
guint32
ring_buffer_capacity(RingBuffer *self)
{
return self->buffer ? self->capacity : 0;
}
guint32
ring_buffer_count(RingBuffer *self)
{
return self->count;
}
gpointer
ring_buffer_element_at(RingBuffer *self, guint32 idx)
{
g_assert(self->buffer != NULL);
if (idx >= self->count)
return NULL;
return (guint8 *) (self->buffer) + ((self->head + idx) % self->capacity) * self->element_size;
}
guint32
ring_buffer_get_continual_range_length(RingBuffer *self, RingBufferIsContinuousPredicate pred)
{
guint32 r = 0, i;
g_assert(self->buffer != NULL);
for (i = 0; i < ring_buffer_count(self); i++)
{
if (!pred(ring_buffer_element_at(self, i)))
{
break;
}
++r;
}
return r;
}
|