--- rpl/src/gestion_objets.c 2010/05/24 10:58:30 1.20 +++ rpl/src/gestion_objets.c 2010/05/25 18:09:44 1.21 @@ -3565,20 +3565,24 @@ copie_etat_processus(struct_processus *s #undef malloc #undef realloc #undef free +#undef fork #ifdef return # undef return #endif -#undef fprintf - #ifdef __BACKTRACE +#define PROFONDEUR_PILE 64 #define return(a) { if (a == NULL) \ - { BACKTRACE(20); fprintf(stderr, ">>> MEDITATION %d\n", __LINE__); } \ + { BACKTRACE(PROFONDEUR_PILE); \ + fprintf(stderr, ">>> MEDITATION %d\n", __LINE__); } \ return(a); } while(0) -#define PROFONDEUR_PILE 64 #endif +#undef fprintf +#define check(a, b) ((strcmp(#a, fonction) == 0) && (ligne == b)) +#undef CORE_DUMP + typedef struct memoire { void *pointeur; @@ -3596,10 +3600,20 @@ typedef struct memoire static struct_memoire *debug = NULL; static unsigned long long ordre = 0; -static pthread_mutex_t mutex_allocation = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t mutex_allocation; -#define check(a, b) ((strcmp(#a, fonction) == 0) && (ligne == b)) -#undef CORE_DUMP +void +debug_memoire_initialisation() +{ + pthread_mutexattr_t attributs_mutex; + + pthread_mutexattr_init(&attributs_mutex); + pthread_mutexattr_settype(&attributs_mutex, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&mutex_allocation, &attributs_mutex); + pthread_mutexattr_destroy(&attributs_mutex); + + return; +} void * debug_memoire_ajout(size_t taille, const unsigned char *fonction, @@ -3607,6 +3621,8 @@ debug_memoire_ajout(size_t taille, const { struct_memoire *ancienne_base; + void *pointeur; + pthread_mutex_lock(&mutex_allocation); ancienne_base = debug; @@ -3628,6 +3644,8 @@ debug_memoire_ajout(size_t taille, const (*debug).taille = taille; (*debug).ordre = ordre; + pointeur = (*debug).pointeur; + # ifdef __BACKTRACE (*debug).profondeur = backtrace((*debug).pile, PROFONDEUR_PILE); # endif @@ -3649,10 +3667,12 @@ debug_memoire_ajout(size_t taille, const strcpy((*debug).fonction, fonction); strcpy((*debug).argument, argument); + memset((*debug).pointeur, 0, (*debug).taille); + pthread_mutex_unlock(&mutex_allocation); ordre++; - return((*debug).pointeur); + return(pointeur); } void * @@ -3666,9 +3686,11 @@ debug_memoire_modification(void *pointeu { if (taille == 0) { - // Revient à free() + // Revient à free(). Il n'y a pas de parenthèses car on ne veut + // pas utiliser la macro return(). + debug_memoire_retrait(pointeur); - return(NULL); + return NULL ; } else { @@ -3691,41 +3713,50 @@ debug_memoire_modification(void *pointeu if (element_courant == NULL) { pthread_mutex_unlock(&mutex_allocation); - return(NULL); - } - if (((*element_courant).pointeur = realloc(pointeur, taille)) - == NULL) - { - pthread_mutex_unlock(&mutex_allocation); - return(NULL); + uprintf("[%d-%llu] ILLEGAL POINTER\n", + getpid(), (unsigned long long) pthread_self()); +# ifdef __BACKTRACE + BACKTRACE(PROFONDEUR_PILE); +# endif + + return(realloc(pointeur, taille)); } + else + { + if (((*element_courant).pointeur = realloc(pointeur, taille)) + == NULL) + { + pthread_mutex_unlock(&mutex_allocation); + return(NULL); + } - (*element_courant).ligne = ligne; - (*element_courant).taille = taille; - free((*element_courant).fonction); - free((*element_courant).argument); + (*element_courant).ligne = ligne; + (*element_courant).taille = taille; + free((*element_courant).fonction); + free((*element_courant).argument); - if (((*element_courant).fonction = malloc((strlen(fonction) + 1) * - sizeof(unsigned char))) == NULL) - { - pthread_mutex_unlock(&mutex_allocation); - return(NULL); - } + if (((*element_courant).fonction = malloc((strlen(fonction) + + 1) * sizeof(unsigned char))) == NULL) + { + pthread_mutex_unlock(&mutex_allocation); + return(NULL); + } - if (((*element_courant).argument = malloc((strlen(argument) + 1) * - sizeof(unsigned char))) == NULL) - { - pthread_mutex_unlock(&mutex_allocation); - return(NULL); - } + if (((*element_courant).argument = malloc((strlen(argument) + + 1) * sizeof(unsigned char))) == NULL) + { + pthread_mutex_unlock(&mutex_allocation); + return(NULL); + } - strcpy((*element_courant).fonction, fonction); - strcpy((*element_courant).argument, argument); + strcpy((*element_courant).fonction, fonction); + strcpy((*element_courant).argument, argument); - pthread_mutex_unlock(&mutex_allocation); + pthread_mutex_unlock(&mutex_allocation); - return((*element_courant).pointeur); + return((*element_courant).pointeur); + } } } else @@ -3760,6 +3791,11 @@ debug_memoire_retrait(void *pointeur) (*element_precedent).suivant = (*element_courant).suivant; } + if (pointeur != NULL) + { + memset(pointeur, 0, (*element_courant).taille); + } + free((*element_courant).fonction); free((*element_courant).argument); free(element_courant); @@ -3773,8 +3809,16 @@ debug_memoire_retrait(void *pointeur) pthread_mutex_unlock(&mutex_allocation); - free(pointeur); + if (element_courant == NULL) + { + uprintf("[%d-%llu] ILLEGAL POINTER\n", + getpid(), (unsigned long long) pthread_self()); +# ifdef __BACKTRACE + BACKTRACE(PROFONDEUR_PILE); +# endif + } + free(pointeur); return; } @@ -3876,6 +3920,7 @@ debug_memoire_verification() } pthread_mutex_unlock(&mutex_allocation); + pthread_mutex_destroy(&mutex_allocation); fprintf(stderr, "[%d-%llu] END OF LIST\n", getpid(), (unsigned long long) pthread_self()); @@ -3884,17 +3929,41 @@ debug_memoire_verification() } void -debug_memoire_reinitialisation() +debug_memoire_verrouillage() { - ordre = 0; - debug = NULL; + pthread_mutex_lock(&mutex_allocation); + return; +} - pthread_mutex_trylock(&mutex_allocation); +void +debug_memoire_deverrouillage() +{ pthread_mutex_unlock(&mutex_allocation); - return; } +pid_t +debug_fork() +{ + pid_t pid; + + pthread_mutex_lock(&mutex_allocation); + pid = fork(); + + if (pid == 0) + { + pthread_mutex_destroy(&mutex_allocation); + debug_memoire_initialisation(); + } + else + { + pthread_mutex_unlock(&mutex_allocation); + } + + // Pas de parenthèses pour ne pas remplacer return par sa macro. + return pid; +} + void analyse_post_mortem() {