Rev 4243 | Rev 4704 | 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> |
||
| 4192 | mejdrech | 41 | #include <mem.h> |
| 4576 | mejdrech | 42 | #include <unistd.h> |
| 3666 | mejdrech | 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{ \ |
||
| 4243 | mejdrech | 61 | size_t size; \ |
| 3846 | mejdrech | 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 | \ |
||
| 4197 | mejdrech | 87 | tmp = ( name##_item_ref ) realloc( map->items, sizeof( name##_item_t ) * 2 * map->size ); \ |
| 3846 | mejdrech | 88 | if( ! tmp ) return ENOMEM; \ |
| 89 | map->size *= 2; \ |
||
| 90 | map->items = tmp; \ |
||
| 91 | } \ |
||
| 92 | map->items[ map->next ].key = key; \ |
||
| 93 | map->items[ map->next ].value = value; \ |
||
| 94 | map->items[ map->next ].magic = INT_MAP_ITEM_MAGIC_VALUE; \ |
||
| 95 | ++ map->next; \ |
||
| 96 | map->items[ map->next ].magic = 0; \ |
||
| 97 | return map->next - 1; \ |
||
| 98 | } \ |
||
| 99 | return EINVAL; \ |
||
| 100 | } \ |
||
| 101 | \ |
||
| 102 | void name##_clear( name##_ref map ){ \ |
||
| 103 | if( name##_is_valid( map )){ \ |
||
| 104 | int index; \ |
||
| 105 | \ |
||
| 106 | /* map->magic = 0;*/ \ |
||
| 107 | for( index = 0; index < map->next; ++ index ){ \ |
||
| 108 | if( name##_item_is_valid( &( map->items[ index ] ))){ \ |
||
| 109 | name##_item_destroy( &( map->items[ index ] )); \ |
||
| 110 | } \ |
||
| 111 | } \ |
||
| 112 | map->next = 0; \ |
||
| 113 | map->items[ map->next ].magic = 0; \ |
||
| 114 | /* map->magic = INT_MAP_MAGIC_VALUE;*/ \ |
||
| 115 | } \ |
||
| 116 | } \ |
||
| 117 | \ |
||
| 118 | int name##_count( name##_ref map ){ \ |
||
| 119 | return name##_is_valid( map ) ? map->next : -1; \ |
||
| 120 | } \ |
||
| 121 | \ |
||
| 122 | void name##_destroy( name##_ref map ){ \ |
||
| 123 | if( name##_is_valid( map )){ \ |
||
| 124 | int index; \ |
||
| 125 | \ |
||
| 126 | map->magic = 0; \ |
||
| 127 | for( index = 0; index < map->next; ++ index ){ \ |
||
| 128 | if( name##_item_is_valid( &( map->items[ index ] ))){ \ |
||
| 129 | name##_item_destroy( &( map->items[ index ] )); \ |
||
| 130 | } \ |
||
| 131 | } \ |
||
| 132 | free( map->items ); \ |
||
| 133 | } \ |
||
| 134 | } \ |
||
| 135 | \ |
||
| 136 | void name##_exclude( name##_ref map, int key ){ \ |
||
| 137 | if( name##_is_valid( map )){ \ |
||
| 138 | int index; \ |
||
| 139 | \ |
||
| 140 | for( index = 0; index < map->next; ++ index ){ \ |
||
| 3666 | mejdrech | 141 | if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \ |
| 3846 | mejdrech | 142 | name##_item_destroy( &( map->items[ index ] )); \ |
| 143 | } \ |
||
| 144 | } \ |
||
| 145 | } \ |
||
| 146 | } \ |
||
| 147 | \ |
||
| 148 | void name##_exclude_index( name##_ref map, int index ){ \ |
||
| 3666 | mejdrech | 149 | if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){ \ |
| 3846 | mejdrech | 150 | name##_item_destroy( &( map->items[ index ] )); \ |
| 151 | } \ |
||
| 152 | } \ |
||
| 153 | \ |
||
| 154 | type * name##_find( name##_ref map, int key ){ \ |
||
| 155 | if( name##_is_valid( map )){ \ |
||
| 156 | int index; \ |
||
| 157 | \ |
||
| 158 | for( index = 0; index < map->next; ++ index ){ \ |
||
| 3666 | mejdrech | 159 | if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \ |
| 3846 | mejdrech | 160 | return map->items[ index ].value; \ |
| 161 | } \ |
||
| 162 | } \ |
||
| 163 | } \ |
||
| 164 | return NULL; \ |
||
| 165 | } \ |
||
| 166 | \ |
||
| 167 | type * name##_get_index( name##_ref map, int index ){ \ |
||
| 3666 | mejdrech | 168 | if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){ \ |
| 3846 | mejdrech | 169 | return map->items[ index ].value; \ |
| 170 | } \ |
||
| 171 | return NULL; \ |
||
| 172 | } \ |
||
| 173 | \ |
||
| 174 | int name##_initialize( name##_ref map ){ \ |
||
| 175 | if( ! map ) return EINVAL; \ |
||
| 176 | map->size = 2; \ |
||
| 177 | map->next = 0; \ |
||
| 3666 | mejdrech | 178 | map->items = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * map->size ); \ |
| 3846 | mejdrech | 179 | if( ! map->items ) return ENOMEM; \ |
| 180 | map->items[ map->next ].magic = 0; \ |
||
| 181 | map->magic = INT_MAP_MAGIC_VALUE; \ |
||
| 182 | return EOK; \ |
||
| 183 | } \ |
||
| 184 | \ |
||
| 185 | int name##_is_valid( name##_ref map ){ \ |
||
| 186 | return map && ( map->magic == INT_MAP_MAGIC_VALUE ); \ |
||
| 187 | } \ |
||
| 188 | \ |
||
| 189 | void name##_item_destroy( name##_item_ref item ){ \ |
||
| 190 | if( name##_item_is_valid( item )){ \ |
||
| 191 | item->magic = 0; \ |
||
| 192 | if( item->value ){ \ |
||
| 193 | free( item->value ); \ |
||
| 194 | item->value = NULL; \ |
||
| 195 | } \ |
||
| 196 | } \ |
||
| 197 | } \ |
||
| 198 | \ |
||
| 199 | int name##_item_is_valid( name##_item_ref item ){ \ |
||
| 200 | return item && ( item->magic == INT_MAP_ITEM_MAGIC_VALUE ); \ |
||
| 3666 | mejdrech | 201 | } |
| 202 | |||
| 203 | #endif |
||
| 204 | |||
| 205 | /** @} |
||
| 206 | */ |
||
| 207 |