/**************************************************************************** * Data Buffer for the Real-time BBC Codec/Modem * **************************************************************************** * William L. Bahn * * Academy Center for Information Security * * Department of Computer Science * * United States Air Force Academy * * USAFA, CO 80840 * **************************************************************************** * FILE:............ buffer.c * * DATE CREATED:.... 01 SEP 07 * * DATE MODIFIED:... 01 SEP 07 * **************************************************************************** * * REVISION HISTORY * **************************************************************************** * * DESCRIPTION * * The data buffer and its programmer interface is described in buffer.h. * **************************************************************************** */ //------------------------------------------------------------------------ // REQUIRED INCLUDES //------------------------------------------------------------------------ #include // malloc(), free() #include // memset() #include "buffer.h" //------------------------------------------------------------------------ // STRUCTURE DEFINITIONS //------------------------------------------------------------------------ // NOTE: Normally the structure definition would be in the *.c file to make // the structure members inaccessible to outside functions except through // public function calls. But for the real-time code it has been decided // to make the structure members directly visible to the functions that // manipulate them. //------------------------------------------------------------------------ // PRIMITIVE FUNCTION DEFINITIONS //------------------------------------------------------------------------ //------------------------------------------------------------------------ // PRIVATE FUNCTION DEFINITIONS //------------------------------------------------------------------------ //------------------------------------------------------------------------ // PUBLIC FUNCTION DEFINITIONS //------------------------------------------------------------------------ BUFFER *BUFFER_Del(BUFFER *p) { if (p) { if (p->buffer) { free (p->buffer); p->buffer = NULL; } } return NULL; } BUFFER *BUFFER_New(CONFIG *c, DWORD *errcode) { BUFFER *p; DWORD err; p = NULL; err = 0; if (!err) { p = (BUFFER *) malloc(sizeof(BUFFER)); if (!p) err |= 1 << 1; } if (!err) { p->minsize = (size_t) (c->bufferbytes_per_packet * c->buffer_packets); p->size = 1; while ((0 != p->size)&&(p->size < p->minsize)) p->size <<= 1; if (0 == p->size) err |= 1 << 2; } if (!err) { // Allocate buffer memory p->buffer = (BYTE *) malloc(p->size*sizeof(BYTE)); if (!p->buffer) err |= 1 << 3; // Initialize buffer state // Common to TX and RX p->buffermask = p->size - 1; p->scope = c->bufferbytes_per_packet; p->read = 0; p->write = 0; p->overflows = 0; // TX and RX specific if (c->scheduler_TX_notRX) { p->margin = p->size - p->scope; p->ready = 0; p->empty = 0; // Not used } else { p->margin = -((SDWORD)p->scope); p->ready = 0; // Not used p->empty = p->size; } } // Clear entire buffer if (!err) memset(p->buffer, 0, p->size); if (err) p = BUFFER_Del(p); if (c->diagnostics) { // Diagnostic Report printf("------------------------------------------\n"); printf("PACKET BUFFER\n"); printf(" Creation:............... %s\n", ((err)? "FAILED":"SUCCEEDED")); printf(" Location:............... %p\n", (void *) p); printf(" Minimum buffer size:.... %lu bytes\n", (unsigned long) p->minsize); printf(" Buffer size:............ %lu bytes\n", (unsigned long) p->size); printf(" Buffer location:........ %p\n", (void *) p->buffer); printf(" Packet size in buffer:.. %lu bytes\n", (unsigned long) p->scope); printf(" read:................... %lu bytes\n", (unsigned long) p->read); printf(" write:.................. %lu bytes\n", (unsigned long) p->write); printf(" empty:.................. %lu bytes\n", (unsigned long) p->empty); printf(" ready:.................. %lu bytes\n", (unsigned long) p->ready); printf(" margin:................. %li bytes\n", (long) p->margin); printf(" overflows:.............. %lu bytes\n", (unsigned long) p->overflows); printf("------------------------------------------\n"); } *errcode = err; return p; } //------------------------------------------------------------------------