--- rpl/src/gestion_variables.c 2012/10/05 13:12:39 1.60 +++ rpl/src/gestion_variables.c 2012/12/19 09:58:23 1.67 @@ -1,6 +1,6 @@ /* ================================================================================ - RPL/2 (R) version 4.1.11 + RPL/2 (R) version 4.1.12 Copyright (C) 1989-2012 Dr. BERTRAND Joël This file is part of RPL/2. @@ -237,7 +237,7 @@ liste_variables_par_feuilles(struct_proc ================================================================================ */ -static inline struct_arbre_variables * +struct_arbre_variables * allocation_noeud(struct_processus *s_etat_processus) { struct_arbre_variables *objet; @@ -272,7 +272,7 @@ liberation_noeud(struct_processus *s_eta return; } -static inline struct_arbre_variables ** +struct_arbre_variables ** allocation_tableau_noeuds(struct_processus *s_etat_processus) { struct_arbre_variables **objet; @@ -1712,6 +1712,8 @@ liberation_arbre_variables(struct_proces free((*(*l_element_statique_courant).variable).nom); liberation(s_etat_processus, (*(*l_element_statique_courant) .variable).objet); + free((*l_element_statique_courant).variable); + free(l_element_statique_courant); l_element_statique_courant = l_element_statique_suivant; } @@ -1787,8 +1789,8 @@ liberation_arbre_variables(struct_proces ================================================================================ */ -int -nombre_variables(struct_processus *s_etat_processus, +static int +nombre_variables_locales(struct_processus *s_etat_processus, struct_arbre_variables *l_element_courant) { int i; @@ -1797,6 +1799,11 @@ nombre_variables(struct_processus *s_eta struct_liste_variables *l_variable; struct_liste_variables_statiques *l_variable_statique; + if (l_element_courant == NULL) + { + return(0); + } + n = 0; if ((*l_element_courant).feuille != NULL) @@ -1832,7 +1839,53 @@ nombre_variables(struct_processus *s_eta { if ((*l_element_courant).noeuds[i] != NULL) { - n += nombre_variables(s_etat_processus, + n += nombre_variables_locales(s_etat_processus, + (*l_element_courant).noeuds[i]); + } + } + + return(n); +} + + +static int +nombre_variables_partagees(struct_processus *s_etat_processus, + struct_arbre_variables_partagees *l_element_courant) +{ + int i; + int n; + + struct_liste_variables_partagees *l_variable; + + if (l_element_courant == NULL) + { + return(0); + } + + if (pthread_mutex_lock(&((*l_element_courant).mutex_feuille)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_processus; + return(0); + } + + n = 0; + + if ((*l_element_courant).feuille != NULL) + { + l_variable = (*l_element_courant).feuille; + + do + { + n++; + l_variable = (*l_variable).suivant; + } while(l_variable != NULL); + } + + for(i = 0; i < (*s_etat_processus).nombre_caracteres_variables; i++) + { + if ((*l_element_courant).noeuds[i] != NULL) + { + n += nombre_variables_partagees(s_etat_processus, (*l_element_courant).noeuds[i]); } } @@ -1842,7 +1895,53 @@ nombre_variables(struct_processus *s_eta int -liste_variables(struct_processus *s_etat_processus, +nombre_variables(struct_processus *s_etat_processus) +{ + return(nombre_variables_locales(s_etat_processus, + (*s_etat_processus).s_arbre_variables) + + nombre_variables_partagees(s_etat_processus, + (*(*s_etat_processus).s_arbre_variables_partagees))); +} + + +void +liberation_mutexes_arbre_variables_partagees(struct_processus *s_etat_processus, + struct_arbre_variables_partagees *l_element_courant) +{ + int i; + + if (l_element_courant == NULL) + { + return; + } + + if (pthread_mutex_trylock(&((*l_element_courant).mutex_feuille)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_processus; + return; + } + + if (pthread_mutex_unlock(&((*l_element_courant).mutex_feuille)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_processus; + return; + } + + for(i = 0; i < (*s_etat_processus).nombre_caracteres_variables; i++) + { + if ((*l_element_courant).noeuds[i] != NULL) + { + liberation_mutexes_arbre_variables_partagees(s_etat_processus, + (*l_element_courant).noeuds[i]); + } + } + + return; +} + + +static int +liste_variables_locales(struct_processus *s_etat_processus, struct_tableau_variables *tableau, int position, struct_arbre_variables *l_element_courant) { @@ -1851,6 +1950,11 @@ liste_variables(struct_processus *s_etat struct_liste_variables *l_variable; struct_liste_variables_statiques *l_variable_statique; + if (l_element_courant == NULL) + { + return(0); + } + if ((*l_element_courant).feuille != NULL) { l_variable = (*l_element_courant).feuille; @@ -1905,7 +2009,7 @@ liste_variables(struct_processus *s_etat { if ((*l_element_courant).noeuds[i] != NULL) { - position = liste_variables(s_etat_processus, + position = liste_variables_locales(s_etat_processus, tableau, position, (*l_element_courant).noeuds[i]); } } @@ -1914,6 +2018,98 @@ liste_variables(struct_processus *s_etat } +static int +liste_variables_partagees(struct_processus *s_etat_processus, + struct_tableau_variables *tableau, int position, + struct_arbre_variables_partagees *l_element_courant) +{ + int i; + + struct_liste_variables_partagees *l_variable; + + if (l_element_courant == NULL) + { + return(0); + } + + if ((*l_element_courant).feuille != NULL) + { + l_variable = (*l_element_courant).feuille; + + do + { + tableau[position].origine = 'E'; + tableau[position].nom = (*(*l_variable).variable).nom; + tableau[position].niveau = (*(*l_variable).variable).niveau; + tableau[position].objet = (*(*l_variable).variable).objet; + tableau[position].variable_verrouillee = d_faux; + tableau[position].variable_partagee = + (*(*l_variable).variable).variable_partagee; + tableau[position].variable_partagee.pointeur = NULL; + tableau[position].variable_masquee = d_faux; + + position++; + + l_variable = (*l_variable).suivant; + } while(l_variable != NULL); + } + + for(i = 0; i < (*s_etat_processus).nombre_caracteres_variables; i++) + { + if ((*l_element_courant).noeuds[i] != NULL) + { + position = liste_variables_partagees(s_etat_processus, + tableau, position, (*l_element_courant).noeuds[i]); + } + } + + return(position); +} + + +static int +fonction_ordre_variables(const void *argument_1, const void *argument_2) +{ + int comparaison; + + struct_tableau_variables *a1; + struct_tableau_variables *a2; + + a1 = (struct_tableau_variables *) argument_1; + a2 = (struct_tableau_variables *) argument_2; + + comparaison = strcmp((*a1).nom, (*a2).nom); + + if (comparaison != 0) + { + return(comparaison); + } + else + { + return((*a1).niveau - (*a2).niveau); + } +} + + +int +liste_variables(struct_processus *s_etat_processus, + struct_tableau_variables *tableau) +{ + int nombre_elements; + + nombre_elements = liste_variables_locales(s_etat_processus, + tableau, 0, (*s_etat_processus).s_arbre_variables); + nombre_elements += liste_variables_partagees(s_etat_processus, + tableau, nombre_elements, (*(*s_etat_processus) + .s_arbre_variables_partagees)); + + qsort(tableau, nombre_elements, sizeof(struct_tableau_variables), + fonction_ordre_variables); + + return(nombre_elements); +} + + /* ================================================================================ Procédure de copie de l'arbre des variables @@ -1956,6 +2152,7 @@ copie_arbre_variables(struct_processus * struct_liste_variables_statiques *l_element_statique_courant; struct_variable s_variable; + struct_variable_statique s_variable_statique; unsigned char *ptr; @@ -2179,8 +2376,31 @@ copie_arbre_variables(struct_processus * ptr++; } + // Il faut copier la variable pour la dissocier de la variable + // restant dans le thread parent. + + s_variable_statique = (*(*l_element_statique_courant).variable); + + if (copie_objet(s_etat_processus, s_variable_statique.objet, 'P') + == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if ((s_variable_statique.nom = malloc((strlen( + (*(*l_element_statique_courant).variable).nom) + 1) * + sizeof(unsigned char))) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + strcpy(s_variable_statique.nom, (*(*l_element_statique_courant) + .variable).nom); + if (creation_variable_statique(s_nouvel_etat_processus, - (*l_element_statique_courant).variable) == d_erreur) + &s_variable_statique) == d_erreur) { (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; return;