Subversion Repositories HelenOS

Rev

Rev 3666 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3666 Rev 3846
Line 39... Line 39...
39
 
39
 
40
#include <errno.h>
40
#include <errno.h>
41
#include <malloc.h>
41
#include <malloc.h>
42
#include <string.h>
42
#include <string.h>
43
 
43
 
44
#define INT_MAP_MAGIC_VALUE     0x11223344
44
#define INT_MAP_MAGIC_VALUE         0x11223344
45
#define INT_MAP_ITEM_MAGIC_VALUE    0x55667788
45
#define INT_MAP_ITEM_MAGIC_VALUE    0x55667788
46
 
46
 
47
#define INT_MAP_DECLARE( name, type )                       \
47
#define INT_MAP_DECLARE( name, type )                                           \
48
                                        \
48
                                                                                \
49
typedef struct name     name##_t;                   \
49
typedef struct name         name##_t;                                           \
50
typedef name##_t *      name##_ref;                 \
50
typedef name##_t *          name##_ref;                                         \
51
typedef struct name##_item  name##_item_t;                  \
51
typedef struct name##_item  name##_item_t;                                      \
52
typedef name##_item_t *     name##_item_ref;                \
52
typedef name##_item_t *     name##_item_ref;                                    \
53
                                        \
53
                                                                                \
54
struct  name##_item{                                \
54
struct  name##_item{                                                            \
55
    int key;                                \
55
    int     key;                                                                \
56
    type *  value;                              \
56
    type *  value;                                                              \
57
    int magic;                              \
57
    int     magic;                                                              \
58
};                                      \
58
};                                                                              \
59
                                        \
59
                                                                                \
60
struct  name{                                   \
60
struct  name{                                                                   \
61
    int         size;                       \
61
    int             size;                                                       \
62
    int         next;                       \
62
    int             next;                                                       \
63
    name##_item_ref     items;                      \
63
    name##_item_ref items;                                                      \
64
    int magic;                              \
64
    int             magic;                                                      \
65
};                                      \
65
};                                                                              \
66
                                        \
66
                                                                                \
67
int name##_add( name##_ref map, int key, type * value );            \
67
int     name##_add( name##_ref map, int key, type * value );                    \
-
 
68
void    name##_clear( name##_ref map );                                         \
68
int name##_count( name##_ref map );                     \
69
int     name##_count( name##_ref map );                                         \
69
void    name##_destroy( name##_ref map );                   \
70
void    name##_destroy( name##_ref map );                                       \
70
void    name##_exclude( name##_ref map, int key );              \
71
void    name##_exclude( name##_ref map, int key );                              \
71
void    name##_exclude_index( name##_ref map, int index );          \
72
void    name##_exclude_index( name##_ref map, int index );                      \
72
type *  name##_find( name##_ref map, int key );                 \
73
type *  name##_find( name##_ref map, int key );                                 \
73
type *  name##_get_index( name##_ref map, int index );              \
74
type *  name##_get_index( name##_ref map, int index );                          \
74
int name##_initialize( name##_ref map );                    \
75
int     name##_initialize( name##_ref map );                                    \
75
int name##_is_valid( name##_ref map );                  \
76
int     name##_is_valid( name##_ref map );                                      \
76
void    name##_item_destroy( name##_item_ref item );                \
77
void    name##_item_destroy( name##_item_ref item );                            \
77
int name##_item_is_valid( name##_item_ref item );
78
int     name##_item_is_valid( name##_item_ref item );
78
 
79
 
79
#define INT_MAP_IMPLEMENT( name, type )                     \
80
#define INT_MAP_IMPLEMENT( name, type )                                         \
80
                                        \
81
                                                                                \
81
int name##_add( name##_ref map, int key, type * value ){            \
82
int name##_add( name##_ref map, int key, type * value ){                        \
82
    if( name##_is_valid( map )){                        \
83
    if( name##_is_valid( map )){                                                \
83
        if( map->next == ( map->size - 1 )){                \
84
        if( map->next == ( map->size - 1 )){                                    \
84
            name##_item_ref tmp;                    \
85
            name##_item_ref tmp;                                                \
85
                                        \
86
                                                                                \
86
            tmp = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * 2 * map->size );    \
87
            tmp = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * 2 * map->size );    \
87
            if( ! tmp ) return ENOMEM;              \
88
            if( ! tmp ) return ENOMEM;                                          \
88
            map->size *= 2;                     \
89
            map->size *= 2;                                                     \
89
            memcpy( tmp, map->items, sizeof( name##_item_t ) * map->next ); \
90
            memcpy( tmp, map->items, sizeof( name##_item_t ) * map->next );     \
90
            free( map->items );                 \
91
            free( map->items );                                                 \
91
            map->items = tmp;                   \
92
            map->items = tmp;                                                   \
92
        }                               \
93
        }                                                                       \
93
        map->items[ map->next ].key = key;              \
94
        map->items[ map->next ].key = key;                                      \
94
        map->items[ map->next ].value = value;              \
95
        map->items[ map->next ].value = value;                                  \
95
        map->items[ map->next ].magic = INT_MAP_ITEM_MAGIC_VALUE;   \
96
        map->items[ map->next ].magic = INT_MAP_ITEM_MAGIC_VALUE;               \
96
        ++ map->next;                           \
97
        ++ map->next;                                                           \
97
        map->items[ map->next ].magic = 0;              \
98
        map->items[ map->next ].magic = 0;                                      \
98
        return map->next - 1;                       \
99
        return map->next - 1;                                                   \
99
    }                                   \
100
    }                                                                           \
100
    return EINVAL;                              \
101
    return EINVAL;                                                              \
-
 
102
}                                                                               \
-
 
103
                                                                                \
-
 
104
void name##_clear( name##_ref map ){                                            \
-
 
105
    if( name##_is_valid( map )){                                                \
-
 
106
        int index;                                                              \
-
 
107
                                                                                \
-
 
108
/*      map->magic = 0;*/                                                       \
-
 
109
        for( index = 0; index < map->next; ++ index ){                          \
-
 
110
            if( name##_item_is_valid( &( map->items[ index ] ))){               \
-
 
111
                name##_item_destroy( &( map->items[ index ] ));                 \
-
 
112
            }                                                                   \
-
 
113
        }                                                                       \
-
 
114
        map->next = 0;                                                          \
-
 
115
        map->items[ map->next ].magic = 0;                                      \
-
 
116
/*      map->magic = INT_MAP_MAGIC_VALUE;*/                                     \
-
 
117
    }                                                                           \
101
}                                       \
118
}                                                                               \
102
                                        \
119
                                                                                \
103
int name##_count( name##_ref map ){                     \
120
int name##_count( name##_ref map ){                                             \
104
    return name##_is_valid( map ) ? map->next : -1;             \
121
    return name##_is_valid( map ) ? map->next : -1;                             \
105
}                                       \
122
}                                                                               \
106
                                        \
123
                                                                                \
107
void name##_destroy( name##_ref map ){                      \
124
void name##_destroy( name##_ref map ){                                          \
108
    if( name##_is_valid( map )){                        \
125
    if( name##_is_valid( map )){                                                \
109
        int index;                          \
126
        int index;                                                              \
110
                                        \
127
                                                                                \
111
        map->magic = 0;                         \
128
        map->magic = 0;                                                         \
112
        for( index = 0; index < map->next; ++ index ){          \
129
        for( index = 0; index < map->next; ++ index ){                          \
113
            if( name##_item_is_valid( &( map->items[ index ] ))){   \
130
            if( name##_item_is_valid( &( map->items[ index ] ))){               \
114
                name##_item_destroy( &( map->items[ index ] )); \
131
                name##_item_destroy( &( map->items[ index ] ));                 \
115
            }                           \
132
            }                                                                   \
116
        }                               \
133
        }                                                                       \
117
        free( map->items );                     \
134
        free( map->items );                                                     \
118
    }                                   \
135
    }                                                                           \
119
}                                       \
136
}                                                                               \
120
                                        \
137
                                                                                \
121
void name##_exclude( name##_ref map, int key ){                 \
138
void name##_exclude( name##_ref map, int key ){                                 \
122
    if( name##_is_valid( map )){                        \
139
    if( name##_is_valid( map )){                                                \
123
        int index;                          \
140
        int index;                                                              \
124
                                        \
141
                                                                                \
125
        for( index = 0; index < map->next; ++ index ){          \
142
        for( index = 0; index < map->next; ++ index ){                          \
126
            if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
143
            if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
127
                name##_item_destroy( &( map->items[ index ] )); \
144
                name##_item_destroy( &( map->items[ index ] ));                 \
128
            }                           \
145
            }                                                                   \
129
        }                               \
146
        }                                                                       \
130
    }                                   \
147
    }                                                                           \
131
}                                       \
148
}                                                                               \
132
                                        \
149
                                                                                \
133
void name##_exclude_index( name##_ref map, int index ){             \
150
void name##_exclude_index( name##_ref map, int index ){                         \
134
    if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){  \
151
    if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){  \
135
        name##_item_destroy( &( map->items[ index ] ));         \
152
        name##_item_destroy( &( map->items[ index ] ));                         \
136
    }                                   \
153
    }                                                                           \
137
}                                       \
154
}                                                                               \
138
                                        \
155
                                                                                \
139
type * name##_find( name##_ref map, int key ){                  \
156
type * name##_find( name##_ref map, int key ){                                  \
140
    if( name##_is_valid( map )){                        \
157
    if( name##_is_valid( map )){                                                \
141
        int index;                          \
158
        int index;                                                              \
142
                                        \
159
                                                                                \
143
        for( index = 0; index < map->next; ++ index ){          \
160
        for( index = 0; index < map->next; ++ index ){                          \
144
            if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
161
            if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
145
                return map->items[ index ].value;       \
162
                return map->items[ index ].value;                               \
146
            }                           \
163
            }                                                                   \
147
        }                               \
164
        }                                                                       \
148
    }                                   \
165
    }                                                                           \
149
    return NULL;                                \
166
    return NULL;                                                                \
150
}                                       \
167
}                                                                               \
151
                                        \
168
                                                                                \
152
type * name##_get_index( name##_ref map, int index ){               \
169
type * name##_get_index( name##_ref map, int index ){                           \
153
    if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){  \
170
    if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){  \
154
        return map->items[ index ].value;               \
171
        return map->items[ index ].value;                                       \
155
    }                                   \
172
    }                                                                           \
156
    return NULL;                                \
173
    return NULL;                                                                \
157
}                                       \
174
}                                                                               \
158
                                        \
175
                                                                                \
159
int name##_initialize( name##_ref map ){                    \
176
int name##_initialize( name##_ref map ){                                        \
160
    if( ! map ) return EINVAL;                      \
177
    if( ! map ) return EINVAL;                                                  \
161
    map->size = 2;                              \
178
    map->size = 2;                                                              \
162
    map->next = 0;                              \
179
    map->next = 0;                                                              \
163
    map->items = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * map->size ); \
180
    map->items = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * map->size ); \
164
    if( ! map->items ) return ENOMEM;                   \
181
    if( ! map->items ) return ENOMEM;                                           \
165
    map->items[ map->next ].magic = 0;                  \
182
    map->items[ map->next ].magic = 0;                                          \
166
    map->magic = INT_MAP_MAGIC_VALUE;                   \
183
    map->magic = INT_MAP_MAGIC_VALUE;                                           \
167
    return EOK;                             \
184
    return EOK;                                                                 \
168
}                                       \
185
}                                                                               \
169
                                        \
186
                                                                                \
170
int name##_is_valid( name##_ref map ){                      \
187
int name##_is_valid( name##_ref map ){                                          \
171
    return map && ( map->magic == INT_MAP_MAGIC_VALUE );            \
188
    return map && ( map->magic == INT_MAP_MAGIC_VALUE );                        \
172
}                                       \
189
}                                                                               \
173
                                        \
190
                                                                                \
174
void name##_item_destroy( name##_item_ref item ){               \
191
void name##_item_destroy( name##_item_ref item ){                               \
175
    if( name##_item_is_valid( item )){                  \
192
    if( name##_item_is_valid( item )){                                          \
176
        item->magic = 0;                        \
193
        item->magic = 0;                                                        \
177
        if( item->value ){                      \
194
        if( item->value ){                                                      \
178
            free( item->value );                    \
195
            free( item->value );                                                \
179
            item->value = NULL;                 \
196
            item->value = NULL;                                                 \
180
        }                               \
197
        }                                                                       \
181
    }                                   \
198
    }                                                                           \
182
}                                       \
199
}                                                                               \
183
                                        \
200
                                                                                \
184
int name##_item_is_valid( name##_item_ref item ){               \
201
int name##_item_is_valid( name##_item_ref item ){                               \
185
    return item && ( item->magic == INT_MAP_ITEM_MAGIC_VALUE );     \
202
    return item && ( item->magic == INT_MAP_ITEM_MAGIC_VALUE );                 \
186
}
203
}
187
 
204
 
188
#endif
205
#endif
189
 
206
 
190
/** @}
207
/** @}