Changeset aadf01e in mainline for uspace/srv/net/structures/packet


Ignore:
Timestamp:
2010-03-07T15:13:28Z (16 years ago)
Author:
Lukas Mejdrech <lukasmejdrech@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
936835e
Parents:
aa85487
Message:

Coding style (no functional change)

Location:
uspace/srv/net/structures/packet
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/structures/packet/packet.c

    raa85487 raadf01e  
    5959 *  @param[in] packet_id The packet identifier.
    6060 */
    61 #define PACKET_MAP_PAGE( packet_id )    ((( packet_id ) - 1 ) / PACKET_MAP_SIZE )
     61#define PACKET_MAP_PAGE(packet_id)      (((packet_id) - 1) / PACKET_MAP_SIZE)
    6262
    6363/** Returns the packet index in the corresponding packet map page.
    6464 *  @param[in] packet_id The packet identifier.
    6565 */
    66 #define PACKET_MAP_INDEX( packet_id )   ((( packet_id ) - 1 ) % PACKET_MAP_SIZE )
     66#define PACKET_MAP_INDEX(packet_id)     (((packet_id) - 1) % PACKET_MAP_SIZE)
    6767
    6868/** Type definition of the packet map page.
    6969 */
    70 typedef packet_t packet_map_t[ PACKET_MAP_SIZE ];
     70typedef packet_t packet_map_t[PACKET_MAP_SIZE];
    7171/** Type definition of the packet map page pointer.
    7272 */
     
    7777 *  @see generic_field.h
    7878 */
    79 GENERIC_FIELD_DECLARE( gpm, packet_map_t );
     79GENERIC_FIELD_DECLARE(gpm, packet_map_t);
    8080
    8181/** Releases the packet.
     
    8484 *  @returns EINVAL if the packet is not valid.
    8585 */
    86 int packet_destroy( packet_t packet );
     86int packet_destroy(packet_t packet);
    8787
    8888/** Packet map global data.
     
    9191        /** Safety lock.
    9292         */
    93         fibril_rwlock_t lock;
     93        fibril_rwlock_t lock;
    9494        /** Packet map.
    9595         */
    96         gpm_t   packet_map;
     96        gpm_t packet_map;
    9797} pm_globals;
    9898
    99 GENERIC_FIELD_IMPLEMENT( gpm, packet_map_t );
    100 
    101 int packet_destroy( packet_t packet ){
    102         if( ! packet_is_valid( packet )) return EINVAL;
    103         return munmap( packet, packet->length );
    104 }
    105 
    106 int pm_init( void ){
     99GENERIC_FIELD_IMPLEMENT(gpm, packet_map_t);
     100
     101int packet_destroy(packet_t packet){
     102        if(! packet_is_valid(packet)){
     103                return EINVAL;
     104        }
     105        return munmap(packet, packet->length);
     106}
     107
     108int pm_init(void){
    107109        ERROR_DECLARE;
    108110
    109         fibril_rwlock_initialize( & pm_globals.lock );
    110         fibril_rwlock_write_lock( & pm_globals.lock );
    111         ERROR_PROPAGATE( gpm_initialize( & pm_globals.packet_map ));
    112         fibril_rwlock_write_unlock( & pm_globals.lock );
    113         return EOK;
    114 }
    115 
    116 packet_t pm_find( packet_id_t packet_id ){
     111        fibril_rwlock_initialize(&pm_globals.lock);
     112        fibril_rwlock_write_lock(&pm_globals.lock);
     113        ERROR_PROPAGATE(gpm_initialize(&pm_globals.packet_map));
     114        fibril_rwlock_write_unlock(&pm_globals.lock);
     115        return EOK;
     116}
     117
     118packet_t pm_find(packet_id_t packet_id){
    117119        packet_map_ref map;
    118120        packet_t packet;
    119121
    120         if( ! packet_id ) return NULL;
    121         fibril_rwlock_read_lock( & pm_globals.lock );
    122         if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )){
    123                 fibril_rwlock_read_unlock( & pm_globals.lock );
    124                 return NULL;
    125         }
    126         map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet_id ));
    127         if( ! map ){
    128                 fibril_rwlock_read_unlock( & pm_globals.lock );
    129                 return NULL;
    130         }
    131         packet = ( * map )[ PACKET_MAP_INDEX( packet_id ) ];
    132         fibril_rwlock_read_unlock( & pm_globals.lock );
     122        if(! packet_id){
     123                return NULL;
     124        }
     125        fibril_rwlock_read_lock(&pm_globals.lock);
     126        if(packet_id > PACKET_MAP_SIZE * gpm_count(&pm_globals.packet_map)){
     127                fibril_rwlock_read_unlock(&pm_globals.lock);
     128                return NULL;
     129        }
     130        map = gpm_get_index(&pm_globals.packet_map, PACKET_MAP_PAGE(packet_id));
     131        if(! map){
     132                fibril_rwlock_read_unlock(&pm_globals.lock);
     133                return NULL;
     134        }
     135        packet = (*map)[PACKET_MAP_INDEX(packet_id)];
     136        fibril_rwlock_read_unlock(&pm_globals.lock);
    133137        return packet;
    134138}
    135139
    136 int pm_add( packet_t packet ){
     140int pm_add(packet_t packet){
    137141        ERROR_DECLARE;
    138142
    139143        packet_map_ref map;
    140144
    141         if( ! packet_is_valid( packet )) return EINVAL;
    142         fibril_rwlock_write_lock( & pm_globals.lock );
    143         if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.packet_map )){
    144                 map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet->packet_id ));
     145        if(! packet_is_valid(packet)){
     146                return EINVAL;
     147        }
     148        fibril_rwlock_write_lock(&pm_globals.lock);
     149        if(PACKET_MAP_PAGE(packet->packet_id) < gpm_count(&pm_globals.packet_map)){
     150                map = gpm_get_index(&pm_globals.packet_map, PACKET_MAP_PAGE(packet->packet_id));
    145151        }else{
    146152                do{
    147                         map = ( packet_map_ref ) malloc( sizeof( packet_map_t ));
    148                         if( ! map ){
    149                                 fibril_rwlock_write_unlock( & pm_globals.lock );
     153                        map = (packet_map_ref) malloc(sizeof(packet_map_t));
     154                        if(! map){
     155                                fibril_rwlock_write_unlock(&pm_globals.lock);
    150156                                return ENOMEM;
    151157                        }
    152                         bzero( map, sizeof( packet_map_t ));
    153                         if(( ERROR_CODE = gpm_add( & pm_globals.packet_map, map )) < 0 ){
    154                                 fibril_rwlock_write_unlock( & pm_globals.lock );
    155                                 free( map );
     158                        bzero(map, sizeof(packet_map_t));
     159                        if((ERROR_CODE = gpm_add(&pm_globals.packet_map, map)) < 0){
     160                                fibril_rwlock_write_unlock(&pm_globals.lock);
     161                                free(map);
    156162                                return ERROR_CODE;
    157163                        }
    158                 }while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.packet_map ));
    159         }
    160         ( * map )[ PACKET_MAP_INDEX( packet->packet_id ) ] = packet;
    161         fibril_rwlock_write_unlock( & pm_globals.lock );
    162         return EOK;
    163 }
    164 
    165 void pm_destroy( void ){
     164                }while(PACKET_MAP_PAGE(packet->packet_id) >= gpm_count(&pm_globals.packet_map));
     165        }
     166        (*map)[PACKET_MAP_INDEX(packet->packet_id)] = packet;
     167        fibril_rwlock_write_unlock(&pm_globals.lock);
     168        return EOK;
     169}
     170
     171void pm_destroy(void){
    166172        int count;
    167173        int index;
     
    169175        packet_t packet;
    170176
    171         fibril_rwlock_write_lock( & pm_globals.lock );
    172         count = gpm_count( & pm_globals.packet_map );
    173         while( count > 0 ){
    174                 map = gpm_get_index( & pm_globals.packet_map, count - 1 );
    175                 for( index = PACKET_MAP_SIZE - 1; index >= 0; -- index ){
    176                         packet = ( * map )[ index ];
    177                         if( packet_is_valid( packet )){
    178                                 munmap( packet, packet->length );
     177        fibril_rwlock_write_lock(&pm_globals.lock);
     178        count = gpm_count(&pm_globals.packet_map);
     179        while(count > 0){
     180                map = gpm_get_index(&pm_globals.packet_map, count - 1);
     181                for(index = PACKET_MAP_SIZE - 1; index >= 0; -- index){
     182                        packet = (*map)[index];
     183                        if(packet_is_valid(packet)){
     184                                munmap(packet, packet->length);
    179185                        }
    180186                }
    181187        }
    182         gpm_destroy( & pm_globals.packet_map );
     188        gpm_destroy(&pm_globals.packet_map);
    183189        // leave locked
    184190}
    185191
    186 int pq_add( packet_t * first, packet_t packet, size_t order, size_t metric ){
    187         packet_t        item;
    188 
    189         if(( ! first ) || ( ! packet_is_valid( packet ))) return EINVAL;
    190         pq_set_order( packet, order, metric );
    191         if( packet_is_valid( * first )){
     192int pq_add(packet_t * first, packet_t packet, size_t order, size_t metric){
     193        packet_t item;
     194
     195        if((! first) || (! packet_is_valid(packet))){
     196                return EINVAL;
     197        }
     198        pq_set_order(packet, order, metric);
     199        if(packet_is_valid(*first)){
    192200                item = * first;
    193201                do{
    194                         if( item->order < order ){
    195                                 if( item->next ){
    196                                         item = pm_find( item->next );
     202                        if(item->order < order){
     203                                if(item->next){
     204                                        item = pm_find(item->next);
    197205                                }else{
    198206                                        item->next = packet->packet_id;
     
    204212                                packet->next = item->packet_id;
    205213                                item->previous = packet->packet_id;
    206                                 item = pm_find( packet->previous );
    207                                 if( item ){
     214                                item = pm_find(packet->previous);
     215                                if(item){
    208216                                        item->next = packet->packet_id;
    209217                                }else{
    210                                         * first = packet;
     218                                        *first = packet;
    211219                                }
    212220                                return EOK;
    213221                        }
    214                 }while( packet_is_valid( item ));
    215         }
    216         * first = packet;
    217         return EOK;
    218 }
    219 
    220 packet_t pq_find( packet_t packet, size_t order ){
    221         packet_t        item;
    222 
    223         if( ! packet_is_valid( packet )) return NULL;
    224         if( packet->order == order ) return packet;
    225         item = pm_find( packet->next );
    226         while( item && ( item != packet )){
    227                 item = pm_find( item->next );
    228                 if( item->order == order ){
     222                }while(packet_is_valid(item));
     223        }
     224        *first = packet;
     225        return EOK;
     226}
     227
     228packet_t pq_find(packet_t packet, size_t order){
     229        packet_t item;
     230
     231        if(! packet_is_valid(packet)){
     232                return NULL;
     233        }
     234        if(packet->order == order){
     235                return packet;
     236        }
     237        item = pm_find(packet->next);
     238        while(item && (item != packet)){
     239                item = pm_find(item->next);
     240                if(item->order == order){
    229241                        return item;
    230242                }
     
    233245}
    234246
    235 int     pq_insert_after( packet_t packet, packet_t new_packet ){
    236         packet_t        item;
    237 
    238         if( !( packet_is_valid( packet ) && packet_is_valid( new_packet ))) return EINVAL;
     247int pq_insert_after(packet_t packet, packet_t new_packet){
     248        packet_t item;
     249
     250        if(!(packet_is_valid(packet) && packet_is_valid(new_packet))){
     251                return EINVAL;
     252        }
    239253        new_packet->previous = packet->packet_id;
    240254        new_packet->next = packet->next;
    241         item = pm_find( packet->next );
    242         if( item ) item->previous = new_packet->packet_id;
     255        item = pm_find(packet->next);
     256        if(item){
     257                item->previous = new_packet->packet_id;
     258        }
    243259        packet->next = new_packet->packet_id;
    244260        return EOK;
    245261}
    246262
    247 packet_t pq_detach( packet_t packet ){
     263packet_t pq_detach(packet_t packet){
    248264        packet_t next;
    249265        packet_t previous;
    250266
    251         if( ! packet_is_valid( packet )) return NULL;
    252         next = pm_find( packet->next );
    253         if( next ){
     267        if(! packet_is_valid(packet)){
     268                return NULL;
     269        }
     270        next = pm_find(packet->next);
     271        if(next){
    254272                next->previous = packet->previous;
    255                 previous = pm_find( next->previous );
    256                 if( previous ){
     273                previous = pm_find(next->previous);
     274                if(previous){
    257275                        previous->next = next->packet_id;
    258276                }
     
    263281}
    264282
    265 int pq_set_order( packet_t packet, size_t order, size_t metric ){
    266         if( ! packet_is_valid( packet )) return EINVAL;
     283int pq_set_order(packet_t packet, size_t order, size_t metric){
     284        if(! packet_is_valid(packet)){
     285                return EINVAL;
     286        }
    267287        packet->order = order;
    268288        packet->metric = metric;
     
    270290}
    271291
    272 int pq_get_order( packet_t packet, size_t * order, size_t * metric ){
    273         if( ! packet_is_valid( packet )) return EINVAL;
    274         if( order ) * order = packet->order;
    275         if( metric ) * metric = packet->metric;
    276         return EOK;
    277 }
    278 
    279 void pq_destroy( packet_t first, void ( * packet_release )( packet_t packet )){
    280         packet_t        actual;
    281         packet_t        next;
     292int pq_get_order(packet_t packet, size_t * order, size_t * metric){
     293        if(! packet_is_valid(packet)){
     294                return EINVAL;
     295        }
     296        if(order){
     297                *order = packet->order;
     298        }
     299        if(metric){
     300                *metric = packet->metric;
     301        }
     302        return EOK;
     303}
     304
     305void pq_destroy(packet_t first, void (*packet_release)(packet_t packet)){
     306        packet_t actual;
     307        packet_t next;
    282308
    283309        actual = first;
    284         while( packet_is_valid( actual )){
    285                 next = pm_find( actual->next );
     310        while(packet_is_valid(actual)){
     311                next = pm_find(actual->next);
    286312                actual->next = 0;
    287313                actual->previous = 0;
    288                 if( packet_release ) packet_release( actual );
     314                if(packet_release){
     315                        packet_release(actual);
     316                }
    289317                actual = next;
    290318        }
    291319}
    292320
    293 packet_t pq_next( packet_t packet ){
    294         if( ! packet_is_valid( packet )) return NULL;
    295         return pm_find( packet->next );
    296 }
    297 
    298 packet_t pq_previous( packet_t packet ){
    299         if( ! packet_is_valid( packet )) return NULL;
    300         return pm_find( packet->previous );
     321packet_t pq_next(packet_t packet){
     322        if(! packet_is_valid(packet)){
     323                return NULL;
     324        }
     325        return pm_find(packet->next);
     326}
     327
     328packet_t pq_previous(packet_t packet){
     329        if(! packet_is_valid(packet)){
     330                return NULL;
     331        }
     332        return pm_find(packet->previous);
    301333}
    302334
  • uspace/srv/net/structures/packet/packet.h

    raa85487 raadf01e  
    6868        /** Reserved packet prefix length.
    6969         */
    70         size_t                  prefix;
     70        size_t prefix;
    7171        /** Maximal packet content length.
    7272         */
    73         size_t                  content;
     73        size_t content;
    7474        /** Reserved packet suffix length.
    7575         */
    76         size_t                  suffix;
     76        size_t suffix;
    7777        /** Maximal packet address length.
    7878         */
    79         size_t                  addr_len;
     79        size_t addr_len;
    8080};
    8181
     
    8989 *  @returns NULL if the mapping does not exist.
    9090 */
    91 packet_t        pm_find( packet_id_t packet_id );
     91packet_t pm_find(packet_id_t packet_id);
    9292
    9393/** Adds the packet mapping.
     
    9898 *  @returns ENOMEM if there is not enough memory left.
    9999 */
    100 int     pm_add( packet_t packet );
     100int pm_add(packet_t packet);
    101101
    102102/** Initializes the packet map.
     
    104104 *  @returns ENOMEM if there is not enough memory left.
    105105 */
    106 int     pm_init( void );
     106int pm_init(void);
    107107
    108108/** Releases the packet map.
    109109 */
    110 void    pm_destroy( void );
     110void pm_destroy(void);
    111111
    112112/** Add packet to the sorted queue.
     
    121121 *  @returns EINVAL if the packet is not valid.
    122122 */
    123 int     pq_add( packet_t * first, packet_t packet, size_t order, size_t metric );
     123int pq_add(packet_t * first, packet_t packet, size_t order, size_t metric);
    124124
    125125/** Finds the packet with the given order.
     
    130130 *  @returns NULL if the packet is not found.
    131131 */
    132 packet_t        pq_find( packet_t first, size_t order );
     132packet_t pq_find(packet_t first, size_t order);
    133133
    134134/** Inserts packet after the given one.
     
    138138 *  @returns EINVAL if etiher of the packets is invalid.
    139139 */
    140 int     pq_insert_after( packet_t packet, packet_t new_packet );
     140int pq_insert_after(packet_t packet, packet_t new_packet);
    141141
    142142/** Detach the packet from the queue.
     
    146146 *  @returns NULL if the packet is not valid.
    147147 */
    148 packet_t        pq_detach( packet_t packet );
     148packet_t pq_detach(packet_t packet);
    149149
    150150/** Sets the packet order and metric attributes.
     
    155155 *  @returns EINVAL if the packet is invalid..
    156156 */
    157 int     pq_set_order( packet_t packet, size_t order, size_t metric );
     157int pq_set_order(packet_t packet, size_t order, size_t metric);
    158158
    159159/** Sets the packet order and metric attributes.
     
    164164 *  @returns EINVAL if the packet is invalid..
    165165 */
    166 int     pq_get_order( packet_t packet, size_t * order, size_t * metric );
     166int pq_get_order(packet_t packet, size_t * order, size_t * metric);
    167167
    168168/** Releases the whole queue.
     
    171171 *  @param[in] packet_release The releasing function called for each of the packets after its detachment.
    172172 */
    173 void    pq_destroy( packet_t first, void ( * packet_release )( packet_t packet ));
     173void pq_destroy(packet_t first, void (*packet_release)(packet_t packet));
    174174
    175175/** Returns the next packet in the queue.
     
    179179 *  @returns NULL if the packet is not valid.
    180180 */
    181 packet_t        pq_next( packet_t packet );
     181packet_t pq_next(packet_t packet);
    182182
    183183/** Returns the previous packet in the queue.
     
    187187 *  @returns NULL if the packet is not valid.
    188188 */
    189 packet_t        pq_previous( packet_t packet );
     189packet_t pq_previous(packet_t packet);
    190190
    191191/*@}*/
  • uspace/srv/net/structures/packet/packet_client.c

    raa85487 raadf01e  
    4848#include "packet_client.h"
    4949
    50 int packet_copy_data( packet_t packet, const void * data, size_t length ){
    51         if( ! packet_is_valid( packet )) return EINVAL;
    52         if( packet->data_start + length >= packet->length ) return ENOMEM;
    53         memcpy(( void * ) packet + packet->data_start, data, length );
    54         if( packet->data_start + length > packet->data_end ){
     50int packet_copy_data(packet_t packet, const void * data, size_t length){
     51        if(! packet_is_valid(packet)){
     52                return EINVAL;
     53        }
     54        if(packet->data_start + length >= packet->length){
     55                return ENOMEM;
     56        }
     57        memcpy((void *) packet + packet->data_start, data, length);
     58        if(packet->data_start + length > packet->data_end){
    5559                packet->data_end = packet->data_start + length;
    5660        }
     
    5862}
    5963
    60 void * packet_prefix( packet_t packet, size_t length ){
    61         if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * ( packet->dest_addr - packet->src_addr ) < length )) return NULL;
     64void * packet_prefix(packet_t packet, size_t length){
     65        if((! packet_is_valid(packet)) || (packet->data_start - sizeof(struct packet) - 2 * (packet->dest_addr - packet->src_addr) < length)){
     66                return NULL;
     67        }
    6268        packet->data_start -= length;
    63         return ( void * ) packet + packet->data_start;
     69        return (void *) packet + packet->data_start;
    6470}
    6571
    66 void * packet_suffix( packet_t packet, size_t length ){
    67         if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL;
     72void * packet_suffix(packet_t packet, size_t length){
     73        if((! packet_is_valid(packet)) || (packet->data_end + length >= packet->length)){
     74                return NULL;
     75        }
    6876        packet->data_end += length;
    69         return ( void * ) packet + packet->data_end - length;
     77        return (void *) packet + packet->data_end - length;
    7078}
    7179
    72 int packet_trim( packet_t packet, size_t prefix, size_t suffix ){
    73         if( ! packet_is_valid( packet )) return EINVAL;
    74         if( prefix + suffix > PACKET_DATA_LENGTH( packet )) return ENOMEM;
     80int packet_trim(packet_t packet, size_t prefix, size_t suffix){
     81        if(! packet_is_valid(packet)){
     82                return EINVAL;
     83        }
     84        if(prefix + suffix > PACKET_DATA_LENGTH(packet)){
     85                return ENOMEM;
     86        }
    7587        packet->data_start += prefix;
    7688        packet->data_end -= suffix;
     
    7890}
    7991
    80 packet_id_t packet_get_id( const packet_t packet ){
    81         return packet_is_valid( packet ) ? packet->packet_id : 0;
     92packet_id_t packet_get_id(const packet_t packet){
     93        return packet_is_valid(packet) ? packet->packet_id : 0;
    8294}
    8395
    84 int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){
    85         if( ! packet_is_valid( packet )) return EINVAL;
    86         if( ! packet->addr_len ) return 0;
    87         if( src ) * src = ( void * ) packet + packet->src_addr;
    88         if( dest ) * dest = ( void * ) packet + packet->dest_addr;
     96int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest){
     97        if(! packet_is_valid(packet)){
     98                return EINVAL;
     99        }
     100        if(! packet->addr_len){
     101                return 0;
     102        }
     103        if(src){
     104                *src = (void *) packet + packet->src_addr;
     105        }
     106        if(dest){
     107                *dest = (void *) packet + packet->dest_addr;
     108        }
    89109        return packet->addr_len;
    90110}
    91111
    92 size_t packet_get_data_length( const packet_t packet ){
    93         if( ! packet_is_valid( packet )) return 0;
    94         return PACKET_DATA_LENGTH( packet );
     112size_t packet_get_data_length(const packet_t packet){
     113        if(! packet_is_valid(packet)){
     114                return 0;
     115        }
     116        return PACKET_DATA_LENGTH(packet);
    95117}
    96118
    97 void * packet_get_data( const packet_t packet ){
    98         if( ! packet_is_valid( packet )) return NULL;
    99         return ( void * ) packet + packet->data_start;
     119void * packet_get_data(const packet_t packet){
     120        if(! packet_is_valid(packet)){
     121                return NULL;
     122        }
     123        return (void *) packet + packet->data_start;
    100124}
    101125
    102 int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ){
    103         size_t  padding;
    104         size_t  allocated;
     126int packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len){
     127        size_t padding;
     128        size_t allocated;
    105129
    106         if( ! packet_is_valid( packet )) return EINVAL;
    107         allocated = PACKET_MAX_ADDRESS_LENGTH( packet );
    108         if( allocated < addr_len ) return ENOMEM;
     130        if(! packet_is_valid(packet)){
     131                return EINVAL;
     132        }
     133        allocated = PACKET_MAX_ADDRESS_LENGTH(packet);
     134        if(allocated < addr_len){
     135                return ENOMEM;
     136        }
    109137        padding = allocated - addr_len;
    110138        packet->addr_len = addr_len;
    111         if( src ){
    112                 memcpy(( void * ) packet + packet->src_addr, src, addr_len );
    113                 if( padding ) bzero(( void * ) packet + packet->src_addr + addr_len, padding );
     139        if(src){
     140                memcpy((void *) packet + packet->src_addr, src, addr_len);
     141                if(padding){
     142                        bzero((void *) packet + packet->src_addr + addr_len, padding);
     143                }
    114144        }else{
    115                 bzero(( void * ) packet + packet->src_addr, allocated );
     145                bzero((void *) packet + packet->src_addr, allocated);
    116146        }
    117         if( dest ){
    118                 memcpy(( void * ) packet + packet->dest_addr, dest, addr_len );
    119                 if( padding ) bzero(( void * ) packet + packet->dest_addr + addr_len, padding );
     147        if(dest){
     148                memcpy((void *) packet + packet->dest_addr, dest, addr_len);
     149                if(padding){
     150                        bzero((void *) packet + packet->dest_addr + addr_len, padding);
     151                }
    120152        }else{
    121                 bzero(( void * ) packet + packet->dest_addr, allocated );
     153                bzero((void *) packet + packet->dest_addr, allocated);
    122154        }
    123155        return EOK;
    124156}
    125157
    126 packet_t packet_get_copy( int phone, packet_t packet ){
    127         packet_t        copy;
    128         uint8_t *       src;
    129         uint8_t *       dest;
    130         size_t          addrlen;
     158packet_t packet_get_copy(int phone, packet_t packet){
     159        packet_t copy;
     160        uint8_t * src;
     161        uint8_t * dest;
     162        size_t addrlen;
    131163
    132         if( ! packet_is_valid( packet )) return NULL;
     164        if(! packet_is_valid(packet)){
     165                return NULL;
     166        }
    133167        // get a new packet
    134         copy = packet_get_4( phone, PACKET_DATA_LENGTH( packet ), PACKET_MAX_ADDRESS_LENGTH( packet ), packet->max_prefix, PACKET_MIN_SUFFIX( packet ));
    135         if( ! copy ) return NULL;
     168        copy = packet_get_4(phone, PACKET_DATA_LENGTH(packet), PACKET_MAX_ADDRESS_LENGTH(packet), packet->max_prefix, PACKET_MIN_SUFFIX(packet));
     169        if(! copy){
     170                return NULL;
     171        }
    136172        // get addresses
    137         addrlen = packet_get_addr( packet, & src, & dest );
     173        addrlen = packet_get_addr(packet, &src, &dest);
    138174        // copy data
    139         if(( packet_copy_data( copy, packet_get_data( packet ), PACKET_DATA_LENGTH( packet )) == EOK )
     175        if((packet_copy_data(copy, packet_get_data(packet), PACKET_DATA_LENGTH(packet)) == EOK)
    140176        // copy addresses if present
    141         && (( addrlen <= 0 ) || ( packet_set_addr( copy, src, dest, addrlen ) == EOK ))){
     177                && ((addrlen <= 0) || (packet_set_addr(copy, src, dest, addrlen) == EOK))){
    142178                copy->order = packet->order;
    143179                copy->metric = packet->metric;
    144180                return copy;
    145181        }else{
    146                 pq_release( phone, copy->packet_id );
     182                pq_release(phone, copy->packet_id);
    147183                return NULL;
    148184        }
  • uspace/srv/net/structures/packet/packet_client.h

    raa85487 raadf01e  
    5959 *  @returns NULL if there is not enough memory left.
    6060 */
    61 #define PACKET_PREFIX( packet, type )   ( type * ) packet_prefix(( packet ), sizeof( type ))
     61#define PACKET_PREFIX(packet, type)     (type *) packet_prefix((packet), sizeof(type))
    6262
    6363/** Allocates the specified type right after the actual packet content and returns its pointer.
     
    6969 *  @returns NULL if there is not enough memory left.
    7070 */
    71 #define PACKET_SUFFIX( packet, type )   ( type * ) packet_suffix(( packet ), sizeof( type ))
     71#define PACKET_SUFFIX(packet, type)     (type *) packet_suffix((packet), sizeof(type))
    7272
    7373/** Trims the actual packet content by the specified prefix and suffix types.
     
    8080 *  @returns ENOMEM if there is not enough memory left.
    8181 */
    82 #define PACKET_TRIM( packet, prefix, suffix )   packet_trim(( packet ), sizeof( prefix ), sizeof( suffix ))
     82#define PACKET_TRIM(packet, prefix, suffix)     packet_trim((packet), sizeof(prefix), sizeof(suffix))
    8383
    8484/** Allocates the specified space right before the actual packet content and returns its pointer.
     
    8888 *  @returns NULL if there is not enough memory left.
    8989 */
    90 void *  packet_prefix( packet_t packet, size_t length );
     90void * packet_prefix(packet_t packet, size_t length);
    9191
    9292/** Allocates the specified space right after the actual packet content and returns its pointer.
     
    9696 *  @returns NULL if there is not enough memory left.
    9797 */
    98 void *  packet_suffix( packet_t packet, size_t length );
     98void * packet_suffix(packet_t packet, size_t length);
    9999
    100100/** Trims the actual packet content by the specified prefix and suffix lengths.
     
    106106 *  @returns ENOMEM if there is not enough memory left.
    107107 */
    108 int     packet_trim( packet_t packet, size_t prefix, size_t suffix );
     108int packet_trim(packet_t packet, size_t prefix, size_t suffix);
    109109
    110110/** Copies the specified data to the beginning of the actual packet content.
     
    117117 *  @returns ENOMEM if there is not enough memory left.
    118118 */
    119 int     packet_copy_data( packet_t packet, const void * data, size_t length );
     119int packet_copy_data(packet_t packet, const void * data, size_t length);
    120120
    121121/** Returns the packet identifier.
     
    124124 *  @returns Zero (0) if the packet is not valid.
    125125 */
    126 packet_id_t packet_get_id( const packet_t packet );
     126packet_id_t packet_get_id(const packet_t packet);
    127127
    128128/** Returns the packet content length.
     
    131131 *  @returns Zero (0) if the packet is not valid.
    132132 */
    133 size_t  packet_get_data_length( const packet_t packet );
     133size_t packet_get_data_length(const packet_t packet);
    134134
    135135/** Returns the pointer to the beginning of the packet content.
     
    138138 *  @returns NULL if the packet is not valid.
    139139 */
    140 void *  packet_get_data( const packet_t packet );
     140void * packet_get_data(const packet_t packet);
    141141
    142142/** Returns the stored packet addresses and their length.
     
    148148 *  @returns EINVAL if the packet is not valid.
    149149 */
    150 int     packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest );
     150int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest);
    151151
    152152/** Sets the packet addresses.
     
    159159 *  @returns ENOMEM if there is not enough memory left.
    160160 */
    161 int     packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len );
     161int packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len);
    162162
    163163/** Translates the packet identifier to the packet reference.
     
    172172 *  @returns Other error codes as defined for the packet_return() function.
    173173 */
    174 int packet_translate( int phone, packet_ref packet, packet_id_t packet_id );
     174int packet_translate(int phone, packet_ref packet, packet_id_t packet_id);
    175175
    176176/** Obtains the packet of the given dimensions.
     
    184184 *  @returns NULL on error.
    185185 */
    186 packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix );
     186packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix);
    187187
    188188/** Obtains the packet of the given content size.
     
    193193 *  @returns NULL on error.
    194194 */
    195 packet_t packet_get_1( int phone, size_t content );
     195packet_t packet_get_1(int phone, size_t content);
    196196
    197197/** Releases the packet queue.
     
    202202 *  @param[in] packet_id The packet identifier.
    203203 */
    204 void pq_release( int phone, packet_id_t packet_id );
     204void pq_release(int phone, packet_id_t packet_id);
    205205
    206206/** Returns the packet copy.
     
    212212 *  @returns NULL on error.
    213213 */
    214 packet_t        packet_get_copy( int phone, packet_t packet );
     214packet_t packet_get_copy(int phone, packet_t packet);
    215215
    216216/*@}*/
  • uspace/srv/net/structures/packet/packet_header.h

    raa85487 raadf01e  
    4343 *  @param[in] header The packet header.
    4444 */
    45 #define PACKET_DATA_LENGTH( header )            (( header )->data_end - ( header )->data_start )
     45#define PACKET_DATA_LENGTH(header)              ((header)->data_end - (header)->data_start)
    4646
    4747/** Returns the maximum packet address length.
    4848 *  @param[in] header The packet header.
    4949 */
    50 #define PACKET_MAX_ADDRESS_LENGTH( header )             (( header )->dest_addr - ( header )->src_addr )
     50#define PACKET_MAX_ADDRESS_LENGTH(header)               ((header)->dest_addr - (header)->src_addr)
    5151
    5252/** Returns the minimum packet suffix.
    5353 *  @param[in] header The packet header.
    5454 */
    55 #define PACKET_MIN_SUFFIX( header )             (( header )->length - ( header )->data_start - ( header )->max_content )
     55#define PACKET_MIN_SUFFIX(header)               ((header)->length - (header)->data_start - (header)->max_content)
    5656
    5757/** Packet integrity check magic value.
     
    6464        /** Packet identifier.
    6565         */
    66         packet_id_t             packet_id;
     66        packet_id_t packet_id;
    6767        /** Packet queue sorting value.
    6868         *  The packet queue is sorted the ascending order.
    6969         */
    70         size_t                  order;
     70        size_t order;
    7171        /** Packet metric.
    7272         */
    73         size_t                  metric;
     73        size_t metric;
    7474        /** Previous packet in the queue.
    7575         */
    76         packet_id_t             previous;
     76        packet_id_t previous;
    7777        /** Next packet in the queue.
    7878         */
    79         packet_id_t             next;
     79        packet_id_t next;
    8080        /** Total length of the packet.
    8181         *  Contains the header, the addresses and the data of the packet.
    8282         *  Corresponds to the mapped sharable memory block.
    8383         */
    84         size_t                  length;
     84        size_t length;
    8585        /** Stored source and destination addresses length.
    8686         */
    87         size_t                  addr_len;
     87        size_t addr_len;
    8888        /** Souce address offset in bytes from the beginning of the packet header.
    8989         */
    90         size_t                  src_addr;
     90        size_t src_addr;
    9191        /** Destination address offset in bytes from the beginning of the packet header.
    9292         */
    93         size_t                  dest_addr;
     93        size_t dest_addr;
    9494        /** Reserved data prefix length in bytes.
    9595         */
    96         size_t                  max_prefix;
     96        size_t max_prefix;
    9797        /** Reserved content length in bytes.
    9898         */
    99         size_t                  max_content;
     99        size_t max_content;
    100100        /** Actual data start offset in bytes from the beginning of the packet header.
    101101         */
    102         size_t                  data_start;
     102        size_t data_start;
    103103        /** Actual data end offset in bytes from the beginning of the packet header.
    104104         */
    105         size_t                  data_end;
     105        size_t data_end;
    106106        /** Integrity check magic value.
    107107         */
    108         int                             magic_value;
     108        int magic_value;
    109109};
    110110
     
    114114 *  @returns false otherwise.
    115115 */
    116 static inline int       packet_is_valid( const packet_t packet ){
    117         return packet && ( packet->magic_value == PACKET_MAGIC_VALUE );
     116static inline int packet_is_valid(const packet_t packet){
     117        return packet && (packet->magic_value == PACKET_MAGIC_VALUE);
    118118}
    119119
  • uspace/srv/net/structures/packet/packet_messages.h

    raa85487 raadf01e  
    6969/** Returns the protocol service message parameter.
    7070 */
    71 #define ARP_GET_PROTO( call )           ( services_t ) IPC_GET_ARG2( * call )
     71#define ARP_GET_PROTO(call)             (services_t) IPC_GET_ARG2(*call)
    7272
    7373/** Returns the packet identifier message parameter.
    7474 */
    75 #define IPC_GET_ID( call )                      ( packet_id_t ) IPC_GET_ARG1( * call )
     75#define IPC_GET_ID(call)                        (packet_id_t) IPC_GET_ARG1(*call)
    7676
    7777/** Returns the maximal content length message parameter.
    7878 */
    79 #define IPC_GET_CONTENT( call )         ( size_t ) IPC_GET_ARG1( * call )
     79#define IPC_GET_CONTENT(call)           (size_t) IPC_GET_ARG1(*call)
    8080
    8181/** Returns the maximal address length message parameter.
    8282 */
    83 #define IPC_GET_ADDR_LEN( call )        ( size_t ) IPC_GET_ARG2( * call )
     83#define IPC_GET_ADDR_LEN(call)  (size_t) IPC_GET_ARG2(*call)
    8484
    8585/** Returns the maximal prefix length message parameter.
    8686 */
    87 #define IPC_GET_PREFIX( call )          ( size_t ) IPC_GET_ARG3( * call )
     87#define IPC_GET_PREFIX(call)            (size_t) IPC_GET_ARG3(*call)
    8888
    8989/** Returns the maximal suffix length message parameter.
    9090 */
    91 #define IPC_GET_SUFFIX( call )          ( size_t ) IPC_GET_ARG4( * call )
     91#define IPC_GET_SUFFIX(call)            (size_t) IPC_GET_ARG4(*call)
    9292
    9393#endif
  • uspace/srv/net/structures/packet/packet_remote.c

    raa85487 raadf01e  
    6060 *  @returns Other error codes as defined for the async_share_in_start() function.
    6161 */
    62 int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size );
     62int packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size);
    6363
    64 int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){
     64int packet_translate(int phone, packet_ref packet, packet_id_t packet_id){
    6565        ERROR_DECLARE;
    6666
    67         ipcarg_t                        size;
    68         packet_t                        next;
     67        ipcarg_t size;
     68        packet_t next;
    6969
    70         if( ! packet ) return EINVAL;
    71         * packet = pm_find( packet_id );
    72         if( !( * packet )){
    73                 ERROR_PROPAGATE( async_req_1_1( phone, NET_PACKET_GET_SIZE, packet_id, & size ));
    74                 ERROR_PROPAGATE( packet_return( phone, packet, packet_id, size ));
     70        if(! packet){
     71                return EINVAL;
    7572        }
    76         if(( ** packet ).next ){
    77                 return packet_translate( phone, & next, ( ** packet ).next );
     73        *packet = pm_find(packet_id);
     74        if(!(*packet)){
     75                ERROR_PROPAGATE(async_req_1_1(phone, NET_PACKET_GET_SIZE, packet_id, &size));
     76                ERROR_PROPAGATE(packet_return(phone, packet, packet_id, size));
     77        }
     78        if((** packet).next){
     79                return packet_translate(phone, &next, (** packet).next);
    7880        }else return EOK;
    7981}
    8082
    81 int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size ){
     83int packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size){
    8284        ERROR_DECLARE;
    8385
    84         aid_t           message;
    85         ipc_call_t      answer;
    86         ipcarg_t        result;
     86        aid_t message;
     87        ipc_call_t answer;
     88        ipcarg_t result;
    8789
    88         message = async_send_1( phone, NET_PACKET_GET, packet_id, & answer );
    89         * packet = ( packet_t ) as_get_mappable_page( size );
    90         if( ERROR_OCCURRED( async_share_in_start_0_0( phone, * packet, size ))
    91         || ERROR_OCCURRED( pm_add( * packet ))){
    92                 munmap( * packet, size );
    93                 async_wait_for( message, NULL );
     90        message = async_send_1(phone, NET_PACKET_GET, packet_id, &answer);
     91        *packet = (packet_t) as_get_mappable_page(size);
     92        if(ERROR_OCCURRED(async_share_in_start_0_0(phone, * packet, size))
     93                || ERROR_OCCURRED(pm_add(*packet))){
     94                munmap(*packet, size);
     95                async_wait_for(message, NULL);
    9496                return ERROR_CODE;
    9597        }
    96         async_wait_for( message, & result );
     98        async_wait_for(message, &result);
    9799        return result;
    98100}
    99101
    100 packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){
     102packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix){
    101103        ERROR_DECLARE;
    102104
     
    105107        packet_t packet;
    106108
    107         if( ERROR_OCCURRED( async_req_4_2( phone, NET_PACKET_CREATE_4, max_content, addr_len, max_prefix, max_suffix, & packet_id, & size ))){
     109        if(ERROR_OCCURRED(async_req_4_2(phone, NET_PACKET_CREATE_4, max_content, addr_len, max_prefix, max_suffix, &packet_id, &size))){
    108110                return NULL;
    109111        }
    110         packet = pm_find( packet_id );
    111         if( ! packet ){
    112                 if( ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size ))){
     112        packet = pm_find(packet_id);
     113        if(! packet){
     114                if(ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size))){
    113115                        return NULL;
    114116                }
     
    117119}
    118120
    119 packet_t packet_get_1( int phone, size_t content ){
     121packet_t packet_get_1(int phone, size_t content){
    120122        ERROR_DECLARE;
    121123
    122         ipcarg_t        packet_id;
    123         ipcarg_t        size;
    124         packet_t        packet;
     124        ipcarg_t packet_id;
     125        ipcarg_t size;
     126        packet_t packet;
    125127
    126         if( ERROR_OCCURRED( async_req_1_2( phone, NET_PACKET_CREATE_1, content, & packet_id, & size ))){
     128        if(ERROR_OCCURRED(async_req_1_2(phone, NET_PACKET_CREATE_1, content, &packet_id, &size))){
    127129                return NULL;
    128130        }
    129         packet = pm_find( packet_id );
    130         if( ! packet ){
    131                 if( ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size ))){
     131        packet = pm_find(packet_id);
     132        if(! packet){
     133                if(ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size))){
    132134                        return NULL;
    133135                }
     
    136138}
    137139
    138 void pq_release( int phone, packet_id_t packet_id ){
    139         async_msg_1( phone, NET_PACKET_RELEASE, packet_id );
     140void pq_release(int phone, packet_id_t packet_id){
     141        async_msg_1(phone, NET_PACKET_RELEASE, packet_id);
    140142}
    141143
  • uspace/srv/net/structures/packet/packet_server.c

    raa85487 raadf01e  
    7676        /** Free packet queues.
    7777         */
    78         packet_t free[ FREE_QUEUES_COUNT ];
     78        packet_t free[FREE_QUEUES_COUNT];
    7979        /** Packet length upper bounds of the free packet queues.
    8080         *  The maximal lengths of packets in each queue in the ascending order.
    8181         *  The last queue is not limited.
    8282         */
    83         size_t sizes[ FREE_QUEUES_COUNT ];
     83        size_t sizes[FREE_QUEUES_COUNT];
    8484        /** Total packets allocated.
    8585         */
     
    8989                .counter = 1,
    9090                .waiters = {
    91                         .prev = & ps_globals.lock.waiters,
    92                         .next = & ps_globals.lock.waiters,
     91                        .prev = &ps_globals.lock.waiters,
     92                        .next = &ps_globals.lock.waiters,
    9393                }
    9494        },
    95         .free = { NULL, NULL, NULL, NULL, NULL, NULL, NULL },
    96         .sizes = { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64 },
     95        .free = {NULL, NULL, NULL, NULL, NULL, NULL, NULL},
     96        .sizes = {PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64},
    9797        .count = 0
    9898};
     
    113113 *  @returns NULL if there is not enough memory left.
    114114 */
    115 packet_t        packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
     115packet_t packet_get(size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix);
    116116
    117117/** Releases the packet queue.
     
    120120 *  @returns ENOENT if there is no such packet.
    121121 */
    122 int     packet_release_wrapper( packet_id_t packet_id );
     122int packet_release_wrapper(packet_id_t packet_id);
    123123
    124124/** Releases the packet and returns it to the appropriate free packet queue.
     
    126126 *  @param[in] packet The packet to be released.
    127127 */
    128 void packet_release( packet_t packet );
     128void packet_release(packet_t packet);
    129129
    130130/** Creates a&nbsp;new packet of dimensions at least as given.
     
    138138 *  @returns NULL if there is not enough memory left.
    139139 */
    140 packet_t        packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
     140packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix);
    141141
    142142/** Clears and initializes the packet according to the given dimensions.
     
    147147 *  @param[in] max_suffix The maximal suffix length in bytes.
    148148 */
    149 void    packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
     149void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix);
    150150
    151151/** Shares the packet memory block.
     
    157157 *  @returns Other error codes as defined for the async_share_in_finalize() function.
    158158 */
    159 int packet_reply( const packet_t packet );
     159int packet_reply(const packet_t packet);
    160160
    161161/*@}*/
    162162
    163 int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){
    164         if( ! packet ) return EINVAL;
    165         * packet = pm_find( packet_id );
    166         return ( * packet ) ? EOK : ENOENT;
    167 }
    168 
    169 packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){
    170         return packet_get( addr_len, max_prefix, max_content, max_suffix );
    171 }
    172 
    173 packet_t packet_get_1( int phone, size_t content ){
    174         return packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX );
    175 }
    176 
    177 void pq_release( int phone, packet_id_t packet_id ){
    178         ( void ) packet_release_wrapper( packet_id );
    179 }
    180 
    181 int     packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
     163int packet_translate(int phone, packet_ref packet, packet_id_t packet_id){
     164        if(! packet){
     165                return EINVAL;
     166        }
     167        *packet = pm_find(packet_id);
     168        return (*packet) ? EOK : ENOENT;
     169}
     170
     171packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix){
     172        return packet_get(addr_len, max_prefix, max_content, max_suffix);
     173}
     174
     175packet_t packet_get_1(int phone, size_t content){
     176        return packet_get(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX);
     177}
     178
     179void pq_release(int phone, packet_id_t packet_id){
     180        (void) packet_release_wrapper(packet_id);
     181}
     182
     183int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
    182184        packet_t packet;
    183185
    184         * answer_count = 0;
    185         switch( IPC_GET_METHOD( * call )){
     186        *answer_count = 0;
     187        switch(IPC_GET_METHOD(*call)){
    186188                case IPC_M_PHONE_HUNGUP:
    187189                        return EOK;
    188190                case NET_PACKET_CREATE_1:
    189                         packet = packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT( call ), DEFAULT_SUFFIX );
    190                         if( ! packet ) return ENOMEM;
    191                         * answer_count = 2;
    192                         IPC_SET_ARG1( * answer, packet->packet_id );
    193                         IPC_SET_ARG2( * answer, packet->length );
     191                        packet = packet_get(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
     192                        if(! packet){
     193                                return ENOMEM;
     194                        }
     195                        *answer_count = 2;
     196                        IPC_SET_ARG1(*answer, packet->packet_id);
     197                        IPC_SET_ARG2(*answer, packet->length);
    194198                        return EOK;
    195199                case NET_PACKET_CREATE_4:
    196                         packet = packet_get((( DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN( call )) ? IPC_GET_ADDR_LEN( call ) : DEFAULT_ADDR_LEN ), DEFAULT_PREFIX + IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), DEFAULT_SUFFIX + IPC_GET_SUFFIX( call ));
    197                         if( ! packet ) return ENOMEM;
    198                         * answer_count = 2;
    199                         IPC_SET_ARG1( * answer, packet->packet_id );
    200                         IPC_SET_ARG2( * answer, packet->length );
     200                        packet = packet_get(((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(call)) ? IPC_GET_ADDR_LEN(call) : DEFAULT_ADDR_LEN), DEFAULT_PREFIX + IPC_GET_PREFIX(call), IPC_GET_CONTENT(call), DEFAULT_SUFFIX + IPC_GET_SUFFIX(call));
     201                        if(! packet){
     202                                return ENOMEM;
     203                        }
     204                        *answer_count = 2;
     205                        IPC_SET_ARG1(*answer, packet->packet_id);
     206                        IPC_SET_ARG2(*answer, packet->length);
    201207                        return EOK;
    202208                case NET_PACKET_GET:
    203                         packet = pm_find( IPC_GET_ID( call ));
    204                         if( ! packet_is_valid( packet )) return ENOENT;
    205                         return packet_reply( packet );
     209                        packet = pm_find(IPC_GET_ID(call));
     210                        if(! packet_is_valid(packet)){
     211                                return ENOENT;
     212                        }
     213                        return packet_reply(packet);
    206214                case NET_PACKET_GET_SIZE:
    207                         packet = pm_find( IPC_GET_ID( call ));
    208                         if( ! packet_is_valid( packet )) return ENOENT;
    209                         IPC_SET_ARG1( * answer, packet->length );
    210                         * answer_count = 1;
     215                        packet = pm_find(IPC_GET_ID(call));
     216                        if(! packet_is_valid(packet)){
     217                                return ENOENT;
     218                        }
     219                        IPC_SET_ARG1(*answer, packet->length);
     220                        *answer_count = 1;
    211221                        return EOK;
    212222                case NET_PACKET_RELEASE:
    213                         return packet_release_wrapper( IPC_GET_ID( call ));
     223                        return packet_release_wrapper(IPC_GET_ID(call));
    214224        }
    215225        return ENOTSUP;
    216226}
    217227
    218 int packet_release_wrapper( packet_id_t packet_id ){
    219         packet_t        packet;
    220 
    221         packet = pm_find( packet_id );
    222         if( ! packet_is_valid( packet )) return ENOENT;
    223         fibril_mutex_lock( & ps_globals.lock );
    224         pq_destroy( packet, packet_release );
    225         fibril_mutex_unlock( & ps_globals.lock );
     228int packet_release_wrapper(packet_id_t packet_id){
     229        packet_t packet;
     230
     231        packet = pm_find(packet_id);
     232        if(! packet_is_valid(packet)){
     233                return ENOENT;
     234        }
     235        fibril_mutex_lock(&ps_globals.lock);
     236        pq_destroy(packet, packet_release);
     237        fibril_mutex_unlock(&ps_globals.lock);
    226238        return EOK;
    227239}
    228240
    229 void packet_release( packet_t packet ){
     241void packet_release(packet_t packet){
    230242        int index;
    231243        int result;
    232244
    233245        // remove debug dump
    234 //      printf( "packet %d released\n", packet->packet_id );
    235         for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index );
    236         result = pq_add( & ps_globals.free[ index ], packet, packet->length, packet->length );
    237         assert( result == EOK );
    238 }
    239 
    240 packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
     246//      printf("packet %d released\n", packet->packet_id);
     247        for(index = 0; (index < FREE_QUEUES_COUNT - 1) && (packet->length > ps_globals.sizes[index]); ++ index);
     248        result = pq_add(&ps_globals.free[index], packet, packet->length, packet->length);
     249        assert(result == EOK);
     250}
     251
     252packet_t packet_get(size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
    241253        int index;
    242254        packet_t packet;
    243255        size_t length;
    244256
    245         length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE );
    246         fibril_mutex_lock( & ps_globals.lock );
    247         for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){
    248                 if( length <= ps_globals.sizes[ index ] ){
    249                         packet = ps_globals.free[ index ];
    250                         while( packet_is_valid( packet ) && ( packet->length < length )){
    251                                 packet = pm_find( packet->next );
    252                         }
    253                         if( packet_is_valid( packet )){
    254                                 if( packet == ps_globals.free[ index ] ){
    255                                         ps_globals.free[ index ] = pq_detach( packet );
     257        length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE);
     258        fibril_mutex_lock(&ps_globals.lock);
     259        for(index = 0; index < FREE_QUEUES_COUNT - 1; ++ index){
     260                if(length <= ps_globals.sizes[index]){
     261                        packet = ps_globals.free[index];
     262                        while(packet_is_valid(packet) && (packet->length < length)){
     263                                packet = pm_find(packet->next);
     264                        }
     265                        if(packet_is_valid(packet)){
     266                                if(packet == ps_globals.free[index]){
     267                                        ps_globals.free[index] = pq_detach(packet);
    256268                                }else{
    257                                         pq_detach( packet );
     269                                        pq_detach(packet);
    258270                                }
    259                                 packet_init( packet, addr_len, max_prefix, max_content, max_suffix );
    260                                 fibril_mutex_unlock( & ps_globals.lock );
     271                                packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
     272                                fibril_mutex_unlock(&ps_globals.lock);
    261273                                // remove debug dump
    262 //                              printf( "packet %d got\n", packet->packet_id );
     274//                              printf("packet %d got\n", packet->packet_id);
    263275                                return packet;
    264276                        }
    265277                }
    266278        }
    267         packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix );
    268         fibril_mutex_unlock( & ps_globals.lock );
     279        packet = packet_create(length, addr_len, max_prefix, max_content, max_suffix);
     280        fibril_mutex_unlock(&ps_globals.lock);
    269281        // remove debug dump
    270 //      printf( "packet %d created\n", packet->packet_id );
     282//      printf("packet %d created\n", packet->packet_id);
    271283        return packet;
    272284}
    273285
    274 packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
     286packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
    275287        ERROR_DECLARE;
    276288
    277         packet_t        packet;
     289        packet_t packet;
    278290
    279291        // already locked
    280         packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
    281         if( packet == MAP_FAILED ) return NULL;
     292        packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
     293        if(packet == MAP_FAILED){
     294                return NULL;
     295        }
    282296        ++ ps_globals.count;
    283297        packet->packet_id = ps_globals.count;
    284298        packet->length = length;
    285         packet_init( packet, addr_len, max_prefix, max_content, max_suffix );
     299        packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
    286300        packet->magic_value = PACKET_MAGIC_VALUE;
    287         if( ERROR_OCCURRED( pm_add( packet ))){
    288                 munmap( packet, packet->length );
     301        if(ERROR_OCCURRED(pm_add(packet))){
     302                munmap(packet, packet->length);
    289303                return NULL;
    290304        }
     
    292306}
    293307
    294 void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
     308void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
    295309        // clear the packet content
    296         bzero((( void * ) packet ) + sizeof( struct packet ), packet->length - sizeof( struct packet ));
     310        bzero(((void *) packet) + sizeof(struct packet), packet->length - sizeof(struct packet));
    297311        // clear the packet header
    298312        packet->order = 0;
     
    301315        packet->next = 0;
    302316        packet->addr_len = 0;
    303         packet->src_addr = sizeof( struct packet );
     317        packet->src_addr = sizeof(struct packet);
    304318        packet->dest_addr = packet->src_addr + addr_len;
    305319        packet->max_prefix = max_prefix;
     
    309323}
    310324
    311 int packet_reply( const packet_t packet ){
    312         ipc_callid_t    callid;
    313         size_t                  size;
    314 
    315         if( ! packet_is_valid( packet )) return EINVAL;
    316         if( async_share_in_receive( & callid, & size ) <= 0 ) return EINVAL;
    317         if( size != packet->length ) return ENOMEM;
    318         return async_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE );
     325int packet_reply(const packet_t packet){
     326        ipc_callid_t callid;
     327        size_t size;
     328
     329        if(! packet_is_valid(packet)){
     330                return EINVAL;
     331        }
     332        if(async_share_in_receive(&callid, &size) <= 0) return EINVAL;
     333        if(size != packet->length){
     334                return ENOMEM;
     335        }
     336        return async_share_in_finalize(callid, packet, PROTO_READ | PROTO_WRITE);
    319337}
    320338
  • uspace/srv/net/structures/packet/packet_server.h

    raa85487 raadf01e  
    5656 *  @returns Other error codes as defined for the packet_release_wrapper() function.
    5757 */
    58 int     packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
     58int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count);
    5959
    6060#endif
Note: See TracChangeset for help on using the changeset viewer.