source: mainline/uspace/srv/net/structures/int_map.h@ ede63e4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ede63e4 was ede63e4, checked in by Lukas Mejdrech <lukasmejdrech@…>, 15 years ago
  • socket identifier generation moved to libsocket, + data fragment size fix and enhancement, + [ICMP|TCP|UDP]_HEADER_SIZE definition, + int_map_update() function to alter item key
  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[21580dd]1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup net
30 * @{
31 */
32
33/** @file
34 * Integer to generic type map.
35 */
36
37#ifndef __NET_INT_MAP_H__
38#define __NET_INT_MAP_H__
39
40#include <errno.h>
41#include <malloc.h>
42#include <mem.h>
43#include <unistd.h>
44
45/** Internal magic value for a&nbsp;map consistency check.
46 */
47#define INT_MAP_MAGIC_VALUE 0x11223344
48
49/** Internal magic value for an item consistency check.
50 */
51#define INT_MAP_ITEM_MAGIC_VALUE 0x55667788
52
53/** Integer to generic type map declaration.
54 * @param[in] name Name of the map.
55 * @param[in] type Inner object type.
56 */
57#define INT_MAP_DECLARE( name, type ) \
58 \
59typedef struct name name##_t; \
60typedef name##_t * name##_ref; \
61typedef struct name##_item name##_item_t; \
62typedef name##_item_t * name##_item_ref; \
63 \
64struct name##_item{ \
65 int key; \
66 type * value; \
67 int magic; \
68}; \
69 \
70struct name{ \
71 int size; \
72 int next; \
73 name##_item_ref items; \
74 int magic; \
75}; \
76 \
77int name##_add( name##_ref map, int key, type * value ); \
78void name##_clear( name##_ref map ); \
79int name##_count( name##_ref map ); \
80void name##_destroy( name##_ref map ); \
81void name##_exclude( name##_ref map, int key ); \
82void name##_exclude_index( name##_ref map, int index ); \
83type * name##_find( name##_ref map, int key ); \
[ede63e4]84int name##_update( name##_ref map, int key, int new_key ); \
[21580dd]85type * name##_get_index( name##_ref map, int index ); \
86int name##_initialize( name##_ref map ); \
87int name##_is_valid( name##_ref map ); \
88void name##_item_destroy( name##_item_ref item ); \
89int name##_item_is_valid( name##_item_ref item );
90
91/** Integer to generic type map implementation.
92 * Should follow declaration with the same parameters.
93 * @param[in] name Name of the map.
94 * @param[in] type Inner object type.
95 */
96#define INT_MAP_IMPLEMENT( name, type ) \
97 \
98int name##_add( name##_ref map, int key, type * value ){ \
99 if( name##_is_valid( map )){ \
100 if( map->next == ( map->size - 1 )){ \
101 name##_item_ref tmp; \
102 \
103 tmp = ( name##_item_ref ) realloc( map->items, sizeof( name##_item_t ) * 2 * map->size ); \
104 if( ! tmp ) return ENOMEM; \
105 map->size *= 2; \
106 map->items = tmp; \
107 } \
108 map->items[ map->next ].key = key; \
109 map->items[ map->next ].value = value; \
110 map->items[ map->next ].magic = INT_MAP_ITEM_MAGIC_VALUE; \
111 ++ map->next; \
112 map->items[ map->next ].magic = 0; \
113 return map->next - 1; \
114 } \
115 return EINVAL; \
116} \
117 \
118void name##_clear( name##_ref map ){ \
119 if( name##_is_valid( map )){ \
120 int index; \
121 \
122/* map->magic = 0;*/ \
123 for( index = 0; index < map->next; ++ index ){ \
124 if( name##_item_is_valid( &( map->items[ index ] ))){ \
125 name##_item_destroy( &( map->items[ index ] )); \
126 } \
127 } \
128 map->next = 0; \
129 map->items[ map->next ].magic = 0; \
130/* map->magic = INT_MAP_MAGIC_VALUE;*/ \
131 } \
132} \
133 \
134int name##_count( name##_ref map ){ \
135 return name##_is_valid( map ) ? map->next : -1; \
136} \
137 \
138void name##_destroy( name##_ref map ){ \
139 if( name##_is_valid( map )){ \
140 int index; \
141 \
142 map->magic = 0; \
143 for( index = 0; index < map->next; ++ index ){ \
144 if( name##_item_is_valid( &( map->items[ index ] ))){ \
145 name##_item_destroy( &( map->items[ index ] )); \
146 } \
147 } \
148 free( map->items ); \
149 } \
150} \
151 \
152void name##_exclude( name##_ref map, int key ){ \
153 if( name##_is_valid( map )){ \
154 int index; \
155 \
156 for( index = 0; index < map->next; ++ index ){ \
157 if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
158 name##_item_destroy( &( map->items[ index ] )); \
159 } \
160 } \
161 } \
162} \
163 \
164void name##_exclude_index( name##_ref map, int index ){ \
165 if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){ \
166 name##_item_destroy( &( map->items[ index ] )); \
167 } \
168} \
169 \
170type * name##_find( name##_ref map, int key ){ \
171 if( name##_is_valid( map )){ \
172 int index; \
173 \
174 for( index = 0; index < map->next; ++ index ){ \
175 if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
176 return map->items[ index ].value; \
177 } \
178 } \
179 } \
180 return NULL; \
181} \
182 \
[ede63e4]183int name##_update( name##_ref map, int key, int new_key ){ \
184 if( name##_is_valid( map )){ \
185 int index; \
186 \
187 for( index = 0; index < map->next; ++ index ){ \
188 if( name##_item_is_valid( &( map->items[ index ] ))){ \
189 if( map->items[ index ].key == new_key ){ \
190 return EEXIST; \
191 }else if( map->items[ index ].key == key ){ \
192 map->items[ index ].key = new_key; \
193 return EOK; \
194 } \
195 } \
196 } \
197 } \
198 return ENOENT; \
199} \
200 \
[21580dd]201type * name##_get_index( name##_ref map, int index ){ \
202 if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){ \
203 return map->items[ index ].value; \
204 } \
205 return NULL; \
206} \
207 \
208int name##_initialize( name##_ref map ){ \
209 if( ! map ) return EINVAL; \
210 map->size = 2; \
211 map->next = 0; \
212 map->items = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * map->size ); \
213 if( ! map->items ) return ENOMEM; \
214 map->items[ map->next ].magic = 0; \
215 map->magic = INT_MAP_MAGIC_VALUE; \
216 return EOK; \
217} \
218 \
219int name##_is_valid( name##_ref map ){ \
220 return map && ( map->magic == INT_MAP_MAGIC_VALUE ); \
221} \
222 \
223void name##_item_destroy( name##_item_ref item ){ \
224 if( name##_item_is_valid( item )){ \
225 item->magic = 0; \
226 if( item->value ){ \
227 free( item->value ); \
228 item->value = NULL; \
229 } \
230 } \
231} \
232 \
233int name##_item_is_valid( name##_item_ref item ){ \
234 return item && ( item->magic == INT_MAP_ITEM_MAGIC_VALUE ); \
235}
236
237#endif
238
239/** @}
240 */
241
Note: See TracBrowser for help on using the repository browser.