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