Subversion Repositories HelenOS

Rev

Rev 3846 | Rev 3912 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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