Subversion Repositories HelenOS

Compare Revisions

No changes between revisions

Ignore whitespace Rev 4587 → Rev 4588

/branches/network/uspace/srv/net/structures/dynamic_fifo.c
0,0 → 1,118
/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
*/
 
#include <errno.h>
#include <malloc.h>
#include <mem.h>
 
#include "dynamic_fifo.h"
 
#define DYN_FIFO_MAGIC_VALUE 0x58627659
 
#define NEXT_INDEX( fifo, index ) ((( index ) + 1 ) % (( fifo )->size + 1 ))
 
int dyn_fifo_is_valid( dyn_fifo_ref fifo );
 
int dyn_fifo_is_valid( dyn_fifo_ref fifo ){
return fifo && ( fifo->magic_value == DYN_FIFO_MAGIC_VALUE );
}
 
int dyn_fifo_initialize( dyn_fifo_ref fifo, int size ){
if( ! fifo ) return EBADMEM;
if( size <= 0 ) return EINVAL;
fifo->items = ( int * ) malloc( sizeof( int ) * size + 1 );
if( ! fifo->items ) return ENOMEM;
fifo->size = size;
fifo->head = 0;
fifo->tail = 0;
fifo->magic_value = DYN_FIFO_MAGIC_VALUE;
return EOK;
}
 
int dyn_fifo_push( dyn_fifo_ref fifo, int value, int max_size ){
int * new_items;
 
if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
if( NEXT_INDEX( fifo, fifo->tail ) == fifo->head ){
if(( max_size > 0 ) && (( fifo->size * 2 ) > max_size )){
if( fifo->size >= max_size ) return ENOMEM;
}else{
max_size = fifo->size * 2;
}
new_items = realloc( fifo->items, sizeof( int ) * max_size + 1 );
if( ! new_items ) return ENOMEM;
fifo->items = new_items;
if( fifo->tail < fifo->head ){
if( fifo->tail < max_size - fifo->size ){
memcpy( fifo->items + fifo->size + 1, fifo->items, fifo->tail * sizeof( int ));
fifo->tail += fifo->size + 1;
}else{
memcpy( fifo->items + fifo->size + 1, fifo->items, ( max_size - fifo->size ) * sizeof( int ));
memcpy( fifo->items, fifo->items + max_size - fifo->size, fifo->tail - max_size + fifo->size );
fifo->tail -= max_size - fifo->size;
}
}
fifo->size = max_size;
}
fifo->items[ fifo->tail ] = value;
fifo->tail = NEXT_INDEX( fifo, fifo->tail );
return EOK;
}
 
int dyn_fifo_pop( dyn_fifo_ref fifo ){
int value;
 
if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
if( fifo->head == fifo->tail ) return ENOENT;
value = fifo->items[ fifo->head ];
fifo->head = NEXT_INDEX( fifo, fifo->head );
return value;
}
 
int dyn_fifo_value( dyn_fifo_ref fifo ){
if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
if( fifo->head == fifo->tail ) return ENOENT;
return fifo->items[ fifo->head ];
}
 
int dyn_fifo_destroy( dyn_fifo_ref fifo ){
if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
free( fifo->items );
fifo->magic_value = 0;
return EOK;
}
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/structures/dynamic_fifo.h
0,0 → 1,65
/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
*/
 
#ifndef __NET_DYNAMIC_FIFO_H__
#define __NET_DYNAMIC_FIFO_H__
 
/* For possitive values only */
 
typedef struct dyn_fifo dyn_fifo_t;
typedef dyn_fifo_t * dyn_fifo_ref;
 
struct dyn_fifo{
int * items;
int size;
int head;
int tail;
int magic_value;
};
 
int dyn_fifo_initialize( dyn_fifo_ref fifo, int size );
 
int dyn_fifo_push( dyn_fifo_ref fifo, int value, int max_size );
 
int dyn_fifo_pop( dyn_fifo_ref fifo );
 
int dyn_fifo_value( dyn_fifo_ref fifo );
 
int dyn_fifo_destroy( dyn_fifo_ref fifo );
 
#endif
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/net/start/Makefile
42,6 → 42,7
$(NET_BASE)modules.c \
$(NET_BASE)self_test.c \
$(STRUCTURES)char_map.c \
$(STRUCTURES)dynamic_fifo.c \
$(STRUCTURES)measured_strings.c \
$(NET_BASE)crc.c
 
/branches/network/uspace/srv/net/configuration.h
77,6 → 77,12
*/
#define NET_SELF_TEST_CRC 0
 
/** Activate the dynamic fifo self test.
* The NET_SELF_TEST has to be activated.
* @see dynamic_fifo.h
*/
#define NET_SELF_TEST_DYNAMIC_FIFO 0
 
#endif
 
/** @}
/branches/network/uspace/srv/net/self_test.c
47,6 → 47,7
#include "structures/char_map.h"
#include "structures/generic_char_map.h"
#include "structures/measured_strings.h"
#include "structures/dynamic_fifo.h"
 
#include "self_test.h"
 
58,10 → 59,10
#define TEST( name, function_call, result ); { \
printf( "\n\t%s", ( name )); \
if(( function_call ) != ( result )){ \
printf( "\tERROR" ); \
printf( "\tERROR\n" ); \
error = 1; \
}else{ \
printf( "\tOK" ); \
printf( "\tOK\n" ); \
} \
}
 
328,6 → 329,50
 
#endif
 
#if NET_SELF_TEST_DYNAMIC_FIFO
dyn_fifo_t fifo;
 
printf( "\nDynamic fifo test" );
TEST( "add 1 einval", dyn_fifo_push( & fifo, 1, 0 ), EINVAL );
TEST( "initialize", dyn_fifo_initialize( & fifo, 1 ), EOK );
TEST( "add 1 eok", dyn_fifo_push( & fifo, 1, 0 ), EOK );
TEST( "pop 1", dyn_fifo_pop( & fifo ), 1 );
TEST( "pop enoent", dyn_fifo_pop( & fifo ), ENOENT );
TEST( "add 2 eok", dyn_fifo_push( & fifo, 2, 1 ), EOK );
TEST( "add 3 enomem", dyn_fifo_push( & fifo, 3, 1 ), ENOMEM );
TEST( "add 3 eok", dyn_fifo_push( & fifo, 3, 0 ), EOK );
TEST( "pop 2", dyn_fifo_pop( & fifo ), 2 );
TEST( "pop 3", dyn_fifo_pop( & fifo ), 3 );
TEST( "add 4 eok", dyn_fifo_push( & fifo, 4, 2 ), EOK );
TEST( "add 5 eok", dyn_fifo_push( & fifo, 5, 2 ), EOK );
TEST( "add 6 enomem", dyn_fifo_push( & fifo, 6, 2 ), ENOMEM );
TEST( "add 6 eok", dyn_fifo_push( & fifo, 6, 5 ), EOK );
TEST( "add 7 eok", dyn_fifo_push( & fifo, 7, 5 ), EOK );
TEST( "pop 4", dyn_fifo_pop( & fifo ), 4 );
TEST( "pop 5", dyn_fifo_pop( & fifo ), 5 );
TEST( "add 8 eok", dyn_fifo_push( & fifo, 8, 5 ), EOK );
TEST( "add 9 eok", dyn_fifo_push( & fifo, 9, 5 ), EOK );
TEST( "add 10 eok", dyn_fifo_push( & fifo, 10, 6 ), EOK );
TEST( "add 11 eok", dyn_fifo_push( & fifo, 11, 6 ), EOK );
TEST( "pop 6", dyn_fifo_pop( & fifo ), 6 );
TEST( "pop 7", dyn_fifo_pop( & fifo ), 7 );
TEST( "add 12 eok", dyn_fifo_push( & fifo, 12, 6 ), EOK );
TEST( "add 13 eok", dyn_fifo_push( & fifo, 13, 6 ), EOK );
TEST( "add 14 enomem", dyn_fifo_push( & fifo, 14, 6 ), ENOMEM );
TEST( "add 14 eok", dyn_fifo_push( & fifo, 14, 8 ), EOK );
TEST( "pop 8", dyn_fifo_pop( & fifo ), 8 );
TEST( "pop 9", dyn_fifo_pop( & fifo ), 9 );
TEST( "pop 10", dyn_fifo_pop( & fifo ), 10 );
TEST( "pop 11", dyn_fifo_pop( & fifo ), 11 );
TEST( "pop 12", dyn_fifo_pop( & fifo ), 12 );
TEST( "pop 13", dyn_fifo_pop( & fifo ), 13 );
TEST( "pop 14", dyn_fifo_pop( & fifo ), 14 );
TEST( "destroy", dyn_fifo_destroy( & fifo ), EOK );
TEST( "add 15 einval", dyn_fifo_push( & fifo, 1, 0 ), EINVAL );
if( error ) return EINVAL;
 
#endif
 
return EOK;
}