Rev 3912 | Rev 4197 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3912 | Rev 4192 | ||
---|---|---|---|
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 | */ |
34 | */ |
35 | 35 | ||
36 | #ifndef __GENERIC_FIELD_H__ |
36 | #ifndef __GENERIC_FIELD_H__ |
37 | #define __GENERIC_FIELD_H__ |
37 | #define __GENERIC_FIELD_H__ |
38 | 38 | ||
39 | #include <errno.h> |
39 | #include <errno.h> |
40 | #include <malloc.h> |
40 | #include <malloc.h> |
41 | #include <string.h> |
41 | #include <mem.h> |
42 | #include <unistd.h> |
42 | #include <unistd.h> |
43 | 43 | ||
44 | #define GENERIC_FIELD_MAGIC_VALUE 0x55667788 |
44 | #define GENERIC_FIELD_MAGIC_VALUE 0x55667788 |
45 | 45 | ||
46 | #define GENERIC_FIELD_DECLARE( name, type ) \ |
46 | #define GENERIC_FIELD_DECLARE( name, type ) \ |
47 | \ |
47 | \ |
48 | typedef struct name name##_t; \ |
48 | typedef struct name name##_t; \ |
49 | typedef name##_t * name##_ref; \ |
49 | typedef name##_t * name##_ref; \ |
50 | \ |
50 | \ |
51 | struct name{ \ |
51 | struct name{ \ |
52 | int size; \ |
52 | int size; \ |
53 | int next; \ |
53 | int next; \ |
54 | type ** items; \ |
54 | type ** items; \ |
55 | int magic; \ |
55 | int magic; \ |
56 | }; \ |
56 | }; \ |
57 | \ |
57 | \ |
58 | int name##_add( name##_ref field, type * value ); \ |
58 | int name##_add( name##_ref field, type * value ); \ |
59 | int name##_count( name##_ref field ); \ |
59 | int name##_count( name##_ref field ); \ |
60 | void name##_destroy( name##_ref field ); \ |
60 | void name##_destroy( name##_ref field ); \ |
61 | void name##_exclude_index( name##_ref field, int index ); \ |
61 | void name##_exclude_index( name##_ref field, int index ); \ |
62 | type ** name##_get_field( name##_ref field ); \ |
62 | type ** name##_get_field( name##_ref field ); \ |
63 | type * name##_get_index( name##_ref field, int index ); \ |
63 | type * name##_get_index( name##_ref field, int index ); \ |
64 | int name##_initialize( name##_ref field ); \ |
64 | int name##_initialize( name##_ref field ); \ |
65 | int name##_is_valid( name##_ref field ); |
65 | int name##_is_valid( name##_ref field ); |
66 | 66 | ||
67 | #define GENERIC_FIELD_IMPLEMENT( name, type ) \ |
67 | #define GENERIC_FIELD_IMPLEMENT( name, type ) \ |
68 | \ |
68 | \ |
69 | int name##_add( name##_ref field, type * value ){ \ |
69 | int name##_add( name##_ref field, type * value ){ \ |
70 | if( name##_is_valid( field )){ \ |
70 | if( name##_is_valid( field )){ \ |
71 | if( field->next == ( field->size - 1 )){ \ |
71 | if( field->next == ( field->size - 1 )){ \ |
72 | type ** tmp; \ |
72 | type ** tmp; \ |
73 | \ |
73 | \ |
74 | tmp = ( type ** ) malloc( sizeof( type * ) * 2 * field->size ); \ |
74 | tmp = ( type ** ) malloc( sizeof( type * ) * 2 * field->size ); \ |
75 | if( ! tmp ) return ENOMEM; \ |
75 | if( ! tmp ) return ENOMEM; \ |
76 | field->size *= 2; \ |
76 | field->size *= 2; \ |
77 | memcpy( tmp, field->items, sizeof( type * ) * field->next ); \ |
77 | memcpy( tmp, field->items, sizeof( type * ) * field->next ); \ |
78 | free( field->items ); \ |
78 | free( field->items ); \ |
79 | field->items = tmp; \ |
79 | field->items = tmp; \ |
80 | } \ |
80 | } \ |
81 | field->items[ field->next ] = value; \ |
81 | field->items[ field->next ] = value; \ |
82 | ++ field->next; \ |
82 | ++ field->next; \ |
83 | field->items[ field->next ] = NULL; \ |
83 | field->items[ field->next ] = NULL; \ |
84 | return field->next - 1; \ |
84 | return field->next - 1; \ |
85 | } \ |
85 | } \ |
86 | return EINVAL; \ |
86 | return EINVAL; \ |
87 | } \ |
87 | } \ |
88 | \ |
88 | \ |
89 | int name##_count( name##_ref field ){ \ |
89 | int name##_count( name##_ref field ){ \ |
90 | return name##_is_valid( field ) ? field->next : -1; \ |
90 | return name##_is_valid( field ) ? field->next : -1; \ |
91 | } \ |
91 | } \ |
92 | \ |
92 | \ |
93 | void name##_destroy( name##_ref field ){ \ |
93 | void name##_destroy( name##_ref field ){ \ |
94 | if( name##_is_valid( field )){ \ |
94 | if( name##_is_valid( field )){ \ |
95 | int index; \ |
95 | int index; \ |
96 | \ |
96 | \ |
97 | field->magic = 0; \ |
97 | field->magic = 0; \ |
98 | for( index = 0; index < field->next; ++ index ){ \ |
98 | for( index = 0; index < field->next; ++ index ){ \ |
99 | free( field->items[ index ] ); \ |
99 | free( field->items[ index ] ); \ |
100 | } \ |
100 | } \ |
101 | free( field->items ); \ |
101 | free( field->items ); \ |
102 | } \ |
102 | } \ |
103 | } \ |
103 | } \ |
104 | \ |
104 | \ |
105 | void name##_exclude_index( name##_ref field, int index ){ \ |
105 | void name##_exclude_index( name##_ref field, int index ){ \ |
106 | if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ |
106 | if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ |
107 | free( field->items[ index ] ); \ |
107 | free( field->items[ index ] ); \ |
108 | field->items[ index ] = NULL; \ |
108 | field->items[ index ] = NULL; \ |
109 | } \ |
109 | } \ |
110 | } \ |
110 | } \ |
111 | \ |
111 | \ |
112 | type * name##_get_index( name##_ref field, int index ){ \ |
112 | type * name##_get_index( name##_ref field, int index ){ \ |
113 | if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ |
113 | if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ |
114 | return field->items[ index ]; \ |
114 | return field->items[ index ]; \ |
115 | } \ |
115 | } \ |
116 | return NULL; \ |
116 | return NULL; \ |
117 | } \ |
117 | } \ |
118 | \ |
118 | \ |
119 | type ** name##_get_field( name##_ref field ){ \ |
119 | type ** name##_get_field( name##_ref field ){ \ |
120 | return name##_is_valid( field ) ? field->items : NULL; \ |
120 | return name##_is_valid( field ) ? field->items : NULL; \ |
121 | } \ |
121 | } \ |
122 | \ |
122 | \ |
123 | int name##_initialize( name##_ref field ){ \ |
123 | int name##_initialize( name##_ref field ){ \ |
124 | if( ! field ) return EINVAL; \ |
124 | if( ! field ) return EINVAL; \ |
125 | field->size = 2; \ |
125 | field->size = 2; \ |
126 | field->next = 0; \ |
126 | field->next = 0; \ |
127 | field->items = ( type ** ) malloc( sizeof( type * ) * field->size ); \ |
127 | field->items = ( type ** ) malloc( sizeof( type * ) * field->size ); \ |
128 | if( ! field->items ) return ENOMEM; \ |
128 | if( ! field->items ) return ENOMEM; \ |
129 | field->items[ field->next ] = NULL; \ |
129 | field->items[ field->next ] = NULL; \ |
130 | field->magic = GENERIC_FIELD_MAGIC_VALUE; \ |
130 | field->magic = GENERIC_FIELD_MAGIC_VALUE; \ |
131 | return EOK; \ |
131 | return EOK; \ |
132 | } \ |
132 | } \ |
133 | \ |
133 | \ |
134 | int name##_is_valid( name##_ref field ){ \ |
134 | int name##_is_valid( name##_ref field ){ \ |
135 | return field && ( field->magic == GENERIC_FIELD_MAGIC_VALUE ); \ |
135 | return field && ( field->magic == GENERIC_FIELD_MAGIC_VALUE ); \ |
136 | } |
136 | } |
137 | 137 | ||
138 | #endif |
138 | #endif |
139 | 139 | ||
140 | /** @} |
140 | /** @} |
141 | */ |
141 | */ |
142 | 142 | ||
143 | 143 |