00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00033 #ifndef _CIRBUF_H_
00034 #define _CIRBUF_H_
00035
00036 #include <aversive.h>
00037 #include <stdio.h>
00038
00039 #ifdef CONFIG_MODULE_CIRBUF_LARGE
00040 typedef signed int cirbuf_int;
00041 typedef unsigned int cirbuf_uint;
00042 #else
00043 typedef signed char cirbuf_int;
00044 typedef unsigned char cirbuf_uint;
00045 #endif
00046
00050 struct cirbuf {
00051 cirbuf_uint maxlen;
00052 volatile cirbuf_uint start;
00053 volatile cirbuf_uint end;
00054 volatile cirbuf_uint len;
00055 char *buf;
00056 };
00057
00058
00059
00060 #ifdef CIRBUF_DEBUG
00061 #define dprintf(fmt, ...) printf("line %3.3d - " fmt, __LINE__, ##__VA_ARGS__)
00062 #else
00063 #define dprintf(args...) do {} while(0)
00064 #endif
00065
00066
00070 void cirbuf_init(struct cirbuf *cbuf, char *buf, cirbuf_uint start, cirbuf_uint maxlen);
00071
00072
00076 #define CIRBUF_IS_FULL(cirbuf) ((cirbuf)->maxlen == (cirbuf)->len)
00077
00081 #define CIRBUF_IS_EMPTY(cirbuf) ((cirbuf)->len == 0)
00082
00086 #define CIRBUF_GET_LEN(cirbuf) ((cirbuf)->len)
00087
00091 #define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen)
00092
00096 #define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len)
00097
00104 #define CIRBUF_FOREACH(c, i, e) \
00105 for ( i=0, e=(c)->buf[(c)->start] ; \
00106 i<((c)->len) ; \
00107 i ++, e=(c)->buf[((c)->start+i)%((c)->maxlen)])
00108
00109
00114 cirbuf_int cirbuf_add_head_safe(struct cirbuf *cbuf, char c);
00115
00120 void cirbuf_add_head(struct cirbuf *cbuf, char c);
00121
00126 cirbuf_int cirbuf_add_tail_safe(struct cirbuf *cbuf, char c);
00127
00132 void cirbuf_add_tail(struct cirbuf *cbuf, char c);
00133
00138 cirbuf_int cirbuf_del_head_safe(struct cirbuf *cbuf);
00139
00144 void cirbuf_del_head(struct cirbuf *cbuf);
00145
00150 cirbuf_int cirbuf_del_tail_safe(struct cirbuf *cbuf);
00151
00156 void cirbuf_del_tail(struct cirbuf *cbuf);
00157
00162 char cirbuf_get_head(struct cirbuf *cbuf);
00163
00168 char cirbuf_get_tail(struct cirbuf *cbuf);
00169
00170
00171
00177 cirbuf_int cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, cirbuf_uint n);
00178
00184 cirbuf_int cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, cirbuf_uint n);
00185
00190 cirbuf_int cirbuf_del_buf_head(struct cirbuf *cbuf, cirbuf_uint size);
00191
00196 cirbuf_int cirbuf_del_buf_tail(struct cirbuf *cbuf, cirbuf_uint size);
00197
00203 cirbuf_int cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, cirbuf_uint size);
00204
00210 cirbuf_int cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, cirbuf_uint size);
00211
00212
00216 void cirbuf_align_left(struct cirbuf *cbuf);
00217
00221 void cirbuf_align_right(struct cirbuf *cbuf);
00222
00223 #endif