transport.c

Go to the documentation of this file.
00001 
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <errno.h>
00037 #include <assert.h>
00038 #include <string.h>
00039 
00040 #ifdef _MSC_VER
00041 #include <io.h>
00042 #endif
00043 
00044 #include "transport.h"
00045 
00046 trans_obj_t* create_object(void) {
00047         
00048         trans_obj_t *obj = NULL;
00049 
00050         if (!(obj = (trans_obj_t*)calloc(1, sizeof(trans_obj_t)))) {
00051                 printf("Could not alloc memory for transport object!\n");
00052                 return NULL;
00053         }
00054 
00055         return obj;
00056 }
00057 
00058 trans_block_t* create_block(void) {
00059  
00060         trans_block_t *block = NULL;
00061 
00062         if (!(block = (trans_block_t*)calloc(1, sizeof(trans_block_t)))) {
00063                 printf("Could not alloc memory for transport block!\n");
00064                 return NULL;
00065         }
00066 
00067         return block;
00068 }
00069 
00070 trans_unit_t* create_units(unsigned int number) {
00071         
00072         trans_unit_t *unit = NULL;
00073 
00074         if (!(unit = (trans_unit_t*)calloc(number, sizeof(trans_unit_t)))) {
00075                 printf("Could not alloc memory for %i transport units!\n", number);
00076         }
00077 
00078         return unit;
00079 }
00080 
00081 void free_units(trans_block_t *tb) {
00082 
00083   trans_unit_t *tu = NULL;
00084   trans_unit_t *next_tu = NULL;
00085 
00086   next_tu = tb->unit_list;
00087 
00088   while(next_tu != NULL) {
00089     tu = next_tu;
00090 
00091     if(tu->data != NULL) {
00092       free(tu->data);
00093       tu->data = NULL;
00094     }
00095     
00096     next_tu = tu->next;
00097     free(tu);
00098   }
00099   
00100   tb->unit_list = NULL;
00101 }
00102 
00103 #ifdef USE_RETRIEVE_UNIT
00104 
00105 trans_unit_t* retrieve_unit(alc_session_t *s, unsigned short es_len) {
00106 
00107         trans_unit_container_t *container = NULL;
00108         trans_unit_container_t *tmp;
00109         trans_unit_container_t *start_search;
00110 
00111         if(s->last_given != NULL) {
00112                 start_search = s->last_given->next;
00113         } else {
00114                 start_search = s->unit_pool;
00115         }
00116 
00117         tmp = start_search;
00118 
00119         while(tmp != NULL ) { 
00120                 if(tmp->u.used == 0) {
00121                         if(tmp->u.len < es_len) {
00122                                 continue;
00123                         }
00124 
00125                         tmp->u.used = 1;
00126                         s->last_given = tmp;
00127             assert(tmp->u.data != NULL);
00128                         return &(tmp->u);
00129                 }
00130                 tmp = tmp->next;
00131         }
00132         
00133         tmp = s->unit_pool;
00134         while(tmp != start_search) {    
00135                 if(tmp->u.used == 0) {
00136 
00137                         if(tmp->u.len < es_len) {
00138                                 continue;
00139                         }
00140 
00141                         tmp->u.used = 1;
00142                         s->last_given = tmp;
00143             assert(tmp->u.data != NULL);
00144                         return &(tmp->u);
00145                 }
00146                 tmp = tmp->next;
00147         }
00148 
00149         if(!(container = (trans_unit_container_t*)calloc(1, sizeof(trans_unit_container_t)))) {
00150                 printf("Could not alloc memory for a transport unit container!\n");
00151                 return NULL;
00152         }
00153 
00154         container->next = s->unit_pool;
00155         s->unit_pool = container;
00156         container->u.used = 1;
00157         s->last_given = s->unit_pool;
00158 
00159     if(!(container->u.data = (char*)calloc(es_len, sizeof(char)))) { 
00160         printf("Could not alloc memory for transport unit's data!\n");
00161                 free(container);
00162         return NULL;
00163     }
00164 
00165     return &(container->u);
00166 }
00167 
00168 void free_units2(trans_block_t *tb) {
00169     trans_unit_t *tu = NULL;
00170     trans_unit_t *current_tu = NULL;
00171 
00172         char *data = NULL;
00173         unsigned short  data_buffer_len;
00174 
00175     current_tu = tb->unit_list;
00176         
00177     while(current_tu != NULL) {
00178         tu = current_tu;
00179 
00180                 /* backup the data pointer */
00181 
00182                 data = tu->data;
00183                 data_buffer_len = tu->len;
00184 
00185                 current_tu = tu->next;
00186 
00187                 memset(tu, 0, sizeof(trans_unit_t));
00188 
00189                 /* reinstall the backup */
00190                 tu->data = data;
00191                 tu->len = data_buffer_len;
00192     }
00193 
00194     tb->unit_list = NULL;
00195 }
00196 
00197 #endif
00198 
00199 /*
00200 void insert_object(trans_obj_t *to, alc_session_t *s, int type) {
00201         
00202         trans_obj_t *tmp;
00203         
00204         if(type == 0) {
00205                 tmp = s->fdt_list;
00206         }
00207         else {
00208                 tmp = s->obj_list;
00209         }
00210 
00211         if(tmp == NULL) {
00212 
00213                 if(type == 0) {
00214                         s->fdt_list = to;
00215                 }       
00216                 else {
00217                         s->obj_list = to;
00218                 }
00219         }
00220         else {
00221                 for(;;) {
00222                         if(to->toi < tmp->toi) {
00223 
00224                                 if(tmp->prev == NULL) {
00225 
00226                                         to->next = tmp;
00227                                         to->prev = tmp->prev;
00228                                 
00229                                         tmp->prev = to;
00230 
00231                 if(type == 0) {
00232                         s->fdt_list = to;
00233                 }
00234                 else {
00235                         s->obj_list = to;
00236                 }
00237 
00238                                 }
00239                                 else {
00240 
00241                                         to->next = tmp;
00242                                         to->prev = tmp->prev;
00243                                 
00244                                         tmp->prev->next = to;
00245                                         tmp->prev = to;
00246                                 }
00247                                 break;
00248                         }
00249 
00250                         if(tmp->next == NULL) {
00251 
00252                                 to->next = tmp->next;
00253                                 to->prev = tmp;
00254                                 
00255                                 tmp->next = to;
00256                                 break;
00257                         }
00258 
00259                         tmp = tmp->next;
00260                 }
00261         }
00262 }*/
00263 
00264 void insert_object(trans_obj_t *to, alc_session_t *s, int type) {
00265 
00266     trans_obj_t *tmp;
00267 
00268     if(type == 0) {
00269         tmp = s->fdt_list;
00270     }
00271     else {
00272         tmp = s->obj_list;
00273     }
00274 
00275     if(tmp == NULL) {
00276 
00277         if(type == 0) {
00278             s->fdt_list = to;
00279         }       
00280         else {
00281             s->obj_list = to;
00282         }
00283         return;
00284     }
00285 
00286     for(;;) {
00287         if(to->toi < tmp->toi) {
00288 
00289             if(tmp->prev == NULL) {
00290 
00291                 to->next = tmp;
00292                 to->prev = tmp->prev;
00293 
00294                 tmp->prev = to;
00295 
00296                 if(type == 0) {
00297                     s->fdt_list = to;
00298                 }
00299                 else {
00300                     s->obj_list = to;
00301                 }
00302 
00303             }
00304             else {
00305 
00306                 to->next = tmp;
00307                 to->prev = tmp->prev;
00308 
00309                 tmp->prev->next = to;
00310                 tmp->prev = to;
00311             }
00312             break;
00313         }
00314 
00315         if(tmp->next == NULL) {
00316 
00317             to->next = tmp->next;
00318             to->prev = tmp;
00319 
00320             tmp->next = to;
00321             break;
00322         }
00323 
00324         tmp = tmp->next;
00325     }
00326 }
00327 
00328 /*
00329  * This function inserts transport unit to transport block.
00330  *
00331  * Params:      trans_unit_t *tu: Pointer to transport unit to be inserted,
00332  *                      trans_block_t *tb: Pointer to transport block,
00333  *                      trans_obj_t *tu: Pointer to transport object.
00334  *
00335  * Return:      int: 0 when transport unit is inserted, 1 when duplicated transport unit.
00336  *
00337  */
00338 /*
00339 int insert_unit(trans_unit_t *tu, trans_block_t *tb, trans_obj_t *to) {
00340 
00341         trans_unit_t *tmp;
00342         int retval = 0;
00343 
00344         tmp = tb->unit_list;
00345 
00346         if(tmp == NULL) {
00347 
00348                 to->rx_bytes += tu->len;
00349                 
00350                 // for percentage counter
00351                 if(to->rx_bytes > to->len) {
00352                         to->rx_bytes = to->len;         
00353                 }
00354 
00355                 tb->nb_of_rx_units++;
00356 
00357                 tb->unit_list = tu;
00358         }
00359         else {
00360                 for(;;) {
00361                         if(tu->esi < tmp->esi) {
00362                                 //Delayed unit
00363 
00364                                 to->rx_bytes += tu->len;
00365 
00366                                 if(to->rx_bytes > to->len) {
00367                                         to->rx_bytes = to->len;         
00368                                 }
00369 
00370                                 tb->nb_of_rx_units++;
00371                                 
00372                                 if(tmp->prev == NULL) {
00373                                         tu->next = tmp;
00374                                         tu->prev = tmp->prev;
00375                                 
00376                                         tmp->prev = tu;
00377 
00378                                         tb->unit_list = tu;
00379                                 }
00380                                 else {
00381                                         tu->next = tmp;
00382                                         tu->prev = tmp->prev;
00383                                 
00384                                         tmp->prev->next = tu;
00385                                         tmp->prev = tu;
00386                                         
00387                                 }
00388                                 break;
00389                         }
00390                         else if(tu->esi == tmp->esi) {
00391                                 //Duplicated unit
00392                                 retval = 1;
00393                                 break;
00394                         }
00395                         if(tmp->next == NULL) {
00396                                 // Last unit (normal order)
00397 
00398                                 to->rx_bytes += tu->len;
00399                         
00400 
00401                                 if(to->rx_bytes > to->len) {
00402                                         to->rx_bytes = to->len;         
00403                                 }
00404 
00405                                 tb->nb_of_rx_units++;
00406 
00407                                 tu->next = tmp->next;
00408                                 tu->prev = tmp;
00409                                 
00410                                 tmp->next = tu;
00411                                 break;
00412                         }
00413                         tmp = tmp->next;
00414                 }
00415         }
00416 
00417         return retval;
00418 }*/
00419 
00420 int insert_unit(trans_unit_t *tu, trans_block_t *tb, trans_obj_t *to) {
00421 
00422     trans_unit_t *tmp;
00423     int retval = 0;
00424 
00425     tmp = tb->unit_list;
00426 
00427     if(tmp == NULL) {
00428 
00429         to->rx_bytes += tu->len;
00430 
00431         // for percentage counter
00432         if(to->rx_bytes > to->len) {
00433             to->rx_bytes = to->len;             
00434         }
00435 
00436         tb->nb_of_rx_units++;
00437 
00438         tb->unit_list = tu;
00439         return retval;
00440     }
00441 
00442     for(;;) {
00443         if(tu->esi < tmp->esi) {
00444             //Delayed unit
00445 
00446             to->rx_bytes += tu->len;
00447 
00448             if(to->rx_bytes > to->len) {
00449                 to->rx_bytes = to->len;         
00450             }
00451 
00452             tb->nb_of_rx_units++;
00453 
00454             if(tmp->prev == NULL) {
00455                 tu->next = tmp;
00456                 tu->prev = tmp->prev;
00457 
00458                 tmp->prev = tu;
00459 
00460                 tb->unit_list = tu;
00461             }
00462             else {
00463                 tu->next = tmp;
00464                 tu->prev = tmp->prev;
00465 
00466                 tmp->prev->next = tu;
00467                 tmp->prev = tu;
00468 
00469             }
00470             break;
00471         }
00472         else if(tu->esi == tmp->esi) {
00473             //Duplicated unit
00474             retval = 1;
00475             break;
00476         }
00477         if(tmp->next == NULL) {
00478             // Last unit (normal order)
00479 
00480             to->rx_bytes += tu->len;
00481 
00482 
00483             if(to->rx_bytes > to->len) {
00484                 to->rx_bytes = to->len;         
00485             }
00486 
00487             tb->nb_of_rx_units++;
00488 
00489             tu->next = tmp->next;
00490             tu->prev = tmp;
00491 
00492             tmp->next = tu;
00493             break;
00494         }
00495         tmp = tmp->next;
00496     }
00497 
00498     return retval;
00499 }
00500 
00501 void free_object(trans_obj_t *to, alc_session_t *s, int type) {
00502 
00503 #ifndef USE_RETRIEVE_UNIT
00504   trans_block_t *tb;
00505   trans_unit_t *tu;
00506   trans_unit_t *next_tu;
00507   unsigned int i;
00508   
00509   for(i=0; i < to->bs->N; i++) {
00510     
00511     tb = to->block_list+i;
00512     
00513     next_tu = tb->unit_list;
00514     
00515     while(next_tu != NULL) {
00516       tu = next_tu;
00517       
00518       if(tu->data != NULL) {
00519                 free(tu->data);
00520                 tu->data = NULL;
00521       }
00522       
00523       next_tu = tu->next;
00524       free(tu);
00525     }
00526   }
00527 #endif
00528   
00529   free(to->bs);
00530   
00531   if(to->next != NULL) {
00532     to->next->prev = to->prev;
00533   }
00534   if(to->prev != NULL) {
00535     to->prev->next = to->next;
00536   }
00537   if(((type == 0)&&(to == s->fdt_list))) {
00538     s->fdt_list = to->next;
00539   }
00540   else if(((type == 1)&&(to == s->obj_list))) {
00541     s->obj_list = to->next;
00542   }
00543 
00544   if(to->tmp_filename != NULL) {
00545     free(to->tmp_filename);
00546     to->tmp_filename = NULL;
00547     if(type == 1) {
00548       if(close(to->fd) == -1) {
00549                 printf("close failed, errno: %i\n", errno);
00550       }
00551     }
00552   }
00553   
00554   if(to->tmp_st_filename != NULL) {
00555     if(type == 1) {
00556       if(close(to->fd_st) == -1) {
00557                 printf("close failed, errno: %i\n", errno);
00558       }
00559     }
00560     remove(to->tmp_st_filename);
00561     free(to->tmp_st_filename);
00562     to->tmp_st_filename = NULL;
00563   }        
00564   
00565   if(to->block_list != NULL) {
00566     free(to->block_list);
00567   }
00568   
00569   free(to);
00570 }
00571 

Generated on Fri Mar 9 19:59:52 2007 for MAD-FCL by  doxygen 1.5.0