Subversion Repositories HelenOS

Rev

Rev 4243 | Rev 4704 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4243 Rev 4576
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup net
29
/** @addtogroup net
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#ifndef __NET_INT_MAP_H__
36
#ifndef __NET_INT_MAP_H__
37
#define __NET_INT_MAP_H__
37
#define __NET_INT_MAP_H__
38
 
38
 
39
#include <errno.h>
39
#include <errno.h>
40
#include <malloc.h>
40
#include <malloc.h>
41
#include <mem.h>
41
#include <mem.h>
-
 
42
#include <unistd.h>
42
 
43
 
43
#define INT_MAP_MAGIC_VALUE         0x11223344
44
#define INT_MAP_MAGIC_VALUE         0x11223344
44
#define INT_MAP_ITEM_MAGIC_VALUE    0x55667788
45
#define INT_MAP_ITEM_MAGIC_VALUE    0x55667788
45
 
46
 
46
#define INT_MAP_DECLARE( name, type )                                           \
47
#define INT_MAP_DECLARE( name, type )                                           \
47
                                                                                \
48
                                                                                \
48
typedef struct name         name##_t;                                           \
49
typedef struct name         name##_t;                                           \
49
typedef name##_t *          name##_ref;                                         \
50
typedef name##_t *          name##_ref;                                         \
50
typedef struct name##_item  name##_item_t;                                      \
51
typedef struct name##_item  name##_item_t;                                      \
51
typedef name##_item_t *     name##_item_ref;                                    \
52
typedef name##_item_t *     name##_item_ref;                                    \
52
                                                                                \
53
                                                                                \
53
struct  name##_item{                                                            \
54
struct  name##_item{                                                            \
54
    int     key;                                                                \
55
    int     key;                                                                \
55
    type *  value;                                                              \
56
    type *  value;                                                              \
56
    int     magic;                                                              \
57
    int     magic;                                                              \
57
};                                                                              \
58
};                                                                              \
58
                                                                                \
59
                                                                                \
59
struct  name{                                                                   \
60
struct  name{                                                                   \
60
    size_t          size;                                                       \
61
    size_t          size;                                                       \
61
    int             next;                                                       \
62
    int             next;                                                       \
62
    name##_item_ref items;                                                      \
63
    name##_item_ref items;                                                      \
63
    int             magic;                                                      \
64
    int             magic;                                                      \
64
};                                                                              \
65
};                                                                              \
65
                                                                                \
66
                                                                                \
66
int     name##_add( name##_ref map, int key, type * value );                    \
67
int     name##_add( name##_ref map, int key, type * value );                    \
67
void    name##_clear( name##_ref map );                                         \
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 ) realloc( map->items, sizeof( name##_item_t ) * 2 * map->size );   \
87
            tmp = ( name##_item_ref ) realloc( map->items, 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
            map->items = tmp;                                                   \
90
            map->items = tmp;                                                   \
90
        }                                                                       \
91
        }                                                                       \
91
        map->items[ map->next ].key = key;                                      \
92
        map->items[ map->next ].key = key;                                      \
92
        map->items[ map->next ].value = value;                                  \
93
        map->items[ map->next ].value = value;                                  \
93
        map->items[ map->next ].magic = INT_MAP_ITEM_MAGIC_VALUE;               \
94
        map->items[ map->next ].magic = INT_MAP_ITEM_MAGIC_VALUE;               \
94
        ++ map->next;                                                           \
95
        ++ map->next;                                                           \
95
        map->items[ map->next ].magic = 0;                                      \
96
        map->items[ map->next ].magic = 0;                                      \
96
        return map->next - 1;                                                   \
97
        return map->next - 1;                                                   \
97
    }                                                                           \
98
    }                                                                           \
98
    return EINVAL;                                                              \
99
    return EINVAL;                                                              \
99
}                                                                               \
100
}                                                                               \
100
                                                                                \
101
                                                                                \
101
void name##_clear( name##_ref map ){                                            \
102
void name##_clear( name##_ref map ){                                            \
102
    if( name##_is_valid( map )){                                                \
103
    if( name##_is_valid( map )){                                                \
103
        int index;                                                              \
104
        int index;                                                              \
104
                                                                                \
105
                                                                                \
105
/*      map->magic = 0;*/                                                       \
106
/*      map->magic = 0;*/                                                       \
106
        for( index = 0; index < map->next; ++ index ){                          \
107
        for( index = 0; index < map->next; ++ index ){                          \
107
            if( name##_item_is_valid( &( map->items[ index ] ))){               \
108
            if( name##_item_is_valid( &( map->items[ index ] ))){               \
108
                name##_item_destroy( &( map->items[ index ] ));                 \
109
                name##_item_destroy( &( map->items[ index ] ));                 \
109
            }                                                                   \
110
            }                                                                   \
110
        }                                                                       \
111
        }                                                                       \
111
        map->next = 0;                                                          \
112
        map->next = 0;                                                          \
112
        map->items[ map->next ].magic = 0;                                      \
113
        map->items[ map->next ].magic = 0;                                      \
113
/*      map->magic = INT_MAP_MAGIC_VALUE;*/                                     \
114
/*      map->magic = INT_MAP_MAGIC_VALUE;*/                                     \
114
    }                                                                           \
115
    }                                                                           \
115
}                                                                               \
116
}                                                                               \
116
                                                                                \
117
                                                                                \
117
int name##_count( name##_ref map ){                                             \
118
int name##_count( name##_ref map ){                                             \
118
    return name##_is_valid( map ) ? map->next : -1;                             \
119
    return name##_is_valid( map ) ? map->next : -1;                             \
119
}                                                                               \
120
}                                                                               \
120
                                                                                \
121
                                                                                \
121
void name##_destroy( name##_ref map ){                                          \
122
void name##_destroy( name##_ref map ){                                          \
122
    if( name##_is_valid( map )){                                                \
123
    if( name##_is_valid( map )){                                                \
123
        int index;                                                              \
124
        int index;                                                              \
124
                                                                                \
125
                                                                                \
125
        map->magic = 0;                                                         \
126
        map->magic = 0;                                                         \
126
        for( index = 0; index < map->next; ++ index ){                          \
127
        for( index = 0; index < map->next; ++ index ){                          \
127
            if( name##_item_is_valid( &( map->items[ index ] ))){               \
128
            if( name##_item_is_valid( &( map->items[ index ] ))){               \
128
                name##_item_destroy( &( map->items[ index ] ));                 \
129
                name##_item_destroy( &( map->items[ index ] ));                 \
129
            }                                                                   \
130
            }                                                                   \
130
        }                                                                       \
131
        }                                                                       \
131
        free( map->items );                                                     \
132
        free( map->items );                                                     \
132
    }                                                                           \
133
    }                                                                           \
133
}                                                                               \
134
}                                                                               \
134
                                                                                \
135
                                                                                \
135
void name##_exclude( name##_ref map, int key ){                                 \
136
void name##_exclude( name##_ref map, int key ){                                 \
136
    if( name##_is_valid( map )){                                                \
137
    if( name##_is_valid( map )){                                                \
137
        int index;                                                              \
138
        int index;                                                              \
138
                                                                                \
139
                                                                                \
139
        for( index = 0; index < map->next; ++ index ){                          \
140
        for( index = 0; index < map->next; ++ index ){                          \
140
            if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
141
            if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
141
                name##_item_destroy( &( map->items[ index ] ));                 \
142
                name##_item_destroy( &( map->items[ index ] ));                 \
142
            }                                                                   \
143
            }                                                                   \
143
        }                                                                       \
144
        }                                                                       \
144
    }                                                                           \
145
    }                                                                           \
145
}                                                                               \
146
}                                                                               \
146
                                                                                \
147
                                                                                \
147
void name##_exclude_index( name##_ref map, int index ){                         \
148
void name##_exclude_index( name##_ref map, int index ){                         \
148
    if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){  \
149
    if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){  \
149
        name##_item_destroy( &( map->items[ index ] ));                         \
150
        name##_item_destroy( &( map->items[ index ] ));                         \
150
    }                                                                           \
151
    }                                                                           \
151
}                                                                               \
152
}                                                                               \
152
                                                                                \
153
                                                                                \
153
type * name##_find( name##_ref map, int key ){                                  \
154
type * name##_find( name##_ref map, int key ){                                  \
154
    if( name##_is_valid( map )){                                                \
155
    if( name##_is_valid( map )){                                                \
155
        int index;                                                              \
156
        int index;                                                              \
156
                                                                                \
157
                                                                                \
157
        for( index = 0; index < map->next; ++ index ){                          \
158
        for( index = 0; index < map->next; ++ index ){                          \
158
            if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
159
            if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
159
                return map->items[ index ].value;                               \
160
                return map->items[ index ].value;                               \
160
            }                                                                   \
161
            }                                                                   \
161
        }                                                                       \
162
        }                                                                       \
162
    }                                                                           \
163
    }                                                                           \
163
    return NULL;                                                                \
164
    return NULL;                                                                \
164
}                                                                               \
165
}                                                                               \
165
                                                                                \
166
                                                                                \
166
type * name##_get_index( name##_ref map, int index ){                           \
167
type * name##_get_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
    if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){  \
168
        return map->items[ index ].value;                                       \
169
        return map->items[ index ].value;                                       \
169
    }                                                                           \
170
    }                                                                           \
170
    return NULL;                                                                \
171
    return NULL;                                                                \
171
}                                                                               \
172
}                                                                               \
172
                                                                                \
173
                                                                                \
173
int name##_initialize( name##_ref map ){                                        \
174
int name##_initialize( name##_ref map ){                                        \
174
    if( ! map ) return EINVAL;                                                  \
175
    if( ! map ) return EINVAL;                                                  \
175
    map->size = 2;                                                              \
176
    map->size = 2;                                                              \
176
    map->next = 0;                                                              \
177
    map->next = 0;                                                              \
177
    map->items = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * map->size ); \
178
    map->items = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * map->size ); \
178
    if( ! map->items ) return ENOMEM;                                           \
179
    if( ! map->items ) return ENOMEM;                                           \
179
    map->items[ map->next ].magic = 0;                                          \
180
    map->items[ map->next ].magic = 0;                                          \
180
    map->magic = INT_MAP_MAGIC_VALUE;                                           \
181
    map->magic = INT_MAP_MAGIC_VALUE;                                           \
181
    return EOK;                                                                 \
182
    return EOK;                                                                 \
182
}                                                                               \
183
}                                                                               \
183
                                                                                \
184
                                                                                \
184
int name##_is_valid( name##_ref map ){                                          \
185
int name##_is_valid( name##_ref map ){                                          \
185
    return map && ( map->magic == INT_MAP_MAGIC_VALUE );                        \
186
    return map && ( map->magic == INT_MAP_MAGIC_VALUE );                        \
186
}                                                                               \
187
}                                                                               \
187
                                                                                \
188
                                                                                \
188
void name##_item_destroy( name##_item_ref item ){                               \
189
void name##_item_destroy( name##_item_ref item ){                               \
189
    if( name##_item_is_valid( item )){                                          \
190
    if( name##_item_is_valid( item )){                                          \
190
        item->magic = 0;                                                        \
191
        item->magic = 0;                                                        \
191
        if( item->value ){                                                      \
192
        if( item->value ){                                                      \
192
            free( item->value );                                                \
193
            free( item->value );                                                \
193
            item->value = NULL;                                                 \
194
            item->value = NULL;                                                 \
194
        }                                                                       \
195
        }                                                                       \
195
    }                                                                           \
196
    }                                                                           \
196
}                                                                               \
197
}                                                                               \
197
                                                                                \
198
                                                                                \
198
int name##_item_is_valid( name##_item_ref item ){                               \
199
int name##_item_is_valid( name##_item_ref item ){                               \
199
    return item && ( item->magic == INT_MAP_ITEM_MAGIC_VALUE );                 \
200
    return item && ( item->magic == INT_MAP_ITEM_MAGIC_VALUE );                 \
200
}
201
}
201
 
202
 
202
#endif
203
#endif
203
 
204
 
204
/** @}
205
/** @}
205
 */
206
 */
206
 
207
 
207
 
208