1
0
mirror of https://github.com/elua/elua.git synced 2025-01-08 20:56:17 +08:00

Modification of buf.c so that logsize^2 represents number of bytes in buffer.

Element byte counts must be a power of 2.
This commit is contained in:
James Snyder 2009-02-10 05:50:48 +00:00
parent c6fd54ce7e
commit a973e8f4e9
4 changed files with 28 additions and 17 deletions

View File

@ -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)

View File

@ -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

View File

@ -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 --;

View File

@ -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 ) )
{
@ -295,4 +294,3 @@ unsigned int intlog2( unsigned int v )
}
return r;
}