annotate src/neon/rb.c @ 2273:f893d05f707b

Hotkey: some cleaning and simplifying
author Sascha Hlusiak <contact@saschahlusiak.de>
date Mon, 31 Dec 2007 17:38:27 +0100
parents b73ea297d197
children 89624b51e73d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1751
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
1 /*
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
2 * This program is free software; you can redistribute it and/or modify
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
3 * it under the terms of the GNU General Public License as published by
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
4 * the Free Software Foundation; either version 2 of the License, or
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
5 * (at your option) any later version.
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
6 *
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
7 * This program is distributed in the hope that it will be useful,
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
10 * GNU General Public License for more details.
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
11 *
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
12 * You should have received a copy of the GNU General Public License
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
13 * along with this program; if not, write to the Free Software
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
15 */
dc83901850df - Add license texts to the neon source
Ralf Ertzinger <ralf@skytale.net>
parents: 1719
diff changeset
16
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
17 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
18 * Ringbuffer implementation
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
19 *
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
20 * GPL
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
21 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
22 #include <string.h>
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
23 #include "rb.h"
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
24 #include "debug.h"
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
25
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
26 #ifdef RB_DEBUG
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
27 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
28 * An internal assertion function to make sure that the
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
29 * ringbuffer structure is consistient.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
30 *
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
31 * WARNING: This function will call abort() if the ringbuffer
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
32 * is found to be inconsistient.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
33 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
34 static void _assert_rb(struct ringbuf* rb) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
35
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
36 unsigned int realused;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
37
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
38 _ENTER;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
39
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
40 _DEBUG("rb->buf=%p, rb->end=%p, rb->wp=%p, rb->rp=%p, rb->free=%u, rb->used=%u, rb->size=%u",
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
41 rb->buf, rb->end, rb->wp, rb->rp, rb->free, rb->used, rb->size);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
42
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
43 if (0 == rb->size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
44 _ERROR("Buffer size is 0");
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
45 abort();
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
46 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
47
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
48 if (NULL == rb->buf) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
49 _ERROR("Buffer start is NULL");
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
50 abort();
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
51 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
52
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
53 if (rb->used+rb->free != rb->size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
54 _ERROR("rb->free and rb->used do not add up to rb->size");
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
55 abort();
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
56 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
57
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
58 if (rb->buf+(rb->size-1) != rb->end) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
59 _ERROR("rb->buf and rb->end not rb->size bytes apart");
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
60 abort();
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
61 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
62
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
63 if ((rb->wp < rb->buf) || (rb->wp > rb->end)) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
64 _ERROR("Write pointer outside buffer space");
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
65 abort();
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
66 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
67
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
68 if ((rb->rp < rb->buf) || (rb->rp > rb->end)) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
69 _ERROR("Read pointer outside buffer space");
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
70 abort();
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
71 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
72
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
73 if (rb->rp <= rb->wp) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
74 realused = rb->wp - rb->rp;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
75 } else {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
76 realused = (rb->end - rb->rp) + 1 + (rb->wp-rb->buf);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
77 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
78
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
79 if (rb->used != realused) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
80 _ERROR("Usage count is inconsistient (is %d, should be %d)", rb->used, realused);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
81 abort();
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
82 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
83
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
84 _LEAVE;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
85 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
86 #endif
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
87
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
88 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
89 * Reset a ringbuffer structure (i.e. discard
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
90 * all data inside of it)
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
91 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
92 void reset_rb(struct ringbuf* rb) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
93
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
94 _ENTER;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
95
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
96 _RB_LOCK(rb->lock);
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
97
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
98 rb->wp = rb->buf;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
99 rb->rp = rb->buf;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
100 rb->free = rb->size;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
101 rb->used = 0;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
102 rb->end = rb->buf+(rb->size-1);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
103
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
104 _RB_UNLOCK(rb->lock);
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
105
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
106 _LEAVE;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
107 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
108
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
109 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
110 * Initialize a ringbuffer structure (including
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
111 * memory allocation.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
112 *
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
113 * Return -1 on error
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
114 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
115 int init_rb(struct ringbuf* rb, unsigned int size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
116
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
117 _ENTER;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
118
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
119 if (0 == size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
120 _LEAVE -1;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
121 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
122
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
123 if (NULL == (rb->buf = malloc(size))) {
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
124 _LEAVE -1;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
125 }
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
126 rb->size = size;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
127
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
128 #ifdef _RB_USE_GLIB
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
129 if (NULL == (rb->lock = g_mutex_new())) {
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
130 _LEAVE -1;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
131 }
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
132 #else
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
133 if (NULL == (rb->lock = malloc(sizeof(pthread_mutex_t)))) {
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
134 _LEAVE -1;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
135 }
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
136
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
137 if (0 != pthread_mutex_init(rb->lock, NULL)) {
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
138 free(rb->lock);
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
139 _LEAVE -1;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
140 }
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
141 #endif
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
142 rb->_free_lock = 1;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
143
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
144 reset_rb(rb);
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
145
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
146 ASSERT_RB(rb);
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
147
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
148 _LEAVE 0;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
149 }
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
150
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
151 /*
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
152 * Initialize a ringbuffer structure (including
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
153 * memory allocation.
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
154 * The mutex to be used is passed in the function call.
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
155 * The mutex must not be held while calling this function.
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
156 *
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
157 * Return -1 on error
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
158 */
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
159 int init_rb_with_lock(struct ringbuf* rb, unsigned int size, rb_mutex_t* lock) {
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
160
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
161 _ENTER;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
162
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
163 if (0 == size) {
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
164 _LEAVE -1;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
165 }
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
166
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
167 rb->lock = lock;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
168 rb->_free_lock = 0;
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
169
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
170 if (NULL == (rb->buf = malloc(size))) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
171 _LEAVE -1;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
172 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
173 rb->size = size;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
174 reset_rb(rb);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
175
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
176 ASSERT_RB(rb);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
177
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
178 _LEAVE 0;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
179 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
180
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
181 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
182 * Write size bytes at buf into the ringbuffer.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
183 * Return -1 on error (not enough space in buffer)
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
184 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
185 int write_rb(struct ringbuf* rb, void* buf, unsigned int size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
186
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
187 int ret = -1;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
188 int endfree;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
189
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
190 _ENTER;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
191
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
192 _RB_LOCK(rb->lock);
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
193
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
194 ASSERT_RB(rb);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
195
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
196 if (rb->free < size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
197 ret = -1;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
198 goto out;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
199 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
200
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
201 endfree = (rb->end - rb->wp)+1;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
202 if (endfree < size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
203 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
204 * There is enough space in the buffer, but not in
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
205 * one piece. We need to split the copy into two parts.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
206 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
207 memcpy(rb->wp, buf, endfree);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
208 memcpy(rb->buf, buf+endfree, size-endfree);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
209 rb->wp = rb->buf + (size-endfree);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
210 } else if (endfree > size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
211 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
212 * There is more space than needed at the end
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
213 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
214 memcpy(rb->wp, buf, size);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
215 rb->wp += size;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
216 } else {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
217 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
218 * There is exactly the space needed at the end.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
219 * We need to wrap around the read pointer.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
220 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
221 memcpy(rb->wp, buf, size);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
222 rb->wp = rb->buf;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
223 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
224
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
225 rb->free -= size;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
226 rb->used += size;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
227
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
228 ret = 0;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
229
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
230 out:
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
231 ASSERT_RB(rb);
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
232 _RB_UNLOCK(rb->lock);
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
233
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
234 _LEAVE ret;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
235 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
236
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
237 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
238 * Read size byes from buffer into buf.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
239 * Return -1 on error (not enough data in buffer)
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
240 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
241 int read_rb(struct ringbuf* rb, void* buf, unsigned int size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
242
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
243 int ret;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
244
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
245 _ENTER;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
246
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
247 _RB_LOCK(rb->lock);
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
248 ret = read_rb_locked(rb, buf, size);
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
249 _RB_UNLOCK(rb->lock);
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
250
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
251 _LEAVE ret;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
252 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
253
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
254 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
255 * Read size bytes from buffer into buf, assuming the buffer lock
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
256 * is already held.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
257 * Return -1 on error (not enough data in buffer)
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
258 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
259 int read_rb_locked(struct ringbuf* rb, void* buf, unsigned int size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
260
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
261 int endused;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
262
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
263 _ENTER;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
264
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
265 ASSERT_RB(rb);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
266
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
267 if (rb->used < size) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
268 /* Not enough bytes in buffer */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
269 _LEAVE -1;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
270 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
271
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
272 if (rb->rp < rb->wp) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
273 /*
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
274 Read pointer is behind write pointer, all the data is available in one chunk
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
275 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
276 memcpy(buf, rb->rp, size);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
277 rb->rp += size;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
278 } else {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
279 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
280 * Read pointer is before write pointer
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
281 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
282 endused = (rb->end - rb->rp)+1;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
283
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
284 if (size < endused) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
285 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
286 * Data is available in one chunk
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
287 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
288 memcpy(buf, rb->rp, size);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
289 rb->rp += size;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
290 } else {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
291 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
292 * There is enough data in the buffer, but it is fragmented.
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
293 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
294 memcpy(buf, rb->rp, endused);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
295 memcpy(buf+endused, rb->buf, size-endused);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
296 rb->rp = rb->buf + (size-endused);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
297 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
298 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
299
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
300 rb->free += size;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
301 rb->used -= size;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
302
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
303 ASSERT_RB(rb);
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
304
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
305 _LEAVE 0;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
306 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
307
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
308 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
309 * Return the amount of free space currently in the rb
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
310 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
311 unsigned int free_rb(struct ringbuf* rb) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
312
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
313 unsigned int f;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
314
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
315 _ENTER;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
316
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
317 _RB_LOCK(rb->lock);
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
318 f = free_rb_locked(rb);
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
319 _RB_UNLOCK(rb->lock);
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
320
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
321 _LEAVE f;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
322 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
323
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
324 /*
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
325 * Return the amount of free space currently in the rb.
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
326 * Assume the rb lock is already being held.
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
327 */
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
328 unsigned int free_rb_locked(struct ringbuf* rb) {
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
329
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
330 _ENTER;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
331
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
332 _LEAVE rb->free;
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
333 }
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
334
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
335
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
336 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
337 * Return the amount of used space currently in the rb
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
338 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
339 unsigned int used_rb(struct ringbuf* rb) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
340
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
341 unsigned int u;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
342
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
343 _ENTER;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
344
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
345 _RB_LOCK(rb->lock);
2183
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
346 u = used_rb_locked(rb);
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
347 _RB_UNLOCK(rb->lock);
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
348
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
349 _LEAVE u;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
350 }
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
351
2183
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
352 /*
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
353 * Return the amount of used space currently in the rb.
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
354 * Assume the rb lock is already being held.
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
355 */
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
356 unsigned int used_rb_locked(struct ringbuf* rb) {
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
357
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
358 _ENTER;
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
359
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
360 _LEAVE rb->used;
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
361 }
b73ea297d197 - New version of ringbuffer code
Ralf Ertzinger <ralf@skytale.net>
parents: 2151
diff changeset
362
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
363
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
364 /*
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
365 * destroy a ringbuffer
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
366 */
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
367 void destroy_rb(struct ringbuf* rb) {
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
368
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
369 _ENTER;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
370 free(rb->buf);
2151
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
371 if (rb->_free_lock) {
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
372 #ifdef _RB_USE_GLIB
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
373 g_mutex_free(rb->lock);
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
374 #else
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
375 pthread_mutex_destroy(rb->lock);
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
376 free(rb->lock);
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
377 #endif
9a9f406374c6 - Update ring buffer code to newer version
Ralf Ertzinger <ralf@skytale.net>
parents: 1751
diff changeset
378 }
1719
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
379
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
380 _LEAVE;
29c35cb8873e - Add neon HTTP transport plugin sources (for real)
Ralf Ertzinger <ralf@skytale.net>
parents:
diff changeset
381 }