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