?? rnglib.c
字號:
#include "rngLib.h"typedef struct { int pToBuf; int pFromBuf; int bufSize; char *buf;}RING;typedef RING *RING_ID;/RING_ID rngCreate ( int nbytes /* number of bytes in ring buffer */ ) { char *buffer; RING_ID ringId = (RING_ID) kmalloc (sizeof (RING),GFP_KERNEL); if (ringId == NULL) return (NULL); /* bump number of bytes requested because ring buffer algorithm * always leaves at least one empty byte in buffer */ buffer = (char *) kmalloc ((unsigned) ++nbytes,GFP_KERNEL); if (buffer == NULL) { kfree ((char *)ringId); return (NULL); } ringId->bufSize = nbytes; ringId->buf = buffer; rngFlush (ringId); return (ringId); }void rngDelete ( RING_ID ringId /* ring buffer to delete */ ) { kfree (ringId->buf); kfree ((char *)ringId); }void rngFlush ( RING_ID ringId /* ring buffer to initialize */ ) { ringId->pToBuf = 0; ringId->pFromBuf = 0; }int rngBufGet ( RING_ID rngId, /* ring buffer to get data from */ char *buffer, /* pointer to buffer to receive data */ int maxbytes /* maximum number of bytes to get */ ) { int bytesgot = 0; int pToBuf = rngId->pToBuf; int bytes2; int pRngTmp = 0; if (pToBuf >= rngId->pFromBuf) { /* pToBuf has not wrapped around */ bytesgot = min (maxbytes, pToBuf - rngId->pFromBuf); /*bcopy (&rngId->buf [rngId->pFromBuf], buffer, bytesgot);*/ memcpy(buffer,&rngId->buf [rngId->pFromBuf],bytesgot); rngId->pFromBuf += bytesgot; } else { /* pToBuf has wrapped around. Grab chars up to the end of the * buffer, then wrap around if we need to. */ bytesgot = min (maxbytes, rngId->bufSize - rngId->pFromBuf); /*bcopy (&rngId->buf [rngId->pFromBuf], buffer, bytesgot);*/ memcpy(buffer,&rngId->buf [rngId->pFromBuf],bytesgot); pRngTmp = rngId->pFromBuf + bytesgot; /* If pFromBuf is equal to bufSize, we've read the entire buffer, * and need to wrap now. If bytesgot < maxbytes, copy some more chars * in now. */ if (pRngTmp == rngId->bufSize) { bytes2 = min (maxbytes - bytesgot, pToBuf); /*bcopy (rngId->buf, buffer + bytesgot, bytes2);*/ memcpy(buffer + bytesgot,rngId->buf,bytes2); rngId->pFromBuf = bytes2; bytesgot += bytes2; } else rngId->pFromBuf = pRngTmp; } return (bytesgot); }int rngBufPut ( RING_ID rngId, /* ring buffer to put data into */ char *buffer, /* buffer to get data from */ int nbytes /* number of bytes to try to put */ ) { int bytesput = 0; int pFromBuf = rngId->pFromBuf; int bytes2; int pRngTmp = 0;//printk("3\n"); if (pFromBuf > rngId->pToBuf) { /* pFromBuf is ahead of pToBuf. We can fill up to two bytes * before it */ bytesput = min (nbytes, pFromBuf - rngId->pToBuf - 1); /*bcopy (buffer, &rngId->buf [rngId->pToBuf], bytesput);*/ memcpy(&rngId->buf [rngId->pToBuf],buffer,bytesput); rngId->pToBuf += bytesput; } else if (pFromBuf == 0) { /* pFromBuf is at the beginning of the buffer. We can fill till * the next-to-last element */ bytesput = min (nbytes, rngId->bufSize - rngId->pToBuf - 1); /*bcopy (buffer, &rngId->buf [rngId->pToBuf], bytesput);*/ memcpy(&rngId->buf[rngId->pToBuf],buffer,bytesput); rngId->pToBuf += bytesput; } else { /* pFromBuf has wrapped around, and its not 0, so we can fill * at least to the end of the ring buffer. Do so, then see if * we need to wrap and put more at the beginning of the buffer. */ bytesput = min (nbytes, rngId->bufSize - rngId->pToBuf); /*bcopy (buffer, &rngId->buf [rngId->pToBuf], bytesput);*/ memcpy(&rngId->buf [rngId->pToBuf],buffer,bytesput); pRngTmp = rngId->pToBuf + bytesput; if (pRngTmp == rngId->bufSize) { /* We need to wrap, and perhaps put some more chars */ bytes2 = min (nbytes - bytesput, pFromBuf - 1); /*bcopy (buffer + bytesput, rngId->buf, bytes2);*/ memcpy(rngId->buf,buffer + bytesput,bytes2); rngId->pToBuf = bytes2; bytesput += bytes2; } else rngId->pToBuf = pRngTmp; } //printk("4\n"); return (bytesput); }int rngIsEmpty ( RING_ID ringId /* ring buffer to test */ ) { if (ringId->pToBuf == ringId->pFromBuf) return 1; else return 0; }int rngIsFull ( RING_ID ringId /* ring buffer to test */ ) { int n = ringId->pToBuf - ringId->pFromBuf + 1; if ((n == 0) || (n == ringId->bufSize)) return 0; else return -1; }int rngFreeBytes ( RING_ID ringId /* ring buffer to examine */ ) { int n = ringId->pFromBuf - ringId->pToBuf - 1; if (n < 0) n += ringId->bufSize; return (n); }int rngNBytes ( RING_ID ringId /* ring buffer to be enumerated */ ) { int n = ringId->pToBuf - ringId->pFromBuf; if (n < 0) n += ringId->bufSize; return (n); }void rngPutAhead ( RING_ID ringId, /* ring buffer to put byte in */ char byte, /* byte to be put in ring */ int offset /* offset beyond next input byte where to put byte */ ) { int n = ringId->pToBuf + offset; if (n >= ringId->bufSize) n -= ringId->bufSize; *(ringId->buf + n) = byte; }void rngMoveAhead ( RING_ID ringId, /* ring buffer to be advanced */ int n /* number of bytes ahead to move input pointer */ ) { n += ringId->pToBuf; if (n >= ringId->bufSize) n -= ringId->bufSize; ringId->pToBuf = n; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -