Ilya Zakharevich on Thu, 24 Nov 2005 23:30:52 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: pari_init, RE-INIT, and programs using libpari.


On Thu, Nov 24, 2005 at 10:11:53AM +0100, Bill Allombert wrote:
> > This is what Math::Pari is using for many years now:
> > 
> >    INIT_JMP_off;
> >    INIT_SIG_off;
> 
> So if we were to provide pari_init_fancy(), would you use it ?

This depends.  Note that Math::Pari must support older versions of
GP/PARI too.

BTW, I attach my attempt to make GP/PARI 2.1.7 restartable (this is a
part of Math::Pari now; grep for reset_on_reload in Pari.xs); I still
get segfaults at some moment if I try to reset...

Hope this helps,
Ilya

--- ./src/language/anal.c~	Fri Sep 16 09:50:48 2005
+++ ./src/language/anal.c	Sat Oct 29 08:37:28 2005
@@ -1998,6 +1998,8 @@ manage_var(long n, entree *ep)
 
   if (n) /* special behaviour */
   {
+    int leave_vars = 1;			/* x undestructible */
+
     switch(n)
     {
       case 2: return nvar=0;
@@ -2010,10 +2012,12 @@ manage_var(long n, entree *ep)
         setlg(polvar, nvar);
         return --nvar;
       }
+      case 100:
+	leave_vars = 0;
     }
 
     /* user wants to delete one of his/her/its variables */
-    if (max_avail == MAXVARN-1) return 0; /* nothing to delete */
+    if (max_avail == MAXVARN-leave_vars) return 0; /* nothing to delete */
     free(polx[++max_avail]); /* frees both polun and polx */
     return max_avail+1;
   }
--- ./src/language/init.c~	Fri Oct 28 17:45:22 2005
+++ ./src/language/init.c	Sat Oct 29 08:55:22 2005
@@ -394,6 +394,7 @@ reset_traps(int warn)
   for (i=0; i <= noer; i++) err_catch_array[i] = 0;
 }
 
+static need_force_init_defaults = 0;
 /* initialise les donnees de la bibliotheque PARI. Peut être précédée d'un
  * appel à pari_addfunctions si on ajoute d'autres fonctions au pool de base.
  */
@@ -407,7 +408,9 @@ pari_init(long parisize, long maxprime)
 #ifdef STACK_CHECK
   pari_init_stackcheck(&i);
 #endif
-  init_defaults(0);
+  init_defaults(need_force_init_defaults);
+  need_force_init_defaults = 0;
+
   if (INIT_JMP && setjmp(environnement))
   {
     fprintferr("  ***   Error in the PARI system. End of program.\n");
@@ -503,7 +506,7 @@ freeall(void)
   long i;
   entree *ep,*ep1;
 
-  while (delete_var()) /* empty */;
+  while (delete_var());				/* empty all but x */;
   for (i = 0; i < functions_tblsz; i++)
   {
     for (ep = functions_hash[i]; ep; ep = ep1)
@@ -518,17 +521,28 @@ freeall(void)
     members_hash[i] = NULL;
   }
   free((void*)varentries); free((void*)ordvar); free((void*)polvar);
-  free((void*)polx[MAXVARN]); free((void*)polx); free((void*)polun);
-  free((void*)primetab);
-  free((void*)universal_constants);
+  varentries = NULL; ordvar = NULL; polvar = NULL;
+#if 1
+  manage_var(100, (entree *)0);			/* now empty x too */
+#else
+  free((void*)polx[MAXVARN]);
+  polx[MAXVARN] = NULL;
+#endif
+  free((void*)polx); free((void*)polun);
+  polx = NULL; polun = NULL;
+  free((void*)primetab); primetab = NULL;
+  free((void*)universal_constants); universal_constants = NULL;
 
   /* set first cell to 0 to inhibit recursion in all cases */
   while (cur_bloc) { *cur_bloc=0; killbloc(cur_bloc); }
   killallfiles(1);
   free((void *)functions_hash);
+  functions_hash = NULL;
   free((void *)bot); free((void *)diffptr);
-  free(current_logfile);
-  free(current_psfile);
+  bot = 0; diffptr = 0;
+  free(current_logfile);  current_logfile = NULL;
+  free(current_psfile);   current_psfile  = NULL;
+  need_force_init_defaults = 1;
 
   if (gp_history_fun)
     gp_history_fun(0,-1,NULL,NULL);