00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <string.h>
00024
00025 #include <cirbuf.h>
00026
00027 static inline void
00028 __cirbuf_shift_left(struct cirbuf * cbuf)
00029 {
00030 cirbuf_uint i;
00031 char tmp = cbuf->buf[cbuf->start];
00032
00033 for (i=0 ; i<cbuf->len ; i++) {
00034 cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
00035 cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen];
00036 }
00037 cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp;
00038 cbuf->start += (cbuf->maxlen - 1);
00039 cbuf->start %= cbuf->maxlen;
00040 cbuf->end += (cbuf->maxlen - 1);
00041 cbuf->end %= cbuf->maxlen;
00042 }
00043
00044 static inline void
00045 __cirbuf_shift_right(struct cirbuf * cbuf)
00046 {
00047 cirbuf_uint i;
00048 char tmp = cbuf->buf[cbuf->end];
00049
00050 for (i=0 ; i<cbuf->len ; i++) {
00051 cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
00052 cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen];
00053 }
00054 cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp;
00055 cbuf->start += 1;
00056 cbuf->start %= cbuf->maxlen;
00057 cbuf->end += 1;
00058 cbuf->end %= cbuf->maxlen;
00059 }
00060
00061
00062 void cirbuf_align_left(struct cirbuf * cbuf)
00063 {
00064 if (cbuf->start < cbuf->maxlen/2) {
00065 while (cbuf->start != 0) {
00066 __cirbuf_shift_left(cbuf);
00067 }
00068 }
00069 else {
00070 while (cbuf->start != 0) {
00071 __cirbuf_shift_right(cbuf);
00072 }
00073 }
00074 }
00075
00076
00077 void cirbuf_align_right(struct cirbuf * cbuf)
00078 {
00079 if (cbuf->start >= cbuf->maxlen/2) {
00080 while (cbuf->end != cbuf->maxlen-1) {
00081 __cirbuf_shift_left(cbuf);
00082 }
00083 }
00084 else {
00085 while (cbuf->start != cbuf->maxlen-1) {
00086 __cirbuf_shift_right(cbuf);
00087 }
00088 }
00089 }
00090