From a973e8f4e92bec0bfa132c74880275e4d345c820 Mon Sep 17 00:00:00 2001 From: James Snyder Date: Tue, 10 Feb 2009 05:50:48 +0000 Subject: [PATCH] Modification of buf.c so that logsize^2 represents number of bytes in buffer. Element byte counts must be a power of 2. --- inc/buf.h | 2 +- inc/platform.h | 5 +++++ src/buf.c | 34 +++++++++++++++++++++------------- src/common.c | 4 +--- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/inc/buf.h b/inc/buf.h index 03d21a23..d381335d 100644 --- a/inc/buf.h +++ b/inc/buf.h @@ -25,7 +25,7 @@ typedef struct u8 logsize; volatile u16 wptr, rptr, count; t_buf_data *buf; - size_t dsize; + u8 logdsize; } buf_desc; // Buffer sizes (there are power of 2 to speed up modulo operations) diff --git a/inc/platform.h b/inc/platform.h index fbfbb188..2c0e948d 100644 --- a/inc/platform.h +++ b/inc/platform.h @@ -230,4 +230,9 @@ u32 platform_eth_get_elapsed_time(); void* platform_get_first_free_ram( unsigned id ); void* platform_get_last_free_ram( unsigned id ); +// ***************************************************************************** +// Misc support + +unsigned int intlog2( unsigned int v ); + #endif diff --git a/src/buf.c b/src/buf.c index a6ff3a99..76974a08 100644 --- a/src/buf.c +++ b/src/buf.c @@ -22,7 +22,11 @@ static buf_desc buf_desc_uart[ 0 ]; #endif -static buf_desc buf_desc_adc [ NUM_ADC ]; +#ifdef BUF_ENABLE_ADC + static buf_desc buf_desc_adc [ NUM_ADC ]; +#else + static buf_desc buf_desc_adc [ 0 ]; +#endif // NOTE: the order of descriptors here MUST match the order of the BUF_ID_xx // enum in inc/buf.h @@ -33,8 +37,10 @@ static const buf_desc* buf_desc_array[ BUF_ID_TOTAL ] = }; // Helper macros -#define BUF_MOD_INCR( p, m ) p->m = ( p->m + p->dsize ) & ( ( ( u16 )1 << ( p->logsize + ( p->dsize - 1 ) ) ) - 1 ) -#define BUF_REALSIZE( p ) ( ( u16 )1 << p->logsize ) +#define BUF_MOD_INCR( p, m ) p->m = ( p->m + ( ( u16 )1 << p->logdsize ) ) & ( ( ( u16 )1 << ( p->logsize + p->logdsize) ) - 1 ) +#define BUF_REALSIZE( p ) ( ( u16 )1 << ( p->logsize - p->logdsize ) ) +#define BUF_BYTESIZE( p ) ( ( u16 )1 << p->logsize ) +#define BUF_REALDSIZE( p ) ( ( u16 )1 << p->logdsize ) #define BUF_GETPTR( resid, resnum ) buf_desc *pbuf = ( buf_desc* )buf_desc_array[ resid ] + resnum // READ16 and WRITE16 macros are here to ensure _atomic_ reads and writes of @@ -42,23 +48,25 @@ static const buf_desc* buf_desc_array[ BUF_ID_TOTAL ] = #define READ16( p ) p #define WRITE16( p, x ) p = x - - // Initialize the buffer of the specified resource // resid - resource ID (BUF_ID_UART ...) // resnum - resource number (0, 1, 2...) // bufsize - new size of the buffer (one of the BUF_SIZE_xxx constants from -// dsize - number of bytes held by each element +// dsize - number of bytes held by each element (must be a power of 2) // buf.h, or BUF_SIZE_NONE to disable buffering // Returns 1 on success, 0 on failure int buf_set( unsigned resid, unsigned resnum, u8 logsize, size_t dsize ) { BUF_GETPTR( resid, resnum ); - pbuf->logsize = logsize; - pbuf->dsize = dsize; + // Make sure dsize is a power of 2 + if ( ( dsize = 0 ) || ( dsize & ( dsize - 1 ) ) ) + return PLATFORM_ERR; - if( ( pbuf->buf = ( t_buf_data* )realloc( pbuf->buf, BUF_REALSIZE( pbuf ) * pbuf->dsize ) ) == NULL ) + pbuf->logsize = logsize; + pbuf->logdsize = intlog2( dsize ); + + if( ( pbuf->buf = ( t_buf_data* )realloc( pbuf->buf, BUF_BYTESIZE( pbuf ) ) ) == NULL ) { pbuf->logsize = BUF_SIZE_NONE; pbuf->rptr = pbuf->wptr = pbuf->count = 0; @@ -80,10 +88,10 @@ int buf_write( unsigned resid, unsigned resnum, t_buf_data *data, size_t dsize ) BUF_GETPTR( resid, resnum ); // Make sure we only add more of same type - if (pbuf->dsize != dsize) + if ( dsize != BUF_REALDSIZE( pbuf ) ) return PLATFORM_ERR; - memcpy(&pbuf->buf[ pbuf->wptr ], data, dsize); + memcpy( &pbuf->buf[ pbuf->wptr ], data, dsize ); BUF_MOD_INCR( pbuf, wptr ); @@ -133,13 +141,13 @@ int buf_read( unsigned resid, unsigned resnum, t_buf_data *data, size_t dsize ) BUF_GETPTR( resid, resnum ); // Make sure buffer contains right type - if (pbuf->dsize != dsize) + if ( dsize != BUF_REALDSIZE( pbuf ) ) return PLATFORM_ERR; if( READ16( pbuf->count ) == 0 ) return PLATFORM_UNDERFLOW; - memcpy(data, &pbuf->buf[ pbuf->rptr ], dsize); + memcpy( data, &pbuf->buf[ pbuf->rptr ], dsize ); platform_cpu_disable_interrupts(); pbuf->count --; diff --git a/src/common.c b/src/common.c index 79f93ff7..6dfc898f 100644 --- a/src/common.c +++ b/src/common.c @@ -71,7 +71,6 @@ static int cmn_recv_helper( unsigned id, s32 timeout ) { #ifdef BUF_ENABLE_UART t_buf_data data; - int res; if( buf_is_enabled( BUF_ID_UART, id ) ) { @@ -294,5 +293,4 @@ unsigned int intlog2( unsigned int v ) r++; } return r; -} - +} \ No newline at end of file