|
|
|
|
|
|
|
||
|
||
|
|
||
May 1999 |
Revision 1.01 |
|
Persistor Instruments Inc. |
© 1998 All rights reserved. |
|
Ping-pong buffers are a specialized form of FIFO (First-In, First-out) queue that divides a block of storage into two equal halves, with one half (the write buffer) generally always available for writing, and the other half (the read buffer) generally emptied in one fell swoop when the write buffer fills and the ping-pong action makes the former write buffer now available for reading. Ping-pong buffers are similar to ring or circular buffers in that you you can keep writing to them so long as the corrosponding reads empty the buffer before it fills. They differ in that the read data does not become available until after the write buffer has filled, and more important (for the common memory buffers) the read buffer when available is guaranteed to be one contiguous chunk. This means you can send the output from a ping-pong buffer as a pointer argument to other routines, like memcpy(), write(), fwrite() and so on. You could construct a ping-pong buffer from a ring buffer by not reading from the buffer until it became half-full, but you could not access the data as a contigous block. The ping-pong buffer presented here is tailored for maximum efficiency in ping-pong mode.
Ping-pong buffers are especially useful for holding data that will ultimately be stored to devices that work best when handling large discrete blocks. For example, the Persistor GB2 uses a pair of ping-pong buffers to allow it to continuously accept incomming data streams at rates of up to 100kb/s while keeping the hard disk drive fully shut down whenever it's not absolutely essential that it be on. Incomming data steams into a 64KB ping-pong buffer (32KB/32KB) and when one half fills up and the buffer ping-pongs, the 32kb of available data is streamed into 40MB CompactFlash card ping-pong buffer (20MB/20MB) that takes advantage of the 32KB block size to write much faster and consume less power than it would working in smaller blocks. When the CompactFlash buffer ping-pongs, the hard drive is powered up (which typically takes four seconds, but is specified for a worst case of 20 seconds) then the CompactFlash data is pulled from the ping-pong buffer (in 32KB chuncks to allow the RAM ping-pong buffer to write new data) and written to the hard drive, after which it gets shut back down.
enum
{
ppbErrorStart = PPB_ERRORS
, ppbWrOvflBufNotEmpty // write operation would overflow to non-empty buffer
, ppbWrIOFailed // low level write failed
, ppbRdIOFailed // low level read failed
, ppbRdBufferEmpty // no data in read buffer
, ppbMemAccReqToNonMem // attempted direct memory access to non-memory device
, pppWrBufferInUse // attempted to write while another write was in process
, pppRdBufferInUse // attempted to read while another read was in process
};
Description
typedef long PPBWrf(void *buf, void *wrp, ulong wrofs, ulong n); typedef long PPBRdf(void *buf, void *rdp, ulong rdofs, ulong n);
|
Prototype: |
void *PPBOpen(long totSize, void *buf, PPBRdf rdf, PPBWrf wrf, vfptr ppnotify); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
totSize is the combined size in bytes of both
halves of the user supplied ping-pong buffer |
|
Returns: |
Returns a generic pointer used internally by PPB to manage the buffers or zero on failure |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
void PPBClose(void *ppb); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and used internally to manage the buffers. |
|
Returns: |
Returns nothing |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
short PPBFlush(void *ppb); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and used internally to manage the buffers. |
|
Returns: |
Returns zero for success or a non-zero error code |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
long PPBWrite(void *ppb, void *buf, long nbyte); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and
used internally to manage the buffers. |
|
Returns: |
Returns nbyte for complete success or the number of bytes written |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
long PPBRead(void *ppb, void *buf, long nbyte); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and
used internally to manage the buffers. |
|
Returns: |
Returns nbyte for complete success or the number of bytes read |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
short PPBPutByte(void *ppb, uchar byte); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and
used internally to manage the buffers. |
|
Returns: |
Returns zero for success or a non-zero error code |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
short PPBPutWord(); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and
used internally to manage the buffers. |
|
Returns: |
Returns zero for success or a non-zero error code |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
void *PPBGetMemBuf(void *ppb, long *size, bool flush); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and
used internally to manage the buffers. |
|
Returns: |
Returns a direct pointer to the read data buffer |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
long PPBCheckRdAvail(void *ppb); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and used internally to manage the buffers. |
|
Returns: |
Returns the count of available bytes in the read buffer |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
long PPBCheckWrFree(void *ppb); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and used internally to manage the buffers. |
|
Returns: |
Returns the number of bytes that can be written before the buffer ping-pongs |
|
Notes: |
Notes |
|
Timing: |
TBD |
Description
|
Prototype: |
short PPBErrorCode(void *ppb, bool clear); |
|
Definition: |
#include <cf1pico.h> |
|
Inputs: |
ppb is the generic pointer returned by PPBOpen and
used internally to manage the buffers. |
|
Returns: |
Returns the most recent error code from the enumerated list |
|
Notes: |
Notes |
|
Timing: |
TBD |
|
|
|
|
|
|
Tel: 508-759-6434 |
|
Fax: 508-759-6436 |
|
|
||