Ignore:
Timestamp:
2010-03-07T22:51:38Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
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.
Message:

Merge from lp:~lukasmejdrech/helenos/network.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/structures/measured_strings.c

    rb5cbff4 r71b00dcc  
    5454 *  @returns NULL if there is not enough memory left.
    5555 */
    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;
     56size_t * prepare_lengths(const measured_string_ref strings, size_t count);
     57
     58measured_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        }
    6670        new->length = length;
    67         new->value = (( char * ) new ) + sizeof( measured_string_t );
     71        new->value = ((char *) new) + sizeof(measured_string_t);
    6872        // 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';
    7175        return new;
    7276}
    7377
    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 ){
     78measured_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){
    8288                        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';
    8591                        return new;
    8692                }else{
    87                         free( new );
     93                        free(new);
    8894                }
    8995        }
     
    9197}
    9298
    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 );
     99int 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);
    123133                return ENOMEM;
    124134        }
    125135        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);
    134144                                return EINVAL;
    135145                        }
    136                         ERROR_PROPAGATE( async_data_write_finalize( callid, next, lengths[ index ] ));
    137                         ( * strings)[ index ].value = next;
    138                         next += lengths[ index ];
    139                         * next = '\0';
     146                        ERROR_PROPAGATE(async_data_write_finalize(callid, next, lengths[index]));
     147                        (*strings)[index].value = next;
     148                        next += lengths[index];
     149                        *next = '\0';
    140150                        ++ next;
    141151                }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
     159int 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)){
    176188                                return EINVAL;
    177189                        }
    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
     196int 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);
    206222                return ENOMEM;
    207223        }
    208224        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                         * next = '\0';
     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';
    216232                        ++ next;
    217233                }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
     241int 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
     267size_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        }
    256276        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;
    262282        return lengths;
    263283}
Note: See TracChangeset for help on using the changeset viewer.