forked from maemo-leste/libcmtspeechdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sal_ring.h
140 lines (126 loc) · 3.68 KB
/
sal_ring.h
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
/*
* This file is part of libcmtspeechdata.
*
* Copyright (C) 2008,2009,2010 Nokia Corporation.
*
* Contact: Kai Vehmanen <[email protected]>
*
* 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
*/
/** @file sal_ring.h
*
* Ring buffer implementation for libcmtspeechdata.
*
* The implementation is a simplified ring buffer with
* no fill count tracking, but instead one octet is kept
* open in the buffer (maximum fill is thus 'ring->size - 1').
*
* Ring buffer size is not limited to power of two sizes.
*/
#ifndef INCLUDED_SAL_RING_H
#define INCLUDED_SAL_RING_H
#include <stdint.h>
struct ring_buffer_s {
int read_idx;
int write_idx;
int size; /**< octets of allocated space */
uint8_t *data; /**< pointer to a buffer of 'size' octets */
};
typedef struct ring_buffer_s ring_buffer_t;
/**
* Initializes the ring buffer for use.
*
* @param ring self
* @param buf pointer to buffer data area
* @param size size of the data area in octets
*/
static inline void ring_buffer_init(ring_buffer_t *ring, uint8_t *buf, int size)
{
ring->read_idx = 0;
ring->write_idx = 0;
ring->size = size;
ring->data = buf;
}
/**
* Resets the ring buffer state.
*
* @param ring self
*/
static inline void ring_buffer_reset(ring_buffer_t *ring)
{
ring->read_idx = 0;
ring->write_idx = 0;
}
/**
* Returns number of octets that can be read (between 0 and
* ring->size-1).
*/
static inline int ring_buffer_avail_for_read(ring_buffer_t *ring)
{
return (ring->write_idx - ring->read_idx + ring->size) % ring->size;
}
/**
* Returns number of octets that can be read from a continuous
* buffer segment (between 0 and ring->size-1).
*/
static inline int ring_buffer_cavail_for_read(ring_buffer_t *ring)
{
if (ring->write_idx >= ring->read_idx)
return ring->write_idx - ring->read_idx;
else
return ring->size - ring->read_idx;
}
/**
* Returns number of octets that can be written (between 0
* and ring->size-1).
*/
static inline int ring_buffer_avail_for_write(ring_buffer_t *ring)
{
if (ring->read_idx == ring->write_idx)
return ring->size - 1;
else
return (ring->read_idx - ring->write_idx - 1 + ring->size) % ring->size;
}
/**
* Returns number of octets that can be written to
* a continuous buffer segment (between 0 and ring->size-1).
*/
static inline int ring_buffer_cavail_for_write(ring_buffer_t *ring)
{
if (ring->read_idx > ring->write_idx)
return ring_buffer_avail_for_write(ring);
else if (ring->read_idx == 0)
return ring->size - ring->write_idx -1;
else
return ring->size - ring->write_idx;
}
/**
* Moves read pointer ahead for 'n' octets. Does not care about
* possible overrun.
*/
static inline void ring_buffer_move_read(ring_buffer_t *ring, int n)
{
ring->read_idx = (ring->read_idx + n) % ring->size;
}
/**
* Moves write pointer ahead for 'n' octets. Does not care about
* possible overrun.
*/
static inline void ring_buffer_move_write(ring_buffer_t *ring, int n)
{
ring->write_idx = (ring->write_idx + n) % ring->size;
}
#endif /* INCLUDED_SAL_RING_H */