Ignore:
Timestamp:
2010-03-07T22:51:38Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
60ab6c3
Parents:
b5cbff4 (diff), 31c80a5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~lukasmejdrech/helenos/network.

File:
1 edited

Legend:

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

    rb5cbff4 r71b00dcc  
    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        item = packet;
     235        do{
     236                if(item->order == order){
    229237                        return item;
    230238                }
    231         }
     239                item = pm_find(item->next);
     240        }while(item && (item != packet) && packet_is_valid(item));
    232241        return NULL;
    233242}
    234243
    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;
     244int pq_insert_after(packet_t packet, packet_t new_packet){
     245        packet_t item;
     246
     247        if(!(packet_is_valid(packet) && packet_is_valid(new_packet))){
     248                return EINVAL;
     249        }
    239250        new_packet->previous = packet->packet_id;
    240251        new_packet->next = packet->next;
    241         item = pm_find( packet->next );
    242         if( item ) item->previous = new_packet->packet_id;
     252        item = pm_find(packet->next);
     253        if(item){
     254                item->previous = new_packet->packet_id;
     255        }
    243256        packet->next = new_packet->packet_id;
    244257        return EOK;
    245258}
    246259
    247 packet_t pq_detach( packet_t packet ){
     260packet_t pq_detach(packet_t packet){
    248261        packet_t next;
    249262        packet_t previous;
    250263
    251         if( ! packet_is_valid( packet )) return NULL;
    252         next = pm_find( packet->next );
    253         if( next ){
     264        if(! packet_is_valid(packet)){
     265                return NULL;
     266        }
     267        next = pm_find(packet->next);
     268        if(next){
    254269                next->previous = packet->previous;
    255                 previous = pm_find( next->previous );
    256                 if( previous ){
     270                previous = pm_find(next->previous);
     271                if(previous){
    257272                        previous->next = next->packet_id;
    258273                }
     
    263278}
    264279
    265 int pq_set_order( packet_t packet, size_t order, size_t metric ){
    266         if( ! packet_is_valid( packet )) return EINVAL;
     280int pq_set_order(packet_t packet, size_t order, size_t metric){
     281        if(! packet_is_valid(packet)){
     282                return EINVAL;
     283        }
    267284        packet->order = order;
    268285        packet->metric = metric;
     
    270287}
    271288
    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;
     289int pq_get_order(packet_t packet, size_t * order, size_t * metric){
     290        if(! packet_is_valid(packet)){
     291                return EINVAL;
     292        }
     293        if(order){
     294                *order = packet->order;
     295        }
     296        if(metric){
     297                *metric = packet->metric;
     298        }
     299        return EOK;
     300}
     301
     302void pq_destroy(packet_t first, void (*packet_release)(packet_t packet)){
     303        packet_t actual;
     304        packet_t next;
    282305
    283306        actual = first;
    284         while( packet_is_valid( actual )){
    285                 next = pm_find( actual->next );
     307        while(packet_is_valid(actual)){
     308                next = pm_find(actual->next);
    286309                actual->next = 0;
    287310                actual->previous = 0;
    288                 if( packet_release ) packet_release( actual );
     311                if(packet_release){
     312                        packet_release(actual);
     313                }
    289314                actual = next;
    290315        }
    291316}
    292317
    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 );
     318packet_t pq_next(packet_t packet){
     319        if(! packet_is_valid(packet)){
     320                return NULL;
     321        }
     322        return pm_find(packet->next);
     323}
     324
     325packet_t pq_previous(packet_t packet){
     326        if(! packet_is_valid(packet)){
     327                return NULL;
     328        }
     329        return pm_find(packet->previous);
    301330}
    302331
Note: See TracChangeset for help on using the changeset viewer.