Changeset 71b00dcc in mainline for uspace/srv/net/structures


Ignore:
Timestamp:
2010-03-07T22:51:38Z (15 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.

Location:
uspace/srv/net/structures
Files:
20 edited

Legend:

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

    rb5cbff4 r71b00dcc  
    5757 *  @returns EEXIST if the key character string is already used.
    5858 */
    59 int     char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value );
     59int char_map_add_item(char_map_ref map, const char * identifier, size_t length, const int value);
    6060
    6161/** Returns the node assigned to the key from the map.
     
    6666 *  @returns NULL if the key is not assigned a&nbsp;node.
    6767 */
    68 char_map_ref    char_map_find_node( const char_map_ref map, const char * identifier, const size_t length );
     68char_map_ref char_map_find_node(const char_map_ref map, const char * identifier, const size_t length);
    6969
    7070/** Returns the value assigned to the map.
     
    7373 *  @returns CHAR_MAP_NULL if the map is not assigned a&nbsp;value.
    7474 */
    75 int     char_map_get_value( const char_map_ref map );
     75int char_map_get_value(const char_map_ref map);
    7676
    7777/** Checks if the map is valid.
     
    8080 *  @returns FALSE otherwise.
    8181 */
    82 int     char_map_is_valid( const char_map_ref map );
    83 
    84 int char_map_add( char_map_ref map, const char * identifier, size_t length, const int value ){
    85         if( char_map_is_valid( map ) && ( identifier ) && (( length ) || ( * identifier ))){
    86                 int     index;
    87 
    88                 for( index = 0; index < map->next; ++ index ){
    89                         if( map->items[ index ]->c == * identifier ){
     82int char_map_is_valid(const char_map_ref map);
     83
     84int char_map_add(char_map_ref map, const char * identifier, size_t length, const int value){
     85        if(char_map_is_valid(map) && (identifier) && ((length) || (*identifier))){
     86                int index;
     87
     88                for(index = 0; index < map->next; ++ index){
     89                        if(map->items[index]->c == * identifier){
    9090                                ++ identifier;
    91                                 if(( length > 1 ) || (( length == 0 ) && ( * identifier ))){
    92                                         return char_map_add( map->items[ index ], identifier, length ? length - 1 : 0, value );
     91                                if((length > 1) || ((length == 0) && (*identifier))){
     92                                        return char_map_add(map->items[index], identifier, length ? length - 1 : 0, value);
    9393                                }else{
    94                                         if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS;
    95                                         map->items[ index ]->value = value;
     94                                        if(map->items[index]->value != CHAR_MAP_NULL){
     95                                                return EEXISTS;
     96                                        }
     97                                        map->items[index]->value = value;
    9698                                        return EOK;
    9799                                }
    98100                        }
    99101                }
    100                 return char_map_add_item( map, identifier, length, value );
     102                return char_map_add_item(map, identifier, length, value);
    101103        }
    102104        return EINVAL;
    103105}
    104106
    105 int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value ){
    106         if( map->next == ( map->size - 1 )){
    107                 char_map_ref    * tmp;
    108 
    109                 tmp = ( char_map_ref * ) realloc( map->items, sizeof( char_map_ref ) * 2 * map->size );
    110                 if( ! tmp ) return ENOMEM;
     107int char_map_add_item(char_map_ref map, const char * identifier, size_t length, const int value){
     108        if(map->next == (map->size - 1)){
     109                char_map_ref *tmp;
     110
     111                tmp = (char_map_ref *) realloc(map->items, sizeof(char_map_ref) * 2 * map->size);
     112                if(! tmp){
     113                        return ENOMEM;
     114                }
    111115                map->size *= 2;
    112116                map->items = tmp;
    113117        }
    114         map->items[ map->next ] = ( char_map_ref ) malloc( sizeof( char_map_t ));
    115         if( ! map->items[ map->next ] ) return ENOMEM;
    116         if( char_map_initialize( map->items[ map->next ] ) != EOK ){
    117                 free( map->items[ map->next ] );
    118                 map->items[ map->next ] = NULL;
     118        map->items[map->next] = (char_map_ref) malloc(sizeof(char_map_t));
     119        if(! map->items[map->next]){
    119120                return ENOMEM;
    120121        }
    121         map->items[ map->next ]->c = * identifier;
     122        if(char_map_initialize(map->items[map->next]) != EOK){
     123                free(map->items[map->next]);
     124                map->items[map->next] = NULL;
     125                return ENOMEM;
     126        }
     127        map->items[map->next]->c = * identifier;
    122128        ++ identifier;
    123129        ++ map->next;
    124         if(( length > 1 ) || (( length == 0 ) && ( * identifier ))){
    125                 map->items[ map->next - 1 ]->value = CHAR_MAP_NULL;
    126                 return char_map_add_item( map->items[ map->next - 1 ], identifier, length ? length - 1 : 0, value );
     130        if((length > 1) || ((length == 0) && (*identifier))){
     131                map->items[map->next - 1]->value = CHAR_MAP_NULL;
     132                return char_map_add_item(map->items[map->next - 1], identifier, length ? length - 1 : 0, value);
    127133        }else{
    128                 map->items[ map->next - 1 ]->value = value;
     134                map->items[map->next - 1]->value = value;
    129135        }
    130136        return EOK;
    131137}
    132138
    133 void char_map_destroy( char_map_ref map ){
    134         if( char_map_is_valid( map )){
    135                 int     index;
     139void char_map_destroy(char_map_ref map){
     140        if(char_map_is_valid(map)){
     141                int index;
    136142
    137143                map->magic = 0;
    138                 for( index = 0; index < map->next; ++ index ){
    139                         char_map_destroy( map->items[ index ] );
    140                 }
    141                 free( map->items );
     144                for(index = 0; index < map->next; ++ index){
     145                        char_map_destroy(map->items[index]);
     146                }
     147                free(map->items);
    142148                map->items = NULL;
    143149        }
    144150}
    145151
    146 int char_map_exclude( char_map_ref map, const char * identifier, size_t length ){
    147         char_map_ref    node;
    148 
    149         node = char_map_find_node( map, identifier, length );
    150         if( node ){
    151                 int     value;
     152int char_map_exclude(char_map_ref map, const char * identifier, size_t length){
     153        char_map_ref node;
     154
     155        node = char_map_find_node(map, identifier, length);
     156        if(node){
     157                int value;
    152158
    153159                value = node->value;
     
    158164}
    159165
    160 int char_map_find( const char_map_ref map, const char * identifier, size_t length ){
    161         char_map_ref    node;
    162 
    163         node = char_map_find_node( map, identifier, length );
     166int char_map_find(const char_map_ref map, const char * identifier, size_t length){
     167        char_map_ref node;
     168
     169        node = char_map_find_node(map, identifier, length);
    164170        return node ? node->value : CHAR_MAP_NULL;
    165171}
    166172
    167 char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, size_t length ){
    168         if( ! char_map_is_valid( map )) return NULL;
    169         if( length || ( * identifier )){
    170                 int     index;
    171 
    172                 for( index = 0; index < map->next; ++ index ){
    173                         if( map->items[ index ]->c == * identifier ){
     173char_map_ref char_map_find_node(const char_map_ref map, const char * identifier, size_t length){
     174        if(! char_map_is_valid(map)){
     175                return NULL;
     176        }
     177        if(length || (*identifier)){
     178                int index;
     179
     180                for(index = 0; index < map->next; ++ index){
     181                        if(map->items[index]->c == * identifier){
    174182                                ++ identifier;
    175                                 if( length == 1 ) return map->items[ index ];
    176                                 return char_map_find_node( map->items[ index ], identifier, length ? length - 1 : 0 );
     183                                if(length == 1){
     184                                        return map->items[index];
     185                                }
     186                                return char_map_find_node(map->items[index], identifier, length ? length - 1 : 0);
    177187                        }
    178188                }
     
    182192}
    183193
    184 int char_map_get_value( const char_map_ref map ){
    185         return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL;
    186 }
    187 
    188 int char_map_initialize( char_map_ref map ){
    189         if( ! map ) return EINVAL;
     194int char_map_get_value(const char_map_ref map){
     195        return char_map_is_valid(map) ? map->value : CHAR_MAP_NULL;
     196}
     197
     198int char_map_initialize(char_map_ref map){
     199        if(! map){
     200                return EINVAL;
     201        }
    190202        map->c = '\0';
    191203        map->value = CHAR_MAP_NULL;
    192204        map->size = 2;
    193205        map->next = 0;
    194         map->items = malloc( sizeof( char_map_ref ) * map->size );
    195         if( ! map->items ){
     206        map->items = malloc(sizeof(char_map_ref) * map->size);
     207        if(! map->items){
    196208                map->magic = 0;
    197209                return ENOMEM;
    198210        }
    199         map->items[ map->next ] = NULL;
     211        map->items[map->next] = NULL;
    200212        map->magic = CHAR_MAP_MAGIC_VALUE;
    201213        return EOK;
    202214}
    203215
    204 int char_map_is_valid( const char_map_ref map ){
    205         return map && ( map->magic == CHAR_MAP_MAGIC_VALUE );
    206 }
    207 
    208 int char_map_update( char_map_ref map, const char * identifier, const size_t length, const int value ){
    209         char_map_ref    node;
    210 
    211 //      if( ! char_map_is_valid( map )) return EINVAL;
    212         node = char_map_find_node( map, identifier, length );
    213         if( node ){
     216int char_map_is_valid(const char_map_ref map){
     217        return map && (map->magic == CHAR_MAP_MAGIC_VALUE);
     218}
     219
     220int char_map_update(char_map_ref map, const char * identifier, const size_t length, const int value){
     221        char_map_ref node;
     222
     223//      if(! char_map_is_valid(map)) return EINVAL;
     224        node = char_map_find_node(map, identifier, length);
     225        if(node){
    214226                node->value = value;
    215227                return EOK;
    216228        }else{
    217                 return char_map_add( map, identifier, length, value );
     229                return char_map_add(map, identifier, length, value);
    218230        }
    219231}
  • uspace/srv/net/structures/char_map.h

    rb5cbff4 r71b00dcc  
    4040/** Invalid assigned value used also if an&nbsp;entry does not exist.
    4141 */
    42 #define CHAR_MAP_NULL   ( -1 )
     42#define CHAR_MAP_NULL   (-1)
    4343
    4444/** Type definition of the character string to integer map.
     
    5959        /** Actually mapped character.
    6060         */
    61         char                    c;
     61        char c;
    6262        /** Stored integral value.
    6363         */
    64         int                             value;
     64        int value;
    6565        /** Next character array size.
    6666         */
    67         int                             size;
     67        int size;
    6868        /** First free position in the next character array.
    6969         */
    70         int                             next;
     70        int next;
    7171        /** Next character array.
    7272         */
    73         char_map_ref *  items;
     73        char_map_ref * items;
    7474        /** Consistency check magic value.
    7575         */
    76         int                             magic;
     76        int magic;
    7777};
    7878
     
    8989 *  @returns Other error codes as defined for the char_map_add_item() function.
    9090 */
    91 int     char_map_add( char_map_ref map, const char * identifier, size_t length, const int value );
     91int char_map_add(char_map_ref map, const char * identifier, size_t length, const int value);
    9292
    9393/** Clears and destroys the map.
    9494 *  @param[in,out] map The character string to integer map.
    9595 */
    96 void    char_map_destroy( char_map_ref map );
     96void char_map_destroy(char_map_ref map);
    9797
    9898/** Excludes the value assigned to the key from the map.
     
    104104 *  @returns CHAR_MAP_NULL if the key is not assigned a&nbsp;value.
    105105 */
    106 int     char_map_exclude( char_map_ref map, const char * identifier, size_t length );
     106int char_map_exclude(char_map_ref map, const char * identifier, size_t length);
    107107
    108108/** Returns the value assigned to the key from the map.
     
    113113 *  @returns CHAR_MAP_NULL if the key is not assigned a&nbsp;value.
    114114 */
    115 int     char_map_find( const char_map_ref map, const char * identifier, size_t length );
     115int char_map_find(const char_map_ref map, const char * identifier, size_t length);
    116116
    117117/** Initializes the map.
     
    121121 *  @returns ENOMEM if there is not enough memory left.
    122122 */
    123 int     char_map_initialize( char_map_ref map );
     123int char_map_initialize(char_map_ref map);
    124124
    125125/** Adds or updates the value with the key to the map.
     
    135135 *  @returns Other error codes as defined for the char_map_add_item() function.
    136136 */
    137 int     char_map_update( char_map_ref map, const char * identifier, size_t length, const int value );
     137int char_map_update(char_map_ref map, const char * identifier, size_t length, const int value);
    138138
    139139#endif
  • uspace/srv/net/structures/dynamic_fifo.c

    rb5cbff4 r71b00dcc  
    5050 *  @param[in] index The actual index to be shifted.
    5151 */
    52 #define NEXT_INDEX( fifo, index )       ((( index ) + 1 ) % (( fifo )->size + 1 ))
     52#define NEXT_INDEX(fifo, index) (((index) + 1) % ((fifo)->size + 1))
    5353
    5454/** Checks if the queue is valid.
     
    5757 *  @returns FALSE otherwise.
    5858 */
    59 int     dyn_fifo_is_valid( dyn_fifo_ref fifo );
     59int dyn_fifo_is_valid(dyn_fifo_ref fifo);
    6060
    61 int dyn_fifo_is_valid( dyn_fifo_ref fifo ){
    62         return fifo && ( fifo->magic_value == DYN_FIFO_MAGIC_VALUE );
     61int dyn_fifo_is_valid(dyn_fifo_ref fifo){
     62        return fifo && (fifo->magic_value == DYN_FIFO_MAGIC_VALUE);
    6363}
    6464
    65 int dyn_fifo_initialize( dyn_fifo_ref fifo, int size ){
    66         if( ! fifo ) return EBADMEM;
    67         if( size <= 0 ) return EINVAL;
    68         fifo->items = ( int * ) malloc( sizeof( int ) * size + 1 );
    69         if( ! fifo->items ) return ENOMEM;
     65int dyn_fifo_initialize(dyn_fifo_ref fifo, int size){
     66        if(! fifo){
     67                return EBADMEM;
     68        }
     69        if(size <= 0){
     70                return EINVAL;
     71        }
     72        fifo->items = (int *) malloc(sizeof(int) * size + 1);
     73        if(! fifo->items){
     74                return ENOMEM;
     75        }
    7076        fifo->size = size;
    7177        fifo->head = 0;
     
    7581}
    7682
    77 int     dyn_fifo_push( dyn_fifo_ref fifo, int value, int max_size ){
    78         int *   new_items;
     83int dyn_fifo_push(dyn_fifo_ref fifo, int value, int max_size){
     84        int * new_items;
    7985
    80         if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
    81         if( NEXT_INDEX( fifo, fifo->tail ) == fifo->head ){
    82                 if(( max_size > 0 ) && (( fifo->size * 2 ) > max_size )){
    83                         if( fifo->size >= max_size ) return ENOMEM;
     86        if(! dyn_fifo_is_valid(fifo)){
     87                return EINVAL;
     88        }
     89        if(NEXT_INDEX(fifo, fifo->tail) == fifo->head){
     90                if((max_size > 0) && ((fifo->size * 2) > max_size)){
     91                        if(fifo->size >= max_size){
     92                                return ENOMEM;
     93                        }
    8494                }else{
    8595                        max_size = fifo->size * 2;
    8696                }
    87                 new_items = realloc( fifo->items, sizeof( int ) * max_size + 1 );
    88                 if( ! new_items ) return ENOMEM;
     97                new_items = realloc(fifo->items, sizeof(int) * max_size + 1);
     98                if(! new_items){
     99                        return ENOMEM;
     100                }
    89101                fifo->items = new_items;
    90                 if( fifo->tail < fifo->head ){
    91                         if( fifo->tail < max_size - fifo->size ){
    92                                 memcpy( fifo->items + fifo->size + 1, fifo->items, fifo->tail * sizeof( int ));
     102                if(fifo->tail < fifo->head){
     103                        if(fifo->tail < max_size - fifo->size){
     104                                memcpy(fifo->items + fifo->size + 1, fifo->items, fifo->tail * sizeof(int));
    93105                                fifo->tail += fifo->size + 1;
    94106                        }else{
    95                                 memcpy( fifo->items + fifo->size + 1, fifo->items, ( max_size - fifo->size ) * sizeof( int ));
    96                                 memcpy( fifo->items, fifo->items + max_size - fifo->size, fifo->tail - max_size + fifo->size );
     107                                memcpy(fifo->items + fifo->size + 1, fifo->items, (max_size - fifo->size) * sizeof(int));
     108                                memcpy(fifo->items, fifo->items + max_size - fifo->size, fifo->tail - max_size + fifo->size);
    97109                                fifo->tail -= max_size - fifo->size;
    98110                        }
     
    100112                fifo->size = max_size;
    101113        }
    102         fifo->items[ fifo->tail ] = value;
    103         fifo->tail = NEXT_INDEX( fifo, fifo->tail );
     114        fifo->items[fifo->tail] = value;
     115        fifo->tail = NEXT_INDEX(fifo, fifo->tail);
    104116        return EOK;
    105117}
    106118
    107 int dyn_fifo_pop( dyn_fifo_ref fifo ){
    108         int     value;
     119int dyn_fifo_pop(dyn_fifo_ref fifo){
     120        int value;
    109121
    110         if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
    111         if( fifo->head == fifo->tail ) return ENOENT;
    112         value = fifo->items[ fifo->head ];
    113         fifo->head = NEXT_INDEX( fifo, fifo->head );
     122        if(! dyn_fifo_is_valid(fifo)){
     123                return EINVAL;
     124        }
     125        if(fifo->head == fifo->tail){
     126                return ENOENT;
     127        }
     128        value = fifo->items[fifo->head];
     129        fifo->head = NEXT_INDEX(fifo, fifo->head);
    114130        return value;
    115131}
    116132
    117 int dyn_fifo_value( dyn_fifo_ref fifo ){
    118         if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
    119         if( fifo->head == fifo->tail ) return ENOENT;
    120         return fifo->items[ fifo->head ];
     133int dyn_fifo_value(dyn_fifo_ref fifo){
     134        if(! dyn_fifo_is_valid(fifo)){
     135                return EINVAL;
     136        }
     137        if(fifo->head == fifo->tail){
     138                return ENOENT;
     139        }
     140        return fifo->items[fifo->head];
    121141}
    122142
    123 int dyn_fifo_destroy( dyn_fifo_ref fifo ){
    124         if( ! dyn_fifo_is_valid( fifo )) return EINVAL;
    125         free( fifo->items );
     143int dyn_fifo_destroy(dyn_fifo_ref fifo){
     144        if(! dyn_fifo_is_valid(fifo)){
     145                return EINVAL;
     146        }
     147        free(fifo->items);
    126148        fifo->magic_value = 0;
    127149        return EOK;
  • uspace/srv/net/structures/dynamic_fifo.h

    rb5cbff4 r71b00dcc  
    5656        /** Stored item field.
    5757         */
    58         int     *       items;
     58        int *   items;
    5959        /** Actual field size.
    6060         */
    61         int             size;
     61        int size;
    6262        /** First item in the queue index.
    6363         */
    64         int             head;
     64        int head;
    6565        /** Last item in the queue index.
    6666         */
    67         int             tail;
     67        int tail;
    6868        /** Consistency check magic value.
    6969         */
    70         int             magic_value;
     70        int magic_value;
    7171};
    7272
     
    7979 *  @returns ENOMEM if there is not enough memory left.
    8080 */
    81 int     dyn_fifo_initialize( dyn_fifo_ref fifo, int size );
     81int dyn_fifo_initialize(dyn_fifo_ref fifo, int size);
    8282
    8383/** Appends a new item to the queue end.
     
    8989 *  @returns ENOMEM if there is not enough memory left.
    9090 */
    91 int     dyn_fifo_push( dyn_fifo_ref fifo, int value, int max_size );
     91int dyn_fifo_push(dyn_fifo_ref fifo, int value, int max_size);
    9292
    9393/** Returns and excludes the first item in the queue.
     
    9797 *  @returns ENOENT if the queue is empty.
    9898 */
    99 int     dyn_fifo_pop( dyn_fifo_ref fifo );
     99int dyn_fifo_pop(dyn_fifo_ref fifo);
    100100
    101101/** Returns and keeps the first item in the queue.
     
    105105 *  @returns ENOENT if the queue is empty.
    106106 */
    107 int     dyn_fifo_value( dyn_fifo_ref fifo );
     107int dyn_fifo_value(dyn_fifo_ref fifo);
    108108
    109109/** Clears and destroys the queue.
     
    112112 *  @returns EINVAL if the queue is not valid.
    113113 */
    114 int dyn_fifo_destroy( dyn_fifo_ref fifo );
     114int dyn_fifo_destroy(dyn_fifo_ref fifo);
    115115
    116116#endif
  • uspace/srv/net/structures/generic_char_map.h

    rb5cbff4 r71b00dcc  
    5454 *  @param[in] type Inner object type.
    5555 */
    56 #define GENERIC_CHAR_MAP_DECLARE( name, type )                                                                  \
     56#define GENERIC_CHAR_MAP_DECLARE(name, type)                                                                    \
    5757                                                                                                                                                                \
    58 GENERIC_FIELD_DECLARE( name##_items, type )                                                                             \
     58GENERIC_FIELD_DECLARE(name##_items, type)                                                                               \
    5959                                                                                                                                                                \
    6060typedef struct name             name##_t;                                                                                               \
     
    6262                                                                                                                                                                \
    6363struct  name{                                                                                                                                   \
    64         char_map_t              names;                                                                                                          \
    65         name##_items_t  values;                                                                                                         \
    66         int                             magic;                                                                                                          \
     64        char_map_t names;                                                                                                               \
     65        name##_items_t values;                                                                                                          \
     66        int magic;                                                                                                              \
    6767};                                                                                                                                                              \
    6868                                                                                                                                                                \
    69 int     name##_add( name##_ref map, const char * name, const size_t length, type * value );     \
    70 int     name##_count( name##_ref map );                                                                                         \
    71 void    name##_destroy( name##_ref map );                                                                               \
    72 void    name##_exclude( name##_ref map, const char * name, const size_t length );       \
    73 type *  name##_find( name##_ref map, const char * name, const size_t length );  \
    74 int     name##_initialize( name##_ref map );                                                                            \
    75 int     name##_is_valid( name##_ref map );
     69int name##_add(name##_ref map, const char * name, const size_t length, type * value);   \
     70int name##_count(name##_ref map);                                                                                               \
     71void name##_destroy(name##_ref map);                                                                                    \
     72void name##_exclude(name##_ref map, const char * name, const size_t length);    \
     73type * name##_find(name##_ref map, const char * name, const size_t length);             \
     74int name##_initialize(name##_ref map);                                                                                  \
     75int name##_is_valid(name##_ref map);
    7676
    7777/** Character string to generic type map implementation.
     
    8080 *  @param[in] type Inner object type.
    8181 */
    82 #define GENERIC_CHAR_MAP_IMPLEMENT( name, type )                                                                \
     82#define GENERIC_CHAR_MAP_IMPLEMENT(name, type)                                                                  \
    8383                                                                                                                                                                \
    84 GENERIC_FIELD_IMPLEMENT( name##_items, type )                                                                   \
     84GENERIC_FIELD_IMPLEMENT(name##_items, type)                                                                             \
    8585                                                                                                                                                                \
    86 int name##_add( name##_ref map, const char * name, const size_t length, type * value ){ \
     86int name##_add(name##_ref map, const char * name, const size_t length, type * value){   \
    8787        ERROR_DECLARE;                                                                                                                          \
    8888                                                                                                                                                                \
    89         int     index;                                                                                                                                  \
     89        int index;                                                                                                                                      \
    9090                                                                                                                                                                \
    91         if( ! name##_is_valid( map )) return EINVAL;                                                            \
    92         index = name##_items_add( & map->values, value );                                                       \
    93         if( index < 0 ) return index;                                                                                           \
    94         if( ERROR_OCCURRED( char_map_add( & map->names, name, length, index ))){                \
    95                 name##_items_exclude_index( & map->values, index );                                             \
     91        if(! name##_is_valid(map)){                                                                                                     \
     92                return EINVAL;                                                                                                                  \
     93        }                                                                                                                                                       \
     94        index = name##_items_add(&map->values, value);                                                          \
     95        if(index < 0){                                                                                                                          \
     96                return index;                                                                                                                   \
     97        }                                                                                                                                                       \
     98        if(ERROR_OCCURRED(char_map_add(&map->names, name, length, index))){                     \
     99                name##_items_exclude_index(&map->values, index);                                                \
    96100                return ERROR_CODE;                                                                                                              \
    97101        }                                                                                                                                                       \
     
    99103}                                                                                                                                                               \
    100104                                                                                                                                                                \
    101 int name##_count( name##_ref map ){                                                                                             \
    102         return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1;       \
     105int name##_count(name##_ref map){                                                                                               \
     106        return name##_is_valid(map) ? name##_items_count(&map->values) : -1;            \
    103107}                                                                                                                                                               \
    104108                                                                                                                                                                \
    105 void name##_destroy( name##_ref map ){                                                                                  \
    106         if( name##_is_valid( map )){                                                                                            \
    107                 char_map_destroy( & map->names );                                                                               \
    108                 name##_items_destroy( & map->values );                                                                  \
     109void name##_destroy(name##_ref map){                                                                                    \
     110        if(name##_is_valid(map)){                                                                                                       \
     111                char_map_destroy(&map->names);                                                                                  \
     112                name##_items_destroy(&map->values);                                                                             \
    109113        }                                                                                                                                                       \
    110114}                                                                                                                                                               \
    111115                                                                                                                                                                \
    112 void name##_exclude( name##_ref map, const char * name, const size_t length ){  \
    113         if( name##_is_valid( map )){                                                                                            \
    114                 int     index;                                                                                                                          \
     116void name##_exclude(name##_ref map, const char * name, const size_t length){    \
     117        if(name##_is_valid(map)){                                                                                                       \
     118                int index;                                                                                                                              \
    115119                                                                                                                                                                \
    116                 index = char_map_exclude( & map->names, name, length );                                 \
    117                 if( index != CHAR_MAP_NULL ){                                                                                   \
    118                         name##_items_exclude_index( & map->values, index );                                     \
     120                index = char_map_exclude(&map->names, name, length);                                    \
     121                if(index != CHAR_MAP_NULL){                                                                                             \
     122                        name##_items_exclude_index(&map->values, index);                                        \
    119123                }                                                                                                                                               \
    120124        }                                                                                                                                                       \
    121125}                                                                                                                                                               \
    122126                                                                                                                                                                \
    123 type * name##_find( name##_ref map, const char * name, const size_t length ){   \
    124         if( name##_is_valid( map )){                                                                                            \
    125                 int     index;                                                                                                                          \
     127type * name##_find(name##_ref map, const char * name, const size_t length){             \
     128        if(name##_is_valid(map)){                                                                                                       \
     129                int index;                                                                                                                              \
    126130                                                                                                                                                                \
    127                 index = char_map_find( & map->names, name, length );                                    \
    128                 if( index != CHAR_MAP_NULL ){                                                                                   \
    129                         return name##_items_get_index( & map->values, index );                          \
     131                index = char_map_find(&map->names, name, length);                                               \
     132                if(index != CHAR_MAP_NULL){                                                                                             \
     133                        return name##_items_get_index(&map->values, index);                                     \
    130134                }                                                                                                                                               \
    131135        }                                                                                                                                                       \
     
    133137}                                                                                                                                                               \
    134138                                                                                                                                                                \
    135 int name##_initialize( name##_ref map ){                                                                                \
     139int name##_initialize(name##_ref map){                                                                                  \
    136140        ERROR_DECLARE;                                                                                                                          \
    137141                                                                                                                                                                \
    138         if( ! map ) return EINVAL;                                                                                                      \
    139         ERROR_PROPAGATE( char_map_initialize( & map->names ));                                          \
    140         if( ERROR_OCCURRED( name##_items_initialize( & map->values ))){                         \
    141                 char_map_destroy( & map->names );                                                                               \
     142        if(! map){                                                                                                                                      \
     143                return EINVAL;                                                                                                                  \
     144        }                                                                                                                                                       \
     145        ERROR_PROPAGATE(char_map_initialize(&map->names));                                                      \
     146        if(ERROR_OCCURRED(name##_items_initialize(&map->values))){                                      \
     147                char_map_destroy(&map->names);                                                                                  \
    142148                return ERROR_CODE;                                                                                                              \
    143149        }                                                                                                                                                       \
     
    146152}                                                                                                                                                               \
    147153                                                                                                                                                                \
    148 int name##_is_valid( name##_ref map ){                                                                                  \
    149         return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE );                           \
     154int name##_is_valid(name##_ref map){                                                                                    \
     155        return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE);                                     \
    150156}
    151157
  • uspace/srv/net/structures/generic_field.h

    rb5cbff4 r71b00dcc  
    5151 *  @param[in] type Inner object type.
    5252 */
    53 #define GENERIC_FIELD_DECLARE( name, type )                                     \
    54                                                                                 \
    55 typedef struct name             name##_t;                                       \
    56 typedef name##_t *              name##_ref;                                     \
    57                                                                                 \
    58 struct  name{                                                                   \
    59         int     size;                                                           \
    60         int     next;                                                           \
    61         type ** items;                                                          \
    62         int     magic;                                                          \
    63 };                                                                              \
    64                                                                                 \
    65 int     name##_add( name##_ref field, type * value );                           \
    66 int     name##_count( name##_ref field );                                       \
    67 void    name##_destroy( name##_ref field );                                     \
    68 void    name##_exclude_index( name##_ref field, int index );                    \
    69 type ** name##_get_field( name##_ref field );                                   \
    70 type *  name##_get_index( name##_ref field, int index );                        \
    71 int     name##_initialize( name##_ref field );                                  \
    72 int     name##_is_valid( name##_ref field );
     53#define GENERIC_FIELD_DECLARE(name, type)                                                                               \
     54                                                                                                                                                                \
     55typedef struct name             name##_t;                                                                                               \
     56typedef name##_t *              name##_ref;                                                                                             \
     57                                                                                                                                                                \
     58struct  name{                                                                                                                                   \
     59        int size;                                                                                                                                       \
     60        int next;                                                                                                                                       \
     61        type ** items;                                                                                                                          \
     62        int magic;                                                                                                                                      \
     63};                                                                                                                                                              \
     64                                                                                                                                                                \
     65int name##_add(name##_ref field, type * value);                                                                 \
     66int name##_count(name##_ref field);                                                                                             \
     67void name##_destroy(name##_ref field);                                                                                  \
     68void name##_exclude_index(name##_ref field, int index);                                                 \
     69type ** name##_get_field(name##_ref field);                                                                             \
     70type * name##_get_index(name##_ref field, int index);                                                   \
     71int name##_initialize(name##_ref field);                                                                                \
     72int name##_is_valid(name##_ref field);
    7373
    7474/** Generic type field implementation.
     
    7777 *  @param[in] type Inner object type.
    7878 */
    79 #define GENERIC_FIELD_IMPLEMENT( name, type )                                   \
    80                                                                                 \
    81 int name##_add( name##_ref field, type * value ){                               \
    82         if( name##_is_valid( field )){                                          \
    83                 if( field->next == ( field->size - 1 )){                        \
    84                         type ** tmp;                                    \
    85                                                                                 \
    86                         tmp = ( type ** ) realloc( field->items, sizeof( type * ) * 2 * field->size );  \
    87                         if( ! tmp ) return ENOMEM;                              \
    88                         field->size *= 2;                                       \
    89                         field->items = tmp;                                     \
    90                 }                                                               \
    91                 field->items[ field->next ] = value;                            \
    92                 ++ field->next;                                                 \
    93                 field->items[ field->next ] = NULL;                             \
    94                 return field->next - 1;                                         \
    95         }                                                                       \
    96         return EINVAL;                                                          \
    97 }                                                                               \
    98                                                                                 \
    99 int name##_count( name##_ref field ){                                           \
    100         return name##_is_valid( field ) ? field->next : -1;                     \
    101 }                                                                               \
    102                                                                                 \
    103 void name##_destroy( name##_ref field ){                                        \
    104         if( name##_is_valid( field )){                                          \
    105                 int     index;                                                  \
    106                                                                                 \
    107                 field->magic = 0;                                               \
    108                 for( index = 0; index < field->next; ++ index ){                \
    109                         if( field->items[ index ] ) free( field->items[ index ] );                              \
    110                 }                                                               \
    111                 free( field->items );                                           \
    112         }                                                                       \
    113 }                                                                               \
    114                                                                                 \
    115 void name##_exclude_index( name##_ref field, int index ){                       \
    116         if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){        \
    117                 free( field->items[ index ] );                                  \
    118                 field->items[ index ] = NULL;                                   \
    119         }                                                                       \
    120 }                                                                               \
    121                                                                                 \
    122 type * name##_get_index( name##_ref field, int index ){                         \
    123         if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){        \
    124                 return field->items[ index ];                                   \
    125         }                                                                       \
    126         return NULL;                                                            \
    127 }                                                                               \
    128                                                                                 \
    129 type ** name##_get_field( name##_ref field ){                                   \
    130         return name##_is_valid( field ) ? field->items : NULL;                  \
    131 }                                                                               \
    132                                                                                 \
    133 int name##_initialize( name##_ref field ){                                      \
    134         if( ! field ) return EINVAL;                                            \
    135         field->size = 2;                                                        \
    136         field->next = 0;                                                        \
    137         field->items = ( type ** ) malloc( sizeof( type * ) * field->size );    \
    138         if( ! field->items ) return ENOMEM;                                     \
    139         field->items[ field->next ] = NULL;                                     \
    140         field->magic = GENERIC_FIELD_MAGIC_VALUE;                                       \
    141         return EOK;                                                             \
    142 }                                                                               \
    143                                                                                 \
    144 int name##_is_valid( name##_ref field ){                                        \
    145         return field && ( field->magic == GENERIC_FIELD_MAGIC_VALUE );          \
     79#define GENERIC_FIELD_IMPLEMENT(name, type)                                                                             \
     80                                                                                                                                                                \
     81int name##_add(name##_ref field, type * value){                                                                 \
     82        if(name##_is_valid(field)){                                                                                                     \
     83                if(field->next == (field->size - 1)){                                                                   \
     84                        type ** tmp;                                                                                                            \
     85                                                                                                                                                                \
     86                        tmp = (type **) realloc(field->items, sizeof(type *) * 2 * field->size);        \
     87                        if(! tmp){                                                                                                                      \
     88                                return ENOMEM;                                                                                                  \
     89                        }                                                                                                                                       \
     90                        field->size *= 2;                                                                                                       \
     91                        field->items = tmp;                                                                                                     \
     92                }                                                                                                                                               \
     93                field->items[field->next] = value;                                                                              \
     94                ++ field->next;                                                                                                                 \
     95                field->items[field->next] = NULL;                                                                               \
     96                return field->next - 1;                                                                                                 \
     97        }                                                                                                                                                       \
     98        return EINVAL;                                                                                                                          \
     99}                                                                                                                                                               \
     100                                                                                                                                                                \
     101int name##_count(name##_ref field){                                                                                             \
     102        return name##_is_valid(field) ? field->next : -1;                                                       \
     103}                                                                                                                                                               \
     104                                                                                                                                                                \
     105void name##_destroy(name##_ref field){                                                                                  \
     106        if(name##_is_valid(field)){                                                                                                     \
     107                int index;                                                                                                                              \
     108                                                                                                                                                                \
     109                field->magic = 0;                                                                                                               \
     110                for(index = 0; index < field->next; ++ index){                                                  \
     111                        if(field->items[index]){                                                                                        \
     112                                free(field->items[index]);                                                                              \
     113                        }                                                                                                                                       \
     114                }                                                                                                                                               \
     115                free(field->items);                                                                                                             \
     116        }                                                                                                                                                       \
     117}                                                                                                                                                               \
     118                                                                                                                                                                \
     119void name##_exclude_index(name##_ref field, int index){                                                 \
     120        if(name##_is_valid(field) && (index >= 0) && (index < field->next) && (field->items[index])){   \
     121                free(field->items[index]);                                                                                              \
     122                field->items[index] = NULL;                                                                                             \
     123        }                                                                                                                                                       \
     124}                                                                                                                                                               \
     125                                                                                                                                                                \
     126type * name##_get_index(name##_ref field, int index){                                                   \
     127        if(name##_is_valid(field) && (index >= 0) && (index < field->next) && (field->items[index])){   \
     128                return field->items[index];                                                                                             \
     129        }                                                                                                                                                       \
     130        return NULL;                                                                                                                            \
     131}                                                                                                                                                               \
     132                                                                                                                                                                \
     133type ** name##_get_field(name##_ref field){                                                                             \
     134        return name##_is_valid(field) ? field->items : NULL;                                            \
     135}                                                                                                                                                               \
     136                                                                                                                                                                \
     137int name##_initialize(name##_ref field){                                                                                \
     138        if(! field){                                                                                                                            \
     139                return EINVAL;                                                                                                                  \
     140        }                                                                                                                                                       \
     141        field->size = 2;                                                                                                                        \
     142        field->next = 0;                                                                                                                        \
     143        field->items = (type **) malloc(sizeof(type *) * field->size);                          \
     144        if(! field->items){                                                                                                                     \
     145                return ENOMEM;                                                                                                                  \
     146        }                                                                                                                                                       \
     147        field->items[field->next] = NULL;                                                                                       \
     148        field->magic = GENERIC_FIELD_MAGIC_VALUE;                                                                       \
     149        return EOK;                                                                                                                                     \
     150}                                                                                                                                                               \
     151                                                                                                                                                                \
     152int name##_is_valid(name##_ref field){                                                                                  \
     153        return field && (field->magic == GENERIC_FIELD_MAGIC_VALUE);                            \
    146154}
    147155
  • uspace/srv/net/structures/int_map.h

    rb5cbff4 r71b00dcc  
    5555 *  @param[in] type Inner object type.
    5656 */
    57 #define INT_MAP_DECLARE( name, type )                                                                                   \
     57#define INT_MAP_DECLARE(name, type)                                                                                             \
    5858                                                                                                                                                                \
    5959typedef struct name                     name##_t;                                                                                       \
     
    6363                                                                                                                                                                \
    6464struct  name##_item{                                                                                                                    \
    65         int             key;                                                                                                                            \
    66         type *  value;                                                                                                                          \
    67         int             magic;                                                                                                                          \
     65        int key;                                                                                                                                \
     66        type * value;                                                                                                                           \
     67        int magic;                                                                                                                              \
    6868};                                                                                                                                                              \
    6969                                                                                                                                                                \
    7070struct  name{                                                                                                                                   \
    71         int                             size;                                                                                                           \
    72         int                             next;                                                                                                           \
    73         name##_item_ref items;                                                                                                          \
    74         int                             magic;                                                                                                          \
     71        int size;                                                                                                               \
     72        int next;                                                                                                               \
     73        name##_item_ref items;                                                                                                          \
     74        int magic;                                                                                                              \
    7575};                                                                                                                                                              \
    7676                                                                                                                                                                \
    77 int             name##_add( name##_ref map, int key, type * value );                                    \
    78 void    name##_clear( name##_ref map );                                                                                 \
    79 int             name##_count( name##_ref map );                                                                                 \
    80 void    name##_destroy( name##_ref map );                                                                               \
    81 void    name##_exclude( name##_ref map, int key );                                                              \
    82 void    name##_exclude_index( name##_ref map, int index );                                              \
    83 type *  name##_find( name##_ref map, int key );                                                                 \
    84 int             name##_update( name##_ref map, int key, int new_key );                                  \
    85 type *  name##_get_index( name##_ref map, int index );                                                  \
    86 int             name##_initialize( name##_ref map );                                                                    \
    87 int             name##_is_valid( name##_ref map );                                                                              \
    88 void    name##_item_destroy( name##_item_ref item );                                                    \
    89 int             name##_item_is_valid( name##_item_ref item );
     77int name##_add(name##_ref map, int key, type * value);                                                  \
     78void name##_clear(name##_ref map);                                                                                              \
     79int name##_count(name##_ref map);                                                                                               \
     80void name##_destroy(name##_ref map);                                                                                    \
     81void name##_exclude(name##_ref map, int key);                                                                   \
     82void name##_exclude_index(name##_ref map, int index);                                                   \
     83type * name##_find(name##_ref map, int key);                                                                    \
     84int name##_update(name##_ref map, int key, int new_key);                                                \
     85type * name##_get_index(name##_ref map, int index);                                                             \
     86int name##_initialize(name##_ref map);                                                                                  \
     87int name##_is_valid(name##_ref map);                                                                                    \
     88void name##_item_destroy(name##_item_ref item);                                                                 \
     89int name##_item_is_valid(name##_item_ref item);
    9090
    9191/** Integer to generic type map implementation.
     
    9494 *  @param[in] type Inner object type.
    9595 */
    96 #define INT_MAP_IMPLEMENT( name, type )                                                                                 \
    97                                                                                                                                                                 \
    98 int name##_add( name##_ref map, int key, type * value ){                                                \
    99         if( name##_is_valid( map )){                                                                                            \
    100                 if( map->next == ( map->size - 1 )){                                                                    \
    101                         name##_item_ref tmp;                                                                                            \
    102                                                                                                                                                                 \
    103                         tmp = ( name##_item_ref ) realloc( map->items, sizeof( name##_item_t ) * 2 * map->size );       \
    104                         if( ! tmp ) return ENOMEM;                                                                                      \
     96#define INT_MAP_IMPLEMENT(name, type)                                                                                   \
     97                                                                                                                                                                \
     98int name##_add(name##_ref map, int key, type * value){                                                  \
     99        if(name##_is_valid(map)){                                                                                                       \
     100                if(map->next == (map->size - 1)){                                                                               \
     101                        name##_item_ref tmp;                                                                                            \
     102                                                                                                                                                                \
     103                        tmp = (name##_item_ref) realloc(map->items, sizeof(name##_item_t) * 2 * map->size);     \
     104                        if(! tmp){                                                                                                                      \
     105                                return ENOMEM;                                                                                                  \
     106                        }                                                                                                                                       \
    105107                        map->size *= 2;                                                                                                         \
    106108                        map->items = tmp;                                                                                                       \
    107109                }                                                                                                                                               \
    108                 map->items[ map->next ].key = key;                                                                              \
    109                 map->items[ map->next ].value = value;                                                                  \
    110                 map->items[ map->next ].magic = INT_MAP_ITEM_MAGIC_VALUE;                               \
     110                map->items[map->next].key = key;                                                                                \
     111                map->items[map->next].value = value;                                                                    \
     112                map->items[map->next].magic = INT_MAP_ITEM_MAGIC_VALUE;                                 \
    111113                ++ map->next;                                                                                                                   \
    112                 map->items[ map->next ].magic = 0;                                                                              \
     114                map->items[map->next].magic = 0;                                                                                \
    113115                return map->next - 1;                                                                                                   \
    114116        }                                                                                                                                                       \
     
    116118}                                                                                                                                                               \
    117119                                                                                                                                                                \
    118 void name##_clear( name##_ref map ){                                                                                    \
    119         if( name##_is_valid( map )){                                                                                            \
    120                 int     index;                                                                                                                          \
     120void name##_clear(name##_ref map){                                                                                              \
     121        if(name##_is_valid(map)){                                                                                                       \
     122                int index;                                                                                                                              \
    121123                                                                                                                                                                \
    122124/*              map->magic = 0;*/                                                                                                               \
    123                 for( index = 0; index < map->next; ++ index ){                                                  \
    124                         if( name##_item_is_valid( &( map->items[ index ] ))){                           \
    125                                 name##_item_destroy( &( map->items[ index ] ));                                 \
     125                for(index = 0; index < map->next; ++ index){                                                    \
     126                        if(name##_item_is_valid(&(map->items[index]))){                                         \
     127                                name##_item_destroy(&(map->items[index]));                                              \
    126128                        }                                                                                                                                       \
    127129                }                                                                                                                                               \
    128130                map->next = 0;                                                                                                                  \
    129                 map->items[ map->next ].magic = 0;                                                                              \
     131                map->items[map->next].magic = 0;                                                                                \
    130132/*              map->magic = INT_MAP_MAGIC_VALUE;*/                                                                             \
    131133        }                                                                                                                                                       \
    132134}                                                                                                                                                               \
    133135                                                                                                                                                                \
    134 int name##_count( name##_ref map ){                                                                                             \
    135         return name##_is_valid( map ) ? map->next : -1;                                                         \
    136 }                                                                                                                                                               \
    137                                                                                                                                                                 \
    138 void name##_destroy( name##_ref map ){                                                                                  \
    139         if( name##_is_valid( map )){                                                                                            \
    140                 int     index;                                                                                                                          \
     136int name##_count(name##_ref map){                                                                                               \
     137        return name##_is_valid(map) ? map->next : -1;                                                           \
     138}                                                                                                                                                               \
     139                                                                                                                                                                \
     140void name##_destroy(name##_ref map){                                                                                    \
     141        if(name##_is_valid(map)){                                                                                                       \
     142                int index;                                                                                                                              \
    141143                                                                                                                                                                \
    142144                map->magic = 0;                                                                                                                 \
    143                 for( index = 0; index < map->next; ++ index ){                                                  \
    144                         if( name##_item_is_valid( &( map->items[ index ] ))){                           \
    145                                 name##_item_destroy( &( map->items[ index ] ));                                 \
    146                         }                                                                                                                                       \
    147                 }                                                                                                                                               \
    148                 free( map->items );                                                                                                             \
    149         }                                                                                                                                                       \
    150 }                                                                                                                                                               \
    151                                                                                                                                                                 \
    152 void name##_exclude( name##_ref map, int key ){                                                                 \
    153         if( name##_is_valid( map )){                                                                                            \
    154                 int     index;                                                                                                                          \
    155                                                                                                                                                                 \
    156                 for( index = 0; index < map->next; ++ index ){                                                  \
    157                         if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){     \
    158                                 name##_item_destroy( &( map->items[ index ] ));                                 \
    159                         }                                                                                                                                       \
    160                 }                                                                                                                                               \
    161         }                                                                                                                                                       \
    162 }                                                                                                                                                               \
    163                                                                                                                                                                 \
    164 void name##_exclude_index( name##_ref map, int index ){                                                 \
    165         if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){      \
    166                 name##_item_destroy( &( map->items[ index ] ));                                                 \
    167         }                                                                                                                                                       \
    168 }                                                                                                                                                               \
    169                                                                                                                                                                 \
    170 type * name##_find( name##_ref map, int key ){                                                                  \
    171         if( name##_is_valid( map )){                                                                                            \
    172                 int     index;                                                                                                                          \
    173                                                                                                                                                                 \
    174                 for( index = 0; index < map->next; ++ index ){                                                  \
    175                         if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){     \
    176                                 return map->items[ index ].value;                                                               \
     145                for(index = 0; index < map->next; ++ index){                                                    \
     146                        if(name##_item_is_valid(&(map->items[index]))){                                         \
     147                                name##_item_destroy(&(map->items[index]));                                              \
     148                        }                                                                                                                                       \
     149                }                                                                                                                                               \
     150                free(map->items);                                                                                                               \
     151        }                                                                                                                                                       \
     152}                                                                                                                                                               \
     153                                                                                                                                                                \
     154void name##_exclude(name##_ref map, int key){                                                                   \
     155        if(name##_is_valid(map)){                                                                                                       \
     156                int index;                                                                                                                              \
     157                                                                                                                                                                \
     158                for(index = 0; index < map->next; ++ index){                                                    \
     159                        if(name##_item_is_valid(&(map->items[index])) && (map->items[index].key == key)){       \
     160                                name##_item_destroy(&(map->items[index]));                                              \
     161                        }                                                                                                                                       \
     162                }                                                                                                                                               \
     163        }                                                                                                                                                       \
     164}                                                                                                                                                               \
     165                                                                                                                                                                \
     166void name##_exclude_index(name##_ref map, int index){                                                   \
     167        if(name##_is_valid(map) && (index >= 0) && (index < map->next) && name##_item_is_valid(&(map->items[index]))){  \
     168                name##_item_destroy(&(map->items[index]));                                                              \
     169        }                                                                                                                                                       \
     170}                                                                                                                                                               \
     171                                                                                                                                                                \
     172type * name##_find(name##_ref map, int key){                                                                    \
     173        if(name##_is_valid(map)){                                                                                                       \
     174                int index;                                                                                                                              \
     175                                                                                                                                                                \
     176                for(index = 0; index < map->next; ++ index){                                                    \
     177                        if(name##_item_is_valid(&(map->items[index])) && (map->items[index].key == key)){       \
     178                                return map->items[index].value;                                                                 \
    177179                        }                                                                                                                                       \
    178180                }                                                                                                                                               \
     
    181183}                                                                                                                                                               \
    182184                                                                                                                                                                \
    183 int name##_update( name##_ref map, int key, int new_key ){                                              \
    184         if( name##_is_valid( map )){                                                                                            \
    185                 int     index;                                                                                                                          \
    186                                                                                                                                                                 \
    187                 for( index = 0; index < map->next; ++ index ){                                                  \
    188                         if( name##_item_is_valid( &( map->items[ index ] ))){                           \
    189                                 if( map->items[ index ].key == new_key ){                                               \
     185int name##_update(name##_ref map, int key, int new_key){                                                \
     186        if(name##_is_valid(map)){                                                                                                       \
     187                int index;                                                                                                                              \
     188                                                                                                                                                                \
     189                for(index = 0; index < map->next; ++ index){                                                    \
     190                        if(name##_item_is_valid(&(map->items[index]))){                                         \
     191                                if(map->items[index].key == new_key){                                                   \
    190192                                        return EEXIST;                                                                                          \
    191                                 }else if( map->items[ index ].key == key ){                                             \
    192                                         map->items[ index ].key = new_key;                                                      \
     193                                }else if(map->items[index].key == key){                                                 \
     194                                        map->items[index].key = new_key;                                                        \
    193195                                        return EOK;                                                                                                     \
    194196                                }                                                                                                                               \
     
    199201}                                                                                                                                                               \
    200202                                                                                                                                                                \
    201 type * name##_get_index( name##_ref map, int index ){                                                   \
    202         if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){      \
    203                 return map->items[ index ].value;                                                                               \
     203type * name##_get_index(name##_ref map, int index){                                                             \
     204        if(name##_is_valid(map) && (index >= 0) && (index < map->next) && name##_item_is_valid(&(map->items[index]))){  \
     205                return map->items[index].value;                                                                                 \
    204206        }                                                                                                                                                       \
    205207        return NULL;                                                                                                                            \
    206208}                                                                                                                                                               \
    207209                                                                                                                                                                \
    208 int name##_initialize( name##_ref map ){                                                                                \
    209         if( ! map ) return EINVAL;                                                                                                      \
     210int name##_initialize(name##_ref map){                                                                                  \
     211        if(! map){                                                                                                                                      \
     212                return EINVAL;                                                                                                                  \
     213        }                                                                                                                                                       \
    210214        map->size = 2;                                                                                                                          \
    211215        map->next = 0;                                                                                                                          \
    212         map->items = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * map->size ); \
    213         if( ! map->items ) return ENOMEM;                                                                                       \
    214         map->items[ map->next ].magic = 0;                                                                                      \
     216        map->items = (name##_item_ref) malloc(sizeof(name##_item_t) * map->size);       \
     217        if(! map->items){                                                                                                                       \
     218                return ENOMEM;                                                                                                                  \
     219        }                                                                                                                                                       \
     220        map->items[map->next].magic = 0;                                                                                        \
    215221        map->magic = INT_MAP_MAGIC_VALUE;                                                                                       \
    216222        return EOK;                                                                                                                                     \
    217223}                                                                                                                                                               \
    218224                                                                                                                                                                \
    219 int name##_is_valid( name##_ref map ){                                                                                  \
    220         return map && ( map->magic == INT_MAP_MAGIC_VALUE );                                            \
    221 }                                                                                                                                                               \
    222                                                                                                                                                                 \
    223 void name##_item_destroy( name##_item_ref item ){                                                               \
    224         if( name##_item_is_valid( item )){                                                                                      \
     225int name##_is_valid(name##_ref map){                                                                                    \
     226        return map && (map->magic == INT_MAP_MAGIC_VALUE);                                                      \
     227}                                                                                                                                                               \
     228                                                                                                                                                                \
     229void name##_item_destroy(name##_item_ref item){                                                                 \
     230        if(name##_item_is_valid(item)){                                                                                         \
    225231                item->magic = 0;                                                                                                                \
    226                 if( item->value ){                                                                                                              \
    227                         free( item->value );                                                                                            \
     232                if(item->value){                                                                                                                \
     233                        free(item->value);                                                                                                      \
    228234                        item->value = NULL;                                                                                                     \
    229235                }                                                                                                                                               \
     
    231237}                                                                                                                                                               \
    232238                                                                                                                                                                \
    233 int name##_item_is_valid( name##_item_ref item ){                                                               \
    234         return item && ( item->magic == INT_MAP_ITEM_MAGIC_VALUE );                                     \
     239int name##_item_is_valid(name##_item_ref item){                                                                 \
     240        return item && (item->magic == INT_MAP_ITEM_MAGIC_VALUE);                                       \
    235241}
    236242
  • uspace/srv/net/structures/measured_strings.c

    rb5cbff4 r71b00dcc  
    5454 *  @returns NULL if there is not enough memory left.
    5555 */
    56 size_t *        prepare_lengths( const measured_string_ref strings, size_t count );
    57 
    58 measured_string_ref measured_string_create_bulk( const char * string, size_t length ){
    59         measured_string_ref     new;
    60 
    61         if( length == 0 ){
    62                 while( string[ length ] ) ++ length;
    63         }
    64         new = ( measured_string_ref ) malloc( sizeof( measured_string_t ) + ( sizeof( char ) * ( length + 1 )));
    65         if( ! new ) return NULL;
     56size_t * prepare_lengths(const measured_string_ref strings, size_t count);
     57
     58measured_string_ref measured_string_create_bulk(const char * string, size_t length){
     59        measured_string_ref new;
     60
     61        if(length == 0){
     62                while(string[length]){
     63                        ++ length;
     64                }
     65        }
     66        new = (measured_string_ref) malloc(sizeof(measured_string_t) + (sizeof(char) * (length + 1)));
     67        if(! new){
     68                return NULL;
     69        }
    6670        new->length = length;
    67         new->value = (( char * ) new ) + sizeof( measured_string_t );
     71        new->value = ((char *) new) + sizeof(measured_string_t);
    6872        // append terminating zero explicitly - to be safe
    69         memcpy( new->value, string, new->length );
    70         new->value[ new->length ] = '\0';
     73        memcpy(new->value, string, new->length);
     74        new->value[new->length] = '\0';
    7175        return new;
    7276}
    7377
    74 measured_string_ref measured_string_copy( measured_string_ref source ){
    75         measured_string_ref     new;
    76 
    77         if( ! source ) return NULL;
    78         new = ( measured_string_ref ) malloc( sizeof( measured_string_t ));
    79         if( new ){
    80                 new->value = ( char * ) malloc( source->length + 1 );
    81                 if( new->value ){
     78measured_string_ref measured_string_copy(measured_string_ref source){
     79        measured_string_ref new;
     80
     81        if(! source){
     82                return NULL;
     83        }
     84        new = (measured_string_ref) malloc(sizeof(measured_string_t));
     85        if(new){
     86                new->value = (char *) malloc(source->length + 1);
     87                if(new->value){
    8288                        new->length = source->length;
    83                         memcpy( new->value, source->value, new->length );
    84                         new->value[ new->length ] = '\0';
     89                        memcpy(new->value, source->value, new->length);
     90                        new->value[new->length] = '\0';
    8591                        return new;
    8692                }else{
    87                         free( new );
     93                        free(new);
    8894                }
    8995        }
     
    9197}
    9298
    93 int measured_strings_receive( measured_string_ref * strings, char ** data, size_t count ){
    94         ERROR_DECLARE;
    95 
    96         size_t *                lengths;
    97         size_t                  index;
    98         size_t                  length;
    99         char *                  next;
    100         ipc_callid_t    callid;
    101 
    102         if(( ! strings ) || ( ! data ) || ( count <= 0 )){
    103                 return EINVAL;
    104         }
    105         lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 ));
    106         if( ! lengths ) return ENOMEM;
    107         if(( ! async_data_write_receive( & callid, & length ))
    108         || ( length != sizeof( size_t ) * ( count + 1 ))){
    109                 free( lengths );
    110                 return EINVAL;
    111         }
    112         if( ERROR_OCCURRED( async_data_write_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){
    113                 free( lengths );
    114                 return ERROR_CODE;
    115         }
    116         * data = malloc( lengths[ count ] );
    117         if( !( * data )) return ENOMEM;
    118         ( * data )[ lengths[ count ] - 1 ] = '\0';
    119         * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count );
    120         if( !( * strings )){
    121                 free( lengths );
    122                 free( * data );
     99int measured_strings_receive(measured_string_ref * strings, char ** data, size_t count){
     100        ERROR_DECLARE;
     101
     102        size_t * lengths;
     103        size_t index;
     104        size_t length;
     105        char * next;
     106        ipc_callid_t callid;
     107
     108        if((! strings) || (! data) || (count <= 0)){
     109                return EINVAL;
     110        }
     111        lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
     112        if(! lengths){
     113                return ENOMEM;
     114        }
     115        if((! async_data_write_receive(&callid, &length))
     116                || (length != sizeof(size_t) * (count + 1))){
     117                free(lengths);
     118                return EINVAL;
     119        }
     120        if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
     121                free(lengths);
     122                return ERROR_CODE;
     123        }
     124        *data = malloc(lengths[count]);
     125        if(!(*data)){
     126                return ENOMEM;
     127        }
     128        (*data)[lengths[count] - 1] = '\0';
     129        *strings = (measured_string_ref) malloc(sizeof(measured_string_t) * count);
     130        if(!(*strings)){
     131                free(lengths);
     132                free(*data);
    123133                return ENOMEM;
    124134        }
    125135        next = * data;
    126         for( index = 0; index < count; ++ index ){
    127                 ( * strings)[ index ].length = lengths[ index ];
    128                 if( lengths[ index ] > 0 ){
    129                         if(( ! async_data_write_receive( & callid, & length ))
    130                         || ( length != lengths[ index ] )){
    131                                 free( * data );
    132                                 free( * strings );
    133                                 free( lengths );
     136        for(index = 0; index < count; ++ index){
     137                (*strings)[index].length = lengths[index];
     138                if(lengths[index] > 0){
     139                        if((! async_data_write_receive(&callid, &length))
     140                                || (length != lengths[index])){
     141                                free(*data);
     142                                free(*strings);
     143                                free(lengths);
    134144                                return EINVAL;
    135145                        }
    136                         ERROR_PROPAGATE( async_data_write_finalize( callid, next, lengths[ index ] ));
    137                         ( * strings)[ index ].value = next;
    138                         next += lengths[ index ];
    139                         * next = '\0';
     146                        ERROR_PROPAGATE(async_data_write_finalize(callid, next, lengths[index]));
     147                        (*strings)[index].value = next;
     148                        next += lengths[index];
     149                        *next = '\0';
    140150                        ++ next;
    141151                }else{
    142                         ( * strings )[ index ].value = NULL;
    143                 }
    144         }
    145         free( lengths );
    146         return EOK;
    147 }
    148 
    149 int measured_strings_reply( const measured_string_ref strings, size_t count ){
    150         ERROR_DECLARE;
    151 
    152         size_t *                lengths;
    153         size_t                  index;
    154         size_t                  length;
    155         ipc_callid_t    callid;
    156 
    157         if(( ! strings ) || ( count <= 0 )){
    158                 return EINVAL;
    159         }
    160         lengths = prepare_lengths( strings, count );
    161         if( ! lengths ) return ENOMEM;
    162         if(( ! async_data_read_receive( & callid, & length ))
    163         || ( length != sizeof( size_t ) * ( count + 1 ))){
    164                 free( lengths );
    165                 return EINVAL;
    166         }
    167         if( ERROR_OCCURRED( async_data_read_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){
    168                 free( lengths );
    169                 return ERROR_CODE;
    170         }
    171         free( lengths );
    172         for( index = 0; index < count; ++ index ){
    173                 if( strings[ index ].length > 0 ){
    174                         if(( ! async_data_read_receive( & callid, & length ))
    175                         || ( length != strings[ index ].length )){
     152                        (*strings)[index].value = NULL;
     153                }
     154        }
     155        free(lengths);
     156        return EOK;
     157}
     158
     159int measured_strings_reply(const measured_string_ref strings, size_t count){
     160        ERROR_DECLARE;
     161
     162        size_t * lengths;
     163        size_t index;
     164        size_t length;
     165        ipc_callid_t callid;
     166
     167        if((! strings) || (count <= 0)){
     168                return EINVAL;
     169        }
     170        lengths = prepare_lengths(strings, count);
     171        if(! lengths){
     172                return ENOMEM;
     173        }
     174        if((! async_data_read_receive(&callid, &length))
     175                || (length != sizeof(size_t) * (count + 1))){
     176                free(lengths);
     177                return EINVAL;
     178        }
     179        if(ERROR_OCCURRED(async_data_read_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
     180                free(lengths);
     181                return ERROR_CODE;
     182        }
     183        free(lengths);
     184        for(index = 0; index < count; ++ index){
     185                if(strings[index].length > 0){
     186                        if((! async_data_read_receive(&callid, &length))
     187                                || (length != strings[index].length)){
    176188                                return EINVAL;
    177189                        }
    178                         ERROR_PROPAGATE( async_data_read_finalize( callid, strings[ index ].value, strings[ index ].length ));
    179                 }
    180         }
    181         return EOK;
    182 }
    183 
    184 int measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count ){
    185         ERROR_DECLARE;
    186 
    187         size_t *        lengths;
    188         size_t          index;
    189         char *          next;
    190 
    191         if(( phone <= 0 ) || ( ! strings ) || ( ! data ) || ( count <= 0 )){
    192                 return EINVAL;
    193         }
    194         lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 ));
    195         if( ! lengths ) return ENOMEM;
    196         if( ERROR_OCCURRED( async_data_read_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){
    197                 free( lengths );
    198                 return ERROR_CODE;
    199         }
    200         * data = malloc( lengths[ count ] );
    201         if( !( * data )) return ENOMEM;
    202         * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count );
    203         if( !( * strings )){
    204                 free( lengths );
    205                 free( * data );
     190                        ERROR_PROPAGATE(async_data_read_finalize(callid, strings[index].value, strings[index].length));
     191                }
     192        }
     193        return EOK;
     194}
     195
     196int measured_strings_return(int phone, measured_string_ref * strings, char ** data, size_t count){
     197        ERROR_DECLARE;
     198
     199        size_t * lengths;
     200        size_t index;
     201        char * next;
     202
     203        if((phone <= 0) || (! strings) || (! data) || (count <= 0)){
     204                return EINVAL;
     205        }
     206        lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
     207        if(! lengths){
     208                return ENOMEM;
     209        }
     210        if(ERROR_OCCURRED(async_data_read_start(phone, lengths, sizeof(size_t) * (count + 1)))){
     211                free(lengths);
     212                return ERROR_CODE;
     213        }
     214        *data = malloc(lengths[count]);
     215        if(!(*data)){
     216                return ENOMEM;
     217        }
     218        *strings = (measured_string_ref) malloc(sizeof(measured_string_t) * count);
     219        if(!(*strings)){
     220                free(lengths);
     221                free(*data);
    206222                return ENOMEM;
    207223        }
    208224        next = * data;
    209         for( index = 0; index < count; ++ index ){
    210                 ( * strings )[ index ].length = lengths[ index ];
    211                 if( lengths[ index ] > 0 ){
    212                         ERROR_PROPAGATE( async_data_read_start( phone, next, lengths[ index ] ));
    213                         ( * strings )[ index ].value = next;
    214                         next += lengths[ index ];
    215                         * next = '\0';
     225        for(index = 0; index < count; ++ index){
     226                (*strings)[index].length = lengths[index];
     227                if(lengths[index] > 0){
     228                        ERROR_PROPAGATE(async_data_read_start(phone, next, lengths[index]));
     229                        (*strings)[index].value = next;
     230                        next += lengths[index];
     231                        *next = '\0';
    216232                        ++ next;
    217233                }else{
    218                         ( * strings )[ index ].value = NULL;
    219                 }
    220         }
    221         free( lengths );
    222         return EOK;
    223 }
    224 
    225 int measured_strings_send( int phone, const measured_string_ref strings, size_t count ){
    226         ERROR_DECLARE;
    227 
    228         size_t *        lengths;
    229         size_t          index;
    230 
    231         if(( phone <= 0 ) || ( ! strings ) || ( count <= 0 )){
    232                 return EINVAL;
    233         }
    234         lengths = prepare_lengths( strings, count );
    235         if( ! lengths ) return ENOMEM;
    236         if( ERROR_OCCURRED( async_data_write_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){
    237                 free( lengths );
    238                 return ERROR_CODE;
    239         }
    240         free( lengths );
    241         for( index = 0; index < count; ++ index ){
    242                 if( strings[ index ].length > 0 ){
    243                         ERROR_PROPAGATE( async_data_write_start( phone, strings[ index ].value, strings[ index ].length ));
    244                 }
    245         }
    246         return EOK;
    247 }
    248 
    249 size_t * prepare_lengths( const measured_string_ref strings, size_t count ){
    250         size_t *        lengths;
    251         size_t          index;
    252         size_t          length;
    253 
    254         lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 ));
    255         if( ! lengths ) return NULL;
     234                        (*strings)[index].value = NULL;
     235                }
     236        }
     237        free(lengths);
     238        return EOK;
     239}
     240
     241int measured_strings_send(int phone, const measured_string_ref strings, size_t count){
     242        ERROR_DECLARE;
     243
     244        size_t * lengths;
     245        size_t index;
     246
     247        if((phone <= 0) || (! strings) || (count <= 0)){
     248                return EINVAL;
     249        }
     250        lengths = prepare_lengths(strings, count);
     251        if(! lengths){
     252                return ENOMEM;
     253        }
     254        if(ERROR_OCCURRED(async_data_write_start(phone, lengths, sizeof(size_t) * (count + 1)))){
     255                free(lengths);
     256                return ERROR_CODE;
     257        }
     258        free(lengths);
     259        for(index = 0; index < count; ++ index){
     260                if(strings[index].length > 0){
     261                        ERROR_PROPAGATE(async_data_write_start(phone, strings[index].value, strings[index].length));
     262                }
     263        }
     264        return EOK;
     265}
     266
     267size_t * prepare_lengths(const measured_string_ref strings, size_t count){
     268        size_t * lengths;
     269        size_t index;
     270        size_t length;
     271
     272        lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
     273        if(! lengths){
     274                return NULL;
     275        }
    256276        length = 0;
    257         for( index = 0; index < count; ++ index ){
    258                 lengths[ index ] = strings[ index ].length;
    259                 length += lengths[ index ] + 1;
    260         }
    261         lengths[ count ] = length;
     277        for(index = 0; index < count; ++ index){
     278                lengths[index] = strings[index].length;
     279                length += lengths[index] + 1;
     280        }
     281        lengths[count] = length;
    262282        return lengths;
    263283}
  • uspace/srv/net/structures/measured_strings.h

    rb5cbff4 r71b00dcc  
    5757        /** Character string data.
    5858         */
    59         char *  value;
     59        char * value;
    6060        /** Character string length.
    6161         */
    62         size_t  length;
     62        size_t length;
    6363};
    6464
     
    7171 *  @returns NULL if there is not enough memory left.
    7272 */
    73 measured_string_ref     measured_string_create_bulk( const char * string, size_t length );
     73measured_string_ref measured_string_create_bulk(const char * string, size_t length);
    7474
    7575/** Copies the given measured string with separated header and data parts.
     
    7979 *  @returns NULL if there is not enough memory left.
    8080 */
    81 measured_string_ref     measured_string_copy( measured_string_ref source );
     81measured_string_ref measured_string_copy(measured_string_ref source);
    8282
    8383/** Receives a&nbsp;measured strings array from a&nbsp;calling module.
     
    9595 *  @returns Other error codes as defined for the async_data_write_finalize() function.
    9696 */
    97 int     measured_strings_receive( measured_string_ref * strings, char ** data, size_t count );
     97int measured_strings_receive(measured_string_ref * strings, char ** data, size_t count);
    9898
    9999/** Replies the given measured strings array to a&nbsp;calling module.
     
    108108 *  @returns Other error codes as defined for the async_data_read_finalize() function.
    109109 */
    110 int     measured_strings_reply( const measured_string_ref strings, size_t count );
     110int measured_strings_reply(const measured_string_ref strings, size_t count);
    111111
    112112/** Receives a&nbsp;measured strings array from another module.
     
    124124 *  @returns Other error codes as defined for the async_data_read_start() function.
    125125 */
    126 int     measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count );
     126int measured_strings_return(int phone, measured_string_ref * strings, char ** data, size_t count);
    127127
    128128/** Sends the given measured strings array to another module.
     
    136136 *  @returns Other error codes as defined for the async_data_write_start() function.
    137137 */
    138 int     measured_strings_send( int phone, const measured_string_ref strings, size_t count );
     138int measured_strings_send(int phone, const measured_string_ref strings, size_t count);
    139139
    140140#endif
  • uspace/srv/net/structures/module_map.c

    rb5cbff4 r71b00dcc  
    4747#include "module_map.h"
    4848
    49 GENERIC_CHAR_MAP_IMPLEMENT( modules, module_t )
     49GENERIC_CHAR_MAP_IMPLEMENT(modules, module_t)
    5050
    51 int add_module( module_ref * module, modules_ref modules, const char * name, const char * filename, services_t service, task_id_t task_id, connect_module_t connect_module ){
     51int add_module(module_ref * module, modules_ref modules, const char * name, const char * filename, services_t service, task_id_t task_id, connect_module_t connect_module){
    5252        ERROR_DECLARE;
    5353
    54         module_ref      tmp_module;
     54        module_ref tmp_module;
    5555
    56         tmp_module = ( module_ref ) malloc( sizeof( module_t ));
    57         if( ! tmp_module ) return ENOMEM;
     56        tmp_module = (module_ref) malloc(sizeof(module_t));
     57        if(! tmp_module){
     58                return ENOMEM;
     59        }
    5860        tmp_module->task_id = task_id;
    5961        tmp_module->phone = 0;
     
    6365        tmp_module->service = service;
    6466        tmp_module->connect_module = connect_module;
    65         if( ERROR_OCCURRED( modules_add( modules, tmp_module->name, 0, tmp_module ))){
    66                 free( tmp_module );
     67        if(ERROR_OCCURRED(modules_add(modules, tmp_module->name, 0, tmp_module))){
     68                free(tmp_module);
    6769                return ERROR_CODE;
    6870        }
    69         if( module ) * module = tmp_module;
     71        if(module){
     72                *module = tmp_module;
     73        }
    7074        return EOK;
    7175}
    7276
    73 module_ref get_running_module( modules_ref modules, char * name ){
    74         module_ref      module;
     77module_ref get_running_module(modules_ref modules, char * name){
     78        module_ref module;
    7579
    76         module = modules_find( modules, name, 0 );
    77         if( ! module ) return NULL;
    78         if( ! module->task_id ){
    79                 module->task_id = spawn( module->filename );
    80                 if( ! module->task_id ) return NULL;
     80        module = modules_find(modules, name, 0);
     81        if(! module){
     82                return NULL;
    8183        }
    82         if( ! module->phone ){
    83                 module->phone = module->connect_module( module->service );
     84        if(! module->task_id){
     85                module->task_id = spawn(module->filename);
     86                if(! module->task_id){
     87                        return NULL;
     88                }
     89        }
     90        if(! module->phone){
     91                module->phone = module->connect_module(module->service);
    8492        }
    8593        return module;
    8694}
    8795
    88 task_id_t spawn( const char * fname ){
    89         const char * argv[ 2 ];
    90         task_id_t       res;
     96task_id_t spawn(const char * fname){
     97        const char * argv[2];
     98        task_id_t res;
    9199
    92         argv[ 0 ] = fname;
    93         argv[ 1 ] = NULL;
    94         res = task_spawn( fname, argv );
     100        argv[0] = fname;
     101        argv[1] = NULL;
     102        res = task_spawn(fname, argv);
    95103
    96104        return res;
  • uspace/srv/net/structures/module_map.h

    rb5cbff4 r71b00dcc  
    6060 *  @see generic_char_map.h
    6161 */
    62 GENERIC_CHAR_MAP_DECLARE( modules, module_t )
     62GENERIC_CHAR_MAP_DECLARE(modules, module_t)
    6363
    6464/** Module structure.
     
    6767        /** Module task identifier if running.
    6868         */
    69         task_id_t       task_id;
     69        task_id_t task_id;
    7070        /** Module service identifier.
    7171         */
    72         services_t      service;
     72        services_t service;
    7373        /** Module phone if running and connected.
    7474         */
    75         int                     phone;
     75        int phone;
    7676        /** Usage counter.
    7777         */
    78         int                     usage;
     78        int usage;
    7979        /** Module name.
    8080         */
    81         const char *            name;
     81        const char * name;
    8282        /** Module full path filename.
    8383         */
    84         const char *            filename;
     84        const char * filename;
    8585        /** Connecting function.
    8686         */
    87         connect_module_t *      connect_module;
     87        connect_module_t * connect_module;
    8888};
    8989
     
    9999 *  @returns ENOMEM if there is not enough memory left.
    100100 */
    101 int     add_module( module_ref * module, modules_ref modules, const char * name, const char * filename, services_t service, task_id_t task_id, connect_module_t * connect_module );
     101int add_module(module_ref * module, modules_ref modules, const char * name, const char * filename, services_t service, task_id_t task_id, connect_module_t * connect_module);
    102102
    103103/** Searches and returns the specified module.
     
    109109 *  @returns NULL if there is no such module.
    110110 */
    111 module_ref      get_running_module( modules_ref modules, char * name );
     111module_ref get_running_module(modules_ref modules, char * name);
    112112
    113113/** Starts the given module.
     
    116116 *  @returns 0 if there is no such module.
    117117 */
    118 task_id_t       spawn( const char * fname );
     118task_id_t spawn(const char * fname);
    119119
    120120#endif
  • 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
  • uspace/srv/net/structures/packet/packet.h

    rb5cbff4 r71b00dcc  
    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

    rb5cbff4 r71b00dcc  
    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

    rb5cbff4 r71b00dcc  
    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

    rb5cbff4 r71b00dcc  
    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

    rb5cbff4 r71b00dcc  
    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

    rb5cbff4 r71b00dcc  
    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

    rb5cbff4 r71b00dcc  
    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

    rb5cbff4 r71b00dcc  
    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.