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