Subversion Repositories HelenOS

Compare Revisions

No changes between revisions

Ignore whitespace Rev 3885 → Rev 3886

/branches/network/uspace/srv/net/structures/measured_strings.c
0,0 → 1,249
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
* A character string with measured length implementation file.
* @see measured_strings.h
*/
 
#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
 
#include <ipc/ipc.h>
 
#include "../err.h"
#include "../modules.h"
 
#include "measured_strings.h"
 
/** Computes the lengths of the measured strings in the given array.
* @param strings The measured strings array to be processed. Input parameter.
* @param count The measured strings array size. Input parameter.
* @returns The computed sizes array.
* @returns NULL if there is no memory left.
*/
size_t * prepare_lengths( const measured_string_ref strings, size_t count );
 
measured_string_ref measured_string_create_bulk( const char * string, size_t length ){
measured_string_ref new;
 
if( length == 0 ){
while( string[ length ] ) ++ length;
}
new = ( measured_string_ref ) malloc( sizeof( measured_string_t ) + ( sizeof( char ) * ( length + 1 )));
if( ! new ) return NULL;
new->length = length;
new->value = (( char * ) new ) + sizeof( measured_string_t );
// append terminating zero explicitly - to be safe
memcpy( new->value, string, new->length );
new->value[ new->length ] = '\0';
return new;
}
 
int measured_strings_receive( measured_string_ref * strings, char ** data, size_t count ){
ERROR_DECLARE;
 
size_t * lengths;
int index;
size_t length;
char * next;
ipc_callid_t callid;
 
if(( ! strings ) || ( ! data ) || ( count <= 0 )){
return EINVAL;
}
lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 ));
if( ! lengths ) return ENOMEM;
if(( ! ipc_data_write_receive( & callid, & length ))
|| ( length != sizeof( size_t ) * ( count + 1 ))){
free( lengths );
return EINVAL;
}
if( ERROR_OCCURED( ipc_data_write_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){
free( lengths );
return ERROR_CODE;
}
* data = malloc( lengths[ count ] );
if( !( * data )) return ENOMEM;
( * data )[ lengths[ count ] - 1 ] = '\0';
* strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count );
if( !( * strings )){
free( lengths );
free( * data );
return ENOMEM;
}
next = * data;
for( index = 0; index < count; ++ index ){
( * strings)[ index ].length = lengths[ index ];
if( lengths[ index ] > 0 ){
if(( ! ipc_data_write_receive( & callid, & length ))
|| ( length != lengths[ index ] )){
free( * data );
free( * strings );
free( lengths );
return EINVAL;
}
ERROR_PROPAGATE( ipc_data_write_finalize( callid, next, lengths[ index ] ));
( * strings)[ index ].value = next;
next += lengths[ index ];
* next = '\0';
++ next;
}else{
( * strings )[ index ].value = NULL;
}
}
free( lengths );
return EOK;
}
 
int measured_strings_reply( const measured_string_ref strings, size_t count ){
ERROR_DECLARE;
 
size_t * lengths;
int index;
size_t length;
ipc_callid_t callid;
 
if(( ! strings ) || ( count <= 0 )){
return EINVAL;
}
lengths = prepare_lengths( strings, count );
if( ! lengths ) return ENOMEM;
if(( ! ipc_data_read_receive( & callid, & length ))
|| ( length != sizeof( size_t ) * ( count + 1 ))){
free( lengths );
return EINVAL;
}
if( ERROR_OCCURED( ipc_data_read_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){
free( lengths );
return ERROR_CODE;
}
free( lengths );
for( index = 0; index < count; ++ index ){
if( strings[ index ].length > 0 ){
if(( ! ipc_data_read_receive( & callid, & length ))
|| ( length != strings[ index ].length )){
return EINVAL;
}
ERROR_PROPAGATE( ipc_data_read_finalize( callid, strings[ index ].value, strings[ index ].length ));
}
}
return EOK;
}
 
int measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count ){
ERROR_DECLARE;
 
size_t * lengths;
int index;
char * next;
 
if(( phone <= 0 ) || ( ! strings ) || ( ! data ) || ( count <= 0 )){
return EINVAL;
}
lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 ));
if( ! lengths ) return ENOMEM;
if( ERROR_OCCURED( ipc_data_read_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){
free( lengths );
return ERROR_CODE;
}
* data = malloc( lengths[ count ] );
if( !( * data )) return ENOMEM;
* strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count );
if( !( * strings )){
free( lengths );
free( * data );
return ENOMEM;
}
next = * data;
for( index = 0; index < count; ++ index ){
( * strings )[ index ].length = lengths[ index ];
if( lengths[ index ] > 0 ){
ERROR_PROPAGATE( ipc_data_read_start( phone, next, lengths[ index ] ));
( * strings )[ index ].value = next;
next += lengths[ index ];
* next = '\0';
++ next;
}else{
( * strings )[ index ].value = NULL;
}
}
free( lengths );
return EOK;
}
 
int measured_strings_send( int phone, const measured_string_ref strings, size_t count ){
ERROR_DECLARE;
 
size_t * lengths;
int index;
 
if(( phone <= 0 ) || ( ! strings ) || ( count <= 0 )){
return EINVAL;
}
lengths = prepare_lengths( strings, count );
if( ! lengths ) return ENOMEM;
if( ERROR_OCCURED( ipc_data_write_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){
free( lengths );
return ERROR_CODE;
}
free( lengths );
for( index = 0; index < count; ++ index ){
if( strings[ index ].length > 0 ){
ERROR_PROPAGATE( ipc_data_write_start( phone, strings[ index ].value, strings[ index ].length ));
}
}
return EOK;
}
 
size_t * prepare_lengths( const measured_string_ref strings, size_t count ){
size_t * lengths;
int index;
size_t length;
 
lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 ));
if( ! lengths ) return NULL;
length = 0;
for( index = 0; index < count; ++ index ){
lengths[ index ] = strings[ index ].length;
length += lengths[ index ] + 1;
}
lengths[ count ] = length;
return lengths;
}
 
/** @}
*/
 
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
Added: svn:mergeinfo
/branches/network/uspace/srv/net/structures/char_map.h
0,0 → 1,155
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
* A&nbsp;character string to integer map header file.
*/
 
#ifndef __CHAR_MAP_H__
#define __CHAR_MAP_H__
 
/** An&nbsp;invalid assigned value used also if an&nbsp;entry does not exist.
*/
#define CHAR_MAP_NULL ( -1 )
 
/** A&nbsp;type definition of a&nbsp;character string to integer map.
* @see char_map
*/
typedef struct char_map char_map_t;
 
/** A&nbsp;type definition of a&nbsp;character string to integer map pointer.
* @see char_map
*/
typedef char_map_t * char_map_ref;
 
/** A&nbsp;character string to integer map item.
* This structure recursivelly contains itself as a&nbsp;character by character tree.
* The actually mapped character string consists o fall the parent characters and the actual one.
*/
struct char_map{
 
/** An actually mapped character.
*/
char c;
 
/** A&nbsp;stored integral value.
*/
int value;
 
/** A&nbsp;next character array size.
*/
int size;
 
/** The first free position in the next character array.
*/
int next;
 
/** The next character array.
*/
char_map_ref * items;
 
/** The consistency check magic value.
*/
int magic;
};
 
/** Adds the value with the key to the map.
* @param map The character string to integer map. Input/output parameter.
* @param identifier The key zero ('\0') terminated character string. The key character string is processed until the first terminating zero ('\0') character after the given length is found. Input parameter.
* @param length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\0') character is found. Input parameter.
* @param value The integral value to be stored for the key character string. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the map is not valid.
* @returns EINVAL if the identifier parameter is NULL.
* @returns EINVAL if the length parameter zero (0) and the identifier parameter is an empty character string (the first character is the terminating zero ('\0) character.
* @returns EEXIST if the key character string is already used.
* @returns Other error codes as defined for the char_map_add_item() function.
*/
int char_map_add( char_map_ref map, const char * identifier, size_t length, const int value );
 
/** Clears and destroys the map.
* @param map The character string to integer map. Input/output parameter.
*/
void char_map_destroy( char_map_ref map );
 
/** Excludes the value assigned to the key from the map.
* The entry is cleared from the map.
* @param map The character string to integer map. Input/output parameter.
* @param identifier The key zero ('\0') terminated character string. The key character string is processed until the first terminating zero ('\0') character after the given length is found. Input parameter.
* @param length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\0') character is found. Input parameter.
* @returns The integral value assigned to the key character string.
* @returns CHAR_MAP_NULL if the key is not assigned a&nbsp;value.
*/
int char_map_exclude( char_map_ref map, const char * identifier, size_t length );
 
/** Returns the value assigned to the key from the map.
* @param map The character string to integer map. Input parameter.
* @param identifier The key zero ('\0') terminated character string. The key character string is processed until the first terminating zero ('\0') character after the given length is found. Input parameter.
* @param length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\0') character is found. Input parameter.
* @returns The integral value assigned to the key character string.
* @returns CHAR_MAP_NULL if the key is not assigned a&nbsp;value.
*/
int char_map_find( const char_map_ref map, const char * identifier, size_t length );
 
/** Returns the value assigned to the map.
* @param map The character string to integer map. Input parameter.
* @returns The integral value assigned to the map.
* @returns CHAR_MAP_NULL if the map is not assigned a&nbsp;value.
*/
int char_map_get_value( const char_map_ref map );
 
/** Initializes the map.
* @param map The character string to integer map. Input/output parameter.
* @returns EOK on success.
* @returns EINVAL if the map parameter is NULL.
* @returns ENOMEM if there is no memory left.
*/
int char_map_initialize( char_map_ref map );
 
/** Adds or updates the value with the key to the map.
* @param map The character string to integer map. Input/output parameter.
* @param identifier The key zero ('\0') terminated character string. The key character string is processed until the first terminating zero ('\0') character after the given length is found. Input parameter.
* @param length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\0') character is found. Input parameter.
* @param value The integral value to be stored for the key character string. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the map is not valid.
* @returns EINVAL if the identifier parameter is NULL.
* @returns EINVAL if the length parameter zero (0) and the identifier parameter is an empty character string (the first character is the terminating zero ('\0) character.
* @returns EEXIST if the key character string is already used.
* @returns Other error codes as defined for the char_map_add_item() function.
*/
int char_map_update( char_map_ref map, const char * identifier, size_t length, const int value );
 
#endif
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
Added: svn:mergeinfo
/branches/network/uspace/srv/net/structures/measured_strings.h
0,0 → 1,136
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
* A&nbsp;character string with measured length header file.
* This structure has been designed for serialization of character strings between modules.
*/
 
#ifndef __MEASURED_STRINGS_H__
#define __MEASURED_STRINGS_H__
 
/** A&nbsp;type definition of a&nbsp;character string with measured length.
* @see measured_string
*/
typedef struct measured_string measured_string_t;
 
/** A&nbsp;type definition of a&nbsp;character string with measured length pointer.
* @see measured_string
*/
typedef measured_string_t * measured_string_ref;
 
/** A&nbsp;character string with measured length.
* This structure has been designed for serialization of character strings between modules.
*/
struct measured_string{
 
/** The character string data.
*/
char * value;
 
/** The character string length.
*/
size_t length;
};
 
/** Creates a&nbsp;new measured string bundled with a&nbsp;copy of the given string itself as one memory block.
* If the measured string is being freed, whole memory block is freed.
* The measured string should be used only as a&nbsp;constant.
* @param string The initial character string to be stored. Input parameter.
* @param length The length of the given string without the terminating zero ('/0') character. If the length is zero (0), the actual length is computed. The given length is used and appended with the terminating zero ('\0') character otherwise. Input parameter.
* @returns The new bundled charecter string with measured length.
* @returns NULL if there is no memory left.
*/
measured_string_ref measured_string_create_bulk( const char * string, size_t length );
 
/** Receives a&nbsp;measured strings array from a&nbsp;calling module.
* Creates the array and the data memory blocks.
* This method should be used only while processing IPC messages as the array size has to be negotiated in advance.
* @param strings The received measured strings array. Output parameter.
* @param data The measured strings data. This memory block stores the actual character strings. Output parameter.
* @param count The size of the measured strings array. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the strings or data parameter is NULL.
* @returns EINVAL if the count parameter is zero (0).
* @returns EINVAL if the sent array differs in size.
* @returns EINVAL if there is inconsistency in sent measured strings' lengths (should not occur).
* @returns ENOMEM if there is no memory left.
* @returns Other error codes as defined for the ipc_data_write_finalize() function.
*/
int measured_strings_receive( measured_string_ref * strings, char ** data, size_t count );
 
/** Replies the given measured strings array to a&nbsp;calling module.
* This method should be used only while processing IPC messages as the array size has to be negotiated in advance.
* @param strings The measured strings array to be transferred. Input parameter.
* @param count The measured strings array size. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the strings parameter is NULL.
* @returns EINVAL if the count parameter is zero (0).
* @returns EINVAL if the calling module does not accept the given array size.
* @returns EINVAL if there is inconsistency in sent measured strings' lengths (should not occur).
* @returns Other error codes as defined for the ipc_data_read_finalize() function.
*/
int measured_strings_reply( const measured_string_ref strings, size_t count );
 
/** Receives a&nbsp;measured strings array from another module.
* Creates the array and the data memory blocks.
* This method should be used only following other IPC messages as the array size has to be negotiated in advance.
* @param phone The other module phone. Input parameter.
* @param strings The returned measured strings array. Output parameter.
* @param data The measured strings data. This memory block stores the actual character strings. Output parameter.
* @param count The size of the measured strings array. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the strings or data parameter is NULL.
* @returns EINVAL if the phone or count parameter is not positive (<=0).
* @returns EINVAL if the sent array differs in size.
* @returns ENOMEM if there is no memory left.
* @returns Other error codes as defined for the ipc_data_read_start() function.
*/
int measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count );
 
/** Sends the given measured strings array to another module.
* This method should be used only following other IPC messages as the array size has to be negotiated in advance.
* @param phone The other module phone. Input parameter.
* @param strings The measured strings array to be transferred. Input parameter.
* @param count The measured strings array size. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the strings parameter is NULL.
* @returns EINVAL if the phone or count parameter is not positive (<=0).
* @returns Other error codes as defined for the ipc_data_write_start() function.
*/
int measured_strings_send( int phone, const measured_string_ref strings, size_t count );
 
#endif
 
/** @}
*/
 
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
Added: svn:mergeinfo
/branches/network/uspace/srv/net/structures/generic_field.h
0,0 → 1,143
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/**
* @file
*/
 
#ifndef __GENERIC_FIELD_H__
#define __GENERIC_FIELD_H__
 
#include <errno.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
 
#define GENERIC_FIELD_MAGIC_VALUE 0x55667788
 
#define GENERIC_FIELD_DECLARE( name, type ) \
\
typedef struct name name##_t; \
typedef name##_t * name##_ref; \
\
struct name{ \
int size; \
int next; \
type ** items; \
int magic; \
}; \
\
int name##_add( name##_ref field, type * value ); \
int name##_count( name##_ref field ); \
void name##_destroy( name##_ref field ); \
void name##_exclude_index( name##_ref field, int index ); \
type ** name##_get_field( name##_ref field ); \
type * name##_get_index( name##_ref field, int index ); \
int name##_initialize( name##_ref field ); \
int name##_is_valid( name##_ref field );
 
#define GENERIC_FIELD_IMPLEMENT( name, type ) \
\
int name##_add( name##_ref field, type * value ){ \
if( name##_is_valid( field )){ \
if( field->next == ( field->size - 1 )){ \
type ** tmp; \
\
tmp = ( type ** ) malloc( sizeof( type * ) * 2 * field->size ); \
if( ! tmp ) return ENOMEM; \
field->size *= 2; \
memcpy( tmp, field->items, sizeof( type * ) * field->next ); \
free( field->items ); \
field->items = tmp; \
} \
field->items[ field->next ] = value; \
++ field->next; \
field->items[ field->next ] = NULL; \
return field->next - 1; \
} \
return EINVAL; \
} \
\
int name##_count( name##_ref field ){ \
return name##_is_valid( field ) ? field->next : -1; \
} \
\
void name##_destroy( name##_ref field ){ \
if( name##_is_valid( field )){ \
int index; \
\
field->magic = 0; \
for( index = 0; index < field->next; ++ index ){ \
free( field->items[ index ] ); \
} \
free( field->items ); \
} \
} \
\
void name##_exclude_index( name##_ref field, int index ){ \
if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \
free( field->items[ index ] ); \
field->items[ index ] = NULL; \
} \
} \
\
type * name##_get_index( name##_ref field, int index ){ \
if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \
return field->items[ index ]; \
} \
return NULL; \
} \
\
type ** name##_get_field( name##_ref field ){ \
return name##_is_valid( field ) ? field->items : NULL; \
} \
\
int name##_initialize( name##_ref field ){ \
if( ! field ) return EINVAL; \
field->size = 2; \
field->next = 0; \
field->items = ( type ** ) malloc( sizeof( type * ) * field->size ); \
if( ! field->items ) return ENOMEM; \
field->items[ field->next ] = NULL; \
field->magic = INT_MAP_MAGIC_VALUE; \
return EOK; \
} \
\
int name##_is_valid( name##_ref field ){ \
return field && ( field->magic == INT_MAP_MAGIC_VALUE ); \
}
 
#endif
 
/** @}
*/
 
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
Added: svn:mergeinfo
/branches/network/uspace/srv/net/structures/packet/packet_queue.h
0,0 → 1,61
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
*/
 
#ifndef __NET_PACKET_QUEUE__
#define __NET_PACKET_QUEUE__
 
#include "packet.h"
 
typedef struct pq_item pq_item_t;
typedef pq_item * pq_item_ref;
typedef pq_item_ref pq_head_t;
typedef pq_head * pq_head_ref;
 
struct pq_item{
int order;
size_t metric;
packet_t packet;
pq_item_ref previous;
pq_item_ref next;
};
 
pq_head_t pq_create( packet_t packet, int order, size_t metric );
pq_item_ref pq_add( pq_head_ref queue, packet_t packet, int order, size_t metric );
void pq_destroy( pq_head_t queue );
 
#endif
 
/** @}
*/
/branches/network/uspace/srv/net/structures/packet/packet.c
0,0 → 1,158
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
*/
 
#include <errno.h>
#include <unistd.h>
#include <string.h>
 
#include <ipc/ipc.h>
#include <sys/mman.h>
 
#include "../../err.h"
 
#include "packet.h"
 
#define PACKET_MAGIC_VALUE 0x11227788
 
struct packet{
size_t length;
size_t max_prefix;
size_t max_content;
size_t data_start;
size_t data_end;
int magic_value;
};
 
int packet_is_valid( packet_t packet );
 
packet_t packet_create( size_t max_prefix, size_t max_content, size_t max_sufix ){
size_t length;
packet_t packet;
 
length = max_prefix + max_content + max_sufix;
packet = ( packet_t ) mmap( NULL, sizeof( struct packet ) + length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
if( packet == MAP_FAILED ) return NULL;
packet->length = length;
packet->max_prefix = max_prefix;
packet->max_content = max_content;
packet->data_start = sizeof( struct packet ) + packet->max_prefix;
packet->data_end = packet->data_start;
packet->magic_value = PACKET_MAGIC_VALUE;
return packet;
}
 
int packet_is_valid( packet_t packet ){
return packet && ( packet->magic_value == PACKET_MAGIC_VALUE );
}
 
packet_t packet_copy( packet_t packet ){
packet_t new;
 
if( ! packet_is_valid( packet )) return NULL;
new = ( packet_t ) mmap( NULL, packet->length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
if( new == MAP_FAILED ) return NULL;
memcpy( new, packet, packet->length );
return new;
}
 
int packet_copy_data( packet_t packet, void * data, size_t length ){
if( ! packet_is_valid( packet )) return EINVAL;
if( packet->data_start + length >= (( size_t ) packet ) + packet->length ) return ENOMEM;
memcpy( packet + packet->data_start, data, length );
if( packet->data_start + length > packet->data_end ){
packet->data_end = packet->data_start + length;
}
return EOK;
}
 
void * packet_prepend( packet_t packet, size_t length ){
if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) < length )) return NULL;
packet->data_start -= length;
return packet + packet->data_start;
}
 
void * packet_append( packet_t packet, size_t length ){
if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= (( size_t ) packet ) + packet->length )) return NULL;
packet->data_end += length;
return packet + packet->data_end - length;
}
 
int packet_trim( packet_t packet, size_t prefix, size_t sufix ){
if(( ! packet_is_valid( packet )) || ( prefix + sufix > packet->data_end - packet->data_start )) return EINVAL;
packet->data_start += prefix;
packet->data_end -= sufix;
return EOK;
}
 
int packet_send( packet_t packet, int phone ){
if( ! packet_is_valid( packet )) return EINVAL;
return ipc_share_out_start( phone, packet, PROTO_READ | PROTO_WRITE );
}
 
int packet_receive( packet_ref packet ){
ERROR_DECLARE;
 
ipc_callid_t callid;
size_t size;
int flags;
 
if( ! packet ) return EINVAL;
if( ! ipc_share_out_receive( & callid, & size, & flags )) return EINVAL;
* packet = ( packet_t ) as_get_mappable_page( size );
if( !( * packet )) return ENOMEM;
if( ERROR_OCCURED( ipc_share_out_finalize( callid, * packet ))){
munmap( * packet, size );
return ERROR_CODE;
}
return EOK;
}
 
int packet_destroy( packet_t packet ){
if( ! packet_is_valid( packet )) return EINVAL;
return munmap( packet, sizeof( struct packet ) + packet->length );
}
 
size_t packet_get_data_length( packet_t packet ){
if( ! packet_is_valid( packet )) return 0;
return packet->data_end - packet->data_start;
}
 
void * packet_get_data( packet_t packet ){
if( ! packet_is_valid( packet )) return NULL;
return packet + packet->data_start;
}
 
/** @}
*/
/branches/network/uspace/srv/net/structures/packet/packet_queue.c
0,0 → 1,110
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
*/
 
#include <malloc.h>
 
#include "../../configuration.h"
 
#include "packet.h"
#include "packet_queue.h"
 
#define PQ_MAGIC_VALUE 0x23465968;
 
static inline int pq_is_valid( pq_item_ref item );
 
pq_item_ref pq_add( pq_head * queue, packet_t packet, int order, size_t metric ){
pq_item_ref item;
pq_item_ref tmp;
 
tmp = pq_create( packet, order, metric );
if( ! tmp ) return NULL;
item = queue;
while( pq_is_valid( item )){
if( item->order < order ){
if( item->next && pq_is_valid( item->next )){
item = item->next;
}else{
item->next = tmp;
tmp->previous = item;
return tmp;
}
}else{
tmp->previous = item->previous;
tmp->next = item;
item->previous = tmp;
if( tmp->previous ){
tmp->previous->next = tmp;
}else{
* queue = tmp;
}
return tmp;
}
}
}
 
 
pq_head pq_create( packet_t packet, int order, size_t metric ){
pq_item_ref item;
 
item = ( pq_item_ref ) malloc( sizeof( pq_item_t ));
if( ! item ) return NULL;
item->order = order;
item->metric = metric;
item->packet = packet;
item->previous = NULL;
item->next = NULL;
item->magic_value = PQ_MAGIC_VALUE;
return item;
}
 
void pq_destroy( pq_head queue ){
pq_item_ref actual;
pq_item_ref next;
 
actual = queue;
while( pq_is_valid( actual )){
next = actual->next;
actual->magic_value = 0;
free( actual );
actual = next;
}
}
 
static inline int pq_is_valid( pq_item_ref item ){
return item && ( item->magic_value == PQ_MAGIC_VALUE );
}
 
/** @}
*/
/branches/network/uspace/srv/net/structures/packet/packet.h
0,0 → 1,62
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
*/
 
#ifndef __NET_PACKET_H__
#define __NET_PACKET_H__
 
#define PACKET_PREPEND( packet, type ) ( type * ) packet_prepend(( packet ), sizeof( type ))
#define PACKET_APPEND( packet, type ) ( type * ) packet_append(( packet ), sizeof( type ))
#define PACKET_TRIM( packet, prefix, sufix ) packet_trim(( packet ), sizeof( prefix ), sizeof( sufix ))
 
typedef struct packet * packet_t;
typedef packet_t * packet_ref;
 
packet_t packet_create( size_t max_prefix, size_t max_content, size_t max_sufix );
void * packet_prepend( packet_t packet, size_t length );
void * packet_append( packet_t packet, size_t length );
packet_t packet_copy( packet_t packet );
int packet_copy_data( packet_t packet, void * data, size_t length );
// TODO protocol identification?
int packet_send( packet_t packet, int phone );
int packet_receive( packet_ref packet );
int packet_trim( packet_t packet, size_t prefix, size_t sufix );
int packet_destroy( packet_t packet );
size_t packet_get_data_length( packet_t packet );
void * packet_get_data( packet_t packet );
 
#endif
 
/** @}
*/
/branches/network/uspace/srv/net/structures/packet
Property changes:
Added: svn:mergeinfo
/branches/network/uspace/srv/net/structures/char_map.c
0,0 → 1,210
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
* A&nbsp;character string to integer map implementation file.
*/
 
#include <errno.h>
#include <malloc.h>
#include <unistd.h>
#include <string.h>
 
#include "char_map.h"
 
/** An&nbsp;internal magic value for a&nbsp;consistency check.
*/
#define CHAR_MAP_MAGIC_VALUE 0x12345611
 
/** Adds the value with the key to the map.
* Creates new nodes to map the key.
* @param map The character string to integer map. Input/output parameter.
* @param identifier The key zero ('\0') terminated character string. The key character string is processed until the first terminating zero ('\0') character after the given length is found. Input parameter.
* @param length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\0') character is found. Input parameter.
* @param value The integral value to be stored for the key character string. Input parameter.
* @returns EOK on success.
* @returns ENOMEM if there is no memory left.
* @returns EEXIST if the key character string is already used.
*/
int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value );
 
/** Returns the node assigned to the key from the map.
* @param map The character string to integer map. Input parameter.
* @param identifier The key zero ('\0') terminated character string. The key character string is processed until the first terminating zero ('\0') character after the given length is found. Input parameter.
* @param length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\0') character is found. Input parameter.
* @returns The node holding the integral value assigned to the key character string.
* @returns NULL if the key is not assigned a&nbsp;node.
*/
char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, const size_t length );
 
/** Checks if the map is valid.
* @param map The character string to integer map. Input parameter.
* @returns TRUE if the map is valid.
* @returns FALSE otherwise.
*/
int char_map_is_valid( const char_map_ref map );
 
int char_map_add( char_map_ref map, const char * identifier, size_t length, const int value ){
if( char_map_is_valid( map ) && ( identifier ) && (( length ) || ( * identifier ))){
int index;
 
for( index = 0; index < map->next; ++ index ){
if( map->items[ index ]->c == * identifier ){
++ identifier;
if( length ) -- length;
if( length || ( * identifier )){
return char_map_add( map->items[ index ], identifier, length, value );
}else{
if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS;
map->items[ index ]->value = value;
return EOK;
}
}
}
return char_map_add_item( map, identifier, length, value );
}
return EINVAL;
}
 
int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value ){
if( map->next == ( map->size - 1 )){
char_map_ref * tmp;
 
tmp = ( char_map_ref * ) malloc( sizeof( char_map_ref ) * 2 * map->size );
if( ! tmp ) return ENOMEM;
map->size *= 2;
memcpy( tmp, map->items, sizeof( char_map_ref ) * map->next );
free( map->items );
map->items = tmp;
}
map->items[ map->next ] = ( char_map_ref ) malloc( sizeof( char_map_t ));
if( ! map->items[ map->next ] ) return ENOMEM;
char_map_initialize( map->items[ map->next ] );
map->items[ map->next ]->c = * identifier;
++ identifier;
if( length ) -- length;
++ map->next;
if( length || ( * identifier )){
map->items[ map->next - 1 ]->value = CHAR_MAP_NULL;
return char_map_add_item( map->items[ map->next - 1 ], identifier, length, value );
}else{
map->items[ map->next - 1 ]->value = value;
}
return EOK;
}
 
void char_map_destroy( char_map_ref map ){
if( char_map_is_valid( map )){
int index;
 
map->magic = 0;
for( index = 0; index < map->next; ++ index ){
char_map_destroy( map->items[ index ] );
}
free( map->items );
}
}
 
int char_map_exclude( char_map_ref map, const char * identifier, size_t length ){
char_map_ref node;
 
node = char_map_find_node( map, identifier, length );
if( node ){
int value;
 
value = node->value;
node->value = CHAR_MAP_NULL;
return value;
}
return CHAR_MAP_NULL;
}
 
int char_map_find( const char_map_ref map, const char * identifier, size_t length ){
char_map_ref node;
 
node = char_map_find_node( map, identifier, length );
return node ? node->value : CHAR_MAP_NULL;
}
 
char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, size_t length ){
if( ! char_map_is_valid( map )) return NULL;
if( length ) -- length;
if( length || ( * identifier )){
int index;
 
for( index = 0; index < map->next; ++ index ){
if( map->items[ index ]->c == * identifier ){
++ identifier;
return char_map_find_node( map->items[ index ], identifier, length );
}
}
return NULL;
}
return map;
}
 
int char_map_get_value( const char_map_ref map ){
return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL;
}
 
int char_map_initialize( char_map_ref map ){
if( ! map ) return EINVAL;
map->c = 0;
map->value = CHAR_MAP_NULL;
map->size = 2;
map->next = 0;
map->items = malloc( sizeof( char_map_ref ) * map->size );
if( ! map->items ) return ENOMEM;
map->items[ map->next ] = NULL;
map->magic = CHAR_MAP_MAGIC_VALUE;
return EOK;
}
 
int char_map_is_valid( const char_map_ref map ){
return map && ( map->magic == CHAR_MAP_MAGIC_VALUE );
}
 
int char_map_update( char_map_ref map, const char * identifier, const size_t length, const int value ){
char_map_ref node;
 
// if( ! char_map_is_valid( map )) return EINVAL;
node = char_map_find_node( map, identifier, length );
if( node ){
node->value = value;
return EOK;
}else{
return char_map_add( map, identifier, length, value );
}
}
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
Added: svn:mergeinfo
/branches/network/uspace/srv/net/structures/generic_char_map.h
0,0 → 1,138
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
*/
 
#ifndef __GENERIC_CHAR_MAP_H__
#define __GENERIC_CHAR_MAP_H__
 
#include <unistd.h>
 
#include "../err.h"
 
#include "char_map.h"
#include "generic_field.h"
 
#define GENERIC_CHAR_MAP_MAGIC_VALUE 0x12345622
 
#define GENERIC_CHAR_MAP_DECLARE( name, type ) \
\
GENERIC_FIELD_DECLARE( name##_items, type ) \
\
typedef struct name name##_t; \
typedef name##_t * name##_ref; \
\
struct name{ \
char_map_t names; \
name##_items_t values; \
int magic; \
}; \
\
int name##_add( name##_ref map, const char * name, const size_t length, type * value ); \
int name##_count( name##_ref map ); \
void name##_destroy( name##_ref map ); \
void name##_exclude( name##_ref map, const char * name, const size_t length ); \
type * name##_find( name##_ref map, const char * name, const size_t length ); \
type * name##_get_index( name##_ref map, int index ); \
int name##_initialize( name##_ref map ); \
int name##_is_valid( name##_ref map );
 
#define GENERIC_CHAR_MAP_IMPLEMENT( name, type ) \
\
GENERIC_FIELD_IMPLEMENT( name##_items, type ) \
\
int name##_add( name##_ref map, const char * name, const size_t length, type * value ){ \
ERROR_DECLARE; \
\
int index; \
\
if( ! name##_is_valid( map )) return EINVAL; \
index = name##_items_add( & map->values, value ); \
if( index < 0 ) return index; \
if( ERROR_OCCURED( char_map_add( & map->names, name, length, index ))){ \
name##_items_exclude_index( & map->values, index ); \
return ERROR_CODE; \
} \
return EOK; \
} \
\
int name##_count( name##_ref map ){ \
return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1; \
} \
\
void name##_destroy( name##_ref map ){ \
if( name##_is_valid( map )){ \
char_map_destroy( & map->names ); \
name##_items_destroy( & map->values ); \
} \
} \
\
void name##_exclude( name##_ref map, const char * name, const size_t length ){ \
if( name##_is_valid( map )){ \
int index; \
\
index = char_map_exclude( & map->names, name, length ); \
if( index != CHAR_MAP_NULL ){ \
name##_items_exclude_index( & map->values, index ); \
} \
} \
} \
\
type * name##_find( name##_ref map, const char * name, const size_t length ){ \
if( name##_is_valid( map )){ \
int index; \
\
index = char_map_find( & map->names, name, length ); \
if( index != CHAR_MAP_NULL ){ \
return name##_items_get_index( & map->values, index ); \
} \
} \
return NULL; \
} \
\
int name##_initialize( name##_ref map ){ \
if( ! map ) return EINVAL; \
char_map_initialize( & map->names ); \
name##_items_initialize( & map->values ); \
map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \
return EOK; \
} \
\
int name##_is_valid( name##_ref map ){ \
return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE ); \
}
 
#endif
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
Added: svn:mergeinfo
/branches/network/uspace/srv/net/structures/int_map.h
0,0 → 1,209
/*
* Copyright (c) 2008 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/**
* @file
*/
 
#ifndef __NET_INT_MAP_H__
#define __NET_INT_MAP_H__
 
#include <errno.h>
#include <malloc.h>
#include <string.h>
 
#define INT_MAP_MAGIC_VALUE 0x11223344
#define INT_MAP_ITEM_MAGIC_VALUE 0x55667788
 
#define INT_MAP_DECLARE( name, type ) \
\
typedef struct name name##_t; \
typedef name##_t * name##_ref; \
typedef struct name##_item name##_item_t; \
typedef name##_item_t * name##_item_ref; \
\
struct name##_item{ \
int key; \
type * value; \
int magic; \
}; \
\
struct name{ \
int size; \
int next; \
name##_item_ref items; \
int magic; \
}; \
\
int name##_add( name##_ref map, int key, type * value ); \
void name##_clear( name##_ref map ); \
int name##_count( name##_ref map ); \
void name##_destroy( name##_ref map ); \
void name##_exclude( name##_ref map, int key ); \
void name##_exclude_index( name##_ref map, int index ); \
type * name##_find( name##_ref map, int key ); \
type * name##_get_index( name##_ref map, int index ); \
int name##_initialize( name##_ref map ); \
int name##_is_valid( name##_ref map ); \
void name##_item_destroy( name##_item_ref item ); \
int name##_item_is_valid( name##_item_ref item );
 
#define INT_MAP_IMPLEMENT( name, type ) \
\
int name##_add( name##_ref map, int key, type * value ){ \
if( name##_is_valid( map )){ \
if( map->next == ( map->size - 1 )){ \
name##_item_ref tmp; \
\
tmp = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * 2 * map->size ); \
if( ! tmp ) return ENOMEM; \
map->size *= 2; \
memcpy( tmp, map->items, sizeof( name##_item_t ) * map->next ); \
free( map->items ); \
map->items = tmp; \
} \
map->items[ map->next ].key = key; \
map->items[ map->next ].value = value; \
map->items[ map->next ].magic = INT_MAP_ITEM_MAGIC_VALUE; \
++ map->next; \
map->items[ map->next ].magic = 0; \
return map->next - 1; \
} \
return EINVAL; \
} \
\
void name##_clear( name##_ref map ){ \
if( name##_is_valid( map )){ \
int index; \
\
/* map->magic = 0;*/ \
for( index = 0; index < map->next; ++ index ){ \
if( name##_item_is_valid( &( map->items[ index ] ))){ \
name##_item_destroy( &( map->items[ index ] )); \
} \
} \
map->next = 0; \
map->items[ map->next ].magic = 0; \
/* map->magic = INT_MAP_MAGIC_VALUE;*/ \
} \
} \
\
int name##_count( name##_ref map ){ \
return name##_is_valid( map ) ? map->next : -1; \
} \
\
void name##_destroy( name##_ref map ){ \
if( name##_is_valid( map )){ \
int index; \
\
map->magic = 0; \
for( index = 0; index < map->next; ++ index ){ \
if( name##_item_is_valid( &( map->items[ index ] ))){ \
name##_item_destroy( &( map->items[ index ] )); \
} \
} \
free( map->items ); \
} \
} \
\
void name##_exclude( name##_ref map, int key ){ \
if( name##_is_valid( map )){ \
int index; \
\
for( index = 0; index < map->next; ++ index ){ \
if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
name##_item_destroy( &( map->items[ index ] )); \
} \
} \
} \
} \
\
void name##_exclude_index( name##_ref map, int index ){ \
if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){ \
name##_item_destroy( &( map->items[ index ] )); \
} \
} \
\
type * name##_find( name##_ref map, int key ){ \
if( name##_is_valid( map )){ \
int index; \
\
for( index = 0; index < map->next; ++ index ){ \
if( name##_item_is_valid( &( map->items[ index ] )) && ( map->items[ index ].key == key )){ \
return map->items[ index ].value; \
} \
} \
} \
return NULL; \
} \
\
type * name##_get_index( name##_ref map, int index ){ \
if( name##_is_valid( map ) && ( index >= 0 ) && ( index < map->next ) && name##_item_is_valid( &( map->items[ index ] ))){ \
return map->items[ index ].value; \
} \
return NULL; \
} \
\
int name##_initialize( name##_ref map ){ \
if( ! map ) return EINVAL; \
map->size = 2; \
map->next = 0; \
map->items = ( name##_item_ref ) malloc( sizeof( name##_item_t ) * map->size ); \
if( ! map->items ) return ENOMEM; \
map->items[ map->next ].magic = 0; \
map->magic = INT_MAP_MAGIC_VALUE; \
return EOK; \
} \
\
int name##_is_valid( name##_ref map ){ \
return map && ( map->magic == INT_MAP_MAGIC_VALUE ); \
} \
\
void name##_item_destroy( name##_item_ref item ){ \
if( name##_item_is_valid( item )){ \
item->magic = 0; \
if( item->value ){ \
free( item->value ); \
item->value = NULL; \
} \
} \
} \
\
int name##_item_is_valid( name##_item_ref item ){ \
return item && ( item->magic == INT_MAP_ITEM_MAGIC_VALUE ); \
}
 
#endif
 
/** @}
*/
 
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
Added: svn:mergeinfo