Last active
December 17, 2015 08:49
-
-
Save hidsh/5582658 to your computer and use it in GitHub Desktop.
通信用リングバッファのサンプル。ライトポインタはリードポインタを追い越せない。リードポインタもライトポインタを追い越せない。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* mystd.h */ | |
| #ifndef __MYSTD_H__ | |
| #define __MYSTD_H__ | |
| /* types and values */ | |
| typedef unsigned char u1; | |
| typedef unsigned short u2; | |
| typedef unsigned long u4; | |
| typedef signed char s1; | |
| typedef signed short s2; | |
| typedef signed long s4; | |
| typedef unsigned char bool; | |
| #define FALSE 0 | |
| #define TRUE 1 | |
| #define LO 0 | |
| #define HI 1 | |
| #define OFF 0 | |
| #define ON 1 | |
| #define EXIT_SUCCESS 0 | |
| #define EXIT_FAILURE 1 | |
| /* pseudo functions */ | |
| #define __ei() sei() | |
| #define __di() cli() | |
| #endif /* __MYSTD_H__ */ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $gcc -std=c99 ring_buffer.c && ./a.out | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| FF FF FF FF FF FF FF FF | |
| r | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| FF FF FF FF FF FF FF FF | |
| r | |
| write: 3 | |
| write: 4 | |
| write: 5 | |
| write: 6 | |
| write: 7 | |
| write: 8 | |
| write: 9 | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| FF 3 4 5 6 7 8 9 | |
| r | |
| read: 3 | |
| read: 4 | |
| read: 5 | |
| read: 6 | |
| read: 7 | |
| read: 8 | |
| read: 9 | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| FF 3 4 5 6 7 8 9 | |
| r | |
| write: 17 | |
| write: 18 | |
| write: 19 | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| 17 18 19 5 6 7 8 9 | |
| r | |
| read: 17 | |
| read: 18 | |
| read: 19 | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| 17 18 19 5 6 7 8 9 | |
| r | |
| write: 1A | |
| write: 1B | |
| write: 1C | |
| write: 1D | |
| write: 1E | |
| write: 1F | |
| write: 20 | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| 1F 20 19 1A 1B 1C 1D 1E | |
| r | |
| read: 1A | |
| read: 1B | |
| read: 1C | |
| read: 1D | |
| read: 1E | |
| read: 1F | |
| read: 20 | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| 1F 20 19 1A 1B 1C 1D 1E | |
| r | |
| $ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include "mystd.h" | |
| #define SZ 8 | |
| u1 buf[SZ]; | |
| u1 pw, pr; | |
| void dump(void) | |
| { | |
| u1 dpw[SZ], dpr[SZ]; | |
| for(int i=0; i<SZ; i++) { | |
| dpw[i] = dpr[i] = ' '; | |
| } | |
| dpw[pw] = 'w'; | |
| dpr[pr] = 'r'; | |
| printf("-------------------------------------------------\n"); | |
| printf("[%1d] [%1d] [%1d] [%1d] [%1d] [%1d] [%1d] [%1d]\n", 0, 1, 2, 3, 4, 5, 6, 7); | |
| printf(" %1c %1c %1c %1c %1c %1c %1c %1c \n", dpw[0], dpw[1], dpw[2], dpw[3], dpw[4], dpw[5], dpw[6], dpw[7]); | |
| printf("%2X %2X %2X %2X %2X %2X %2X %2X\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); | |
| printf(" %1c %1c %1c %1c %1c %1c %1c %1c \n", dpr[0], dpr[1], dpr[2], dpr[3], dpr[4], dpr[5], dpr[6], dpr[7]); | |
| } | |
| void init_buf(void) | |
| { | |
| for(int i=0; i<SZ; i++) | |
| buf[i] = 0xFF; | |
| pw = 0; | |
| pr = 0; | |
| } | |
| void write(u1 data) | |
| { | |
| u1 nextw = (pw+1) % SZ; | |
| if(nextw == pr) return; | |
| pw = nextw; | |
| buf[pw] = data; | |
| printf("write: %X\n", buf[pw]); | |
| } | |
| void read() | |
| { | |
| u1 nextw = (pw+1) % SZ; | |
| u1 nextr = (pr+1) % SZ; | |
| if(nextw == nextr) return; | |
| pr = nextr; | |
| printf("read: %X\n", buf[pr]); | |
| } | |
| int main(void) | |
| { | |
| u1 v = 3; | |
| init_buf(); | |
| dump(); | |
| for(int i=0; i<20; i++) read(); | |
| dump(); | |
| for(int i=0; i<20; i++) write(v++); | |
| dump(); | |
| for(int i=0; i<20; i++) read(); | |
| dump(); | |
| for(int i=0; i<3; i++) write(v++); | |
| dump(); | |
| for(int i=0; i<20; i++) read(); | |
| dump(); | |
| for(int i=0; i<20; i++) write(v++); | |
| dump(); | |
| for(int i=0; i<20; i++) read(); | |
| dump(); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment