--- rpl/src/gestion_variables.c 2012/12/13 16:59:41 1.64 +++ rpl/src/gestion_variables.c 2016/09/27 15:29:34 1.99 @@ -1,7 +1,7 @@ /* ================================================================================ - RPL/2 (R) version 4.1.11 - Copyright (C) 1989-2012 Dr. BERTRAND Joël + RPL/2 (R) version 4.1.26 + Copyright (C) 1989-2016 Dr. BERTRAND Joël This file is part of RPL/2. @@ -284,7 +284,8 @@ allocation_tableau_noeuds(struct_process } else { - objet = malloc((*s_etat_processus).nombre_caracteres_variables + objet = malloc(((size_t) (*s_etat_processus) + .nombre_caracteres_variables) * sizeof(struct_arbre_variables *)); } @@ -427,12 +428,9 @@ ajout_variable(struct_processus *s_etat_ (*(*s_etat_processus).s_arbre_variables).feuille = NULL; (*(*s_etat_processus).s_arbre_variables).feuille_statique = NULL; - (*(*s_etat_processus).s_arbre_variables).feuille_partagee = NULL; (*(*s_etat_processus).s_arbre_variables).noeuds_utilises = 0; (*(*s_etat_processus).s_arbre_variables).indice_tableau_pere = -1; (*(*s_etat_processus).s_arbre_variables).noeud_pere = NULL; - INITIALISATION_MUTEX((*(*s_etat_processus).s_arbre_variables) - .mutex_feuille_partagee); if (((*(*s_etat_processus).s_arbre_variables).noeuds = allocation_tableau_noeuds(s_etat_processus)) == NULL) @@ -483,13 +481,7 @@ ajout_variable(struct_processus *s_etat_ .pointeurs_caracteres_variables[*ptr]]).feuille_statique = NULL; (*(*l_variable_courante).noeuds[(*s_etat_processus) - .pointeurs_caracteres_variables[*ptr]]).feuille_partagee - = NULL; - (*(*l_variable_courante).noeuds[(*s_etat_processus) .pointeurs_caracteres_variables[*ptr]]).noeuds_utilises = 0; - INITIALISATION_MUTEX((*(*l_variable_courante).noeuds - [(*s_etat_processus).pointeurs_caracteres_variables[*ptr]]) - .mutex_feuille_partagee); // Le champ noeud_pere de la structure créée pointe sur // la structure parente et l'indice tableau_pere correspond à la @@ -997,7 +989,7 @@ recherche_variable(struct_processus *s_e unsigned char *ptr; - unsigned long niveau_appel; + integer8 niveau_appel; if ((*s_etat_processus).s_arbre_variables == NULL) { @@ -1212,7 +1204,7 @@ retrait_variable(struct_processus *s_eta struct_liste_variables *variable_a_supprimer; struct_liste_variables *variables_par_niveau; - unsigned long niveau; + integer8 niveau; (*s_etat_processus).niveau_supprime = d_faux; @@ -1306,8 +1298,7 @@ retrait_variable(struct_processus *s_eta .feuille = NULL; while(((*s_arbre_courant).noeuds_utilises == 0) && - ((*s_arbre_courant).feuille_statique == NULL) && - ((*s_arbre_courant).feuille_partagee == NULL)) + ((*s_arbre_courant).feuille_statique == NULL)) { s_arbre_a_supprimer = s_arbre_courant; s_arbre_courant = (*s_arbre_courant).noeud_pere; @@ -1540,6 +1531,15 @@ retrait_variables_par_niveau(struct_proc break; } + if (((*s_etat_processus).at_exit != NULL) && + ((*s_etat_processus).niveau_courant == 0)) + { + // Il y a une routine ATEXIT enregistrée. On ne détruit pas + // les variables globales qui pourraient y être utilisées. + + break; + } + while((*(*s_etat_processus).l_liste_variables_par_niveau).liste != NULL) { @@ -1799,16 +1799,21 @@ liberation_arbre_variables(struct_proces ================================================================================ */ -int -nombre_variables(struct_processus *s_etat_processus, +static integer8 +nombre_variables_locales(struct_processus *s_etat_processus, struct_arbre_variables *l_element_courant) { - int i; - int n; + integer8 i; + integer8 n; 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) @@ -1844,7 +1849,7 @@ 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]); } } @@ -1853,16 +1858,114 @@ nombre_variables(struct_processus *s_eta } -int -liste_variables(struct_processus *s_etat_processus, - struct_tableau_variables *tableau, int position, - struct_arbre_variables *l_element_courant) +static integer8 +nombre_variables_partagees(struct_processus *s_etat_processus, + struct_arbre_variables_partagees *l_element_courant) +{ + integer8 i; + integer8 n; + + struct_liste_variables_partagees *l_variable; + + if (l_element_courant == NULL) + { + return(0); + } + + // Mutex deverrouillé par liste_variables_partagees(); + 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]); + } + } + + return(n); +} + + +integer8 +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 integer8 +liste_variables_locales(struct_processus *s_etat_processus, + struct_tableau_variables *tableau, integer8 position, + struct_arbre_variables *l_element_courant) +{ + integer8 i; + struct_liste_variables *l_variable; struct_liste_variables_statiques *l_variable_statique; + if (l_element_courant == NULL) + { + return(position); + } + if ((*l_element_courant).feuille != NULL) { l_variable = (*l_element_courant).feuille; @@ -1880,8 +1983,10 @@ liste_variables(struct_processus *s_etat tableau[position].variable_partagee = (*(*l_variable).variable).variable_partagee; tableau[position].variable_masquee = d_faux; + tableau[position].mutex = NULL; position++; + l_variable = (*l_variable).suivant; } while(l_variable != (*l_element_courant).feuille); } @@ -1898,13 +2003,14 @@ liste_variables(struct_processus *s_etat tableau[position].nom = (*(*l_variable_statique).variable).nom; tableau[position].niveau = (*(*l_variable_statique).variable).niveau; - tableau[position].objet = - (*(*l_variable_statique).variable).objet; + tableau[position].objet = (*(*l_variable_statique).variable) + .objet; tableau[position].variable_verrouillee = d_faux; tableau[position].variable_statique = (*(*l_variable_statique).variable).variable_statique; tableau[position].variable_partagee.pointeur = NULL; tableau[position].variable_masquee = d_vrai; + tableau[position].mutex = NULL; position++; } @@ -1917,7 +2023,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]); } } @@ -1926,6 +2032,107 @@ liste_variables(struct_processus *s_etat } +static integer8 +liste_variables_partagees(struct_processus *s_etat_processus, + struct_tableau_variables *tableau, integer8 position, + struct_arbre_variables_partagees *l_element_courant) +{ + integer8 i; + + struct_liste_variables_partagees *l_variable; + + if (l_element_courant == NULL) + { + return(position); + } + + 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_statique.pointeur = NULL; + tableau[position].variable_masquee = d_faux; + tableau[position].mutex = &((*(*l_variable).variable).mutex); + pthread_mutex_lock(tableau[position].mutex); + + 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]); + } + } + + // Mutex verrouillé par nombre_variables_partagees(); + if (pthread_mutex_unlock(&((*l_element_courant).mutex_feuille)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_processus; + return(0); + } + + 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)) > 0) ? 1 : -1); + } +} + + +integer8 +liste_variables(struct_processus *s_etat_processus, + struct_tableau_variables *tableau) +{ + integer8 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, (size_t) nombre_elements, sizeof(struct_tableau_variables), + fonction_ordre_variables); + + return(nombre_elements); +} + + /* ================================================================================ Procédure de copie de l'arbre des variables @@ -2029,14 +2236,19 @@ copie_arbre_variables(struct_processus * s_variable = (*((struct_variable *) (*l_element_courant).donnee)); - if ((s_variable.nom = strdup((*((struct_variable *) - (*l_element_courant).donnee)).nom)) == NULL) + if ((s_variable.nom = rpl_malloc(s_nouvel_etat_processus, + (strlen((*((struct_variable *) + (*l_element_courant).donnee)).nom) + 1) * + sizeof(unsigned char))) == NULL) { (*s_nouvel_etat_processus).erreur_systeme = d_es_allocation_memoire; return; } + strcpy(s_variable.nom, (*((struct_variable *) + (*l_element_courant).donnee)).nom); + if ((s_variable.objet = copie_objet(s_nouvel_etat_processus, (*((struct_variable *) (*l_element_courant).donnee)) .objet, 'P')) == NULL) @@ -2204,8 +2416,8 @@ copie_arbre_variables(struct_processus * return; } - if ((s_variable_statique.nom = malloc((strlen( - (*(*l_element_statique_courant).variable).nom) + 1) * + if ((s_variable_statique.nom = rpl_malloc(s_nouvel_etat_processus, + (strlen((*(*l_element_statique_courant).variable).nom) + 1) * sizeof(unsigned char))) == NULL) { (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; @@ -2242,7 +2454,7 @@ copie_arbre_variables(struct_processus * */ /* - * Caractères autorisés dans les instructions + * Caractères autorisés dans les variables * * A B C D E F G H I J K L M N O P Q R S T U V W X Y Z * a b c d e f g h i j k l m n o p q r s t u v w x y z @@ -2272,7 +2484,7 @@ initialisation_variables(struct_processu } if (((*s_etat_processus).pointeurs_caracteres_variables = - malloc(longueur_tableau * sizeof(int))) == NULL) + malloc(((size_t) longueur_tableau) * sizeof(int))) == NULL) { (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; return;