gwenhywfar  4.3.3
ringbuffer.c
Go to the documentation of this file.
1 /***************************************************************************
2  $RCSfile$
3  -------------------
4  cvs : $Id$
5  begin : Sun Jan 25 2004
6  copyright : (C) 2004 by Martin Preuss
7  email : martin@libchipcard.de
8 
9  ***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
24  * MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32 
33 #define DISABLE_DEBUGLOG
34 
35 #include "ringbuffer_p.h"
36 #include <gwenhywfar/misc.h>
37 #include <gwenhywfar/debug.h>
38 #include <stdlib.h>
39 
40 
41 
43  GWEN_RINGBUFFER *rb;
44 
45  assert(size);
47  DBG_MEM_INC("GWEN_RINGBUFFER", 0);
48  rb->ptr=(char*)malloc(size);
49  rb->bufferSize=size;
50  return rb;
51 }
52 
53 
54 
56  if (rb) {
57  DBG_MEM_DEC("GWEN_RINGBUFFER");
58  free(rb->ptr);
59  GWEN_FREE_OBJECT(rb);
60  }
61 }
62 
63 
64 
66  const char *buffer,
67  uint32_t *size){
68  uint32_t psize;
69  uint32_t bytesLeft;
70 
71  if ((rb->bufferSize-rb->bytesUsed)==0) {
72  DBG_DEBUG(GWEN_LOGDOMAIN, "Buffer full");
73  rb->fullCounter++;
74  return -1;
75  }
76 
77  bytesLeft=*size;
78  while(bytesLeft) {
79  if ((rb->bufferSize-rb->bytesUsed)==0)
80  break;
81  if (rb->writePos>=rb->readPos)
82  psize=rb->bufferSize-rb->writePos;
83  else
84  psize=rb->readPos-rb->writePos;
85  if (psize>bytesLeft)
86  psize=bytesLeft;
87 
88  memmove(rb->ptr+rb->writePos, buffer, psize);
89  rb->writePos+=psize;
90  if (rb->writePos>=rb->bufferSize)
91  rb->writePos=0;
92  rb->bytesUsed+=psize;
93  buffer+=psize;
94  bytesLeft-=psize;
95  } /* while */
96  *size-=bytesLeft;
97  if (rb->bytesUsed>rb->maxBytesUsed)
98  rb->maxBytesUsed=rb->bytesUsed;
99  return 0;
100 }
101 
102 
103 
104 uint32_t
106  uint32_t psize;
107 
108  assert(rb);
109  if (rb->bytesUsed==0) {
110  DBG_DEBUG(GWEN_LOGDOMAIN, "Buffer empty");
111  rb->emptyCounter++;
112  return 0;
113  }
114 
115  if (rb->readPos>=rb->writePos)
116  psize=rb->bufferSize-rb->readPos;
117  else
118  psize=rb->writePos-rb->readPos;
119 
120  return psize;
121 }
122 
123 
124 
125 uint32_t
127  uint32_t psize;
128 
129  assert(rb);
130  if ((rb->bufferSize-rb->bytesUsed)==0) {
131  DBG_DEBUG(GWEN_LOGDOMAIN, "Buffer full");
132  rb->fullCounter++;
133  return 0;
134  }
135 
136  if (rb->writePos>=rb->readPos)
137  psize=rb->bufferSize-rb->writePos;
138  else
139  psize=rb->readPos-rb->writePos;
140 
141  return psize;
142 }
143 
144 
145 
147  uint32_t psize) {
148  assert(rb);
149 
150  if (rb->bytesUsed<psize) {
151  DBG_ERROR(GWEN_LOGDOMAIN, "Asked to skip more bytes than available");
152  abort();
153  }
154  rb->readPos+=psize;
155  if (rb->readPos>=rb->bufferSize)
156  rb->readPos=0;
157  rb->bytesUsed-=psize;
158  rb->throughput+=psize;
159 }
160 
161 
162 
164  uint32_t psize) {
165  assert(rb);
166 
167  if ((rb->bufferSize-rb->bytesUsed)<psize) {
168  DBG_ERROR(GWEN_LOGDOMAIN, "Asked to skip more bytes than possible");
169  abort();
170  }
171 
172  rb->writePos+=psize;
173  if (rb->writePos>=rb->bufferSize)
174  rb->writePos=0;
175  rb->bytesUsed+=psize;
176  if (rb->bytesUsed>rb->maxBytesUsed)
177  rb->maxBytesUsed=rb->bytesUsed;
178 }
179 
180 
181 
183  char *buffer,
184  uint32_t *size){
185  uint32_t psize;
186  uint32_t bytesLeft;
187 
188  if (rb->bytesUsed==0) {
189  DBG_DEBUG(GWEN_LOGDOMAIN, "Buffer empty");
190  rb->emptyCounter++;
191  return -1;
192  }
193 
194  bytesLeft=*size;
195  while(bytesLeft) {
196  if (rb->bytesUsed==0)
197  break;
198  if (rb->readPos>=rb->writePos)
199  psize=rb->bufferSize-rb->readPos;
200  else
201  psize=rb->writePos-rb->readPos;
202  if (psize>bytesLeft)
203  psize=bytesLeft;
204 
205  memmove(buffer, rb->ptr+rb->readPos, psize);
206  rb->readPos+=psize;
207  if (rb->readPos>=rb->bufferSize)
208  rb->readPos=0;
209  rb->bytesUsed-=psize;
210  buffer+=psize;
211  bytesLeft-=psize;
212  } /* while */
213  *size-=bytesLeft;
214  rb->throughput+=*size;
215  return 0;
216 }
217 
218 
219 
221  assert(rb);
222  return rb->bytesUsed;
223 }
224 
225 
226 
228  assert(rb);
229  return rb->bufferSize-rb->bytesUsed;
230 }
231 
232 
233 
235  assert(rb);
236  return rb->bufferSize;
237 }
238 
239 
240 
242  char c) {
243  assert(rb);
244  if ((rb->bufferSize-rb->bytesUsed)==0) {
245  DBG_DEBUG(GWEN_LOGDOMAIN, "Buffer full");
246  rb->fullCounter++;
247  return -1;
248  }
249 
250  rb->ptr[rb->writePos]=c;
251  rb->writePos++;
252  if (rb->writePos>=rb->bufferSize)
253  rb->writePos=0;
254  rb->bytesUsed++;
255  if (rb->bytesUsed>rb->maxBytesUsed)
256  rb->maxBytesUsed=rb->bytesUsed;
257  return 0;
258 }
259 
260 
261 
263  int c;
264 
265  assert(rb);
266  if (rb->bytesUsed==0) {
267  DBG_DEBUG(GWEN_LOGDOMAIN, "Buffer empty");
268  rb->emptyCounter++;
269  return -1;
270  }
271 
272  c=(unsigned char)rb->ptr[rb->readPos];
273  rb->readPos++;
274  if (rb->readPos>=rb->bufferSize)
275  rb->readPos=0;
276  rb->bytesUsed--;
277  rb->throughput++;
278  return c;
279 }
280 
281 
282 
284  assert(rb);
285  return rb->maxBytesUsed;
286 }
287 
288 
289 
291  assert(rb);
292  rb->maxBytesUsed=0;
293 }
294 
295 
296 
298  assert(rb);
299  return rb->throughput;
300 }
301 
302 
303 
305  assert(rb);
306  rb->throughput=0;
307 }
308 
309 
310 
312  assert(rb);
313  return rb->fullCounter;
314 }
315 
316 
317 
319  assert(rb);
320  rb->fullCounter=0;
321 }
322 
323 
324 
326  assert(rb);
327  return rb->emptyCounter;
328 }
329 
330 
331 
333  assert(rb);
334  rb->emptyCounter=0;
335 }
336 
337 
338 
340  assert(rb);
341  return rb->ptr+rb->readPos;
342 }
343 
344 
345 
347  assert(rb);
348  return rb->ptr+rb->writePos;
349 }
350 
351 
352 
354  assert(rb);
355 
356  rb->readPos=0;
357  rb->writePos=0;
358  rb->bytesUsed=0;
359  rb->maxBytesUsed=0;
360  rb->emptyCounter=0;
361  rb->fullCounter=0;
362  rb->throughput=0;
363 }
364 
365 
366 
367 
368 
369