Changeset 71b00dcc in mainline for uspace/srv/net/structures
- Timestamp:
- 2010-03-07T22:51:38Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 60ab6c3
- Parents:
- b5cbff4 (diff), 31c80a5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- uspace/srv/net/structures
- Files:
-
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/structures/char_map.c
rb5cbff4 r71b00dcc 57 57 * @returns EEXIST if the key character string is already used. 58 58 */ 59 int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value);59 int char_map_add_item(char_map_ref map, const char * identifier, size_t length, const int value); 60 60 61 61 /** Returns the node assigned to the key from the map. … … 66 66 * @returns NULL if the key is not assigned a node. 67 67 */ 68 char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, const size_t length);68 char_map_ref char_map_find_node(const char_map_ref map, const char * identifier, const size_t length); 69 69 70 70 /** Returns the value assigned to the map. … … 73 73 * @returns CHAR_MAP_NULL if the map is not assigned a value. 74 74 */ 75 int char_map_get_value( const char_map_ref map);75 int char_map_get_value(const char_map_ref map); 76 76 77 77 /** Checks if the map is valid. … … 80 80 * @returns FALSE otherwise. 81 81 */ 82 int char_map_is_valid( const char_map_ref map);83 84 int char_map_add( char_map_ref map, const char * identifier, size_t length, const int value){85 if( char_map_is_valid( map ) && ( identifier ) && (( length ) || ( * identifier))){86 int 87 88 for( index = 0; index < map->next; ++ index){89 if( map->items[ index ]->c == * identifier){82 int char_map_is_valid(const char_map_ref map); 83 84 int char_map_add(char_map_ref map, const char * identifier, size_t length, const int value){ 85 if(char_map_is_valid(map) && (identifier) && ((length) || (*identifier))){ 86 int index; 87 88 for(index = 0; index < map->next; ++ index){ 89 if(map->items[index]->c == * identifier){ 90 90 ++ identifier; 91 if(( length > 1 ) || (( length == 0 ) && ( * identifier))){92 return char_map_add( map->items[ index ], identifier, length ? length - 1 : 0, value);91 if((length > 1) || ((length == 0) && (*identifier))){ 92 return char_map_add(map->items[index], identifier, length ? length - 1 : 0, value); 93 93 }else{ 94 if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS; 95 map->items[ index ]->value = value; 94 if(map->items[index]->value != CHAR_MAP_NULL){ 95 return EEXISTS; 96 } 97 map->items[index]->value = value; 96 98 return EOK; 97 99 } 98 100 } 99 101 } 100 return char_map_add_item( map, identifier, length, value);102 return char_map_add_item(map, identifier, length, value); 101 103 } 102 104 return EINVAL; 103 105 } 104 106 105 int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value ){ 106 if( map->next == ( map->size - 1 )){ 107 char_map_ref * tmp; 108 109 tmp = ( char_map_ref * ) realloc( map->items, sizeof( char_map_ref ) * 2 * map->size ); 110 if( ! tmp ) return ENOMEM; 107 int char_map_add_item(char_map_ref map, const char * identifier, size_t length, const int value){ 108 if(map->next == (map->size - 1)){ 109 char_map_ref *tmp; 110 111 tmp = (char_map_ref *) realloc(map->items, sizeof(char_map_ref) * 2 * map->size); 112 if(! tmp){ 113 return ENOMEM; 114 } 111 115 map->size *= 2; 112 116 map->items = tmp; 113 117 } 114 map->items[ map->next ] = ( char_map_ref ) malloc( sizeof( char_map_t )); 115 if( ! map->items[ map->next ] ) return ENOMEM; 116 if( char_map_initialize( map->items[ map->next ] ) != EOK ){ 117 free( map->items[ map->next ] ); 118 map->items[ map->next ] = NULL; 118 map->items[map->next] = (char_map_ref) malloc(sizeof(char_map_t)); 119 if(! map->items[map->next]){ 119 120 return ENOMEM; 120 121 } 121 map->items[ map->next ]->c = * identifier; 122 if(char_map_initialize(map->items[map->next]) != EOK){ 123 free(map->items[map->next]); 124 map->items[map->next] = NULL; 125 return ENOMEM; 126 } 127 map->items[map->next]->c = * identifier; 122 128 ++ identifier; 123 129 ++ map->next; 124 if(( length > 1 ) || (( length == 0 ) && ( * identifier))){125 map->items[ map->next - 1]->value = CHAR_MAP_NULL;126 return char_map_add_item( map->items[ map->next - 1 ], identifier, length ? length - 1 : 0, value);130 if((length > 1) || ((length == 0) && (*identifier))){ 131 map->items[map->next - 1]->value = CHAR_MAP_NULL; 132 return char_map_add_item(map->items[map->next - 1], identifier, length ? length - 1 : 0, value); 127 133 }else{ 128 map->items[ map->next - 1]->value = value;134 map->items[map->next - 1]->value = value; 129 135 } 130 136 return EOK; 131 137 } 132 138 133 void char_map_destroy( char_map_ref map){134 if( char_map_is_valid( map)){135 int 139 void char_map_destroy(char_map_ref map){ 140 if(char_map_is_valid(map)){ 141 int index; 136 142 137 143 map->magic = 0; 138 for( index = 0; index < map->next; ++ index){139 char_map_destroy( map->items[ index ]);140 } 141 free( map->items);144 for(index = 0; index < map->next; ++ index){ 145 char_map_destroy(map->items[index]); 146 } 147 free(map->items); 142 148 map->items = NULL; 143 149 } 144 150 } 145 151 146 int char_map_exclude( char_map_ref map, const char * identifier, size_t length){147 char_map_ref 148 149 node = char_map_find_node( map, identifier, length);150 if( node){151 int 152 int char_map_exclude(char_map_ref map, const char * identifier, size_t length){ 153 char_map_ref node; 154 155 node = char_map_find_node(map, identifier, length); 156 if(node){ 157 int value; 152 158 153 159 value = node->value; … … 158 164 } 159 165 160 int char_map_find( const char_map_ref map, const char * identifier, size_t length){161 char_map_ref 162 163 node = char_map_find_node( map, identifier, length);166 int char_map_find(const char_map_ref map, const char * identifier, size_t length){ 167 char_map_ref node; 168 169 node = char_map_find_node(map, identifier, length); 164 170 return node ? node->value : CHAR_MAP_NULL; 165 171 } 166 172 167 char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, size_t length ){ 168 if( ! char_map_is_valid( map )) return NULL; 169 if( length || ( * identifier )){ 170 int index; 171 172 for( index = 0; index < map->next; ++ index ){ 173 if( map->items[ index ]->c == * identifier ){ 173 char_map_ref char_map_find_node(const char_map_ref map, const char * identifier, size_t length){ 174 if(! char_map_is_valid(map)){ 175 return NULL; 176 } 177 if(length || (*identifier)){ 178 int index; 179 180 for(index = 0; index < map->next; ++ index){ 181 if(map->items[index]->c == * identifier){ 174 182 ++ identifier; 175 if( length == 1 ) return map->items[ index ]; 176 return char_map_find_node( map->items[ index ], identifier, length ? length - 1 : 0 ); 183 if(length == 1){ 184 return map->items[index]; 185 } 186 return char_map_find_node(map->items[index], identifier, length ? length - 1 : 0); 177 187 } 178 188 } … … 182 192 } 183 193 184 int char_map_get_value( const char_map_ref map ){ 185 return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL; 186 } 187 188 int char_map_initialize( char_map_ref map ){ 189 if( ! map ) return EINVAL; 194 int char_map_get_value(const char_map_ref map){ 195 return char_map_is_valid(map) ? map->value : CHAR_MAP_NULL; 196 } 197 198 int char_map_initialize(char_map_ref map){ 199 if(! map){ 200 return EINVAL; 201 } 190 202 map->c = '\0'; 191 203 map->value = CHAR_MAP_NULL; 192 204 map->size = 2; 193 205 map->next = 0; 194 map->items = malloc( sizeof( char_map_ref ) * map->size);195 if( ! map->items){206 map->items = malloc(sizeof(char_map_ref) * map->size); 207 if(! map->items){ 196 208 map->magic = 0; 197 209 return ENOMEM; 198 210 } 199 map->items[ map->next] = NULL;211 map->items[map->next] = NULL; 200 212 map->magic = CHAR_MAP_MAGIC_VALUE; 201 213 return EOK; 202 214 } 203 215 204 int char_map_is_valid( const char_map_ref map){205 return map && ( map->magic == CHAR_MAP_MAGIC_VALUE);206 } 207 208 int char_map_update( char_map_ref map, const char * identifier, const size_t length, const int value){209 char_map_ref 210 211 // if( ! char_map_is_valid( map)) return EINVAL;212 node = char_map_find_node( map, identifier, length);213 if( node){216 int char_map_is_valid(const char_map_ref map){ 217 return map && (map->magic == CHAR_MAP_MAGIC_VALUE); 218 } 219 220 int char_map_update(char_map_ref map, const char * identifier, const size_t length, const int value){ 221 char_map_ref node; 222 223 // if(! char_map_is_valid(map)) return EINVAL; 224 node = char_map_find_node(map, identifier, length); 225 if(node){ 214 226 node->value = value; 215 227 return EOK; 216 228 }else{ 217 return char_map_add( map, identifier, length, value);229 return char_map_add(map, identifier, length, value); 218 230 } 219 231 } -
uspace/srv/net/structures/char_map.h
rb5cbff4 r71b00dcc 40 40 /** Invalid assigned value used also if an entry does not exist. 41 41 */ 42 #define CHAR_MAP_NULL ( -1)42 #define CHAR_MAP_NULL (-1) 43 43 44 44 /** Type definition of the character string to integer map. … … 59 59 /** Actually mapped character. 60 60 */ 61 char 61 char c; 62 62 /** Stored integral value. 63 63 */ 64 int 64 int value; 65 65 /** Next character array size. 66 66 */ 67 int 67 int size; 68 68 /** First free position in the next character array. 69 69 */ 70 int 70 int next; 71 71 /** Next character array. 72 72 */ 73 char_map_ref * 73 char_map_ref * items; 74 74 /** Consistency check magic value. 75 75 */ 76 int 76 int magic; 77 77 }; 78 78 … … 89 89 * @returns Other error codes as defined for the char_map_add_item() function. 90 90 */ 91 int char_map_add( char_map_ref map, const char * identifier, size_t length, const int value);91 int char_map_add(char_map_ref map, const char * identifier, size_t length, const int value); 92 92 93 93 /** Clears and destroys the map. 94 94 * @param[in,out] map The character string to integer map. 95 95 */ 96 void char_map_destroy( char_map_ref map);96 void char_map_destroy(char_map_ref map); 97 97 98 98 /** Excludes the value assigned to the key from the map. … … 104 104 * @returns CHAR_MAP_NULL if the key is not assigned a value. 105 105 */ 106 int char_map_exclude( char_map_ref map, const char * identifier, size_t length);106 int char_map_exclude(char_map_ref map, const char * identifier, size_t length); 107 107 108 108 /** Returns the value assigned to the key from the map. … … 113 113 * @returns CHAR_MAP_NULL if the key is not assigned a value. 114 114 */ 115 int char_map_find( const char_map_ref map, const char * identifier, size_t length);115 int char_map_find(const char_map_ref map, const char * identifier, size_t length); 116 116 117 117 /** Initializes the map. … … 121 121 * @returns ENOMEM if there is not enough memory left. 122 122 */ 123 int char_map_initialize( char_map_ref map);123 int char_map_initialize(char_map_ref map); 124 124 125 125 /** Adds or updates the value with the key to the map. … … 135 135 * @returns Other error codes as defined for the char_map_add_item() function. 136 136 */ 137 int char_map_update( char_map_ref map, const char * identifier, size_t length, const int value);137 int char_map_update(char_map_ref map, const char * identifier, size_t length, const int value); 138 138 139 139 #endif -
uspace/srv/net/structures/dynamic_fifo.c
rb5cbff4 r71b00dcc 50 50 * @param[in] index The actual index to be shifted. 51 51 */ 52 #define NEXT_INDEX( fifo, index ) ((( index ) + 1 ) % (( fifo )->size + 1))52 #define NEXT_INDEX(fifo, index) (((index) + 1) % ((fifo)->size + 1)) 53 53 54 54 /** Checks if the queue is valid. … … 57 57 * @returns FALSE otherwise. 58 58 */ 59 int dyn_fifo_is_valid( dyn_fifo_ref fifo);59 int dyn_fifo_is_valid(dyn_fifo_ref fifo); 60 60 61 int dyn_fifo_is_valid( dyn_fifo_ref fifo){62 return fifo && ( fifo->magic_value == DYN_FIFO_MAGIC_VALUE);61 int dyn_fifo_is_valid(dyn_fifo_ref fifo){ 62 return fifo && (fifo->magic_value == DYN_FIFO_MAGIC_VALUE); 63 63 } 64 64 65 int dyn_fifo_initialize( dyn_fifo_ref fifo, int size ){ 66 if( ! fifo ) return EBADMEM; 67 if( size <= 0 ) return EINVAL; 68 fifo->items = ( int * ) malloc( sizeof( int ) * size + 1 ); 69 if( ! fifo->items ) return ENOMEM; 65 int dyn_fifo_initialize(dyn_fifo_ref fifo, int size){ 66 if(! fifo){ 67 return EBADMEM; 68 } 69 if(size <= 0){ 70 return EINVAL; 71 } 72 fifo->items = (int *) malloc(sizeof(int) * size + 1); 73 if(! fifo->items){ 74 return ENOMEM; 75 } 70 76 fifo->size = size; 71 77 fifo->head = 0; … … 75 81 } 76 82 77 int dyn_fifo_push( dyn_fifo_ref fifo, int value, int max_size){78 int * 83 int dyn_fifo_push(dyn_fifo_ref fifo, int value, int max_size){ 84 int * new_items; 79 85 80 if( ! dyn_fifo_is_valid( fifo )) return EINVAL; 81 if( NEXT_INDEX( fifo, fifo->tail ) == fifo->head ){ 82 if(( max_size > 0 ) && (( fifo->size * 2 ) > max_size )){ 83 if( fifo->size >= max_size ) return ENOMEM; 86 if(! dyn_fifo_is_valid(fifo)){ 87 return EINVAL; 88 } 89 if(NEXT_INDEX(fifo, fifo->tail) == fifo->head){ 90 if((max_size > 0) && ((fifo->size * 2) > max_size)){ 91 if(fifo->size >= max_size){ 92 return ENOMEM; 93 } 84 94 }else{ 85 95 max_size = fifo->size * 2; 86 96 } 87 new_items = realloc( fifo->items, sizeof( int ) * max_size + 1 ); 88 if( ! new_items ) return ENOMEM; 97 new_items = realloc(fifo->items, sizeof(int) * max_size + 1); 98 if(! new_items){ 99 return ENOMEM; 100 } 89 101 fifo->items = new_items; 90 if( fifo->tail < fifo->head){91 if( fifo->tail < max_size - fifo->size){92 memcpy( fifo->items + fifo->size + 1, fifo->items, fifo->tail * sizeof( int));102 if(fifo->tail < fifo->head){ 103 if(fifo->tail < max_size - fifo->size){ 104 memcpy(fifo->items + fifo->size + 1, fifo->items, fifo->tail * sizeof(int)); 93 105 fifo->tail += fifo->size + 1; 94 106 }else{ 95 memcpy( fifo->items + fifo->size + 1, fifo->items, ( max_size - fifo->size ) * sizeof( int));96 memcpy( fifo->items, fifo->items + max_size - fifo->size, fifo->tail - max_size + fifo->size);107 memcpy(fifo->items + fifo->size + 1, fifo->items, (max_size - fifo->size) * sizeof(int)); 108 memcpy(fifo->items, fifo->items + max_size - fifo->size, fifo->tail - max_size + fifo->size); 97 109 fifo->tail -= max_size - fifo->size; 98 110 } … … 100 112 fifo->size = max_size; 101 113 } 102 fifo->items[ fifo->tail] = value;103 fifo->tail = NEXT_INDEX( fifo, fifo->tail);114 fifo->items[fifo->tail] = value; 115 fifo->tail = NEXT_INDEX(fifo, fifo->tail); 104 116 return EOK; 105 117 } 106 118 107 int dyn_fifo_pop( dyn_fifo_ref fifo){108 int 119 int dyn_fifo_pop(dyn_fifo_ref fifo){ 120 int value; 109 121 110 if( ! dyn_fifo_is_valid( fifo )) return EINVAL; 111 if( fifo->head == fifo->tail ) return ENOENT; 112 value = fifo->items[ fifo->head ]; 113 fifo->head = NEXT_INDEX( fifo, fifo->head ); 122 if(! dyn_fifo_is_valid(fifo)){ 123 return EINVAL; 124 } 125 if(fifo->head == fifo->tail){ 126 return ENOENT; 127 } 128 value = fifo->items[fifo->head]; 129 fifo->head = NEXT_INDEX(fifo, fifo->head); 114 130 return value; 115 131 } 116 132 117 int dyn_fifo_value( dyn_fifo_ref fifo ){ 118 if( ! dyn_fifo_is_valid( fifo )) return EINVAL; 119 if( fifo->head == fifo->tail ) return ENOENT; 120 return fifo->items[ fifo->head ]; 133 int dyn_fifo_value(dyn_fifo_ref fifo){ 134 if(! dyn_fifo_is_valid(fifo)){ 135 return EINVAL; 136 } 137 if(fifo->head == fifo->tail){ 138 return ENOENT; 139 } 140 return fifo->items[fifo->head]; 121 141 } 122 142 123 int dyn_fifo_destroy( dyn_fifo_ref fifo ){ 124 if( ! dyn_fifo_is_valid( fifo )) return EINVAL; 125 free( fifo->items ); 143 int dyn_fifo_destroy(dyn_fifo_ref fifo){ 144 if(! dyn_fifo_is_valid(fifo)){ 145 return EINVAL; 146 } 147 free(fifo->items); 126 148 fifo->magic_value = 0; 127 149 return EOK; -
uspace/srv/net/structures/dynamic_fifo.h
rb5cbff4 r71b00dcc 56 56 /** Stored item field. 57 57 */ 58 int 58 int * items; 59 59 /** Actual field size. 60 60 */ 61 int 61 int size; 62 62 /** First item in the queue index. 63 63 */ 64 int 64 int head; 65 65 /** Last item in the queue index. 66 66 */ 67 int 67 int tail; 68 68 /** Consistency check magic value. 69 69 */ 70 int 70 int magic_value; 71 71 }; 72 72 … … 79 79 * @returns ENOMEM if there is not enough memory left. 80 80 */ 81 int dyn_fifo_initialize( dyn_fifo_ref fifo, int size);81 int dyn_fifo_initialize(dyn_fifo_ref fifo, int size); 82 82 83 83 /** Appends a new item to the queue end. … … 89 89 * @returns ENOMEM if there is not enough memory left. 90 90 */ 91 int dyn_fifo_push( dyn_fifo_ref fifo, int value, int max_size);91 int dyn_fifo_push(dyn_fifo_ref fifo, int value, int max_size); 92 92 93 93 /** Returns and excludes the first item in the queue. … … 97 97 * @returns ENOENT if the queue is empty. 98 98 */ 99 int dyn_fifo_pop( dyn_fifo_ref fifo);99 int dyn_fifo_pop(dyn_fifo_ref fifo); 100 100 101 101 /** Returns and keeps the first item in the queue. … … 105 105 * @returns ENOENT if the queue is empty. 106 106 */ 107 int dyn_fifo_value( dyn_fifo_ref fifo);107 int dyn_fifo_value(dyn_fifo_ref fifo); 108 108 109 109 /** Clears and destroys the queue. … … 112 112 * @returns EINVAL if the queue is not valid. 113 113 */ 114 int dyn_fifo_destroy( dyn_fifo_ref fifo);114 int dyn_fifo_destroy(dyn_fifo_ref fifo); 115 115 116 116 #endif -
uspace/srv/net/structures/generic_char_map.h
rb5cbff4 r71b00dcc 54 54 * @param[in] type Inner object type. 55 55 */ 56 #define GENERIC_CHAR_MAP_DECLARE( name, type) \56 #define GENERIC_CHAR_MAP_DECLARE(name, type) \ 57 57 \ 58 GENERIC_FIELD_DECLARE( name##_items, type) \58 GENERIC_FIELD_DECLARE(name##_items, type) \ 59 59 \ 60 60 typedef struct name name##_t; \ … … 62 62 \ 63 63 struct name{ \ 64 char_map_t 65 name##_items_t 66 int 64 char_map_t names; \ 65 name##_items_t values; \ 66 int magic; \ 67 67 }; \ 68 68 \ 69 int name##_add( name##_ref map, const char * name, const size_t length, type * value); \70 int name##_count( name##_ref map); \71 void name##_destroy( name##_ref map );\72 void name##_exclude( name##_ref map, const char * name, const size_t length); \73 type * name##_find( name##_ref map, const char * name, const size_t length );\74 int name##_initialize( name##_ref map );\75 int name##_is_valid( name##_ref map);69 int name##_add(name##_ref map, const char * name, const size_t length, type * value); \ 70 int name##_count(name##_ref map); \ 71 void name##_destroy(name##_ref map); \ 72 void name##_exclude(name##_ref map, const char * name, const size_t length); \ 73 type * name##_find(name##_ref map, const char * name, const size_t length); \ 74 int name##_initialize(name##_ref map); \ 75 int name##_is_valid(name##_ref map); 76 76 77 77 /** Character string to generic type map implementation. … … 80 80 * @param[in] type Inner object type. 81 81 */ 82 #define GENERIC_CHAR_MAP_IMPLEMENT( name, type )\82 #define GENERIC_CHAR_MAP_IMPLEMENT(name, type) \ 83 83 \ 84 GENERIC_FIELD_IMPLEMENT( name##_items, type )\84 GENERIC_FIELD_IMPLEMENT(name##_items, type) \ 85 85 \ 86 int name##_add( name##_ref map, const char * name, const size_t length, type * value){ \86 int name##_add(name##_ref map, const char * name, const size_t length, type * value){ \ 87 87 ERROR_DECLARE; \ 88 88 \ 89 int 89 int index; \ 90 90 \ 91 if( ! name##_is_valid( map )) return EINVAL; \ 92 index = name##_items_add( & map->values, value ); \ 93 if( index < 0 ) return index; \ 94 if( ERROR_OCCURRED( char_map_add( & map->names, name, length, index ))){ \ 95 name##_items_exclude_index( & map->values, index ); \ 91 if(! name##_is_valid(map)){ \ 92 return EINVAL; \ 93 } \ 94 index = name##_items_add(&map->values, value); \ 95 if(index < 0){ \ 96 return index; \ 97 } \ 98 if(ERROR_OCCURRED(char_map_add(&map->names, name, length, index))){ \ 99 name##_items_exclude_index(&map->values, index); \ 96 100 return ERROR_CODE; \ 97 101 } \ … … 99 103 } \ 100 104 \ 101 int name##_count( name##_ref map){ \102 return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1;\105 int name##_count(name##_ref map){ \ 106 return name##_is_valid(map) ? name##_items_count(&map->values) : -1; \ 103 107 } \ 104 108 \ 105 void name##_destroy( name##_ref map){ \106 if( name##_is_valid( map )){\107 char_map_destroy( & map->names );\108 name##_items_destroy( & map->values );\109 void name##_destroy(name##_ref map){ \ 110 if(name##_is_valid(map)){ \ 111 char_map_destroy(&map->names); \ 112 name##_items_destroy(&map->values); \ 109 113 } \ 110 114 } \ 111 115 \ 112 void name##_exclude( name##_ref map, const char * name, const size_t length){ \113 if( name##_is_valid( map )){\114 int 116 void name##_exclude(name##_ref map, const char * name, const size_t length){ \ 117 if(name##_is_valid(map)){ \ 118 int index; \ 115 119 \ 116 index = char_map_exclude( & map->names, name, length); \117 if( index != CHAR_MAP_NULL ){\118 name##_items_exclude_index( & map->values, index); \120 index = char_map_exclude(&map->names, name, length); \ 121 if(index != CHAR_MAP_NULL){ \ 122 name##_items_exclude_index(&map->values, index); \ 119 123 } \ 120 124 } \ 121 125 } \ 122 126 \ 123 type * name##_find( name##_ref map, const char * name, const size_t length ){\124 if( name##_is_valid( map )){\125 int 127 type * name##_find(name##_ref map, const char * name, const size_t length){ \ 128 if(name##_is_valid(map)){ \ 129 int index; \ 126 130 \ 127 index = char_map_find( & map->names, name, length );\128 if( index != CHAR_MAP_NULL ){\129 return name##_items_get_index( & map->values, index );\131 index = char_map_find(&map->names, name, length); \ 132 if(index != CHAR_MAP_NULL){ \ 133 return name##_items_get_index(&map->values, index); \ 130 134 } \ 131 135 } \ … … 133 137 } \ 134 138 \ 135 int name##_initialize( name##_ref map ){\139 int name##_initialize(name##_ref map){ \ 136 140 ERROR_DECLARE; \ 137 141 \ 138 if( ! map ) return EINVAL; \ 139 ERROR_PROPAGATE( char_map_initialize( & map->names )); \ 140 if( ERROR_OCCURRED( name##_items_initialize( & map->values ))){ \ 141 char_map_destroy( & map->names ); \ 142 if(! map){ \ 143 return EINVAL; \ 144 } \ 145 ERROR_PROPAGATE(char_map_initialize(&map->names)); \ 146 if(ERROR_OCCURRED(name##_items_initialize(&map->values))){ \ 147 char_map_destroy(&map->names); \ 142 148 return ERROR_CODE; \ 143 149 } \ … … 146 152 } \ 147 153 \ 148 int name##_is_valid( name##_ref map){ \149 return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE );\154 int name##_is_valid(name##_ref map){ \ 155 return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE); \ 150 156 } 151 157 -
uspace/srv/net/structures/generic_field.h
rb5cbff4 r71b00dcc 51 51 * @param[in] type Inner object type. 52 52 */ 53 #define GENERIC_FIELD_DECLARE( name, type )\54 \55 typedef struct name name##_t; \56 typedef name##_t * name##_ref; \57 \58 struct name{ \59 int size;\60 int next;\61 type ** items; \62 int magic;\63 }; \64 \65 int name##_add( name##_ref field, type * value );\66 int name##_count( name##_ref field );\67 void name##_destroy( name##_ref field );\68 void name##_exclude_index( name##_ref field, int index );\69 type ** name##_get_field( name##_ref field );\70 type * name##_get_index( name##_ref field, int index );\71 int name##_initialize( name##_ref field );\72 int name##_is_valid( name##_ref field);53 #define GENERIC_FIELD_DECLARE(name, type) \ 54 \ 55 typedef struct name name##_t; \ 56 typedef name##_t * name##_ref; \ 57 \ 58 struct name{ \ 59 int size; \ 60 int next; \ 61 type ** items; \ 62 int magic; \ 63 }; \ 64 \ 65 int name##_add(name##_ref field, type * value); \ 66 int name##_count(name##_ref field); \ 67 void name##_destroy(name##_ref field); \ 68 void name##_exclude_index(name##_ref field, int index); \ 69 type ** name##_get_field(name##_ref field); \ 70 type * name##_get_index(name##_ref field, int index); \ 71 int name##_initialize(name##_ref field); \ 72 int name##_is_valid(name##_ref field); 73 73 74 74 /** Generic type field implementation. … … 77 77 * @param[in] type Inner object type. 78 78 */ 79 #define GENERIC_FIELD_IMPLEMENT( name, type ) \ 80 \ 81 int name##_add( name##_ref field, type * value ){ \ 82 if( name##_is_valid( field )){ \ 83 if( field->next == ( field->size - 1 )){ \ 84 type ** tmp; \ 85 \ 86 tmp = ( type ** ) realloc( field->items, sizeof( type * ) * 2 * field->size ); \ 87 if( ! tmp ) return ENOMEM; \ 88 field->size *= 2; \ 89 field->items = tmp; \ 90 } \ 91 field->items[ field->next ] = value; \ 92 ++ field->next; \ 93 field->items[ field->next ] = NULL; \ 94 return field->next - 1; \ 95 } \ 96 return EINVAL; \ 97 } \ 98 \ 99 int name##_count( name##_ref field ){ \ 100 return name##_is_valid( field ) ? field->next : -1; \ 101 } \ 102 \ 103 void name##_destroy( name##_ref field ){ \ 104 if( name##_is_valid( field )){ \ 105 int index; \ 106 \ 107 field->magic = 0; \ 108 for( index = 0; index < field->next; ++ index ){ \ 109 if( field->items[ index ] ) free( field->items[ index ] ); \ 110 } \ 111 free( field->items ); \ 112 } \ 113 } \ 114 \ 115 void name##_exclude_index( name##_ref field, int index ){ \ 116 if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ 117 free( field->items[ index ] ); \ 118 field->items[ index ] = NULL; \ 119 } \ 120 } \ 121 \ 122 type * name##_get_index( name##_ref field, int index ){ \ 123 if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ 124 return field->items[ index ]; \ 125 } \ 126 return NULL; \ 127 } \ 128 \ 129 type ** name##_get_field( name##_ref field ){ \ 130 return name##_is_valid( field ) ? field->items : NULL; \ 131 } \ 132 \ 133 int name##_initialize( name##_ref field ){ \ 134 if( ! field ) return EINVAL; \ 135 field->size = 2; \ 136 field->next = 0; \ 137 field->items = ( type ** ) malloc( sizeof( type * ) * field->size ); \ 138 if( ! field->items ) return ENOMEM; \ 139 field->items[ field->next ] = NULL; \ 140 field->magic = GENERIC_FIELD_MAGIC_VALUE; \ 141 return EOK; \ 142 } \ 143 \ 144 int name##_is_valid( name##_ref field ){ \ 145 return field && ( field->magic == GENERIC_FIELD_MAGIC_VALUE ); \ 79 #define GENERIC_FIELD_IMPLEMENT(name, type) \ 80 \ 81 int name##_add(name##_ref field, type * value){ \ 82 if(name##_is_valid(field)){ \ 83 if(field->next == (field->size - 1)){ \ 84 type ** tmp; \ 85 \ 86 tmp = (type **) realloc(field->items, sizeof(type *) * 2 * field->size); \ 87 if(! tmp){ \ 88 return ENOMEM; \ 89 } \ 90 field->size *= 2; \ 91 field->items = tmp; \ 92 } \ 93 field->items[field->next] = value; \ 94 ++ field->next; \ 95 field->items[field->next] = NULL; \ 96 return field->next - 1; \ 97 } \ 98 return EINVAL; \ 99 } \ 100 \ 101 int name##_count(name##_ref field){ \ 102 return name##_is_valid(field) ? field->next : -1; \ 103 } \ 104 \ 105 void name##_destroy(name##_ref field){ \ 106 if(name##_is_valid(field)){ \ 107 int index; \ 108 \ 109 field->magic = 0; \ 110 for(index = 0; index < field->next; ++ index){ \ 111 if(field->items[index]){ \ 112 free(field->items[index]); \ 113 } \ 114 } \ 115 free(field->items); \ 116 } \ 117 } \ 118 \ 119 void name##_exclude_index(name##_ref field, int index){ \ 120 if(name##_is_valid(field) && (index >= 0) && (index < field->next) && (field->items[index])){ \ 121 free(field->items[index]); \ 122 field->items[index] = NULL; \ 123 } \ 124 } \ 125 \ 126 type * name##_get_index(name##_ref field, int index){ \ 127 if(name##_is_valid(field) && (index >= 0) && (index < field->next) && (field->items[index])){ \ 128 return field->items[index]; \ 129 } \ 130 return NULL; \ 131 } \ 132 \ 133 type ** name##_get_field(name##_ref field){ \ 134 return name##_is_valid(field) ? field->items : NULL; \ 135 } \ 136 \ 137 int name##_initialize(name##_ref field){ \ 138 if(! field){ \ 139 return EINVAL; \ 140 } \ 141 field->size = 2; \ 142 field->next = 0; \ 143 field->items = (type **) malloc(sizeof(type *) * field->size); \ 144 if(! field->items){ \ 145 return ENOMEM; \ 146 } \ 147 field->items[field->next] = NULL; \ 148 field->magic = GENERIC_FIELD_MAGIC_VALUE; \ 149 return EOK; \ 150 } \ 151 \ 152 int name##_is_valid(name##_ref field){ \ 153 return field && (field->magic == GENERIC_FIELD_MAGIC_VALUE); \ 146 154 } 147 155 -
uspace/srv/net/structures/int_map.h
rb5cbff4 r71b00dcc 55 55 * @param[in] type Inner object type. 56 56 */ 57 #define INT_MAP_DECLARE( name, type )\57 #define INT_MAP_DECLARE(name, type) \ 58 58 \ 59 59 typedef struct name name##_t; \ … … 63 63 \ 64 64 struct name##_item{ \ 65 int 66 type * 67 int 65 int key; \ 66 type * value; \ 67 int magic; \ 68 68 }; \ 69 69 \ 70 70 struct name{ \ 71 int 72 int 73 name##_item_ref 74 int 71 int size; \ 72 int next; \ 73 name##_item_ref items; \ 74 int magic; \ 75 75 }; \ 76 76 \ 77 int name##_add( name##_ref map, int key, type * value );\78 void name##_clear( name##_ref map );\79 int name##_count( name##_ref map );\80 void name##_destroy( name##_ref map );\81 void name##_exclude( name##_ref map, int key );\82 void name##_exclude_index( name##_ref map, int index );\83 type * name##_find( name##_ref map, int key); \84 int name##_update( name##_ref map, int key, int new_key );\85 type * name##_get_index( name##_ref map, int index );\86 int name##_initialize( name##_ref map );\87 int name##_is_valid( name##_ref map );\88 void name##_item_destroy( name##_item_ref item );\89 int name##_item_is_valid( name##_item_ref item);77 int name##_add(name##_ref map, int key, type * value); \ 78 void name##_clear(name##_ref map); \ 79 int name##_count(name##_ref map); \ 80 void name##_destroy(name##_ref map); \ 81 void name##_exclude(name##_ref map, int key); \ 82 void name##_exclude_index(name##_ref map, int index); \ 83 type * name##_find(name##_ref map, int key); \ 84 int name##_update(name##_ref map, int key, int new_key); \ 85 type * name##_get_index(name##_ref map, int index); \ 86 int name##_initialize(name##_ref map); \ 87 int name##_is_valid(name##_ref map); \ 88 void name##_item_destroy(name##_item_ref item); \ 89 int name##_item_is_valid(name##_item_ref item); 90 90 91 91 /** Integer to generic type map implementation. … … 94 94 * @param[in] type Inner object type. 95 95 */ 96 #define INT_MAP_IMPLEMENT( name, type ) \ 97 \ 98 int 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; \ 96 #define INT_MAP_IMPLEMENT(name, type) \ 97 \ 98 int 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){ \ 105 return ENOMEM; \ 106 } \ 105 107 map->size *= 2; \ 106 108 map->items = tmp; \ 107 109 } \ 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;\110 map->items[map->next].key = key; \ 111 map->items[map->next].value = value; \ 112 map->items[map->next].magic = INT_MAP_ITEM_MAGIC_VALUE; \ 111 113 ++ map->next; \ 112 map->items[ map->next].magic = 0; \114 map->items[map->next].magic = 0; \ 113 115 return map->next - 1; \ 114 116 } \ … … 116 118 } \ 117 119 \ 118 void name##_clear( name##_ref map ){\119 if( name##_is_valid( map )){\120 int 120 void name##_clear(name##_ref map){ \ 121 if(name##_is_valid(map)){ \ 122 int index; \ 121 123 \ 122 124 /* 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 ] ));\125 for(index = 0; index < map->next; ++ index){ \ 126 if(name##_item_is_valid(&(map->items[index]))){ \ 127 name##_item_destroy(&(map->items[index])); \ 126 128 } \ 127 129 } \ 128 130 map->next = 0; \ 129 map->items[ map->next].magic = 0; \131 map->items[map->next].magic = 0; \ 130 132 /* map->magic = INT_MAP_MAGIC_VALUE;*/ \ 131 133 } \ 132 134 } \ 133 135 \ 134 int name##_count( name##_ref map){ \135 return name##_is_valid( map) ? map->next : -1; \136 } \ 137 \ 138 void name##_destroy( name##_ref map){ \139 if( name##_is_valid( map )){\140 int 136 int name##_count(name##_ref map){ \ 137 return name##_is_valid(map) ? map->next : -1; \ 138 } \ 139 \ 140 void name##_destroy(name##_ref map){ \ 141 if(name##_is_valid(map)){ \ 142 int index; \ 141 143 \ 142 144 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 \ 152 void name##_exclude( name##_ref map, int key){ \153 if( name##_is_valid( map )){\154 int 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 \ 164 void 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 \ 170 type * name##_find( name##_ref map, int key){ \171 if( name##_is_valid( map )){\172 int 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;\145 for(index = 0; index < map->next; ++ index){ \ 146 if(name##_item_is_valid(&(map->items[index]))){ \ 147 name##_item_destroy(&(map->items[index])); \ 148 } \ 149 } \ 150 free(map->items); \ 151 } \ 152 } \ 153 \ 154 void name##_exclude(name##_ref map, int key){ \ 155 if(name##_is_valid(map)){ \ 156 int index; \ 157 \ 158 for(index = 0; index < map->next; ++ index){ \ 159 if(name##_item_is_valid(&(map->items[index])) && (map->items[index].key == key)){ \ 160 name##_item_destroy(&(map->items[index])); \ 161 } \ 162 } \ 163 } \ 164 } \ 165 \ 166 void name##_exclude_index(name##_ref map, int index){ \ 167 if(name##_is_valid(map) && (index >= 0) && (index < map->next) && name##_item_is_valid(&(map->items[index]))){ \ 168 name##_item_destroy(&(map->items[index])); \ 169 } \ 170 } \ 171 \ 172 type * name##_find(name##_ref map, int key){ \ 173 if(name##_is_valid(map)){ \ 174 int index; \ 175 \ 176 for(index = 0; index < map->next; ++ index){ \ 177 if(name##_item_is_valid(&(map->items[index])) && (map->items[index].key == key)){ \ 178 return map->items[index].value; \ 177 179 } \ 178 180 } \ … … 181 183 } \ 182 184 \ 183 int name##_update( name##_ref map, int key, int new_key){ \184 if( name##_is_valid( map )){\185 int 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 ){\185 int name##_update(name##_ref map, int key, int new_key){ \ 186 if(name##_is_valid(map)){ \ 187 int index; \ 188 \ 189 for(index = 0; index < map->next; ++ index){ \ 190 if(name##_item_is_valid(&(map->items[index]))){ \ 191 if(map->items[index].key == new_key){ \ 190 192 return EEXIST; \ 191 }else if( map->items[ index ].key == key ){\192 map->items[ index].key = new_key; \193 }else if(map->items[index].key == key){ \ 194 map->items[index].key = new_key; \ 193 195 return EOK; \ 194 196 } \ … … 199 201 } \ 200 202 \ 201 type * 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;\203 type * name##_get_index(name##_ref map, int index){ \ 204 if(name##_is_valid(map) && (index >= 0) && (index < map->next) && name##_item_is_valid(&(map->items[index]))){ \ 205 return map->items[index].value; \ 204 206 } \ 205 207 return NULL; \ 206 208 } \ 207 209 \ 208 int name##_initialize( name##_ref map ){ \ 209 if( ! map ) return EINVAL; \ 210 int name##_initialize(name##_ref map){ \ 211 if(! map){ \ 212 return EINVAL; \ 213 } \ 210 214 map->size = 2; \ 211 215 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; \ 216 map->items = (name##_item_ref) malloc(sizeof(name##_item_t) * map->size); \ 217 if(! map->items){ \ 218 return ENOMEM; \ 219 } \ 220 map->items[map->next].magic = 0; \ 215 221 map->magic = INT_MAP_MAGIC_VALUE; \ 216 222 return EOK; \ 217 223 } \ 218 224 \ 219 int name##_is_valid( name##_ref map){ \220 return map && ( map->magic == INT_MAP_MAGIC_VALUE );\221 } \ 222 \ 223 void name##_item_destroy( name##_item_ref item ){\224 if( name##_item_is_valid( item )){\225 int name##_is_valid(name##_ref map){ \ 226 return map && (map->magic == INT_MAP_MAGIC_VALUE); \ 227 } \ 228 \ 229 void name##_item_destroy(name##_item_ref item){ \ 230 if(name##_item_is_valid(item)){ \ 225 231 item->magic = 0; \ 226 if( item->value){ \227 free( item->value );\232 if(item->value){ \ 233 free(item->value); \ 228 234 item->value = NULL; \ 229 235 } \ … … 231 237 } \ 232 238 \ 233 int name##_item_is_valid( name##_item_ref item ){\234 return item && ( item->magic == INT_MAP_ITEM_MAGIC_VALUE); \239 int name##_item_is_valid(name##_item_ref item){ \ 240 return item && (item->magic == INT_MAP_ITEM_MAGIC_VALUE); \ 235 241 } 236 242 -
uspace/srv/net/structures/measured_strings.c
rb5cbff4 r71b00dcc 54 54 * @returns NULL if there is not enough memory left. 55 55 */ 56 size_t * prepare_lengths( const measured_string_ref strings, size_t count ); 57 58 measured_string_ref measured_string_create_bulk( const char * string, size_t length ){ 59 measured_string_ref new; 60 61 if( length == 0 ){ 62 while( string[ length ] ) ++ length; 63 } 64 new = ( measured_string_ref ) malloc( sizeof( measured_string_t ) + ( sizeof( char ) * ( length + 1 ))); 65 if( ! new ) return NULL; 56 size_t * prepare_lengths(const measured_string_ref strings, size_t count); 57 58 measured_string_ref measured_string_create_bulk(const char * string, size_t length){ 59 measured_string_ref new; 60 61 if(length == 0){ 62 while(string[length]){ 63 ++ length; 64 } 65 } 66 new = (measured_string_ref) malloc(sizeof(measured_string_t) + (sizeof(char) * (length + 1))); 67 if(! new){ 68 return NULL; 69 } 66 70 new->length = length; 67 new->value = (( char * ) new ) + sizeof( measured_string_t);71 new->value = ((char *) new) + sizeof(measured_string_t); 68 72 // append terminating zero explicitly - to be safe 69 memcpy( new->value, string, new->length);70 new->value[ new->length] = '\0';73 memcpy(new->value, string, new->length); 74 new->value[new->length] = '\0'; 71 75 return new; 72 76 } 73 77 74 measured_string_ref measured_string_copy( measured_string_ref source ){ 75 measured_string_ref new; 76 77 if( ! source ) return NULL; 78 new = ( measured_string_ref ) malloc( sizeof( measured_string_t )); 79 if( new ){ 80 new->value = ( char * ) malloc( source->length + 1 ); 81 if( new->value ){ 78 measured_string_ref measured_string_copy(measured_string_ref source){ 79 measured_string_ref new; 80 81 if(! source){ 82 return NULL; 83 } 84 new = (measured_string_ref) malloc(sizeof(measured_string_t)); 85 if(new){ 86 new->value = (char *) malloc(source->length + 1); 87 if(new->value){ 82 88 new->length = source->length; 83 memcpy( new->value, source->value, new->length);84 new->value[ new->length] = '\0';89 memcpy(new->value, source->value, new->length); 90 new->value[new->length] = '\0'; 85 91 return new; 86 92 }else{ 87 free( new);93 free(new); 88 94 } 89 95 } … … 91 97 } 92 98 93 int measured_strings_receive( measured_string_ref * strings, char ** data, size_t count ){ 94 ERROR_DECLARE; 95 96 size_t * lengths; 97 size_t index; 98 size_t length; 99 char * next; 100 ipc_callid_t callid; 101 102 if(( ! strings ) || ( ! data ) || ( count <= 0 )){ 103 return EINVAL; 104 } 105 lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); 106 if( ! lengths ) return ENOMEM; 107 if(( ! async_data_write_receive( & callid, & length )) 108 || ( length != sizeof( size_t ) * ( count + 1 ))){ 109 free( lengths ); 110 return EINVAL; 111 } 112 if( ERROR_OCCURRED( async_data_write_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){ 113 free( lengths ); 114 return ERROR_CODE; 115 } 116 * data = malloc( lengths[ count ] ); 117 if( !( * data )) return ENOMEM; 118 ( * data )[ lengths[ count ] - 1 ] = '\0'; 119 * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count ); 120 if( !( * strings )){ 121 free( lengths ); 122 free( * data ); 99 int measured_strings_receive(measured_string_ref * strings, char ** data, size_t count){ 100 ERROR_DECLARE; 101 102 size_t * lengths; 103 size_t index; 104 size_t length; 105 char * next; 106 ipc_callid_t callid; 107 108 if((! strings) || (! data) || (count <= 0)){ 109 return EINVAL; 110 } 111 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1)); 112 if(! lengths){ 113 return ENOMEM; 114 } 115 if((! async_data_write_receive(&callid, &length)) 116 || (length != sizeof(size_t) * (count + 1))){ 117 free(lengths); 118 return EINVAL; 119 } 120 if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){ 121 free(lengths); 122 return ERROR_CODE; 123 } 124 *data = malloc(lengths[count]); 125 if(!(*data)){ 126 return ENOMEM; 127 } 128 (*data)[lengths[count] - 1] = '\0'; 129 *strings = (measured_string_ref) malloc(sizeof(measured_string_t) * count); 130 if(!(*strings)){ 131 free(lengths); 132 free(*data); 123 133 return ENOMEM; 124 134 } 125 135 next = * data; 126 for( index = 0; index < count; ++ index){127 ( * strings)[ index ].length = lengths[ index];128 if( lengths[ index ] > 0){129 if(( ! async_data_write_receive( & callid, & length))130 || ( length != lengths[ index ])){131 free( * data);132 free( * strings);133 free( lengths);136 for(index = 0; index < count; ++ index){ 137 (*strings)[index].length = lengths[index]; 138 if(lengths[index] > 0){ 139 if((! async_data_write_receive(&callid, &length)) 140 || (length != lengths[index])){ 141 free(*data); 142 free(*strings); 143 free(lengths); 134 144 return EINVAL; 135 145 } 136 ERROR_PROPAGATE( async_data_write_finalize( callid, next, lengths[ index ]));137 ( * strings)[ index].value = next;138 next += lengths[ index];139 * 146 ERROR_PROPAGATE(async_data_write_finalize(callid, next, lengths[index])); 147 (*strings)[index].value = next; 148 next += lengths[index]; 149 *next = '\0'; 140 150 ++ next; 141 151 }else{ 142 ( * strings )[ index ].value = NULL; 143 } 144 } 145 free( lengths ); 146 return EOK; 147 } 148 149 int measured_strings_reply( const measured_string_ref strings, size_t count ){ 150 ERROR_DECLARE; 151 152 size_t * lengths; 153 size_t index; 154 size_t length; 155 ipc_callid_t callid; 156 157 if(( ! strings ) || ( count <= 0 )){ 158 return EINVAL; 159 } 160 lengths = prepare_lengths( strings, count ); 161 if( ! lengths ) return ENOMEM; 162 if(( ! async_data_read_receive( & callid, & length )) 163 || ( length != sizeof( size_t ) * ( count + 1 ))){ 164 free( lengths ); 165 return EINVAL; 166 } 167 if( ERROR_OCCURRED( async_data_read_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){ 168 free( lengths ); 169 return ERROR_CODE; 170 } 171 free( lengths ); 172 for( index = 0; index < count; ++ index ){ 173 if( strings[ index ].length > 0 ){ 174 if(( ! async_data_read_receive( & callid, & length )) 175 || ( length != strings[ index ].length )){ 152 (*strings)[index].value = NULL; 153 } 154 } 155 free(lengths); 156 return EOK; 157 } 158 159 int measured_strings_reply(const measured_string_ref strings, size_t count){ 160 ERROR_DECLARE; 161 162 size_t * lengths; 163 size_t index; 164 size_t length; 165 ipc_callid_t callid; 166 167 if((! strings) || (count <= 0)){ 168 return EINVAL; 169 } 170 lengths = prepare_lengths(strings, count); 171 if(! lengths){ 172 return ENOMEM; 173 } 174 if((! async_data_read_receive(&callid, &length)) 175 || (length != sizeof(size_t) * (count + 1))){ 176 free(lengths); 177 return EINVAL; 178 } 179 if(ERROR_OCCURRED(async_data_read_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){ 180 free(lengths); 181 return ERROR_CODE; 182 } 183 free(lengths); 184 for(index = 0; index < count; ++ index){ 185 if(strings[index].length > 0){ 186 if((! async_data_read_receive(&callid, &length)) 187 || (length != strings[index].length)){ 176 188 return EINVAL; 177 189 } 178 ERROR_PROPAGATE( async_data_read_finalize( callid, strings[ index ].value, strings[ index ].length )); 179 } 180 } 181 return EOK; 182 } 183 184 int measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count ){ 185 ERROR_DECLARE; 186 187 size_t * lengths; 188 size_t index; 189 char * next; 190 191 if(( phone <= 0 ) || ( ! strings ) || ( ! data ) || ( count <= 0 )){ 192 return EINVAL; 193 } 194 lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); 195 if( ! lengths ) return ENOMEM; 196 if( ERROR_OCCURRED( async_data_read_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){ 197 free( lengths ); 198 return ERROR_CODE; 199 } 200 * data = malloc( lengths[ count ] ); 201 if( !( * data )) return ENOMEM; 202 * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count ); 203 if( !( * strings )){ 204 free( lengths ); 205 free( * data ); 190 ERROR_PROPAGATE(async_data_read_finalize(callid, strings[index].value, strings[index].length)); 191 } 192 } 193 return EOK; 194 } 195 196 int measured_strings_return(int phone, measured_string_ref * strings, char ** data, size_t count){ 197 ERROR_DECLARE; 198 199 size_t * lengths; 200 size_t index; 201 char * next; 202 203 if((phone <= 0) || (! strings) || (! data) || (count <= 0)){ 204 return EINVAL; 205 } 206 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1)); 207 if(! lengths){ 208 return ENOMEM; 209 } 210 if(ERROR_OCCURRED(async_data_read_start(phone, lengths, sizeof(size_t) * (count + 1)))){ 211 free(lengths); 212 return ERROR_CODE; 213 } 214 *data = malloc(lengths[count]); 215 if(!(*data)){ 216 return ENOMEM; 217 } 218 *strings = (measured_string_ref) malloc(sizeof(measured_string_t) * count); 219 if(!(*strings)){ 220 free(lengths); 221 free(*data); 206 222 return ENOMEM; 207 223 } 208 224 next = * data; 209 for( index = 0; index < count; ++ index){210 ( * strings )[ index ].length = lengths[ index];211 if( lengths[ index ] > 0){212 ERROR_PROPAGATE( async_data_read_start( phone, next, lengths[ index ]));213 ( * strings )[ index].value = next;214 next += lengths[ index];215 * 225 for(index = 0; index < count; ++ index){ 226 (*strings)[index].length = lengths[index]; 227 if(lengths[index] > 0){ 228 ERROR_PROPAGATE(async_data_read_start(phone, next, lengths[index])); 229 (*strings)[index].value = next; 230 next += lengths[index]; 231 *next = '\0'; 216 232 ++ next; 217 233 }else{ 218 ( * strings )[ index ].value = NULL; 219 } 220 } 221 free( lengths ); 222 return EOK; 223 } 224 225 int measured_strings_send( int phone, const measured_string_ref strings, size_t count ){ 226 ERROR_DECLARE; 227 228 size_t * lengths; 229 size_t index; 230 231 if(( phone <= 0 ) || ( ! strings ) || ( count <= 0 )){ 232 return EINVAL; 233 } 234 lengths = prepare_lengths( strings, count ); 235 if( ! lengths ) return ENOMEM; 236 if( ERROR_OCCURRED( async_data_write_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){ 237 free( lengths ); 238 return ERROR_CODE; 239 } 240 free( lengths ); 241 for( index = 0; index < count; ++ index ){ 242 if( strings[ index ].length > 0 ){ 243 ERROR_PROPAGATE( async_data_write_start( phone, strings[ index ].value, strings[ index ].length )); 244 } 245 } 246 return EOK; 247 } 248 249 size_t * prepare_lengths( const measured_string_ref strings, size_t count ){ 250 size_t * lengths; 251 size_t index; 252 size_t length; 253 254 lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); 255 if( ! lengths ) return NULL; 234 (*strings)[index].value = NULL; 235 } 236 } 237 free(lengths); 238 return EOK; 239 } 240 241 int measured_strings_send(int phone, const measured_string_ref strings, size_t count){ 242 ERROR_DECLARE; 243 244 size_t * lengths; 245 size_t index; 246 247 if((phone <= 0) || (! strings) || (count <= 0)){ 248 return EINVAL; 249 } 250 lengths = prepare_lengths(strings, count); 251 if(! lengths){ 252 return ENOMEM; 253 } 254 if(ERROR_OCCURRED(async_data_write_start(phone, lengths, sizeof(size_t) * (count + 1)))){ 255 free(lengths); 256 return ERROR_CODE; 257 } 258 free(lengths); 259 for(index = 0; index < count; ++ index){ 260 if(strings[index].length > 0){ 261 ERROR_PROPAGATE(async_data_write_start(phone, strings[index].value, strings[index].length)); 262 } 263 } 264 return EOK; 265 } 266 267 size_t * prepare_lengths(const measured_string_ref strings, size_t count){ 268 size_t * lengths; 269 size_t index; 270 size_t length; 271 272 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1)); 273 if(! lengths){ 274 return NULL; 275 } 256 276 length = 0; 257 for( index = 0; index < count; ++ index){258 lengths[ index ] = strings[ index].length;259 length += lengths[ index] + 1;260 } 261 lengths[ count] = length;277 for(index = 0; index < count; ++ index){ 278 lengths[index] = strings[index].length; 279 length += lengths[index] + 1; 280 } 281 lengths[count] = length; 262 282 return lengths; 263 283 } -
uspace/srv/net/structures/measured_strings.h
rb5cbff4 r71b00dcc 57 57 /** Character string data. 58 58 */ 59 char * 59 char * value; 60 60 /** Character string length. 61 61 */ 62 size_t 62 size_t length; 63 63 }; 64 64 … … 71 71 * @returns NULL if there is not enough memory left. 72 72 */ 73 measured_string_ref measured_string_create_bulk( const char * string, size_t length);73 measured_string_ref measured_string_create_bulk(const char * string, size_t length); 74 74 75 75 /** Copies the given measured string with separated header and data parts. … … 79 79 * @returns NULL if there is not enough memory left. 80 80 */ 81 measured_string_ref measured_string_copy( measured_string_ref source);81 measured_string_ref measured_string_copy(measured_string_ref source); 82 82 83 83 /** Receives a measured strings array from a calling module. … … 95 95 * @returns Other error codes as defined for the async_data_write_finalize() function. 96 96 */ 97 int measured_strings_receive( measured_string_ref * strings, char ** data, size_t count);97 int measured_strings_receive(measured_string_ref * strings, char ** data, size_t count); 98 98 99 99 /** Replies the given measured strings array to a calling module. … … 108 108 * @returns Other error codes as defined for the async_data_read_finalize() function. 109 109 */ 110 int measured_strings_reply( const measured_string_ref strings, size_t count);110 int measured_strings_reply(const measured_string_ref strings, size_t count); 111 111 112 112 /** Receives a measured strings array from another module. … … 124 124 * @returns Other error codes as defined for the async_data_read_start() function. 125 125 */ 126 int measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count);126 int measured_strings_return(int phone, measured_string_ref * strings, char ** data, size_t count); 127 127 128 128 /** Sends the given measured strings array to another module. … … 136 136 * @returns Other error codes as defined for the async_data_write_start() function. 137 137 */ 138 int measured_strings_send( int phone, const measured_string_ref strings, size_t count);138 int measured_strings_send(int phone, const measured_string_ref strings, size_t count); 139 139 140 140 #endif -
uspace/srv/net/structures/module_map.c
rb5cbff4 r71b00dcc 47 47 #include "module_map.h" 48 48 49 GENERIC_CHAR_MAP_IMPLEMENT( modules, module_t)49 GENERIC_CHAR_MAP_IMPLEMENT(modules, module_t) 50 50 51 int add_module( module_ref * module, modules_ref modules, const char * name, const char * filename, services_t service, task_id_t task_id, connect_module_t connect_module){51 int add_module(module_ref * module, modules_ref modules, const char * name, const char * filename, services_t service, task_id_t task_id, connect_module_t connect_module){ 52 52 ERROR_DECLARE; 53 53 54 module_ref 54 module_ref tmp_module; 55 55 56 tmp_module = ( module_ref ) malloc( sizeof( module_t )); 57 if( ! tmp_module ) return ENOMEM; 56 tmp_module = (module_ref) malloc(sizeof(module_t)); 57 if(! tmp_module){ 58 return ENOMEM; 59 } 58 60 tmp_module->task_id = task_id; 59 61 tmp_module->phone = 0; … … 63 65 tmp_module->service = service; 64 66 tmp_module->connect_module = connect_module; 65 if( ERROR_OCCURRED( modules_add( modules, tmp_module->name, 0, tmp_module))){66 free( tmp_module);67 if(ERROR_OCCURRED(modules_add(modules, tmp_module->name, 0, tmp_module))){ 68 free(tmp_module); 67 69 return ERROR_CODE; 68 70 } 69 if( module ) * module = tmp_module; 71 if(module){ 72 *module = tmp_module; 73 } 70 74 return EOK; 71 75 } 72 76 73 module_ref get_running_module( modules_ref modules, char * name){74 module_ref 77 module_ref get_running_module(modules_ref modules, char * name){ 78 module_ref module; 75 79 76 module = modules_find( modules, name, 0 ); 77 if( ! module ) return NULL; 78 if( ! module->task_id ){ 79 module->task_id = spawn( module->filename ); 80 if( ! module->task_id ) return NULL; 80 module = modules_find(modules, name, 0); 81 if(! module){ 82 return NULL; 81 83 } 82 if( ! module->phone ){ 83 module->phone = module->connect_module( module->service ); 84 if(! module->task_id){ 85 module->task_id = spawn(module->filename); 86 if(! module->task_id){ 87 return NULL; 88 } 89 } 90 if(! module->phone){ 91 module->phone = module->connect_module(module->service); 84 92 } 85 93 return module; 86 94 } 87 95 88 task_id_t spawn( const char * fname){89 const char * argv[ 2];90 task_id_t 96 task_id_t spawn(const char * fname){ 97 const char * argv[2]; 98 task_id_t res; 91 99 92 argv[ 0] = fname;93 argv[ 1] = NULL;94 res = task_spawn( fname, argv);100 argv[0] = fname; 101 argv[1] = NULL; 102 res = task_spawn(fname, argv); 95 103 96 104 return res; -
uspace/srv/net/structures/module_map.h
rb5cbff4 r71b00dcc 60 60 * @see generic_char_map.h 61 61 */ 62 GENERIC_CHAR_MAP_DECLARE( modules, module_t)62 GENERIC_CHAR_MAP_DECLARE(modules, module_t) 63 63 64 64 /** Module structure. … … 67 67 /** Module task identifier if running. 68 68 */ 69 task_id_t 69 task_id_t task_id; 70 70 /** Module service identifier. 71 71 */ 72 services_t 72 services_t service; 73 73 /** Module phone if running and connected. 74 74 */ 75 int 75 int phone; 76 76 /** Usage counter. 77 77 */ 78 int 78 int usage; 79 79 /** Module name. 80 80 */ 81 const char * 81 const char * name; 82 82 /** Module full path filename. 83 83 */ 84 const char * 84 const char * filename; 85 85 /** Connecting function. 86 86 */ 87 connect_module_t * 87 connect_module_t * connect_module; 88 88 }; 89 89 … … 99 99 * @returns ENOMEM if there is not enough memory left. 100 100 */ 101 int add_module( module_ref * module, modules_ref modules, const char * name, const char * filename, services_t service, task_id_t task_id, connect_module_t * connect_module);101 int add_module(module_ref * module, modules_ref modules, const char * name, const char * filename, services_t service, task_id_t task_id, connect_module_t * connect_module); 102 102 103 103 /** Searches and returns the specified module. … … 109 109 * @returns NULL if there is no such module. 110 110 */ 111 module_ref get_running_module( modules_ref modules, char * name);111 module_ref get_running_module(modules_ref modules, char * name); 112 112 113 113 /** Starts the given module. … … 116 116 * @returns 0 if there is no such module. 117 117 */ 118 task_id_t spawn( const char * fname);118 task_id_t spawn(const char * fname); 119 119 120 120 #endif -
uspace/srv/net/structures/packet/packet.c
rb5cbff4 r71b00dcc 59 59 * @param[in] packet_id The packet identifier. 60 60 */ 61 #define PACKET_MAP_PAGE( packet_id ) ((( packet_id ) - 1 ) / PACKET_MAP_SIZE)61 #define PACKET_MAP_PAGE(packet_id) (((packet_id) - 1) / PACKET_MAP_SIZE) 62 62 63 63 /** Returns the packet index in the corresponding packet map page. 64 64 * @param[in] packet_id The packet identifier. 65 65 */ 66 #define PACKET_MAP_INDEX( packet_id ) ((( packet_id ) - 1 ) % PACKET_MAP_SIZE)66 #define PACKET_MAP_INDEX(packet_id) (((packet_id) - 1) % PACKET_MAP_SIZE) 67 67 68 68 /** Type definition of the packet map page. 69 69 */ 70 typedef packet_t packet_map_t[ PACKET_MAP_SIZE];70 typedef packet_t packet_map_t[PACKET_MAP_SIZE]; 71 71 /** Type definition of the packet map page pointer. 72 72 */ … … 77 77 * @see generic_field.h 78 78 */ 79 GENERIC_FIELD_DECLARE( gpm, packet_map_t);79 GENERIC_FIELD_DECLARE(gpm, packet_map_t); 80 80 81 81 /** Releases the packet. … … 84 84 * @returns EINVAL if the packet is not valid. 85 85 */ 86 int packet_destroy( packet_t packet);86 int packet_destroy(packet_t packet); 87 87 88 88 /** Packet map global data. … … 91 91 /** Safety lock. 92 92 */ 93 fibril_rwlock_t 93 fibril_rwlock_t lock; 94 94 /** Packet map. 95 95 */ 96 gpm_t 96 gpm_t packet_map; 97 97 } pm_globals; 98 98 99 GENERIC_FIELD_IMPLEMENT( gpm, packet_map_t ); 100 101 int packet_destroy( packet_t packet ){ 102 if( ! packet_is_valid( packet )) return EINVAL; 103 return munmap( packet, packet->length ); 104 } 105 106 int pm_init( void ){ 99 GENERIC_FIELD_IMPLEMENT(gpm, packet_map_t); 100 101 int packet_destroy(packet_t packet){ 102 if(! packet_is_valid(packet)){ 103 return EINVAL; 104 } 105 return munmap(packet, packet->length); 106 } 107 108 int pm_init(void){ 107 109 ERROR_DECLARE; 108 110 109 fibril_rwlock_initialize( & pm_globals.lock);110 fibril_rwlock_write_lock( & pm_globals.lock);111 ERROR_PROPAGATE( gpm_initialize( & pm_globals.packet_map));112 fibril_rwlock_write_unlock( & pm_globals.lock);113 return EOK; 114 } 115 116 packet_t pm_find( packet_id_t packet_id){111 fibril_rwlock_initialize(&pm_globals.lock); 112 fibril_rwlock_write_lock(&pm_globals.lock); 113 ERROR_PROPAGATE(gpm_initialize(&pm_globals.packet_map)); 114 fibril_rwlock_write_unlock(&pm_globals.lock); 115 return EOK; 116 } 117 118 packet_t pm_find(packet_id_t packet_id){ 117 119 packet_map_ref map; 118 120 packet_t packet; 119 121 120 if( ! packet_id ) return NULL; 121 fibril_rwlock_read_lock( & pm_globals.lock ); 122 if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )){ 123 fibril_rwlock_read_unlock( & pm_globals.lock ); 124 return NULL; 125 } 126 map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet_id )); 127 if( ! map ){ 128 fibril_rwlock_read_unlock( & pm_globals.lock ); 129 return NULL; 130 } 131 packet = ( * map )[ PACKET_MAP_INDEX( packet_id ) ]; 132 fibril_rwlock_read_unlock( & pm_globals.lock ); 122 if(! packet_id){ 123 return NULL; 124 } 125 fibril_rwlock_read_lock(&pm_globals.lock); 126 if(packet_id > PACKET_MAP_SIZE * gpm_count(&pm_globals.packet_map)){ 127 fibril_rwlock_read_unlock(&pm_globals.lock); 128 return NULL; 129 } 130 map = gpm_get_index(&pm_globals.packet_map, PACKET_MAP_PAGE(packet_id)); 131 if(! map){ 132 fibril_rwlock_read_unlock(&pm_globals.lock); 133 return NULL; 134 } 135 packet = (*map)[PACKET_MAP_INDEX(packet_id)]; 136 fibril_rwlock_read_unlock(&pm_globals.lock); 133 137 return packet; 134 138 } 135 139 136 int pm_add( packet_t packet){140 int pm_add(packet_t packet){ 137 141 ERROR_DECLARE; 138 142 139 143 packet_map_ref map; 140 144 141 if( ! packet_is_valid( packet )) return EINVAL; 142 fibril_rwlock_write_lock( & pm_globals.lock ); 143 if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.packet_map )){ 144 map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet->packet_id )); 145 if(! packet_is_valid(packet)){ 146 return EINVAL; 147 } 148 fibril_rwlock_write_lock(&pm_globals.lock); 149 if(PACKET_MAP_PAGE(packet->packet_id) < gpm_count(&pm_globals.packet_map)){ 150 map = gpm_get_index(&pm_globals.packet_map, PACKET_MAP_PAGE(packet->packet_id)); 145 151 }else{ 146 152 do{ 147 map = ( packet_map_ref ) malloc( sizeof( packet_map_t));148 if( ! map){149 fibril_rwlock_write_unlock( & pm_globals.lock);153 map = (packet_map_ref) malloc(sizeof(packet_map_t)); 154 if(! map){ 155 fibril_rwlock_write_unlock(&pm_globals.lock); 150 156 return ENOMEM; 151 157 } 152 bzero( map, sizeof( packet_map_t));153 if(( ERROR_CODE = gpm_add( & pm_globals.packet_map, map )) < 0){154 fibril_rwlock_write_unlock( & pm_globals.lock);155 free( map);158 bzero(map, sizeof(packet_map_t)); 159 if((ERROR_CODE = gpm_add(&pm_globals.packet_map, map)) < 0){ 160 fibril_rwlock_write_unlock(&pm_globals.lock); 161 free(map); 156 162 return ERROR_CODE; 157 163 } 158 }while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.packet_map));159 } 160 ( * map )[ PACKET_MAP_INDEX( packet->packet_id )] = packet;161 fibril_rwlock_write_unlock( & pm_globals.lock);162 return EOK; 163 } 164 165 void pm_destroy( void){164 }while(PACKET_MAP_PAGE(packet->packet_id) >= gpm_count(&pm_globals.packet_map)); 165 } 166 (*map)[PACKET_MAP_INDEX(packet->packet_id)] = packet; 167 fibril_rwlock_write_unlock(&pm_globals.lock); 168 return EOK; 169 } 170 171 void pm_destroy(void){ 166 172 int count; 167 173 int index; … … 169 175 packet_t packet; 170 176 171 fibril_rwlock_write_lock( & pm_globals.lock);172 count = gpm_count( & pm_globals.packet_map);173 while( count > 0){174 map = gpm_get_index( & pm_globals.packet_map, count - 1);175 for( index = PACKET_MAP_SIZE - 1; index >= 0; -- index){176 packet = ( * map )[ index];177 if( packet_is_valid( packet)){178 munmap( packet, packet->length);177 fibril_rwlock_write_lock(&pm_globals.lock); 178 count = gpm_count(&pm_globals.packet_map); 179 while(count > 0){ 180 map = gpm_get_index(&pm_globals.packet_map, count - 1); 181 for(index = PACKET_MAP_SIZE - 1; index >= 0; -- index){ 182 packet = (*map)[index]; 183 if(packet_is_valid(packet)){ 184 munmap(packet, packet->length); 179 185 } 180 186 } 181 187 } 182 gpm_destroy( & pm_globals.packet_map);188 gpm_destroy(&pm_globals.packet_map); 183 189 // leave locked 184 190 } 185 191 186 int pq_add( packet_t * first, packet_t packet, size_t order, size_t metric ){ 187 packet_t item; 188 189 if(( ! first ) || ( ! packet_is_valid( packet ))) return EINVAL; 190 pq_set_order( packet, order, metric ); 191 if( packet_is_valid( * first )){ 192 int pq_add(packet_t * first, packet_t packet, size_t order, size_t metric){ 193 packet_t item; 194 195 if((! first) || (! packet_is_valid(packet))){ 196 return EINVAL; 197 } 198 pq_set_order(packet, order, metric); 199 if(packet_is_valid(*first)){ 192 200 item = * first; 193 201 do{ 194 if( item->order < order){195 if( item->next){196 item = pm_find( item->next);202 if(item->order < order){ 203 if(item->next){ 204 item = pm_find(item->next); 197 205 }else{ 198 206 item->next = packet->packet_id; … … 204 212 packet->next = item->packet_id; 205 213 item->previous = packet->packet_id; 206 item = pm_find( packet->previous);207 if( item){214 item = pm_find(packet->previous); 215 if(item){ 208 216 item->next = packet->packet_id; 209 217 }else{ 210 * 218 *first = packet; 211 219 } 212 220 return EOK; 213 221 } 214 }while( packet_is_valid( item));215 } 216 * 217 return EOK; 218 } 219 220 packet_t pq_find( packet_t packet, size_t order){221 packet_t 222 223 if( ! packet_is_valid( packet )) return NULL;224 if( packet->order == order ) return packet;225 item = pm_find( packet->next );226 while( item && ( item != packet )){227 item = pm_find( item->next );228 if( item->order == order){222 }while(packet_is_valid(item)); 223 } 224 *first = packet; 225 return EOK; 226 } 227 228 packet_t pq_find(packet_t packet, size_t order){ 229 packet_t item; 230 231 if(! packet_is_valid(packet)){ 232 return NULL; 233 } 234 item = packet; 235 do{ 236 if(item->order == order){ 229 237 return item; 230 238 } 231 } 239 item = pm_find(item->next); 240 }while(item && (item != packet) && packet_is_valid(item)); 232 241 return NULL; 233 242 } 234 243 235 int pq_insert_after( packet_t packet, packet_t new_packet ){ 236 packet_t item; 237 238 if( !( packet_is_valid( packet ) && packet_is_valid( new_packet ))) return EINVAL; 244 int pq_insert_after(packet_t packet, packet_t new_packet){ 245 packet_t item; 246 247 if(!(packet_is_valid(packet) && packet_is_valid(new_packet))){ 248 return EINVAL; 249 } 239 250 new_packet->previous = packet->packet_id; 240 251 new_packet->next = packet->next; 241 item = pm_find( packet->next ); 242 if( item ) item->previous = new_packet->packet_id; 252 item = pm_find(packet->next); 253 if(item){ 254 item->previous = new_packet->packet_id; 255 } 243 256 packet->next = new_packet->packet_id; 244 257 return EOK; 245 258 } 246 259 247 packet_t pq_detach( packet_t packet){260 packet_t pq_detach(packet_t packet){ 248 261 packet_t next; 249 262 packet_t previous; 250 263 251 if( ! packet_is_valid( packet )) return NULL; 252 next = pm_find( packet->next ); 253 if( next ){ 264 if(! packet_is_valid(packet)){ 265 return NULL; 266 } 267 next = pm_find(packet->next); 268 if(next){ 254 269 next->previous = packet->previous; 255 previous = pm_find( next->previous);256 if( previous){270 previous = pm_find(next->previous); 271 if(previous){ 257 272 previous->next = next->packet_id; 258 273 } … … 263 278 } 264 279 265 int pq_set_order( packet_t packet, size_t order, size_t metric ){ 266 if( ! packet_is_valid( packet )) return EINVAL; 280 int pq_set_order(packet_t packet, size_t order, size_t metric){ 281 if(! packet_is_valid(packet)){ 282 return EINVAL; 283 } 267 284 packet->order = order; 268 285 packet->metric = metric; … … 270 287 } 271 288 272 int pq_get_order( packet_t packet, size_t * order, size_t * metric ){ 273 if( ! packet_is_valid( packet )) return EINVAL; 274 if( order ) * order = packet->order; 275 if( metric ) * metric = packet->metric; 276 return EOK; 277 } 278 279 void pq_destroy( packet_t first, void ( * packet_release )( packet_t packet )){ 280 packet_t actual; 281 packet_t next; 289 int pq_get_order(packet_t packet, size_t * order, size_t * metric){ 290 if(! packet_is_valid(packet)){ 291 return EINVAL; 292 } 293 if(order){ 294 *order = packet->order; 295 } 296 if(metric){ 297 *metric = packet->metric; 298 } 299 return EOK; 300 } 301 302 void pq_destroy(packet_t first, void (*packet_release)(packet_t packet)){ 303 packet_t actual; 304 packet_t next; 282 305 283 306 actual = first; 284 while( packet_is_valid( actual)){285 next = pm_find( actual->next);307 while(packet_is_valid(actual)){ 308 next = pm_find(actual->next); 286 309 actual->next = 0; 287 310 actual->previous = 0; 288 if( packet_release ) packet_release( actual ); 311 if(packet_release){ 312 packet_release(actual); 313 } 289 314 actual = next; 290 315 } 291 316 } 292 317 293 packet_t pq_next( packet_t packet ){ 294 if( ! packet_is_valid( packet )) return NULL; 295 return pm_find( packet->next ); 296 } 297 298 packet_t pq_previous( packet_t packet ){ 299 if( ! packet_is_valid( packet )) return NULL; 300 return pm_find( packet->previous ); 318 packet_t pq_next(packet_t packet){ 319 if(! packet_is_valid(packet)){ 320 return NULL; 321 } 322 return pm_find(packet->next); 323 } 324 325 packet_t pq_previous(packet_t packet){ 326 if(! packet_is_valid(packet)){ 327 return NULL; 328 } 329 return pm_find(packet->previous); 301 330 } 302 331 -
uspace/srv/net/structures/packet/packet.h
rb5cbff4 r71b00dcc 68 68 /** Reserved packet prefix length. 69 69 */ 70 size_t 70 size_t prefix; 71 71 /** Maximal packet content length. 72 72 */ 73 size_t 73 size_t content; 74 74 /** Reserved packet suffix length. 75 75 */ 76 size_t 76 size_t suffix; 77 77 /** Maximal packet address length. 78 78 */ 79 size_t 79 size_t addr_len; 80 80 }; 81 81 … … 89 89 * @returns NULL if the mapping does not exist. 90 90 */ 91 packet_t pm_find( packet_id_t packet_id);91 packet_t pm_find(packet_id_t packet_id); 92 92 93 93 /** Adds the packet mapping. … … 98 98 * @returns ENOMEM if there is not enough memory left. 99 99 */ 100 int pm_add( packet_t packet);100 int pm_add(packet_t packet); 101 101 102 102 /** Initializes the packet map. … … 104 104 * @returns ENOMEM if there is not enough memory left. 105 105 */ 106 int pm_init( void);106 int pm_init(void); 107 107 108 108 /** Releases the packet map. 109 109 */ 110 void pm_destroy( void);110 void pm_destroy(void); 111 111 112 112 /** Add packet to the sorted queue. … … 121 121 * @returns EINVAL if the packet is not valid. 122 122 */ 123 int pq_add( packet_t * first, packet_t packet, size_t order, size_t metric);123 int pq_add(packet_t * first, packet_t packet, size_t order, size_t metric); 124 124 125 125 /** Finds the packet with the given order. … … 130 130 * @returns NULL if the packet is not found. 131 131 */ 132 packet_t pq_find( packet_t first, size_t order);132 packet_t pq_find(packet_t first, size_t order); 133 133 134 134 /** Inserts packet after the given one. … … 138 138 * @returns EINVAL if etiher of the packets is invalid. 139 139 */ 140 int pq_insert_after( packet_t packet, packet_t new_packet);140 int pq_insert_after(packet_t packet, packet_t new_packet); 141 141 142 142 /** Detach the packet from the queue. … … 146 146 * @returns NULL if the packet is not valid. 147 147 */ 148 packet_t pq_detach( packet_t packet);148 packet_t pq_detach(packet_t packet); 149 149 150 150 /** Sets the packet order and metric attributes. … … 155 155 * @returns EINVAL if the packet is invalid.. 156 156 */ 157 int pq_set_order( packet_t packet, size_t order, size_t metric);157 int pq_set_order(packet_t packet, size_t order, size_t metric); 158 158 159 159 /** Sets the packet order and metric attributes. … … 164 164 * @returns EINVAL if the packet is invalid.. 165 165 */ 166 int pq_get_order( packet_t packet, size_t * order, size_t * metric);166 int pq_get_order(packet_t packet, size_t * order, size_t * metric); 167 167 168 168 /** Releases the whole queue. … … 171 171 * @param[in] packet_release The releasing function called for each of the packets after its detachment. 172 172 */ 173 void pq_destroy( packet_t first, void ( * packet_release )( packet_t packet));173 void pq_destroy(packet_t first, void (*packet_release)(packet_t packet)); 174 174 175 175 /** Returns the next packet in the queue. … … 179 179 * @returns NULL if the packet is not valid. 180 180 */ 181 packet_t pq_next( packet_t packet);181 packet_t pq_next(packet_t packet); 182 182 183 183 /** Returns the previous packet in the queue. … … 187 187 * @returns NULL if the packet is not valid. 188 188 */ 189 packet_t pq_previous( packet_t packet);189 packet_t pq_previous(packet_t packet); 190 190 191 191 /*@}*/ -
uspace/srv/net/structures/packet/packet_client.c
rb5cbff4 r71b00dcc 48 48 #include "packet_client.h" 49 49 50 int packet_copy_data( packet_t packet, const void * data, size_t length ){ 51 if( ! packet_is_valid( packet )) return EINVAL; 52 if( packet->data_start + length >= packet->length ) return ENOMEM; 53 memcpy(( void * ) packet + packet->data_start, data, length ); 54 if( packet->data_start + length > packet->data_end ){ 50 int packet_copy_data(packet_t packet, const void * data, size_t length){ 51 if(! packet_is_valid(packet)){ 52 return EINVAL; 53 } 54 if(packet->data_start + length >= packet->length){ 55 return ENOMEM; 56 } 57 memcpy((void *) packet + packet->data_start, data, length); 58 if(packet->data_start + length > packet->data_end){ 55 59 packet->data_end = packet->data_start + length; 56 60 } … … 58 62 } 59 63 60 void * packet_prefix( packet_t packet, size_t length ){ 61 if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * ( packet->dest_addr - packet->src_addr ) < length )) return NULL; 64 void * packet_prefix(packet_t packet, size_t length){ 65 if((! packet_is_valid(packet)) || (packet->data_start - sizeof(struct packet) - 2 * (packet->dest_addr - packet->src_addr) < length)){ 66 return NULL; 67 } 62 68 packet->data_start -= length; 63 return ( void *) packet + packet->data_start;69 return (void *) packet + packet->data_start; 64 70 } 65 71 66 void * packet_suffix( packet_t packet, size_t length ){ 67 if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL; 72 void * packet_suffix(packet_t packet, size_t length){ 73 if((! packet_is_valid(packet)) || (packet->data_end + length >= packet->length)){ 74 return NULL; 75 } 68 76 packet->data_end += length; 69 return ( void *) packet + packet->data_end - length;77 return (void *) packet + packet->data_end - length; 70 78 } 71 79 72 int packet_trim( packet_t packet, size_t prefix, size_t suffix ){ 73 if( ! packet_is_valid( packet )) return EINVAL; 74 if( prefix + suffix > PACKET_DATA_LENGTH( packet )) return ENOMEM; 80 int packet_trim(packet_t packet, size_t prefix, size_t suffix){ 81 if(! packet_is_valid(packet)){ 82 return EINVAL; 83 } 84 if(prefix + suffix > PACKET_DATA_LENGTH(packet)){ 85 return ENOMEM; 86 } 75 87 packet->data_start += prefix; 76 88 packet->data_end -= suffix; … … 78 90 } 79 91 80 packet_id_t packet_get_id( const packet_t packet){81 return packet_is_valid( packet) ? packet->packet_id : 0;92 packet_id_t packet_get_id(const packet_t packet){ 93 return packet_is_valid(packet) ? packet->packet_id : 0; 82 94 } 83 95 84 int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){ 85 if( ! packet_is_valid( packet )) return EINVAL; 86 if( ! packet->addr_len ) return 0; 87 if( src ) * src = ( void * ) packet + packet->src_addr; 88 if( dest ) * dest = ( void * ) packet + packet->dest_addr; 96 int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest){ 97 if(! packet_is_valid(packet)){ 98 return EINVAL; 99 } 100 if(! packet->addr_len){ 101 return 0; 102 } 103 if(src){ 104 *src = (void *) packet + packet->src_addr; 105 } 106 if(dest){ 107 *dest = (void *) packet + packet->dest_addr; 108 } 89 109 return packet->addr_len; 90 110 } 91 111 92 size_t packet_get_data_length( const packet_t packet ){ 93 if( ! packet_is_valid( packet )) return 0; 94 return PACKET_DATA_LENGTH( packet ); 112 size_t packet_get_data_length(const packet_t packet){ 113 if(! packet_is_valid(packet)){ 114 return 0; 115 } 116 return PACKET_DATA_LENGTH(packet); 95 117 } 96 118 97 void * packet_get_data( const packet_t packet ){ 98 if( ! packet_is_valid( packet )) return NULL; 99 return ( void * ) packet + packet->data_start; 119 void * packet_get_data(const packet_t packet){ 120 if(! packet_is_valid(packet)){ 121 return NULL; 122 } 123 return (void *) packet + packet->data_start; 100 124 } 101 125 102 int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len){103 size_t 104 size_t 126 int packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len){ 127 size_t padding; 128 size_t allocated; 105 129 106 if( ! packet_is_valid( packet )) return EINVAL; 107 allocated = PACKET_MAX_ADDRESS_LENGTH( packet ); 108 if( allocated < addr_len ) return ENOMEM; 130 if(! packet_is_valid(packet)){ 131 return EINVAL; 132 } 133 allocated = PACKET_MAX_ADDRESS_LENGTH(packet); 134 if(allocated < addr_len){ 135 return ENOMEM; 136 } 109 137 padding = allocated - addr_len; 110 138 packet->addr_len = addr_len; 111 if( src ){ 112 memcpy(( void * ) packet + packet->src_addr, src, addr_len ); 113 if( padding ) bzero(( void * ) packet + packet->src_addr + addr_len, padding ); 139 if(src){ 140 memcpy((void *) packet + packet->src_addr, src, addr_len); 141 if(padding){ 142 bzero((void *) packet + packet->src_addr + addr_len, padding); 143 } 114 144 }else{ 115 bzero(( void * ) packet + packet->src_addr, allocated);145 bzero((void *) packet + packet->src_addr, allocated); 116 146 } 117 if( dest ){ 118 memcpy(( void * ) packet + packet->dest_addr, dest, addr_len ); 119 if( padding ) bzero(( void * ) packet + packet->dest_addr + addr_len, padding ); 147 if(dest){ 148 memcpy((void *) packet + packet->dest_addr, dest, addr_len); 149 if(padding){ 150 bzero((void *) packet + packet->dest_addr + addr_len, padding); 151 } 120 152 }else{ 121 bzero(( void * ) packet + packet->dest_addr, allocated);153 bzero((void *) packet + packet->dest_addr, allocated); 122 154 } 123 155 return EOK; 124 156 } 125 157 126 packet_t packet_get_copy( int phone, packet_t packet){127 packet_t 128 uint8_t * 129 uint8_t * 130 size_t 158 packet_t packet_get_copy(int phone, packet_t packet){ 159 packet_t copy; 160 uint8_t * src; 161 uint8_t * dest; 162 size_t addrlen; 131 163 132 if( ! packet_is_valid( packet )) return NULL; 164 if(! packet_is_valid(packet)){ 165 return NULL; 166 } 133 167 // get a new packet 134 copy = packet_get_4( phone, PACKET_DATA_LENGTH( packet ), PACKET_MAX_ADDRESS_LENGTH( packet ), packet->max_prefix, PACKET_MIN_SUFFIX( packet )); 135 if( ! copy ) return NULL; 168 copy = packet_get_4(phone, PACKET_DATA_LENGTH(packet), PACKET_MAX_ADDRESS_LENGTH(packet), packet->max_prefix, PACKET_MIN_SUFFIX(packet)); 169 if(! copy){ 170 return NULL; 171 } 136 172 // get addresses 137 addrlen = packet_get_addr( packet, & src, & dest);173 addrlen = packet_get_addr(packet, &src, &dest); 138 174 // copy data 139 if(( packet_copy_data( copy, packet_get_data( packet ), PACKET_DATA_LENGTH( packet )) == EOK)175 if((packet_copy_data(copy, packet_get_data(packet), PACKET_DATA_LENGTH(packet)) == EOK) 140 176 // copy addresses if present 141 && (( addrlen <= 0 ) || ( packet_set_addr( copy, src, dest, addrlen ) == EOK))){177 && ((addrlen <= 0) || (packet_set_addr(copy, src, dest, addrlen) == EOK))){ 142 178 copy->order = packet->order; 143 179 copy->metric = packet->metric; 144 180 return copy; 145 181 }else{ 146 pq_release( phone, copy->packet_id);182 pq_release(phone, copy->packet_id); 147 183 return NULL; 148 184 } -
uspace/srv/net/structures/packet/packet_client.h
rb5cbff4 r71b00dcc 59 59 * @returns NULL if there is not enough memory left. 60 60 */ 61 #define PACKET_PREFIX( packet, type ) ( type * ) packet_prefix(( packet ), sizeof( type))61 #define PACKET_PREFIX(packet, type) (type *) packet_prefix((packet), sizeof(type)) 62 62 63 63 /** Allocates the specified type right after the actual packet content and returns its pointer. … … 69 69 * @returns NULL if there is not enough memory left. 70 70 */ 71 #define PACKET_SUFFIX( packet, type ) ( type * ) packet_suffix(( packet ), sizeof( type))71 #define PACKET_SUFFIX(packet, type) (type *) packet_suffix((packet), sizeof(type)) 72 72 73 73 /** Trims the actual packet content by the specified prefix and suffix types. … … 80 80 * @returns ENOMEM if there is not enough memory left. 81 81 */ 82 #define PACKET_TRIM( packet, prefix, suffix ) packet_trim(( packet ), sizeof( prefix ), sizeof( suffix))82 #define PACKET_TRIM(packet, prefix, suffix) packet_trim((packet), sizeof(prefix), sizeof(suffix)) 83 83 84 84 /** Allocates the specified space right before the actual packet content and returns its pointer. … … 88 88 * @returns NULL if there is not enough memory left. 89 89 */ 90 void * packet_prefix( packet_t packet, size_t length);90 void * packet_prefix(packet_t packet, size_t length); 91 91 92 92 /** Allocates the specified space right after the actual packet content and returns its pointer. … … 96 96 * @returns NULL if there is not enough memory left. 97 97 */ 98 void * packet_suffix( packet_t packet, size_t length);98 void * packet_suffix(packet_t packet, size_t length); 99 99 100 100 /** Trims the actual packet content by the specified prefix and suffix lengths. … … 106 106 * @returns ENOMEM if there is not enough memory left. 107 107 */ 108 int packet_trim( packet_t packet, size_t prefix, size_t suffix);108 int packet_trim(packet_t packet, size_t prefix, size_t suffix); 109 109 110 110 /** Copies the specified data to the beginning of the actual packet content. … … 117 117 * @returns ENOMEM if there is not enough memory left. 118 118 */ 119 int packet_copy_data( packet_t packet, const void * data, size_t length);119 int packet_copy_data(packet_t packet, const void * data, size_t length); 120 120 121 121 /** Returns the packet identifier. … … 124 124 * @returns Zero (0) if the packet is not valid. 125 125 */ 126 packet_id_t packet_get_id( const packet_t packet);126 packet_id_t packet_get_id(const packet_t packet); 127 127 128 128 /** Returns the packet content length. … … 131 131 * @returns Zero (0) if the packet is not valid. 132 132 */ 133 size_t packet_get_data_length( const packet_t packet);133 size_t packet_get_data_length(const packet_t packet); 134 134 135 135 /** Returns the pointer to the beginning of the packet content. … … 138 138 * @returns NULL if the packet is not valid. 139 139 */ 140 void * packet_get_data( const packet_t packet);140 void * packet_get_data(const packet_t packet); 141 141 142 142 /** Returns the stored packet addresses and their length. … … 148 148 * @returns EINVAL if the packet is not valid. 149 149 */ 150 int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest);150 int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest); 151 151 152 152 /** Sets the packet addresses. … … 159 159 * @returns ENOMEM if there is not enough memory left. 160 160 */ 161 int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len);161 int packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len); 162 162 163 163 /** Translates the packet identifier to the packet reference. … … 172 172 * @returns Other error codes as defined for the packet_return() function. 173 173 */ 174 int packet_translate( int phone, packet_ref packet, packet_id_t packet_id);174 int packet_translate(int phone, packet_ref packet, packet_id_t packet_id); 175 175 176 176 /** Obtains the packet of the given dimensions. … … 184 184 * @returns NULL on error. 185 185 */ 186 packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix);186 packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix); 187 187 188 188 /** Obtains the packet of the given content size. … … 193 193 * @returns NULL on error. 194 194 */ 195 packet_t packet_get_1( int phone, size_t content);195 packet_t packet_get_1(int phone, size_t content); 196 196 197 197 /** Releases the packet queue. … … 202 202 * @param[in] packet_id The packet identifier. 203 203 */ 204 void pq_release( int phone, packet_id_t packet_id);204 void pq_release(int phone, packet_id_t packet_id); 205 205 206 206 /** Returns the packet copy. … … 212 212 * @returns NULL on error. 213 213 */ 214 packet_t packet_get_copy( int phone, packet_t packet);214 packet_t packet_get_copy(int phone, packet_t packet); 215 215 216 216 /*@}*/ -
uspace/srv/net/structures/packet/packet_header.h
rb5cbff4 r71b00dcc 43 43 * @param[in] header The packet header. 44 44 */ 45 #define PACKET_DATA_LENGTH( header ) (( header )->data_end - ( header )->data_start)45 #define PACKET_DATA_LENGTH(header) ((header)->data_end - (header)->data_start) 46 46 47 47 /** Returns the maximum packet address length. 48 48 * @param[in] header The packet header. 49 49 */ 50 #define PACKET_MAX_ADDRESS_LENGTH( header ) (( header )->dest_addr - ( header )->src_addr)50 #define PACKET_MAX_ADDRESS_LENGTH(header) ((header)->dest_addr - (header)->src_addr) 51 51 52 52 /** Returns the minimum packet suffix. 53 53 * @param[in] header The packet header. 54 54 */ 55 #define PACKET_MIN_SUFFIX( header ) (( header )->length - ( header )->data_start - ( header )->max_content)55 #define PACKET_MIN_SUFFIX(header) ((header)->length - (header)->data_start - (header)->max_content) 56 56 57 57 /** Packet integrity check magic value. … … 64 64 /** Packet identifier. 65 65 */ 66 packet_id_t 66 packet_id_t packet_id; 67 67 /** Packet queue sorting value. 68 68 * The packet queue is sorted the ascending order. 69 69 */ 70 size_t 70 size_t order; 71 71 /** Packet metric. 72 72 */ 73 size_t 73 size_t metric; 74 74 /** Previous packet in the queue. 75 75 */ 76 packet_id_t 76 packet_id_t previous; 77 77 /** Next packet in the queue. 78 78 */ 79 packet_id_t 79 packet_id_t next; 80 80 /** Total length of the packet. 81 81 * Contains the header, the addresses and the data of the packet. 82 82 * Corresponds to the mapped sharable memory block. 83 83 */ 84 size_t 84 size_t length; 85 85 /** Stored source and destination addresses length. 86 86 */ 87 size_t 87 size_t addr_len; 88 88 /** Souce address offset in bytes from the beginning of the packet header. 89 89 */ 90 size_t 90 size_t src_addr; 91 91 /** Destination address offset in bytes from the beginning of the packet header. 92 92 */ 93 size_t 93 size_t dest_addr; 94 94 /** Reserved data prefix length in bytes. 95 95 */ 96 size_t 96 size_t max_prefix; 97 97 /** Reserved content length in bytes. 98 98 */ 99 size_t 99 size_t max_content; 100 100 /** Actual data start offset in bytes from the beginning of the packet header. 101 101 */ 102 size_t 102 size_t data_start; 103 103 /** Actual data end offset in bytes from the beginning of the packet header. 104 104 */ 105 size_t 105 size_t data_end; 106 106 /** Integrity check magic value. 107 107 */ 108 int 108 int magic_value; 109 109 }; 110 110 … … 114 114 * @returns false otherwise. 115 115 */ 116 static inline int packet_is_valid( const packet_t packet){117 return packet && ( packet->magic_value == PACKET_MAGIC_VALUE);116 static inline int packet_is_valid(const packet_t packet){ 117 return packet && (packet->magic_value == PACKET_MAGIC_VALUE); 118 118 } 119 119 -
uspace/srv/net/structures/packet/packet_messages.h
rb5cbff4 r71b00dcc 69 69 /** Returns the protocol service message parameter. 70 70 */ 71 #define ARP_GET_PROTO( call ) ( services_t ) IPC_GET_ARG2( * call)71 #define ARP_GET_PROTO(call) (services_t) IPC_GET_ARG2(*call) 72 72 73 73 /** Returns the packet identifier message parameter. 74 74 */ 75 #define IPC_GET_ID( call ) ( packet_id_t ) IPC_GET_ARG1( * call)75 #define IPC_GET_ID(call) (packet_id_t) IPC_GET_ARG1(*call) 76 76 77 77 /** Returns the maximal content length message parameter. 78 78 */ 79 #define IPC_GET_CONTENT( call ) ( size_t ) IPC_GET_ARG1( * call)79 #define IPC_GET_CONTENT(call) (size_t) IPC_GET_ARG1(*call) 80 80 81 81 /** Returns the maximal address length message parameter. 82 82 */ 83 #define IPC_GET_ADDR_LEN( call ) ( size_t ) IPC_GET_ARG2( * call)83 #define IPC_GET_ADDR_LEN(call) (size_t) IPC_GET_ARG2(*call) 84 84 85 85 /** Returns the maximal prefix length message parameter. 86 86 */ 87 #define IPC_GET_PREFIX( call ) ( size_t ) IPC_GET_ARG3( * call)87 #define IPC_GET_PREFIX(call) (size_t) IPC_GET_ARG3(*call) 88 88 89 89 /** Returns the maximal suffix length message parameter. 90 90 */ 91 #define IPC_GET_SUFFIX( call ) ( size_t ) IPC_GET_ARG4( * call)91 #define IPC_GET_SUFFIX(call) (size_t) IPC_GET_ARG4(*call) 92 92 93 93 #endif -
uspace/srv/net/structures/packet/packet_remote.c
rb5cbff4 r71b00dcc 60 60 * @returns Other error codes as defined for the async_share_in_start() function. 61 61 */ 62 int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size);62 int packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size); 63 63 64 int packet_translate( int phone, packet_ref packet, packet_id_t packet_id){64 int packet_translate(int phone, packet_ref packet, packet_id_t packet_id){ 65 65 ERROR_DECLARE; 66 66 67 ipcarg_t 68 packet_t 67 ipcarg_t size; 68 packet_t next; 69 69 70 if( ! packet ) return EINVAL; 71 * packet = pm_find( packet_id ); 72 if( !( * packet )){ 73 ERROR_PROPAGATE( async_req_1_1( phone, NET_PACKET_GET_SIZE, packet_id, & size )); 74 ERROR_PROPAGATE( packet_return( phone, packet, packet_id, size )); 70 if(! packet){ 71 return EINVAL; 75 72 } 76 if(( ** packet ).next ){ 77 return packet_translate( phone, & next, ( ** packet ).next ); 73 *packet = pm_find(packet_id); 74 if(!(*packet)){ 75 ERROR_PROPAGATE(async_req_1_1(phone, NET_PACKET_GET_SIZE, packet_id, &size)); 76 ERROR_PROPAGATE(packet_return(phone, packet, packet_id, size)); 77 } 78 if((** packet).next){ 79 return packet_translate(phone, &next, (** packet).next); 78 80 }else return EOK; 79 81 } 80 82 81 int packet_return( int phone, packet_ref packet, packet_id_t packet_id, size_t size){83 int packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size){ 82 84 ERROR_DECLARE; 83 85 84 aid_t 85 ipc_call_t 86 ipcarg_t 86 aid_t message; 87 ipc_call_t answer; 88 ipcarg_t result; 87 89 88 message = async_send_1( phone, NET_PACKET_GET, packet_id, & answer);89 * packet = ( packet_t ) as_get_mappable_page( size);90 if( ERROR_OCCURRED( async_share_in_start_0_0( phone, * packet, size))91 || ERROR_OCCURRED( pm_add( * packet))){92 munmap( * packet, size);93 async_wait_for( message, NULL);90 message = async_send_1(phone, NET_PACKET_GET, packet_id, &answer); 91 *packet = (packet_t) as_get_mappable_page(size); 92 if(ERROR_OCCURRED(async_share_in_start_0_0(phone, * packet, size)) 93 || ERROR_OCCURRED(pm_add(*packet))){ 94 munmap(*packet, size); 95 async_wait_for(message, NULL); 94 96 return ERROR_CODE; 95 97 } 96 async_wait_for( message, & result);98 async_wait_for(message, &result); 97 99 return result; 98 100 } 99 101 100 packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix){102 packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix){ 101 103 ERROR_DECLARE; 102 104 … … 105 107 packet_t packet; 106 108 107 if( ERROR_OCCURRED( async_req_4_2( phone, NET_PACKET_CREATE_4, max_content, addr_len, max_prefix, max_suffix, & packet_id, & size))){109 if(ERROR_OCCURRED(async_req_4_2(phone, NET_PACKET_CREATE_4, max_content, addr_len, max_prefix, max_suffix, &packet_id, &size))){ 108 110 return NULL; 109 111 } 110 packet = pm_find( packet_id);111 if( ! packet){112 if( ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size))){112 packet = pm_find(packet_id); 113 if(! packet){ 114 if(ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size))){ 113 115 return NULL; 114 116 } … … 117 119 } 118 120 119 packet_t packet_get_1( int phone, size_t content){121 packet_t packet_get_1(int phone, size_t content){ 120 122 ERROR_DECLARE; 121 123 122 ipcarg_t 123 ipcarg_t 124 packet_t 124 ipcarg_t packet_id; 125 ipcarg_t size; 126 packet_t packet; 125 127 126 if( ERROR_OCCURRED( async_req_1_2( phone, NET_PACKET_CREATE_1, content, & packet_id, & size))){128 if(ERROR_OCCURRED(async_req_1_2(phone, NET_PACKET_CREATE_1, content, &packet_id, &size))){ 127 129 return NULL; 128 130 } 129 packet = pm_find( packet_id);130 if( ! packet){131 if( ERROR_OCCURRED( packet_return( phone, & packet, packet_id, size))){131 packet = pm_find(packet_id); 132 if(! packet){ 133 if(ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size))){ 132 134 return NULL; 133 135 } … … 136 138 } 137 139 138 void pq_release( int phone, packet_id_t packet_id){139 async_msg_1( phone, NET_PACKET_RELEASE, packet_id);140 void pq_release(int phone, packet_id_t packet_id){ 141 async_msg_1(phone, NET_PACKET_RELEASE, packet_id); 140 142 } 141 143 -
uspace/srv/net/structures/packet/packet_server.c
rb5cbff4 r71b00dcc 76 76 /** Free packet queues. 77 77 */ 78 packet_t free[ FREE_QUEUES_COUNT];78 packet_t free[FREE_QUEUES_COUNT]; 79 79 /** Packet length upper bounds of the free packet queues. 80 80 * The maximal lengths of packets in each queue in the ascending order. 81 81 * The last queue is not limited. 82 82 */ 83 size_t sizes[ FREE_QUEUES_COUNT];83 size_t sizes[FREE_QUEUES_COUNT]; 84 84 /** Total packets allocated. 85 85 */ … … 89 89 .counter = 1, 90 90 .waiters = { 91 .prev = & 92 .next = & 91 .prev = &ps_globals.lock.waiters, 92 .next = &ps_globals.lock.waiters, 93 93 } 94 94 }, 95 .free = { NULL, NULL, NULL, NULL, NULL, NULL, NULL},96 .sizes = { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64},95 .free = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}, 96 .sizes = {PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64}, 97 97 .count = 0 98 98 }; … … 113 113 * @returns NULL if there is not enough memory left. 114 114 */ 115 packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix);115 packet_t packet_get(size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix); 116 116 117 117 /** Releases the packet queue. … … 120 120 * @returns ENOENT if there is no such packet. 121 121 */ 122 int packet_release_wrapper( packet_id_t packet_id);122 int packet_release_wrapper(packet_id_t packet_id); 123 123 124 124 /** Releases the packet and returns it to the appropriate free packet queue. … … 126 126 * @param[in] packet The packet to be released. 127 127 */ 128 void packet_release( packet_t packet);128 void packet_release(packet_t packet); 129 129 130 130 /** Creates a new packet of dimensions at least as given. … … 138 138 * @returns NULL if there is not enough memory left. 139 139 */ 140 packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix);140 packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix); 141 141 142 142 /** Clears and initializes the packet according to the given dimensions. … … 147 147 * @param[in] max_suffix The maximal suffix length in bytes. 148 148 */ 149 void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix);149 void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix); 150 150 151 151 /** Shares the packet memory block. … … 157 157 * @returns Other error codes as defined for the async_share_in_finalize() function. 158 158 */ 159 int packet_reply( const packet_t packet);159 int packet_reply(const packet_t packet); 160 160 161 161 /*@}*/ 162 162 163 int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){ 164 if( ! packet ) return EINVAL; 165 * packet = pm_find( packet_id ); 166 return ( * packet ) ? EOK : ENOENT; 167 } 168 169 packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){ 170 return packet_get( addr_len, max_prefix, max_content, max_suffix ); 171 } 172 173 packet_t packet_get_1( int phone, size_t content ){ 174 return packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX ); 175 } 176 177 void pq_release( int phone, packet_id_t packet_id ){ 178 ( void ) packet_release_wrapper( packet_id ); 179 } 180 181 int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ 163 int packet_translate(int phone, packet_ref packet, packet_id_t packet_id){ 164 if(! packet){ 165 return EINVAL; 166 } 167 *packet = pm_find(packet_id); 168 return (*packet) ? EOK : ENOENT; 169 } 170 171 packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix){ 172 return packet_get(addr_len, max_prefix, max_content, max_suffix); 173 } 174 175 packet_t packet_get_1(int phone, size_t content){ 176 return packet_get(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX); 177 } 178 179 void pq_release(int phone, packet_id_t packet_id){ 180 (void) packet_release_wrapper(packet_id); 181 } 182 183 int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){ 182 184 packet_t packet; 183 185 184 * 185 switch( IPC_GET_METHOD( * call)){186 *answer_count = 0; 187 switch(IPC_GET_METHOD(*call)){ 186 188 case IPC_M_PHONE_HUNGUP: 187 189 return EOK; 188 190 case NET_PACKET_CREATE_1: 189 packet = packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT( call ), DEFAULT_SUFFIX ); 190 if( ! packet ) return ENOMEM; 191 * answer_count = 2; 192 IPC_SET_ARG1( * answer, packet->packet_id ); 193 IPC_SET_ARG2( * answer, packet->length ); 191 packet = packet_get(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT(call), DEFAULT_SUFFIX); 192 if(! packet){ 193 return ENOMEM; 194 } 195 *answer_count = 2; 196 IPC_SET_ARG1(*answer, packet->packet_id); 197 IPC_SET_ARG2(*answer, packet->length); 194 198 return EOK; 195 199 case NET_PACKET_CREATE_4: 196 packet = packet_get((( DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN( call )) ? IPC_GET_ADDR_LEN( call ) : DEFAULT_ADDR_LEN ), DEFAULT_PREFIX + IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), DEFAULT_SUFFIX + IPC_GET_SUFFIX( call )); 197 if( ! packet ) return ENOMEM; 198 * answer_count = 2; 199 IPC_SET_ARG1( * answer, packet->packet_id ); 200 IPC_SET_ARG2( * answer, packet->length ); 200 packet = packet_get(((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(call)) ? IPC_GET_ADDR_LEN(call) : DEFAULT_ADDR_LEN), DEFAULT_PREFIX + IPC_GET_PREFIX(call), IPC_GET_CONTENT(call), DEFAULT_SUFFIX + IPC_GET_SUFFIX(call)); 201 if(! packet){ 202 return ENOMEM; 203 } 204 *answer_count = 2; 205 IPC_SET_ARG1(*answer, packet->packet_id); 206 IPC_SET_ARG2(*answer, packet->length); 201 207 return EOK; 202 208 case NET_PACKET_GET: 203 packet = pm_find( IPC_GET_ID( call )); 204 if( ! packet_is_valid( packet )) return ENOENT; 205 return packet_reply( packet ); 209 packet = pm_find(IPC_GET_ID(call)); 210 if(! packet_is_valid(packet)){ 211 return ENOENT; 212 } 213 return packet_reply(packet); 206 214 case NET_PACKET_GET_SIZE: 207 packet = pm_find( IPC_GET_ID( call )); 208 if( ! packet_is_valid( packet )) return ENOENT; 209 IPC_SET_ARG1( * answer, packet->length ); 210 * answer_count = 1; 215 packet = pm_find(IPC_GET_ID(call)); 216 if(! packet_is_valid(packet)){ 217 return ENOENT; 218 } 219 IPC_SET_ARG1(*answer, packet->length); 220 *answer_count = 1; 211 221 return EOK; 212 222 case NET_PACKET_RELEASE: 213 return packet_release_wrapper( IPC_GET_ID( call));223 return packet_release_wrapper(IPC_GET_ID(call)); 214 224 } 215 225 return ENOTSUP; 216 226 } 217 227 218 int packet_release_wrapper( packet_id_t packet_id ){ 219 packet_t packet; 220 221 packet = pm_find( packet_id ); 222 if( ! packet_is_valid( packet )) return ENOENT; 223 fibril_mutex_lock( & ps_globals.lock ); 224 pq_destroy( packet, packet_release ); 225 fibril_mutex_unlock( & ps_globals.lock ); 228 int packet_release_wrapper(packet_id_t packet_id){ 229 packet_t packet; 230 231 packet = pm_find(packet_id); 232 if(! packet_is_valid(packet)){ 233 return ENOENT; 234 } 235 fibril_mutex_lock(&ps_globals.lock); 236 pq_destroy(packet, packet_release); 237 fibril_mutex_unlock(&ps_globals.lock); 226 238 return EOK; 227 239 } 228 240 229 void packet_release( packet_t packet){241 void packet_release(packet_t packet){ 230 242 int index; 231 243 int result; 232 244 233 245 // remove debug dump 234 // printf( "packet %d released\n", packet->packet_id);235 for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index);236 result = pq_add( & ps_globals.free[ index ], packet, packet->length, packet->length);237 assert( result == EOK);238 } 239 240 packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){246 // printf("packet %d released\n", packet->packet_id); 247 for(index = 0; (index < FREE_QUEUES_COUNT - 1) && (packet->length > ps_globals.sizes[index]); ++ index); 248 result = pq_add(&ps_globals.free[index], packet, packet->length, packet->length); 249 assert(result == EOK); 250 } 251 252 packet_t packet_get(size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){ 241 253 int index; 242 254 packet_t packet; 243 255 size_t length; 244 256 245 length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE);246 fibril_mutex_lock( & ps_globals.lock);247 for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index){248 if( length <= ps_globals.sizes[ index ]){249 packet = ps_globals.free[ index];250 while( packet_is_valid( packet ) && ( packet->length < length)){251 packet = pm_find( packet->next);252 } 253 if( packet_is_valid( packet)){254 if( packet == ps_globals.free[ index ]){255 ps_globals.free[ index ] = pq_detach( packet);257 length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE); 258 fibril_mutex_lock(&ps_globals.lock); 259 for(index = 0; index < FREE_QUEUES_COUNT - 1; ++ index){ 260 if(length <= ps_globals.sizes[index]){ 261 packet = ps_globals.free[index]; 262 while(packet_is_valid(packet) && (packet->length < length)){ 263 packet = pm_find(packet->next); 264 } 265 if(packet_is_valid(packet)){ 266 if(packet == ps_globals.free[index]){ 267 ps_globals.free[index] = pq_detach(packet); 256 268 }else{ 257 pq_detach( packet);269 pq_detach(packet); 258 270 } 259 packet_init( packet, addr_len, max_prefix, max_content, max_suffix);260 fibril_mutex_unlock( & ps_globals.lock);271 packet_init(packet, addr_len, max_prefix, max_content, max_suffix); 272 fibril_mutex_unlock(&ps_globals.lock); 261 273 // remove debug dump 262 // printf( "packet %d got\n", packet->packet_id);274 // printf("packet %d got\n", packet->packet_id); 263 275 return packet; 264 276 } 265 277 } 266 278 } 267 packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix);268 fibril_mutex_unlock( & ps_globals.lock);279 packet = packet_create(length, addr_len, max_prefix, max_content, max_suffix); 280 fibril_mutex_unlock(&ps_globals.lock); 269 281 // remove debug dump 270 // printf( "packet %d created\n", packet->packet_id);282 // printf("packet %d created\n", packet->packet_id); 271 283 return packet; 272 284 } 273 285 274 packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){286 packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){ 275 287 ERROR_DECLARE; 276 288 277 packet_t 289 packet_t packet; 278 290 279 291 // already locked 280 packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); 281 if( packet == MAP_FAILED ) return NULL; 292 packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); 293 if(packet == MAP_FAILED){ 294 return NULL; 295 } 282 296 ++ ps_globals.count; 283 297 packet->packet_id = ps_globals.count; 284 298 packet->length = length; 285 packet_init( packet, addr_len, max_prefix, max_content, max_suffix);299 packet_init(packet, addr_len, max_prefix, max_content, max_suffix); 286 300 packet->magic_value = PACKET_MAGIC_VALUE; 287 if( ERROR_OCCURRED( pm_add( packet))){288 munmap( packet, packet->length);301 if(ERROR_OCCURRED(pm_add(packet))){ 302 munmap(packet, packet->length); 289 303 return NULL; 290 304 } … … 292 306 } 293 307 294 void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){308 void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){ 295 309 // clear the packet content 296 bzero((( void * ) packet ) + sizeof( struct packet ), packet->length - sizeof( struct packet));310 bzero(((void *) packet) + sizeof(struct packet), packet->length - sizeof(struct packet)); 297 311 // clear the packet header 298 312 packet->order = 0; … … 301 315 packet->next = 0; 302 316 packet->addr_len = 0; 303 packet->src_addr = sizeof( struct packet);317 packet->src_addr = sizeof(struct packet); 304 318 packet->dest_addr = packet->src_addr + addr_len; 305 319 packet->max_prefix = max_prefix; … … 309 323 } 310 324 311 int packet_reply( const packet_t packet ){ 312 ipc_callid_t callid; 313 size_t size; 314 315 if( ! packet_is_valid( packet )) return EINVAL; 316 if( async_share_in_receive( & callid, & size ) <= 0 ) return EINVAL; 317 if( size != packet->length ) return ENOMEM; 318 return async_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE ); 325 int packet_reply(const packet_t packet){ 326 ipc_callid_t callid; 327 size_t size; 328 329 if(! packet_is_valid(packet)){ 330 return EINVAL; 331 } 332 if(async_share_in_receive(&callid, &size) <= 0) return EINVAL; 333 if(size != packet->length){ 334 return ENOMEM; 335 } 336 return async_share_in_finalize(callid, packet, PROTO_READ | PROTO_WRITE); 319 337 } 320 338 -
uspace/srv/net/structures/packet/packet_server.h
rb5cbff4 r71b00dcc 56 56 * @returns Other error codes as defined for the packet_release_wrapper() function. 57 57 */ 58 int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count);58 int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count); 59 59 60 60 #endif
Note:
See TracChangeset
for help on using the changeset viewer.