Subversion Repositories HelenOS

Rev

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

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