Changeset 7715994 in mainline for uspace/srv/net/structures/int_map.h


Ignore:
Timestamp:
2010-03-13T12:17:02Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6ba20a6b
Parents:
d0febca (diff), 2070570 (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 mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/structures/int_map.h

    rd0febca r7715994  
    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
Note: See TracChangeset for help on using the changeset viewer.