Enable compilation in maemo fremantle sdk.
[connman] / gatchat / ringbuffer.h
1 /*
2  *
3  *  AT chat library with GLib integration
4  *
5  *  Copyright (C) 2008-2009  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifndef __GATCHAT_RINGBUFFER_H
23 #define __GATCHAT_RINGBUFFER_H
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 struct ring_buffer {
30         unsigned char *buffer;
31         unsigned int size;
32         unsigned int in;
33         unsigned int out;
34 };
35
36 /*!
37  * Creates a new ring buffer with capacity size
38  */
39 struct ring_buffer *ring_buffer_new(unsigned int size);
40
41 /*!
42  * Frees the resources allocated for the ring buffer
43  */
44 void ring_buffer_free(struct ring_buffer *buf);
45
46 /*!
47  * Returns the capacity of the ring buffer
48  */
49 int ring_buffer_capacity(struct ring_buffer *buf);
50
51 /*!
52  * Resets the ring buffer, all data inside the buffer is lost
53  */
54 void ring_buffer_reset(struct ring_buffer *buf);
55
56 /*!
57  * Writes data of size len into the ring buffer buf.  Returns -1 if the
58  * write failed or the number of bytes written
59  */
60 int ring_buffer_write(struct ring_buffer *buf, const void *data,
61                         unsigned int len);
62
63 /*!
64  * Advances the write counter by len, this is meant to be used with
65  * the ring_buffer_write_ptr function.  Returns the number of bytes
66  * actually advanced (the capacity of the buffer)
67  */
68 int ring_buffer_write_advance(struct ring_buffer *buf, unsigned int len);
69
70 /*!
71  * Returns the write pointer.  Careful not to write past the end of the
72  * buffer.  Use the ring_buffer_avail_no_wrap function,
73  * ring_buffer_write_advance.
74  */
75 unsigned char *ring_buffer_write_ptr(struct ring_buffer *buf);
76
77 /*!
78  * Returns the number of free bytes available in the buffer
79  */
80 int ring_buffer_avail(struct ring_buffer *buf);
81
82 /*!
83  * Returns the number of free bytes available in the buffer without wrapping
84  */
85 int ring_buffer_avail_no_wrap(struct ring_buffer *buf);
86
87 /*!
88  * Reads data from the ring buffer buf into memory region pointed to by data.
89  * A maximum of len bytes will be read.  Returns -1 if the read failed or
90  * the number of bytes read
91  */
92 int ring_buffer_read(struct ring_buffer *buf, void *data,
93                         unsigned int len);
94
95 /*!
96  * Returns the read pointer with read offset specified by offset.  No bounds
97  * checking is performed.  Be careful not to read past the end of the buffer.
98  * Use the ring_buffer_len_no_wrap function, and ring_buffer_drain.
99  */
100 unsigned char *ring_buffer_read_ptr(struct ring_buffer *buf,
101                                         unsigned int offset);
102
103 /*!
104  * Returns the number of bytes currently available to be read in the buffer
105  */
106 int ring_buffer_len(struct ring_buffer *buf);
107
108 /*!
109  * Returns the number of bytes currently available to be read in the buffer
110  * without wrapping.
111  */
112 int ring_buffer_len_no_wrap(struct ring_buffer *buf);
113
114 /*!
115  * Drains the ring buffer of len bytes.  Returns the number of bytes the
116  * read counter was actually advanced.
117  */
118 int ring_buffer_drain(struct ring_buffer *buf, unsigned int len);
119
120 #ifdef __cplusplus
121 }
122 #endif
123
124 #endif /* __GATCHAT_RINGBUFFER_H */