Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3911 → Rev 3912

/branches/network/uspace/srv/net/structures/packet/packet.c
1,5 → 1,5
/*
* Copyright (c) 2008 Lukas Mejdrech
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
26,11 → 26,13
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
/** @addtogroup packet
* @{
*/
 
/** @file
* Packet map and queue implementation.
* This file has to be compiled with both the packet server and the client.
*/
 
#include <errno.h>
48,26 → 50,51
#include "packet.h"
 
// TODO power of 2 aritmetic => div and mod speedup?
/** Packet map page size.
*/
#define PACKET_MAP_SIZE 100
 
/** Returns the packet map page index.
* @param packet_id The packet identifier.
*/
#define PACKET_MAP_PAGE( packet_id ) ((( packet_id ) - 1 ) / PACKET_MAP_SIZE )
 
/** Returns the packet index in the corresponding packet map page.
* @param packet_id The packet identifier.
*/
#define PACKET_MAP_INDEX( packet_id ) ((( packet_id ) - 1 ) % PACKET_MAP_SIZE )
 
 
int packet_destroy( packet_t packet );
 
/** Type definition of the packet map page.
*/
typedef packet_t packet_map_t[ PACKET_MAP_SIZE ];
/** Type definition of the packet map page pointer.
*/
typedef packet_map_t * packet_map_ref;
 
/** Packet map.
* Maps packet identifiers to the packet references.
* @see generic_field.h
*/
GENERIC_FIELD_DECLARE( gpm, packet_map_t );
 
GENERIC_FIELD_IMPLEMENT( gpm, packet_map_t );
/** Releases the packet.
* @param packet The packet to be released. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
*/
int packet_destroy( packet_t packet );
 
/** Packet map global data.
*/
static struct{
// TODO lock
gpm_t map;
/** Packet map.
*/
gpm_t packet_map;
} pm_globals;
 
GENERIC_FIELD_IMPLEMENT( gpm, packet_map_t );
 
int packet_destroy( packet_t packet ){
if( ! packet_is_valid( packet )) return EINVAL;
return munmap( packet, packet->length );
74,7 → 101,7
}
 
int pm_init( void ){
return gpm_initialize( & pm_globals.map );
return gpm_initialize( & pm_globals.packet_map );
}
 
packet_t pm_find( packet_id_t packet_id ){
81,8 → 108,8
packet_map_ref map;
 
if( ! packet_id ) return NULL;
if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.map )) return NULL;
map = gpm_get_index( & pm_globals.map, PACKET_MAP_PAGE( packet_id ));
if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )) return NULL;
map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet_id ));
if( ! map ) return NULL;
return ( * map )[ PACKET_MAP_INDEX( packet_id ) ];
}
92,19 → 119,19
 
packet_map_ref map;
 
if(( ! packet_is_valid( packet )) || ( gpm_count( & pm_globals.map ) < -1 )) return EINVAL;
if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.map )){
map = gpm_get_index( & pm_globals.map, PACKET_MAP_PAGE( packet->packet_id ));
if(( ! packet_is_valid( packet )) || ( gpm_count( & pm_globals.packet_map ) < 0 )) return EINVAL;
if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.packet_map )){
map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet->packet_id ));
}else{
do{
map = ( packet_map_ref ) malloc( sizeof( packet_map_t ));
if( ! map ) return ENOMEM;
memset( map, 0, sizeof( packet_map_t ));
if(( ERROR_CODE = gpm_add( & pm_globals.map, map )) < 0 ){
if(( ERROR_CODE = gpm_add( & pm_globals.packet_map, map )) < 0 ){
free( map );
return ERROR_CODE;
}
}while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.map ));
}while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.packet_map ));
}
( * map )[ PACKET_MAP_INDEX( packet->packet_id ) ] = packet;
return EOK;
116,9 → 143,9
packet_map_ref map;
packet_t packet;
 
count = gpm_count( & pm_globals.map );
count = gpm_count( & pm_globals.packet_map );
while( count > 0 ){
map = gpm_get_index( & pm_globals.map, count - 1 );
map = gpm_get_index( & pm_globals.packet_map, count - 1 );
for( index = PACKET_MAP_SIZE - 1; index >= 0; -- index ){
packet = ( * map )[ index ];
if( packet_is_valid( packet )){
126,7 → 153,7
}
}
}
gpm_destroy( & pm_globals.map );
gpm_destroy( & pm_globals.packet_map );
}
 
packet_t pq_add( packet_t first, packet_t packet, int order, size_t metric ){