comparison intercom/cirbuf.cpp @ 2:13be24d74cd2

import intercom-0.4.1
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 09:57:52 +0200
parents
children
comparison
equal deleted inserted replaced
1:9cadc470e3da 2:13be24d74cd2
1 /* cirbuf.cpp
2 *
3 * Copyright (C) DFS Deutsche Flugsicherung (2004, 2005).
4 * All Rights Reserved.
5 *
6 * Circular buffers
7 *
8 * Version 0.2
9 */
10
11 #include <string.h>
12 #include <strings.h>
13
14 #include "oss.h"
15 #include "cirbuf.h"
16 #include "intercomd.h"
17
18 CIRBUF::CIRBUF()
19 {
20 bzero(buf, CIRBUFSIZE);
21 in = out = len = 0;
22 }
23
24 void CIRBUF::init()
25 {
26 bzero(buf, CIRBUFSIZE);
27 in = out = len = 0;
28 }
29
30 int CIRBUF::push(char *from, int size)
31 {
32 memcpy(buf + in, from, size);
33 in += size;
34 if (in >= CIRBUFSIZE) {
35 in -= CIRBUFSIZE;
36 }
37 len += size;
38 if (len > CIRBUFSIZE) {
39 int oversize = (((len - CIRBUFSIZE) / FRAGSIZE)) * FRAGSIZE;
40 if (oversize < len - CIRBUFSIZE) {
41 oversize += FRAGSIZE;
42 }
43 // delete oldest if overrun
44 out += oversize;
45 if (out >= CIRBUFSIZE) {
46 out -= CIRBUFSIZE;
47 }
48 len -= oversize;
49 return -oversize;
50 } else {
51 return OKAY;
52 }
53 }
54
55 int CIRBUF::pop(char *to, int size)
56 {
57 if (len < size) {
58 // play out silence if underrun
59 bzero(to, size);
60 return ERROR;
61 }
62 memcpy(to, buf + out, size);
63 out += size;
64 if (out >= CIRBUFSIZE) {
65 out -= CIRBUFSIZE;
66 }
67 len -= size;
68 return OKAY;
69 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.