Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - language - init.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23866-3f72739fd) Lines: 867 1251 69.3 %
Date: 2019-05-23 05:44:57 Functions: 110 147 74.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2003  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*        INITIALIZING THE SYSTEM, ERRORS, STACK MANAGEMENT        */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : /* _GNU_SOURCE is needed before first include to get RUSAGE_THREAD */
      20             : #undef _GNU_SOURCE /* avoid warning */
      21             : #define _GNU_SOURCE
      22             : #include <string.h>
      23             : #if defined(_WIN32) || defined(__CYGWIN32__)
      24             : #  include "../systems/mingw/mingw.h"
      25             : #  include <process.h>
      26             : #endif
      27             : #include "paricfg.h"
      28             : #if defined(STACK_CHECK) && !defined(__EMX__)
      29             : #  include <sys/types.h>
      30             : #  include <sys/time.h>
      31             : #  include <sys/resource.h>
      32             : #endif
      33             : #if defined(HAS_WAITPID) && defined(HAS_SETSID)
      34             : #  include <sys/wait.h>
      35             : #endif
      36             : #ifdef HAS_MMAP
      37             : #  include <sys/mman.h>
      38             : #endif
      39             : #if defined(USE_GETTIMEOFDAY) || defined(USE_GETRUSAGE) || defined(USE_TIMES)
      40             : #  include <sys/time.h>
      41             : #endif
      42             : #if defined(USE_GETRUSAGE)
      43             : #  include <sys/resource.h>
      44             : #endif
      45             : #if defined(USE_FTIME) || defined(USE_FTIMEFORWALLTIME)
      46             : #  include <sys/timeb.h>
      47             : #endif
      48             : #if defined(USE_CLOCK_GETTIME) || defined(USE_TIMES)
      49             : #  include <time.h>
      50             : #endif
      51             : #if defined(USE_TIMES)
      52             : #  include <sys/times.h>
      53             : #endif
      54             : #include "pari.h"
      55             : #include "paripriv.h"
      56             : #include "anal.h"
      57             : 
      58             : const double LOG10_2 = 0.3010299956639812; /* log_10(2) */
      59             : const double LOG2_10 = 3.321928094887362;  /* log_2(10) */
      60             : 
      61             : GEN gnil, gen_0, gen_1, gen_m1, gen_2, gen_m2, ghalf, err_e_STACK;
      62             : 
      63             : static const ulong readonly_constants[] = {
      64             :   evaltyp(t_INT) | _evallg(2),  /* gen_0 */
      65             :   evallgefint(2),
      66             :   evaltyp(t_INT) | _evallg(2),  /* gnil */
      67             :   evallgefint(2),
      68             :   evaltyp(t_INT) | _evallg(3),  /* gen_1 */
      69             :   evalsigne(1) | evallgefint(3),
      70             :   1,
      71             :   evaltyp(t_INT) | _evallg(3),  /* gen_2 */
      72             :   evalsigne(1) | evallgefint(3),
      73             :   2,
      74             :   evaltyp(t_INT) | _evallg(3),  /* gen_m1 */
      75             :   evalsigne(-1) | evallgefint(3),
      76             :   1,
      77             :   evaltyp(t_INT) | _evallg(3),  /* gen_m2 */
      78             :   evalsigne(-1) | evallgefint(3),
      79             :   2,
      80             :   evaltyp(t_ERROR) | _evallg(2), /* err_e_STACK */
      81             :   e_STACK,
      82             :   evaltyp(t_FRAC) | _evallg(3), /* ghalf */
      83             :   (ulong)(readonly_constants+4),
      84             :   (ulong)(readonly_constants+7)
      85             : };
      86             : THREAD GEN zetazone, bernzone, primetab;
      87             : byteptr diffptr;
      88             : FILE    *pari_outfile, *pari_errfile, *pari_logfile, *pari_infile;
      89             : char    *current_logfile, *current_psfile, *pari_datadir;
      90             : long    gp_colors[c_LAST];
      91             : int     disable_color;
      92             : ulong   DEBUGFILES, DEBUGLEVEL, DEBUGMEM;
      93             : long    DEBUGVAR;
      94             : ulong   pari_mt_nbthreads;
      95             : long    precreal;
      96             : ulong   precdl, logstyle;
      97             : gp_data *GP_DATA;
      98             : 
      99             : entree  **varentries;
     100             : THREAD long *varpriority;
     101             : 
     102             : THREAD pari_sp avma;
     103             : THREAD struct pari_mainstack *pari_mainstack;
     104             : 
     105             : static void ** MODULES;
     106             : static pari_stack s_MODULES;
     107             : const long functions_tblsz = 135; /* size of functions_hash */
     108             : entree **functions_hash, **defaults_hash;
     109             : 
     110             : char *(*cb_pari_fgets_interactive)(char *s, int n, FILE *f);
     111             : int (*cb_pari_get_line_interactive)(const char*, const char*, filtre_t *F);
     112             : void (*cb_pari_quit)(long);
     113             : void (*cb_pari_init_histfile)(void);
     114             : void (*cb_pari_ask_confirm)(const char *);
     115             : int  (*cb_pari_handle_exception)(long);
     116             : int  (*cb_pari_err_handle)(GEN);
     117             : int  (*cb_pari_whatnow)(PariOUT *out, const char *, int);
     118             : void (*cb_pari_sigint)(void);
     119             : void (*cb_pari_pre_recover)(long);
     120             : void (*cb_pari_err_recover)(long);
     121             : int (*cb_pari_break_loop)(int);
     122             : int (*cb_pari_is_interactive)(void);
     123             : void (*cb_pari_start_output)();
     124             : 
     125             : const char * pari_library_path = NULL;
     126             : 
     127             : static THREAD GEN global_err_data;
     128             : THREAD jmp_buf *iferr_env;
     129             : const long CATCH_ALL = -1;
     130             : 
     131             : static void pari_init_timer(void);
     132             : 
     133             : /*********************************************************************/
     134             : /*                                                                   */
     135             : /*                       BLOCKS & CLONES                             */
     136             : /*                                                                   */
     137             : /*********************************************************************/
     138             : /*#define DEBUG*/
     139             : static THREAD long next_block;
     140             : static THREAD GEN cur_block; /* current block in block list */
     141             : #ifdef DEBUG
     142             : static THREAD long NUM;
     143             : #endif
     144             : 
     145             : static void
     146      121885 : pari_init_blocks(void)
     147             : {
     148      121885 :   next_block = 0; cur_block = NULL;
     149             : #ifdef DEBUG
     150             :   NUM = 0;
     151             : #endif
     152      121885 : }
     153             : 
     154             : static void
     155      119009 : pari_close_blocks(void)
     156             : {
     157      119009 :   while (cur_block) killblock(cur_block);
     158      121406 : }
     159             : 
     160             : /* Return x, where:
     161             :  * x[-4]: reference count
     162             :  * x[-3]: adress of next block
     163             :  * x[-2]: adress of preceding block.
     164             :  * x[-1]: number of allocated blocs.
     165             :  * x[0..n-1]: malloc-ed memory. */
     166             : GEN
     167   207684811 : newblock(size_t n)
     168             : {
     169   207684811 :   long *x = (long *) pari_malloc((n + BL_HEAD)*sizeof(long)) + BL_HEAD;
     170             : 
     171   207686403 :   bl_size(x) = n;
     172   207686403 :   bl_refc(x) = 1;
     173   207686403 :   bl_next(x) = NULL;
     174   207686403 :   bl_prev(x) = cur_block;
     175   207686403 :   bl_num(x)  = next_block++;
     176   207686403 :   if (cur_block) bl_next(cur_block) = x;
     177             : #ifdef DEBUG
     178             :   err_printf("+ %ld\n", ++NUM);
     179             : #endif
     180   207686403 :   if (DEBUGMEM > 2)
     181           0 :     err_printf("new block, size %6lu (no %ld): %08lx\n", n, next_block-1, x);
     182   207686276 :   return cur_block = x;
     183             : }
     184             : 
     185             : GEN
     186       51051 : gcloneref(GEN x)
     187             : {
     188       51051 :   if (isclone(x)) { ++bl_refc(x); return x; }
     189       50736 :   else return gclone(x);
     190             : }
     191             : 
     192             : void
     193           0 : gclone_refc(GEN x) { ++bl_refc(x); }
     194             : 
     195             : void
     196   242087377 : gunclone(GEN x)
     197             : {
     198   242087377 :   if (--bl_refc(x) > 0) return;
     199   207680465 :   BLOCK_SIGINT_START;
     200   207684359 :   if (bl_next(x)) bl_prev(bl_next(x)) = bl_prev(x);
     201             :   else
     202             :   {
     203    25521188 :     cur_block = bl_prev(x);
     204    25521188 :     next_block = bl_num(x);
     205             :   }
     206   207684359 :   if (bl_prev(x)) bl_next(bl_prev(x)) = bl_next(x);
     207   207684359 :   if (DEBUGMEM > 2)
     208           0 :     err_printf("killing block (no %ld): %08lx\n", bl_num(x), x);
     209   207685457 :   free((void*)bl_base(x)); /* pari_free not needed: we already block */
     210   207685457 :   BLOCK_SIGINT_END;
     211             : #ifdef DEBUG
     212             :   err_printf("- %ld\n", NUM--);
     213             : #endif
     214             : }
     215             : 
     216             : /* Recursively look for clones in the container and kill them. Then kill
     217             :  * container if clone. SIGINT could be blocked until it returns */
     218             : void
     219  2925400190 : gunclone_deep(GEN x)
     220             : {
     221             :   long i, lx;
     222             :   GEN v;
     223  2925400190 :   if (isclone(x) && bl_refc(x) > 1) { --bl_refc(x); return; }
     224  2873552667 :   BLOCK_SIGINT_START;
     225  2873552667 :   switch(typ(x))
     226             :   {
     227             :     case t_VEC: case t_COL: case t_MAT:
     228   116843518 :       lx = lg(x);
     229   116843518 :       for (i=1;i<lx;i++) gunclone_deep(gel(x,i));
     230   116843518 :       break;
     231             :     case t_LIST:
     232         844 :       v = list_data(x); lx = v? lg(v): 1;
     233         844 :       for (i=1;i<lx;i++) gunclone_deep(gel(v,i));
     234         844 :       if (v) killblock(v);
     235         844 :       break;
     236             :   }
     237  2873552667 :   clone_unlock(x);
     238  2873552667 :   BLOCK_SIGINT_END;
     239             : }
     240             : 
     241             : int
     242      179462 : pop_entree_block(entree *ep, long loc)
     243             : {
     244      179462 :   GEN x = (GEN)ep->value;
     245      179462 :   if (bl_num(x) < loc) return 0; /* older */
     246         252 :   if (DEBUGMEM>2)
     247           0 :     err_printf("popping %s (block no %ld)\n", ep->name, bl_num(x));
     248         252 :   gunclone_deep(x); return 1;
     249             : }
     250             : 
     251             : /***************************************************************************
     252             :  **                                                                       **
     253             :  **                           Export                                      **
     254             :  **                                                                       **
     255             :  ***************************************************************************/
     256             : 
     257             : static hashtable *export_hash;
     258             : static void
     259        1566 : pari_init_export(void)
     260             : {
     261        1566 :   export_hash = hash_create_str(1,0);
     262        1566 : }
     263             : static void
     264        1556 : pari_close_export(void)
     265             : {
     266        1556 :   hash_destroy(export_hash);
     267        1556 : }
     268             : 
     269             : /* Exported values are blocks, but do not have the clone bit set so that they
     270             :  * are not affected by clone_lock and ensure_nb, etc. */
     271             : 
     272             : void
     273           2 : export_add(const char *str, GEN val)
     274             : {
     275             :   hashentry *h;
     276           2 :   val = gclone(val); unsetisclone(val);
     277           2 :   h = hash_search(export_hash, (void*) str);
     278           2 :   if (h)
     279             :   {
     280           0 :     GEN v = (GEN)h->val;
     281           0 :     h->val = val;
     282           0 :     setisclone(v); gunclone(v);
     283             :   }
     284             :   else
     285           2 :     hash_insert(export_hash,(void*)str, (void*) val);
     286           2 : }
     287             : 
     288             : void
     289           2 : export_del(const char *str)
     290             : {
     291           2 :   hashentry *h = hash_remove(export_hash,(void*)str);
     292           2 :   if (h)
     293             :   {
     294           2 :     GEN v = (GEN)h->val;
     295           2 :     setisclone(v); gunclone(v);
     296           2 :     pari_free(h);
     297             :   }
     298           2 : }
     299             : 
     300             : GEN
     301         200 : export_get(const char *str)
     302             : {
     303         200 :   return hash_haskey_GEN(export_hash,(void*)str);
     304             : }
     305             : 
     306             : void
     307           0 : unexportall(void)
     308             : {
     309           0 :   GEN keys = hash_keys(export_hash);
     310           0 :   long i, l = lg(keys);
     311           0 :   for(i=1; i<l; i++)
     312             :   {
     313           0 :     const char *s = (const char *) keys[i];
     314           0 :     mt_export_del(s);
     315             :   }
     316           0 : }
     317             : 
     318             : void
     319           0 : exportall(void)
     320             : {
     321             :   long i;
     322           0 :   for (i = 0; i < functions_tblsz; i++)
     323             :   {
     324           0 :     entree *ep = functions_hash[i];
     325           0 :     while (ep)
     326             :     {
     327           0 :       entree *EP = ep->next;
     328           0 :       if (EpVALENCE(ep)==EpVAR)
     329           0 :         mt_export_add(ep->name, (GEN) ep->value);
     330           0 :       ep = EP;
     331             :     }
     332             :   }
     333           0 : }
     334             : 
     335             : /*********************************************************************/
     336             : /*                                                                   */
     337             : /*                       C STACK SIZE CONTROL                        */
     338             : /*                                                                   */
     339             : /*********************************************************************/
     340             : /* Avoid core dump on deep recursion. Adapted Perl code by Dominic Dunlop */
     341             : THREAD void *PARI_stack_limit = NULL;
     342             : 
     343             : #ifdef STACK_CHECK
     344             : 
     345             : #  ifdef __EMX__                                /* Emulate */
     346             : void
     347             : pari_stackcheck_init(void *pari_stack_base)
     348             : {
     349             :   (void) pari_stack_base;
     350             :   if (!pari_stack_base) { PARI_stack_limit = NULL; return; }
     351             :   PARI_stack_limit = get_stack(1./16, 32*1024);
     352             : }
     353             : #  else /* !__EMX__ */
     354             : /* Set PARI_stack_limit to (a little above) the lowest safe address that can be
     355             :  * used on the stack. Leave PARI_stack_limit at its initial value (NULL) to
     356             :  * show no check should be made [init failed]. Assume stack grows downward. */
     357             : void
     358        1566 : pari_stackcheck_init(void *pari_stack_base)
     359             : {
     360             :   struct rlimit rip;
     361             :   ulong size;
     362        1566 :   if (!pari_stack_base) { PARI_stack_limit = NULL; return; }
     363        1566 :   if (getrlimit(RLIMIT_STACK, &rip)) return;
     364        1566 :   size = rip.rlim_cur;
     365        1566 :   if (size == (ulong)RLIM_INFINITY || size > (ulong)pari_stack_base)
     366           0 :     PARI_stack_limit = (void*)(((ulong)pari_stack_base) / 16);
     367             :   else
     368        1566 :     PARI_stack_limit = (void*)((ulong)pari_stack_base - (size/16)*15);
     369             : }
     370             : #  endif /* !__EMX__ */
     371             : 
     372             : #else
     373             : void
     374             : pari_stackcheck_init(void *pari_stack_base)
     375             : {
     376             :   (void) pari_stack_base; PARI_stack_limit = NULL;
     377             : }
     378             : #endif /* STACK_CHECK */
     379             : 
     380             : /*******************************************************************/
     381             : /*                         HEAP TRAVERSAL                          */
     382             : /*******************************************************************/
     383             : struct getheap_t { long n, l; };
     384             : /* x is a block, not necessarily a clone [x[0] may not be set] */
     385             : static void
     386        6769 : f_getheap(GEN x, void *D)
     387             : {
     388        6769 :   struct getheap_t *T = (struct getheap_t*)D;
     389        6769 :   T->n++;
     390        6769 :   T->l += bl_size(x) + BL_HEAD;
     391        6769 : }
     392             : GEN
     393          84 : getheap(void)
     394             : {
     395          84 :   struct getheap_t T = { 0, 0 };
     396          84 :   traverseheap(&f_getheap, &T); return mkvec2s(T.n, T.l);
     397             : }
     398             : 
     399             : void
     400          84 : traverseheap( void(*f)(GEN, void *), void *data )
     401             : {
     402             :   GEN x;
     403          84 :   for (x = cur_block; x; x = bl_prev(x)) f(x, data);
     404          84 : }
     405             : 
     406             : /*********************************************************************/
     407             : /*                          DAEMON / FORK                            */
     408             : /*********************************************************************/
     409             : #if defined(HAS_WAITPID) && defined(HAS_SETSID)
     410             : /* Properly fork a process, detaching from main process group without creating
     411             :  * zombies on exit. Parent returns 1, son returns 0 */
     412             : int
     413          60 : pari_daemon(void)
     414             : {
     415          60 :   pid_t pid = fork();
     416         120 :   switch(pid) {
     417           0 :       case -1: return 1; /* father, fork failed */
     418             :       case 0:
     419          60 :         (void)setsid(); /* son becomes process group leader */
     420          60 :         if (fork()) _exit(0); /* now son exits, also when fork fails */
     421          60 :         break; /* grandson: its father is the son, which exited,
     422             :                 * hence father becomes 'init', that'll take care of it */
     423             :       default: /* father, fork succeeded */
     424          60 :         (void)waitpid(pid,NULL,0); /* wait for son to exit, immediate */
     425          60 :         return 1;
     426             :   }
     427             :   /* grandson. The silly '!' avoids a gcc-8 warning (unused value) */
     428          60 :   (void)!freopen("/dev/null","r",stdin);
     429          60 :   return 0;
     430             : }
     431             : #else
     432             : int
     433             : pari_daemon(void)
     434             : {
     435             :   pari_err_IMPL("pari_daemon without waitpid & setsid");
     436             :   return 0;
     437             : }
     438             : #endif
     439             : 
     440             : /*********************************************************************/
     441             : /*                                                                   */
     442             : /*                       SYSTEM INITIALIZATION                       */
     443             : /*                                                                   */
     444             : /*********************************************************************/
     445             : static int try_to_recover = 0;
     446             : THREAD VOLATILE int PARI_SIGINT_block = 0, PARI_SIGINT_pending = 0;
     447             : 
     448             : /*********************************************************************/
     449             : /*                         SIGNAL HANDLERS                           */
     450             : /*********************************************************************/
     451             : static void
     452           0 : dflt_sigint_fun(void) { pari_err(e_MISC, "user interrupt"); }
     453             : 
     454             : #if defined(_WIN32) || defined(__CYGWIN32__)
     455             : int win32ctrlc = 0, win32alrm = 0;
     456             : void
     457             : dowin32ctrlc(void)
     458             : {
     459             :   win32ctrlc = 0;
     460             :   cb_pari_sigint();
     461             : }
     462             : #endif
     463             : 
     464             : static void
     465           0 : pari_handle_SIGINT(void)
     466             : {
     467             : #ifdef _WIN32
     468             :   if (++win32ctrlc >= 5) _exit(3);
     469             : #else
     470           0 :   cb_pari_sigint();
     471             : #endif
     472           0 : }
     473             : 
     474             : typedef void (*pari_sighandler_t)(int);
     475             : 
     476             : pari_sighandler_t
     477       17166 : os_signal(int sig, pari_sighandler_t f)
     478             : {
     479             : #ifdef HAS_SIGACTION
     480             :   struct sigaction sa, oldsa;
     481             : 
     482       17166 :   sa.sa_handler = f;
     483       17166 :   sigemptyset(&sa.sa_mask);
     484       17166 :   sa.sa_flags = SA_NODEFER;
     485             : 
     486       17166 :   if (sigaction(sig, &sa, &oldsa)) return NULL;
     487       17166 :   return oldsa.sa_handler;
     488             : #else
     489             :   return signal(sig,f);
     490             : #endif
     491             : }
     492             : 
     493             : void
     494           0 : pari_sighandler(int sig)
     495             : {
     496             :   const char *msg;
     497             : #ifndef HAS_SIGACTION
     498             :   /*SYSV reset the signal handler in the handler*/
     499             :   (void)os_signal(sig,pari_sighandler);
     500             : #endif
     501           0 :   switch(sig)
     502             :   {
     503             : #ifdef SIGBREAK
     504             :     case SIGBREAK:
     505             :       if (PARI_SIGINT_block==1)
     506             :       {
     507             :         PARI_SIGINT_pending=SIGBREAK;
     508             :         mt_sigint();
     509             :       }
     510             :       else pari_handle_SIGINT();
     511             :       return;
     512             : #endif
     513             : 
     514             : #ifdef SIGINT
     515             :     case SIGINT:
     516           0 :       if (PARI_SIGINT_block==1)
     517             :       {
     518           0 :         PARI_SIGINT_pending=SIGINT;
     519           0 :         mt_sigint();
     520             :       }
     521           0 :       else pari_handle_SIGINT();
     522           0 :       return;
     523             : #endif
     524             : 
     525             : #ifdef SIGSEGV
     526             :     case SIGSEGV:
     527           0 :       msg="PARI/GP (Segmentation Fault)"; break;
     528             : #endif
     529             : #ifdef SIGBUS
     530             :     case SIGBUS:
     531           0 :       msg="PARI/GP (Bus Error)"; break;
     532             : #endif
     533             : #ifdef SIGFPE
     534             :     case SIGFPE:
     535           0 :       msg="PARI/GP (Floating Point Exception)"; break;
     536             : #endif
     537             : 
     538             : #ifdef SIGPIPE
     539             :     case SIGPIPE:
     540             :     {
     541           0 :       pariFILE *f = GP_DATA->pp->file;
     542           0 :       if (f && pari_outfile == f->file)
     543             :       {
     544           0 :         GP_DATA->pp->file = NULL; /* to avoid oo recursion on error */
     545           0 :         pari_outfile = stdout; pari_fclose(f);
     546           0 :         pari_err(e_MISC, "Broken Pipe, resetting file stack...");
     547             :       }
     548             :       return; /* LCOV_EXCL_LINE */
     549             :     }
     550             : #endif
     551             : 
     552           0 :     default: msg="signal handling"; break;
     553             :   }
     554           0 :   pari_err_BUG(msg);
     555             : }
     556             : 
     557             : void
     558        3122 : pari_sig_init(void (*f)(int))
     559             : {
     560             : #ifdef SIGBUS
     561        3122 :   (void)os_signal(SIGBUS,f);
     562             : #endif
     563             : #ifdef SIGFPE
     564        3122 :   (void)os_signal(SIGFPE,f);
     565             : #endif
     566             : #ifdef SIGINT
     567        3122 :   (void)os_signal(SIGINT,f);
     568             : #endif
     569             : #ifdef SIGBREAK
     570             :   (void)os_signal(SIGBREAK,f);
     571             : #endif
     572             : #ifdef SIGPIPE
     573        3122 :   (void)os_signal(SIGPIPE,f);
     574             : #endif
     575             : #ifdef SIGSEGV
     576        3122 :   (void)os_signal(SIGSEGV,f);
     577             : #endif
     578        3122 : }
     579             : 
     580             : /*********************************************************************/
     581             : /*                      STACK AND UNIVERSAL CONSTANTS                */
     582             : /*********************************************************************/
     583             : static void
     584        1566 : init_universal_constants(void)
     585             : {
     586        1566 :   gen_0  = (GEN)readonly_constants;
     587        1566 :   gnil   = (GEN)readonly_constants+2;
     588        1566 :   gen_1  = (GEN)readonly_constants+4;
     589        1566 :   gen_2  = (GEN)readonly_constants+7;
     590        1566 :   gen_m1 = (GEN)readonly_constants+10;
     591        1566 :   gen_m2 = (GEN)readonly_constants+13;
     592        1566 :   err_e_STACK = (GEN)readonly_constants+16;
     593        1566 :   ghalf  = (GEN)readonly_constants+18;
     594        1566 : }
     595             : 
     596             : static void
     597      122026 : pari_init_errcatch(void)
     598             : {
     599      122026 :   iferr_env = NULL;
     600      122026 :   global_err_data = NULL;
     601      122026 : }
     602             : 
     603             : /*********************************************************************/
     604             : /*                           INIT DEFAULTS                           */
     605             : /*********************************************************************/
     606             : void
     607        1566 : pari_init_defaults(void)
     608             : {
     609             :   long i;
     610        1566 :   initout(1);
     611             : 
     612             : #ifdef LONG_IS_64BIT
     613        1346 :   precreal = 128;
     614             : #else
     615         220 :   precreal = 96;
     616             : #endif
     617             : 
     618        1566 :   precdl = 16;
     619        1566 :   DEBUGFILES = DEBUGLEVEL = 0;
     620        1566 :   DEBUGMEM = 1;
     621        1566 :   disable_color = 1;
     622        1566 :   logstyle = logstyle_none;
     623             : 
     624        1566 :   current_psfile = pari_strdup("pari.ps");
     625        1566 :   current_logfile= pari_strdup("pari.log");
     626        1566 :   pari_logfile = NULL;
     627             : 
     628        1566 :   pari_datadir = os_getenv("GP_DATA_DIR");
     629        1566 :   if (!pari_datadir)
     630             :   {
     631             : #if defined(_WIN32) || defined(__CYGWIN32__)
     632             :     if (paricfg_datadir[0]=='@' && paricfg_datadir[1]==0)
     633             :       pari_datadir = win32_datadir();
     634             :     else
     635             : #endif
     636        1566 :       pari_datadir = pari_strdup(paricfg_datadir);
     637             :   }
     638           0 :   else pari_datadir= pari_strdup(pari_datadir);
     639        1566 :   for (i=0; i<c_LAST; i++) gp_colors[i] = c_NONE;
     640        1566 : }
     641             : 
     642             : /*********************************************************************/
     643             : /*                   FUNCTION HASHTABLES, MODULES                    */
     644             : /*********************************************************************/
     645             : extern entree functions_basic[], functions_default[];
     646             : static void
     647        1566 : pari_init_functions(void)
     648             : {
     649        1566 :   pari_stack_init(&s_MODULES, sizeof(*MODULES),(void**)&MODULES);
     650        1566 :   pari_stack_pushp(&s_MODULES,functions_basic);
     651        1566 :   functions_hash = (entree**) pari_calloc(sizeof(entree*)*functions_tblsz);
     652        1566 :   pari_fill_hashtable(functions_hash, functions_basic);
     653        1566 :   defaults_hash = (entree**) pari_calloc(sizeof(entree*)*functions_tblsz);
     654        1566 :   pari_add_defaults_module(functions_default);
     655        1566 : }
     656             : 
     657             : void
     658        1556 : pari_add_module(entree *ep)
     659             : {
     660        1556 :   pari_fill_hashtable(functions_hash, ep);
     661        1556 :   pari_stack_pushp(&s_MODULES, ep);
     662        1556 : }
     663             : 
     664             : void
     665        1566 : pari_add_defaults_module(entree *ep)
     666        1566 : { pari_fill_hashtable(defaults_hash, ep); }
     667             : 
     668             : /*********************************************************************/
     669             : /*                       PARI MAIN STACK                             */
     670             : /*********************************************************************/
     671             : 
     672             : #ifdef HAS_MMAP
     673             : #define PARI_STACK_ALIGN (sysconf(_SC_PAGE_SIZE))
     674             : #ifndef MAP_ANONYMOUS
     675             : #define MAP_ANONYMOUS MAP_ANON
     676             : #endif
     677             : #ifndef MAP_NORESERVE
     678             : #define MAP_NORESERVE 0
     679             : #endif
     680             : static void *
     681      122617 : pari_mainstack_malloc(size_t size)
     682             : {
     683             :   /* Check that the system allows reserving "size" bytes. This is just
     684             :    * a check, we immediately free the memory. */
     685      122617 :   void *b = mmap(NULL, size, PROT_READ|PROT_WRITE,
     686             :                              MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
     687      122617 :   if (b == MAP_FAILED) return NULL;
     688      122617 :   munmap(b, size);
     689             : 
     690             :   /* Map again, this time with MAP_NORESERVE. On some operating systems
     691             :    * like Cygwin, this is needed because remapping with PROT_NONE and
     692             :    * MAP_NORESERVE does not work as expected. */
     693      122617 :   b = mmap(NULL, size, PROT_READ|PROT_WRITE,
     694             :                        MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
     695      122617 :   if (b == MAP_FAILED) return NULL;
     696      122617 :   return b;
     697             : }
     698             : 
     699             : static void
     700      122607 : pari_mainstack_mfree(void *s, size_t size)
     701             : {
     702      122607 :   munmap(s, size);
     703      122607 : }
     704             : 
     705             : /* Completely discard the memory mapped between the addresses "from"
     706             :  * and "to" (which must be page-aligned).
     707             :  *
     708             :  * We use mmap() with PROT_NONE, which means that the underlying memory
     709             :  * is freed and that the kernel should not commit memory for it. We
     710             :  * still keep the mapping such that we can change the flags to
     711             :  * PROT_READ|PROT_WRITE later.
     712             :  *
     713             :  * NOTE: remapping with MAP_FIXED and PROT_NONE is not the same as
     714             :  * calling mprotect(..., PROT_NONE) because the latter will keep the
     715             :  * memory committed (this is in particular relevant on Linux with
     716             :  * vm.overcommit = 2). This remains true even when calling
     717             :  * madvise(..., MADV_DONTNEED). */
     718             : static void
     719      205492 : pari_mainstack_mreset(pari_sp from, pari_sp to)
     720             : {
     721      205492 :   size_t s = to - from;
     722             :   void *addr, *res;
     723      205492 :   if (!s) return;
     724             : 
     725           0 :   addr = (void*)from;
     726           0 :   res = mmap(addr, s, PROT_NONE,
     727             :              MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
     728           0 :   if (res != addr) pari_err(e_MEM);
     729             : }
     730             : 
     731             : /* Commit (make available) the virtual memory mapped between the
     732             :  * addresses "from" and "to" (which must be page-aligned).
     733             :  * Return 0 if successful, -1 if failed. */
     734             : static int
     735      205492 : pari_mainstack_mextend(pari_sp from, pari_sp to)
     736             : {
     737      205492 :   size_t s = to - from;
     738      205492 :   return mprotect((void*)from, s, PROT_READ|PROT_WRITE);
     739             : }
     740             : 
     741             : /* Set actual stack size to the given size. This sets st->size and
     742             :  * st->bot. If not enough system memory is available, this can fail.
     743             :  * Return 1 if successful, 0 if failed (in that case, st->size is not
     744             :  * changed) */
     745             : static int
     746      205492 : pari_mainstack_setsize(struct pari_mainstack *st, size_t size)
     747             : {
     748      205492 :   pari_sp newbot = st->top - size;
     749             :   /* Align newbot to pagesize */
     750      205492 :   pari_sp alignbot = newbot & ~(pari_sp)(PARI_STACK_ALIGN - 1);
     751      205492 :   if (pari_mainstack_mextend(alignbot, st->top))
     752             :   {
     753             :     /* Making the memory available did not work: limit vsize to the
     754             :      * current actual stack size. */
     755           0 :     st->vsize = st->size;
     756           0 :     pari_warn(warnstack, st->vsize);
     757           0 :     return 0;
     758             :   }
     759      205492 :   pari_mainstack_mreset(st->vbot, alignbot);
     760      205492 :   st->bot = newbot;
     761      205492 :   st->size = size;
     762      205492 :   return 1;
     763             : }
     764             : 
     765             : #else
     766             : #define PARI_STACK_ALIGN (0x40UL)
     767             : static void *
     768             : pari_mainstack_malloc(size_t s)
     769             : {
     770             :   return malloc(s); /* NOT pari_malloc, e_MEM would be deadly */
     771             : }
     772             : 
     773             : static void
     774             : pari_mainstack_mfree(void *s, size_t size) { (void) size; free(s); }
     775             : 
     776             : static int
     777             : pari_mainstack_setsize(struct pari_mainstack *st, size_t size)
     778             : {
     779             :   st->bot = st->top - size;
     780             :   st->size = size;
     781             :   return 1;
     782             : }
     783             : 
     784             : #endif
     785             : 
     786             : static const size_t MIN_STACK = 500032UL;
     787             : static size_t
     788      245224 : fix_size(size_t a)
     789             : {
     790      245224 :   size_t ps = PARI_STACK_ALIGN;
     791      245224 :   size_t b = a & ~(ps - 1); /* Align */
     792      245224 :   if (b < a && b < ~(ps - 1)) b += ps;
     793      245224 :   if (b < MIN_STACK) b = MIN_STACK;
     794      245224 :   return b;
     795             : }
     796             : 
     797             : static void
     798      122617 : pari_mainstack_alloc(int numerr, struct pari_mainstack *st, size_t rsize, size_t vsize)
     799             : {
     800      122617 :   size_t sizemax = vsize ? vsize: rsize, s = fix_size(sizemax);
     801             :   for (;;)
     802             :   {
     803      122617 :     st->vbot = (pari_sp)pari_mainstack_malloc(s);
     804      122617 :     if (st->vbot) break;
     805           0 :     if (s == MIN_STACK) pari_err(e_MEM); /* no way out. Die */
     806           0 :     s = fix_size(s >> 1);
     807           0 :     pari_warn(numerr, s);
     808             :   }
     809      122617 :   st->vsize = vsize ? s: 0;
     810      122617 :   st->rsize = minuu(rsize, s);
     811      122617 :   st->top = st->vbot+s;
     812      122617 :   if (!pari_mainstack_setsize(st, st->rsize))
     813             :   {
     814             :     /* This should never happen since we only decrease the allocated space */
     815           0 :     pari_err(e_MEM);
     816             :   }
     817      122617 :   st->memused = 0;
     818      122617 : }
     819             : 
     820             : static void
     821      122607 : pari_mainstack_free(struct pari_mainstack *st)
     822             : {
     823      122607 :   pari_mainstack_mfree((void*)st->vbot, st->vsize ? st->vsize : fix_size(st->rsize));
     824      122607 :   st->top = st->bot = st->vbot = 0;
     825      122607 :   st->size = st->vsize = 0;
     826      122607 : }
     827             : 
     828             : static void
     829         292 : pari_mainstack_resize(struct pari_mainstack *st, size_t rsize, size_t vsize)
     830             : {
     831         292 :   BLOCK_SIGINT_START;
     832         292 :   pari_mainstack_free(st);
     833         292 :   pari_mainstack_alloc(warnstack, st, rsize, vsize);
     834         292 :   BLOCK_SIGINT_END;
     835         292 : }
     836             : 
     837             : static void
     838      121391 : pari_mainstack_use(struct pari_mainstack *st)
     839             : {
     840      121391 :   pari_mainstack = st;
     841      121391 :   avma = st->top; /* don't use set_avma */
     842      121391 : }
     843             : 
     844             : static void
     845        1566 : paristack_alloc(size_t rsize, size_t vsize)
     846             : {
     847        1566 :   pari_mainstack_alloc(warnstack, pari_mainstack, rsize, vsize);
     848        1566 :   pari_mainstack_use(pari_mainstack);
     849        1566 : }
     850             : 
     851             : void
     852           0 : paristack_setsize(size_t rsize, size_t vsize)
     853             : {
     854           0 :   pari_mainstack_resize(pari_mainstack, rsize, vsize);
     855           0 :   pari_mainstack_use(pari_mainstack);
     856           0 : }
     857             : 
     858             : void
     859           0 : parivstack_resize(ulong newsize)
     860             : {
     861             :   size_t s;
     862           0 :   if (newsize && newsize < pari_mainstack->rsize)
     863           0 :     pari_err_DIM("stack sizes [parisizemax < parisize]");
     864           0 :   if (newsize == pari_mainstack->vsize) return;
     865           0 :   evalstate_reset();
     866           0 :   paristack_setsize(pari_mainstack->rsize, newsize);
     867           0 :   s = pari_mainstack->vsize ? pari_mainstack->vsize : pari_mainstack->rsize;
     868           0 :   if (DEBUGMEM)
     869           0 :     pari_warn(warner,"new maximum stack size = %lu (%.3f Mbytes)",
     870             :               s, s/1048576.);
     871           0 :   pari_init_errcatch();
     872           0 :   cb_pari_err_recover(-1);
     873             : }
     874             : 
     875             : void
     876         298 : paristack_newrsize(ulong newsize)
     877             : {
     878         298 :   size_t s, vsize = pari_mainstack->vsize;
     879         298 :   if (!newsize) newsize = pari_mainstack->rsize << 1;
     880         298 :   if (newsize != pari_mainstack->rsize)
     881         292 :     pari_mainstack_resize(pari_mainstack, newsize, vsize);
     882         298 :   evalstate_reset();
     883         298 :   s = pari_mainstack->rsize;
     884         298 :   if (DEBUGMEM)
     885         298 :     pari_warn(warner,"new stack size = %lu (%.3f Mbytes)", s, s/1048576.);
     886         298 :   pari_init_errcatch();
     887         298 :   cb_pari_err_recover(-1);
     888           0 : }
     889             : 
     890             : void
     891           0 : paristack_resize(ulong newsize)
     892             : {
     893           0 :   long size = pari_mainstack->size;
     894           0 :   if (!newsize)
     895           0 :     newsize = 2 * size;
     896           0 :   newsize = minuu(newsize, pari_mainstack->vsize);
     897           0 :   if (newsize <= pari_mainstack->size) return;
     898           0 :   if (pari_mainstack_setsize(pari_mainstack, newsize))
     899             :   {
     900           0 :     if (DEBUGMEM)
     901           0 :       pari_warn(warner, "increasing stack size to %lu", pari_mainstack->size);
     902             :   }
     903             :   else
     904             :   {
     905           0 :     pari_mainstack_setsize(pari_mainstack, size);
     906           0 :     pari_err(e_STACK);
     907             :   }
     908             : }
     909             : 
     910             : void
     911       82875 : parivstack_reset(void)
     912             : {
     913       82875 :   pari_mainstack_setsize(pari_mainstack, pari_mainstack->rsize);
     914       82875 :   if (avma < pari_mainstack->bot)
     915           0 :     pari_err_BUG("parivstack_reset [avma < bot]");
     916       82875 : }
     917             : 
     918             : /* Enlarge the stack if needed such that the unused portion of the stack
     919             :  * (between bot and avma) is large enough to contain x longs. */
     920             : void
     921           7 : new_chunk_resize(size_t x)
     922             : {
     923           7 :   if (pari_mainstack->vsize==0
     924           0 :     || x > (avma-pari_mainstack->vbot) / sizeof(long)) pari_err(e_STACK);
     925           0 :   while (x > (avma-pari_mainstack->bot) / sizeof(long))
     926           0 :     paristack_resize(0);
     927           0 : }
     928             : 
     929             : /*********************************************************************/
     930             : /*                       PARI THREAD                                 */
     931             : /*********************************************************************/
     932             : 
     933             : /* Initial PARI thread structure t with a stack of size s and
     934             :  * argument arg */
     935             : 
     936             : static void
     937      120156 : pari_thread_set_global(struct pari_global_state *gs)
     938             : {
     939      120156 :   push_localbitprec(gs->bitprec);
     940      120628 :   pari_set_primetab(gs->primetab);
     941      120040 :   pari_set_seadata(gs->seadata);
     942      119909 :   pari_set_varstate(gs->varpriority, &gs->varstate);
     943      120555 : }
     944             : 
     945             : static void
     946      120759 : pari_thread_get_global(struct pari_global_state *gs)
     947             : {
     948      120759 :   gs->bitprec = get_localbitprec();
     949      120759 :   gs->primetab = primetab;
     950      120759 :   gs->seadata = pari_get_seadata();
     951      120759 :   varstate_save(&gs->varstate);
     952      120759 :   gs->varpriority = varpriority;
     953      120759 : }
     954             : 
     955             : void
     956      120759 : pari_thread_alloc(struct pari_thread *t, size_t s, GEN arg)
     957             : {
     958      120759 :   pari_mainstack_alloc(warnstackthread, &t->st,s,0);
     959      120759 :   pari_thread_get_global(&t->gs);
     960      120759 :   t->data = arg;
     961      120759 : }
     962             : 
     963             : /* Initial PARI thread structure t with a stack of size s and virtual size v
     964             :  * and argument arg */
     965             : 
     966             : void
     967           0 : pari_thread_valloc(struct pari_thread *t, size_t s, size_t v, GEN arg)
     968             : {
     969           0 :   pari_mainstack_alloc(warnstackthread, &t->st,s,v);
     970           0 :   pari_thread_get_global(&t->gs);
     971           0 :   t->data = arg;
     972           0 : }
     973             : 
     974             : void
     975      120759 : pari_thread_free(struct pari_thread *t)
     976             : {
     977      120759 :   pari_mainstack_free(&t->st);
     978      120759 : }
     979             : 
     980             : void
     981      122036 : pari_thread_init(void)
     982             : {
     983      122036 :   pari_init_blocks();
     984      121848 :   pari_init_errcatch();
     985      121773 :   pari_init_rand();
     986      122043 :   pari_init_floats();
     987      121835 :   pari_init_parser();
     988      122210 :   pari_init_compiler();
     989      122181 :   pari_init_evaluator();
     990      122194 :   pari_init_files();
     991      121789 : }
     992             : 
     993             : void
     994      121424 : pari_thread_close(void)
     995             : {
     996      121424 :   pari_thread_close_files();
     997      120080 :   pari_close_evaluator();
     998      121386 :   pari_close_compiler();
     999      119871 :   pari_close_parser();
    1000      121308 :   pari_close_floats();
    1001      119291 :   pari_close_blocks();
    1002      121231 : }
    1003             : 
    1004             : GEN
    1005      119866 : pari_thread_start(struct pari_thread *t)
    1006             : {
    1007      119866 :   pari_mainstack_use(&t->st);
    1008      120483 :   pari_thread_init();
    1009      120219 :   pari_thread_set_global(&t->gs);
    1010      120574 :   return t->data;
    1011             : }
    1012             : 
    1013             : /*********************************************************************/
    1014             : /*                       LIBPARI INIT / CLOSE                        */
    1015             : /*********************************************************************/
    1016             : 
    1017             : static void
    1018           0 : pari_exit(void)
    1019             : {
    1020           0 :   err_printf("  ***   Error in the PARI system. End of program.\n");
    1021           0 :   exit(1);
    1022             : }
    1023             : 
    1024             : static void
    1025           0 : dflt_err_recover(long errnum) { (void) errnum; pari_exit(); }
    1026             : 
    1027             : static void
    1028           0 : dflt_pari_quit(long err) { (void)err; /*do nothing*/; }
    1029             : 
    1030             : static int pari_err_display(GEN err);
    1031             : 
    1032             : /* initialize PARI data. Initialize [new|old]fun to NULL for default set. */
    1033             : void
    1034        1566 : pari_init_opts(size_t parisize, ulong maxprime, ulong init_opts)
    1035             : {
    1036             :   ulong u;
    1037             : 
    1038        1566 :   pari_mt_nbthreads = 0;
    1039        1566 :   cb_pari_quit = dflt_pari_quit;
    1040        1566 :   cb_pari_init_histfile = NULL;
    1041        1566 :   cb_pari_get_line_interactive = NULL;
    1042        1566 :   cb_pari_fgets_interactive = NULL;
    1043        1566 :   cb_pari_whatnow = NULL;
    1044        1566 :   cb_pari_handle_exception = NULL;
    1045        1566 :   cb_pari_err_handle = pari_err_display;
    1046        1566 :   cb_pari_pre_recover = NULL;
    1047        1566 :   cb_pari_break_loop = NULL;
    1048        1566 :   cb_pari_is_interactive = NULL;
    1049        1566 :   cb_pari_start_output = NULL;
    1050        1566 :   cb_pari_sigint = dflt_sigint_fun;
    1051        1566 :   if (init_opts&INIT_JMPm) cb_pari_err_recover = dflt_err_recover;
    1052             : 
    1053        1566 :   pari_stackcheck_init(&u);
    1054        1566 :   pari_init_homedir();
    1055        1566 :   if (init_opts&INIT_DFTm) {
    1056           0 :     pari_init_defaults();
    1057           0 :     GP_DATA = default_gp_data();
    1058           0 :     pari_init_paths();
    1059             :   }
    1060             : 
    1061        1566 :   pari_mainstack = (struct pari_mainstack *) malloc(sizeof(*pari_mainstack));
    1062        1566 :   paristack_alloc(parisize, 0);
    1063        1566 :   init_universal_constants();
    1064        1566 :   diffptr = NULL;
    1065        1566 :   if (!(init_opts&INIT_noPRIMEm))
    1066             :   {
    1067           0 :     GP_DATA->primelimit = maxprime;
    1068           0 :     pari_init_primes(GP_DATA->primelimit);
    1069             :   }
    1070        1566 :   if (!(init_opts&INIT_noINTGMPm)) pari_kernel_init();
    1071        1566 :   pari_init_graphics();
    1072        1566 :   pari_thread_init();
    1073        1566 :   pari_set_primetab(NULL);
    1074        1566 :   pari_set_seadata(NULL);
    1075        1566 :   pari_init_functions();
    1076        1566 :   pari_init_export();
    1077        1566 :   pari_var_init();
    1078        1566 :   pari_init_timer();
    1079        1566 :   pari_init_buffers();
    1080        1566 :   (void)getabstime();
    1081        1566 :   try_to_recover = 1;
    1082        1566 :   if (!(init_opts&INIT_noIMTm)) pari_mt_init();
    1083        1566 :   if ((init_opts&INIT_SIGm)) pari_sig_init(pari_sighandler);
    1084        1566 : }
    1085             : 
    1086             : void
    1087           0 : pari_init(size_t parisize, ulong maxprime)
    1088           0 : { pari_init_opts(parisize, maxprime, INIT_JMPm | INIT_SIGm | INIT_DFTm); }
    1089             : 
    1090             : void
    1091        1556 : pari_close_opts(ulong init_opts)
    1092             : {
    1093             :   long i;
    1094             : 
    1095        1556 :   BLOCK_SIGINT_START;
    1096        1556 :   if ((init_opts&INIT_SIGm)) pari_sig_init(SIG_DFL);
    1097        1556 :   if (!(init_opts&INIT_noIMTm)) pari_mt_close();
    1098             : 
    1099        1556 :   pari_var_close(); /* must come before destruction of functions_hash */
    1100      211616 :   for (i = 0; i < functions_tblsz; i++)
    1101             :   {
    1102      210060 :     entree *ep = functions_hash[i];
    1103     2301128 :     while (ep) {
    1104     1881008 :       entree *EP = ep->next;
    1105     1881008 :       if (!EpSTATIC(ep)) { freeep(ep); free(ep); }
    1106     1881008 :       ep = EP;
    1107             :     }
    1108             :   }
    1109        1556 :   pari_close_mf();
    1110        1556 :   pari_thread_close();
    1111        1556 :   pari_close_export();
    1112        1556 :   pari_close_files();
    1113        1556 :   pari_close_homedir();
    1114        1556 :   if (!(init_opts&INIT_noINTGMPm)) pari_kernel_close();
    1115             : 
    1116        1556 :   free((void*)functions_hash);
    1117        1556 :   free((void*)defaults_hash);
    1118        1556 :   if (diffptr) pari_close_primes();
    1119        1556 :   free(current_logfile);
    1120        1556 :   free(current_psfile);
    1121        1556 :   pari_mainstack_free(pari_mainstack);
    1122        1556 :   free((void*)pari_mainstack);
    1123        1556 :   pari_stack_delete(&s_MODULES);
    1124        1556 :   if (pari_datadir) free(pari_datadir);
    1125        1556 :   if (init_opts&INIT_DFTm)
    1126             :   { /* delete GP_DATA */
    1127        1556 :     pari_close_paths();
    1128        1556 :     if (GP_DATA->hist->v) free((void*)GP_DATA->hist->v);
    1129        1556 :     if (GP_DATA->pp->cmd) free((void*)GP_DATA->pp->cmd);
    1130        1556 :     if (GP_DATA->help) free((void*)GP_DATA->help);
    1131        1556 :     if (GP_DATA->plothsizes) free((void*)GP_DATA->plothsizes);
    1132        1556 :     if (GP_DATA->colormap) pari_free(GP_DATA->colormap);
    1133        1556 :     if (GP_DATA->graphcolors) pari_free(GP_DATA->graphcolors);
    1134        1556 :     free((void*)GP_DATA->prompt);
    1135        1556 :     free((void*)GP_DATA->prompt_cont);
    1136        1556 :     free((void*)GP_DATA->histfile);
    1137             :   }
    1138        1556 :   BLOCK_SIGINT_END;
    1139        1556 : }
    1140             : 
    1141             : void
    1142        1556 : pari_close(void)
    1143        1556 : { pari_close_opts(INIT_JMPm | INIT_SIGm | INIT_DFTm); }
    1144             : 
    1145             : /*******************************************************************/
    1146             : /*                                                                 */
    1147             : /*                         ERROR RECOVERY                          */
    1148             : /*                                                                 */
    1149             : /*******************************************************************/
    1150             : void
    1151       99925 : gp_context_save(struct gp_context* rec)
    1152             : {
    1153       99925 :   rec->prettyp = GP_DATA->fmt->prettyp;
    1154       99925 :   rec->listloc = next_block;
    1155       99925 :   rec->iferr_env = iferr_env;
    1156       99925 :   rec->err_data  = global_err_data;
    1157       99925 :   varstate_save(&rec->var);
    1158       99925 :   evalstate_save(&rec->eval);
    1159       99925 :   parsestate_save(&rec->parse);
    1160       99925 :   filestate_save(&rec->file);
    1161       99925 : }
    1162             : 
    1163             : void
    1164        9383 : gp_context_restore(struct gp_context* rec)
    1165             : {
    1166             :   long i;
    1167             : 
    1168        9383 :   if (!try_to_recover) return;
    1169             :   /* disable gp_context_restore() and SIGINT */
    1170        9383 :   try_to_recover = 0;
    1171        9383 :   BLOCK_SIGINT_START
    1172        9383 :   if (DEBUGMEM>2) err_printf("entering recover(), loc = %ld\n", rec->listloc);
    1173        9383 :   evalstate_restore(&rec->eval);
    1174        9383 :   parsestate_restore(&rec->parse);
    1175        9383 :   filestate_restore(&rec->file);
    1176        9383 :   global_err_data = rec->err_data;
    1177        9383 :   iferr_env = rec->iferr_env;
    1178        9383 :   GP_DATA->fmt->prettyp = rec->prettyp;
    1179             : 
    1180     1276088 :   for (i = 0; i < functions_tblsz; i++)
    1181             :   {
    1182     1266705 :     entree *ep = functions_hash[i];
    1183    14538646 :     while (ep)
    1184             :     {
    1185    12005236 :       entree *EP = ep->next;
    1186    12005236 :       switch(EpVALENCE(ep))
    1187             :       {
    1188             :         case EpVAR:
    1189      204822 :           while (pop_val_if_newer(ep,rec->listloc)) /* empty */;
    1190      204822 :           break;
    1191      625261 :         case EpNEW: break;
    1192             :       }
    1193    12005236 :       ep = EP;
    1194             :     }
    1195             :   }
    1196        9383 :   varstate_restore(&rec->var);
    1197        9383 :   if (DEBUGMEM>2) err_printf("leaving recover()\n");
    1198        9383 :   BLOCK_SIGINT_END
    1199        9383 :   try_to_recover = 1;
    1200             : }
    1201             : 
    1202             : static void
    1203        9315 : err_recover(long numerr)
    1204             : {
    1205        9315 :   if (cb_pari_pre_recover)
    1206        9315 :     cb_pari_pre_recover(numerr);
    1207           0 :   evalstate_reset();
    1208           0 :   killallfiles();
    1209           0 :   pari_init_errcatch();
    1210           0 :   cb_pari_err_recover(numerr);
    1211           0 : }
    1212             : 
    1213             : static void
    1214        9907 : err_init(void)
    1215             : {
    1216             :   /* make sure pari_err msg starts at the beginning of line */
    1217        9907 :   if (!pari_last_was_newline()) pari_putc('\n');
    1218        9907 :   pariOut->flush();
    1219        9907 :   pariErr->flush();
    1220        9907 :   out_term_color(pariErr, c_ERR);
    1221        9907 : }
    1222             : 
    1223             : static void
    1224        9814 : err_init_msg(int user)
    1225             : {
    1226             :   const char *gp_function_name;
    1227        9814 :   out_puts(pariErr, "  *** ");
    1228        9814 :   if (!user && (gp_function_name = closure_func_err()))
    1229        6325 :     out_printf(pariErr, "%s: ", gp_function_name);
    1230             :   else
    1231        3489 :     out_puts(pariErr, "  ");
    1232        9814 : }
    1233             : 
    1234             : void
    1235         571 : pari_warn(int numerr, ...)
    1236             : {
    1237             :   char *ch1;
    1238             :   va_list ap;
    1239             : 
    1240         571 :   va_start(ap,numerr);
    1241             : 
    1242         571 :   err_init();
    1243         571 :   err_init_msg(numerr==warnuser || numerr==warnstack);
    1244         571 :   switch (numerr)
    1245             :   {
    1246             :     case warnuser:
    1247           7 :       out_puts(pariErr, "user warning: ");
    1248           7 :       out_print0(pariErr, NULL, va_arg(ap, GEN), f_RAW);
    1249           7 :       break;
    1250             : 
    1251             :     case warnmem:
    1252           0 :       out_puts(pariErr, "collecting garbage in "); ch1=va_arg(ap, char*);
    1253           0 :       out_vprintf(pariErr, ch1,ap); out_putc(pariErr, '.');
    1254           0 :       break;
    1255             : 
    1256             :     case warner:
    1257         564 :       out_puts(pariErr, "Warning: "); ch1=va_arg(ap, char*);
    1258         564 :       out_vprintf(pariErr, ch1,ap); out_putc(pariErr, '.');
    1259         564 :       break;
    1260             : 
    1261             :     case warnprec:
    1262           0 :       out_vprintf(pariErr, "Warning: increasing prec in %s; new prec = %ld",
    1263             :                       ap);
    1264           0 :       break;
    1265             : 
    1266             :     case warnfile:
    1267           0 :       out_puts(pariErr, "Warning: failed to "),
    1268           0 :       ch1 = va_arg(ap, char*);
    1269           0 :       out_printf(pariErr, "%s: %s", ch1, va_arg(ap, char*));
    1270           0 :       break;
    1271             : 
    1272             :     case warnstack:
    1273             :     case warnstackthread:
    1274             :     {
    1275           0 :       ulong  s = va_arg(ap, ulong);
    1276             :       char buf[128];
    1277           0 :       const char * stk = numerr == warnstackthread
    1278           0 :                          || mt_is_thread() ? "thread": "PARI";
    1279           0 :       sprintf(buf,"Warning: not enough memory, new %s stack %lu", stk, s);
    1280           0 :       out_puts(pariErr,buf);
    1281           0 :       break;
    1282             :     }
    1283             :   }
    1284         571 :   va_end(ap);
    1285         571 :   out_term_color(pariErr, c_NONE);
    1286         571 :   out_putc(pariErr, '\n');
    1287         571 :   pariErr->flush();
    1288         571 : }
    1289             : void
    1290           0 : pari_sigint(const char *time_s)
    1291             : {
    1292           0 :   int recover=0;
    1293           0 :   BLOCK_SIGALRM_START
    1294           0 :   err_init();
    1295           0 :   closure_err(0);
    1296           0 :   err_init_msg(0);
    1297           0 :   out_puts(pariErr, "user interrupt after ");
    1298           0 :   out_puts(pariErr, time_s);
    1299           0 :   out_term_color(pariErr, c_NONE);
    1300           0 :   pariErr->flush();
    1301           0 :   if (cb_pari_handle_exception)
    1302           0 :     recover = cb_pari_handle_exception(-1);
    1303           0 :   if (!recover && !block)
    1304           0 :     PARI_SIGINT_pending = 0;
    1305           0 :   BLOCK_SIGINT_END
    1306           0 :   if (!recover) err_recover(e_MISC);
    1307           0 : }
    1308             : 
    1309             : #define retmkerr2(x,y)\
    1310             :   do { GEN _v = cgetg(3, t_ERROR);\
    1311             :        _v[1] = (x);\
    1312             :        gel(_v,2) = (y); return _v; } while(0)
    1313             : #define retmkerr3(x,y,z)\
    1314             :   do { GEN _v = cgetg(4, t_ERROR);\
    1315             :        _v[1] = (x);\
    1316             :        gel(_v,2) = (y);\
    1317             :        gel(_v,3) = (z); return _v; } while(0)
    1318             : #define retmkerr4(x,y,z,t)\
    1319             :   do { GEN _v = cgetg(5, t_ERROR);\
    1320             :        _v[1] = (x);\
    1321             :        gel(_v,2) = (y);\
    1322             :        gel(_v,3) = (z);\
    1323             :        gel(_v,4) = (t); return _v; } while(0)
    1324             : #define retmkerr5(x,y,z,t,u)\
    1325             :   do { GEN _v = cgetg(6, t_ERROR);\
    1326             :        _v[1] = (x);\
    1327             :        gel(_v,2) = (y);\
    1328             :        gel(_v,3) = (z);\
    1329             :        gel(_v,4) = (t);\
    1330             :        gel(_v,5) = (u); return _v; } while(0)
    1331             : #define retmkerr6(x,y,z,t,u,v)\
    1332             :   do { GEN _v = cgetg(7, t_ERROR);\
    1333             :        _v[1] = (x);\
    1334             :        gel(_v,2) = (y);\
    1335             :        gel(_v,3) = (z);\
    1336             :        gel(_v,4) = (t);\
    1337             :        gel(_v,5) = (u);\
    1338             :        gel(_v,6) = (v); return _v; } while(0)
    1339             : 
    1340             : static GEN
    1341       44063 : pari_err2GEN(long numerr, va_list ap)
    1342             : {
    1343       44063 :   switch ((enum err_list) numerr)
    1344             :   {
    1345             :   case e_SYNTAX:
    1346             :     {
    1347          93 :       const char *msg = va_arg(ap, char*);
    1348          93 :       const char *s = va_arg(ap,char *);
    1349          93 :       const char *entry = va_arg(ap,char *);
    1350          93 :       retmkerr3(numerr,strtoGENstr(msg), mkvecsmall2((long)s,(long)entry));
    1351             :     }
    1352             :   case e_MISC: case e_ALARM:
    1353             :     {
    1354         156 :       const char *ch1 = va_arg(ap, char*);
    1355         156 :       retmkerr2(numerr, gvsprintf(ch1,ap));
    1356             :     }
    1357             :   case e_NOTFUNC:
    1358             :   case e_USER:
    1359        2737 :     retmkerr2(numerr,va_arg(ap, GEN));
    1360             :   case e_FILE:
    1361             :     {
    1362           0 :       const char *f = va_arg(ap, const char*);
    1363           0 :       retmkerr3(numerr, strtoGENstr(f), strtoGENstr(va_arg(ap, char*)));
    1364             :     }
    1365             :   case e_FILEDESC:
    1366             :     {
    1367           0 :       const char *f = va_arg(ap, const char*);
    1368           0 :       retmkerr3(numerr, strtoGENstr(f), stoi(va_arg(ap, long)));
    1369             :     }
    1370             :   case e_OVERFLOW:
    1371             :   case e_IMPL:
    1372             :   case e_DIM:
    1373             :   case e_CONSTPOL:
    1374             :   case e_ROOTS0:
    1375             :   case e_FLAG:
    1376             :   case e_PREC:
    1377             :   case e_BUG:
    1378             :   case e_ARCH:
    1379             :   case e_PACKAGE:
    1380        1407 :     retmkerr2(numerr, strtoGENstr(va_arg(ap, char*)));
    1381             :   case e_MODULUS:
    1382             :   case e_VAR:
    1383             :     {
    1384        1029 :       const char *f = va_arg(ap, const char*);
    1385        1029 :       GEN x = va_arg(ap, GEN);
    1386        1029 :       GEN y = va_arg(ap, GEN);
    1387        1029 :       retmkerr4(numerr, strtoGENstr(f), x,y);
    1388             :     }
    1389             :   case e_INV:
    1390             :   case e_IRREDPOL:
    1391             :   case e_PRIME:
    1392             :   case e_SQRTN:
    1393             :   case e_TYPE:
    1394             :     {
    1395       32695 :       const char *f = va_arg(ap, const char*);
    1396       32695 :       GEN x = va_arg(ap, GEN);
    1397       32695 :       retmkerr3(numerr, strtoGENstr(f), x);
    1398             :     }
    1399             :   case e_COPRIME: case e_OP: case e_TYPE2:
    1400             :     {
    1401        3458 :       const char *f = va_arg(ap, const char*);
    1402        3458 :       GEN x = va_arg(ap, GEN);
    1403        3458 :       GEN y = va_arg(ap, GEN);
    1404        3458 :       retmkerr4(numerr,strtoGENstr(f),x,y);
    1405             :     }
    1406             :   case e_COMPONENT:
    1407             :     {
    1408         200 :       const char *f= va_arg(ap, const char *);
    1409         200 :       const char *op = va_arg(ap, const char *);
    1410         200 :       GEN l = va_arg(ap, GEN);
    1411         200 :       GEN x = va_arg(ap, GEN);
    1412         200 :       retmkerr5(numerr,strtoGENstr(f),strtoGENstr(op),l,x);
    1413             :     }
    1414             :   case e_DOMAIN:
    1415             :     {
    1416        2099 :       const char *f = va_arg(ap, const char*);
    1417        2099 :       const char *v = va_arg(ap, const char *);
    1418        2099 :       const char *op = va_arg(ap, const char *);
    1419        2099 :       GEN l = va_arg(ap, GEN);
    1420        2099 :       GEN x = va_arg(ap, GEN);
    1421        2099 :       retmkerr6(numerr,strtoGENstr(f),strtoGENstr(v),strtoGENstr(op),l,x);
    1422             :     }
    1423             :   case e_PRIORITY:
    1424             :     {
    1425         182 :       const char *f = va_arg(ap, const char*);
    1426         182 :       GEN x = va_arg(ap, GEN);
    1427         182 :       const char *op = va_arg(ap, const char *);
    1428         182 :       long v = va_arg(ap, long);
    1429         182 :       retmkerr5(numerr,strtoGENstr(f),x,strtoGENstr(op),stoi(v));
    1430             :     }
    1431             :   case e_MAXPRIME:
    1432           0 :     retmkerr2(numerr, utoi(va_arg(ap, ulong)));
    1433             :   case e_STACK:
    1434           7 :     return err_e_STACK;
    1435             :   case e_STACKTHREAD:
    1436           0 :     retmkerr3(numerr, utoi(va_arg(ap, ulong)), utoi(va_arg(ap, ulong)));
    1437             :   default:
    1438           0 :     return mkerr(numerr);
    1439             :   }
    1440             : }
    1441             : 
    1442             : static char *
    1443        6328 : type_dim(GEN x)
    1444             : {
    1445        6328 :   char *v = stack_malloc(64);
    1446        6328 :   switch(typ(x))
    1447             :   {
    1448             :     case t_MAT:
    1449             :     {
    1450          84 :       long l = lg(x), r = (l == 1)? 1: lgcols(x);
    1451          84 :       sprintf(v, "t_MAT (%ldx%ld)", r-1,l-1);
    1452          84 :       break;
    1453             :     }
    1454             :     case t_COL:
    1455          91 :       sprintf(v, "t_COL (%ld elts)", lg(x)-1);
    1456          91 :       break;
    1457             :     case t_VEC:
    1458         133 :       sprintf(v, "t_VEC (%ld elts)", lg(x)-1);
    1459         133 :       break;
    1460             :     default:
    1461        6020 :       v = (char*)type_name(typ(x));
    1462             :   }
    1463        6328 :   return v;
    1464             : }
    1465             : 
    1466             : static char *
    1467        2394 : gdisplay(GEN x)
    1468             : {
    1469        2394 :   char *s = GENtostr_raw(x);
    1470        2394 :   if (strlen(s) < 1600) return s;
    1471          21 :   if (! GP_DATA->breakloop) return (char*)"(...)";
    1472           0 :   return stack_sprintf("\n  ***  (...) Huge %s omitted; you can access it via dbg_err()", type_name(typ(x)));
    1473             : }
    1474             : 
    1475             : char *
    1476       16572 : pari_err2str(GEN e)
    1477             : {
    1478       16572 :   long numerr = err_get_num(e);
    1479       16572 :   switch ((enum err_list) numerr)
    1480             :   {
    1481             :   case e_ALARM:
    1482           0 :     return pari_sprintf("alarm interrupt after %Ps.",gel(e,2));
    1483             :   case e_MISC:
    1484         154 :     return pari_sprintf("%Ps.",gel(e,2));
    1485             : 
    1486             :   case e_ARCH:
    1487           0 :     return pari_sprintf("sorry, '%Ps' not available on this system.",gel(e,2));
    1488             :   case e_BUG:
    1489          14 :     return pari_sprintf("bug in %Ps, please report.",gel(e,2));
    1490             :   case e_CONSTPOL:
    1491           7 :     return pari_sprintf("constant polynomial in %Ps.", gel(e,2));
    1492             :   case e_COPRIME:
    1493         189 :     return pari_sprintf("elements not coprime in %Ps:\n    %s\n    %s",
    1494         189 :                         gel(e,2), gdisplay(gel(e,3)), gdisplay(gel(e,4)));
    1495             :   case e_DIM:
    1496         494 :     return pari_sprintf("inconsistent dimensions in %Ps.", gel(e,2));
    1497             :   case e_FILE:
    1498           0 :     return pari_sprintf("error opening %Ps: `%Ps'.", gel(e,2), gel(e,3));
    1499             :   case e_FILEDESC:
    1500           0 :     return pari_sprintf("invalid file descriptor in %Ps [%Ps]", gel(e,2), gel(e,3));
    1501             :   case e_FLAG:
    1502          70 :     return pari_sprintf("invalid flag in %Ps.", gel(e,2));
    1503             :   case e_IMPL:
    1504         406 :     return pari_sprintf("sorry, %Ps is not yet implemented.", gel(e,2));
    1505             :   case e_PACKAGE:
    1506           0 :     return pari_sprintf("package %Ps is required, please install it.", gel(e,2));
    1507             :   case e_INV:
    1508         567 :     return pari_sprintf("impossible inverse in %Ps: %s.", gel(e,2),
    1509         567 :                         gdisplay(gel(e,3)));
    1510             :   case e_IRREDPOL:
    1511          84 :     return pari_sprintf("not an irreducible polynomial in %Ps: %s.",
    1512          84 :                         gel(e,2), gdisplay(gel(e,3)));
    1513             :   case e_MAXPRIME:
    1514             :     {
    1515           0 :       const char * msg = "not enough precomputed primes";
    1516           0 :       ulong c = itou(gel(e,2));
    1517           0 :       if (c) return pari_sprintf("%s, need primelimit ~ %lu.",msg, c);
    1518           0 :       else   return pari_strdup(msg);
    1519             :     }
    1520             :   case e_MEM:
    1521           0 :     return pari_strdup("not enough memory");
    1522             :   case e_MODULUS:
    1523             :     {
    1524         749 :       GEN x = gel(e,3), y = gel(e,4);
    1525        1498 :       return pari_sprintf("inconsistent moduli in %Ps: %s != %s",
    1526         749 :                           gel(e,2), gdisplay(x), gdisplay(y));
    1527             :     }
    1528           0 :   case e_NONE: return NULL;
    1529             :   case e_NOTFUNC:
    1530        2723 :     return pari_strdup("not a function in function call");
    1531             :   case e_OP: case e_TYPE2:
    1532             :     {
    1533        3164 :       pari_sp av = avma;
    1534             :       char *v;
    1535        3164 :       const char *f, *op = GSTR(gel(e,2));
    1536        3164 :       const char *what = numerr == e_OP? "inconsistent": "forbidden";
    1537        3164 :       GEN x = gel(e,3);
    1538        3164 :       GEN y = gel(e,4);
    1539        3164 :       switch(*op)
    1540             :       {
    1541          14 :       case '+': f = "addition"; break;
    1542          91 :       case '*': f = "multiplication"; break;
    1543        2394 :       case '/': case '%': case '\\': f = "division"; break;
    1544           0 :       case '=': op = "-->"; f = "assignment"; break;
    1545         665 :       default:  f = op; op = ","; break;
    1546             :       }
    1547        3164 :       v = pari_sprintf("%s %s %s %s %s.", what,f,type_dim(x),op,type_dim(y));
    1548        3164 :       set_avma(av); return v;
    1549             :     }
    1550             :   case e_COMPONENT:
    1551             :     {
    1552         200 :       const char *f= GSTR(gel(e,2));
    1553         200 :       const char *op= GSTR(gel(e,3));
    1554         200 :       GEN l = gel(e,4);
    1555         200 :       if (!*f)
    1556         147 :         return pari_sprintf("non-existent component: index %s %Ps",op,l);
    1557          53 :       return pari_sprintf("non-existent component in %s: index %s %Ps",f,op,l);
    1558             :     }
    1559             :   case e_DOMAIN:
    1560             :     {
    1561        1989 :       const char *f = GSTR(gel(e,2));
    1562        1989 :       const char *v = GSTR(gel(e,3));
    1563        1989 :       const char *op= GSTR(gel(e,4));
    1564        1989 :       GEN l = gel(e,5);
    1565        1989 :       if (!*op)
    1566          28 :         return pari_sprintf("domain error in %s: %s out of range",f,v);
    1567        1961 :       return pari_sprintf("domain error in %s: %s %s %Ps",f,v,op,l);
    1568             :     }
    1569             :   case e_PRIORITY:
    1570             :     {
    1571         133 :       const char *f = GSTR(gel(e,2));
    1572         133 :       long vx = gvar(gel(e,3));
    1573         133 :       const char *op= GSTR(gel(e,4));
    1574         133 :       long v = itos(gel(e,5));
    1575         133 :       return pari_sprintf("incorrect priority in %s: variable %Ps %s %Ps",f,
    1576             :              pol_x(vx), op, pol_x(v));
    1577             :     }
    1578             :   case e_OVERFLOW:
    1579         105 :     return pari_sprintf("overflow in %Ps.", gel(e,2));
    1580             :   case e_PREC:
    1581         217 :     return pari_sprintf("precision too low in %Ps.", gel(e,2));
    1582             :   case e_PRIME:
    1583         154 :     return pari_sprintf("not a prime number in %Ps: %s.",
    1584         154 :                         gel(e,2), gdisplay(gel(e,3)));
    1585             :   case e_ROOTS0:
    1586          49 :     return pari_sprintf("zero polynomial in %Ps.", gel(e,2));
    1587             :   case e_SQRTN:
    1588         168 :     return pari_sprintf("not an n-th power residue in %Ps: %s.",
    1589         168 :                         gel(e,2), gdisplay(gel(e,3)));
    1590             :   case e_STACK:
    1591             :   case e_STACKTHREAD:
    1592             :     {
    1593           7 :       const char *stack = numerr == e_STACK? "PARI": "thread";
    1594           7 :       const char *var = numerr == e_STACK? "parisizemax": "threadsizemax";
    1595           7 :       size_t rsize = numerr == e_STACKTHREAD && GP_DATA->threadsize ?
    1596           7 :                                 GP_DATA->threadsize: pari_mainstack->rsize;
    1597           7 :       size_t vsize = numerr == e_STACK? pari_mainstack->vsize:
    1598           0 :                                         GP_DATA->threadsizemax;
    1599           7 :       char *buf = (char *) pari_malloc(512*sizeof(char));
    1600           7 :       if (vsize)
    1601             :       {
    1602           0 :         sprintf(buf, "the %s stack overflows !\n"
    1603             :             "  current stack size: %lu (%.3f Mbytes)\n"
    1604             :             "  [hint] you can increase '%s' using default()\n",
    1605           0 :             stack, (ulong)vsize, (double)vsize/1048576., var);
    1606             :       }
    1607             :       else
    1608             :       {
    1609           7 :         sprintf(buf, "the %s stack overflows !\n"
    1610             :             "  current stack size: %lu (%.3f Mbytes)\n"
    1611             :             "  [hint] set '%s' to a non-zero value in your GPRC\n",
    1612           7 :             stack, (ulong)rsize, (double)rsize/1048576., var);
    1613             :       }
    1614           7 :       return buf;
    1615             :     }
    1616             :   case e_SYNTAX:
    1617           0 :     return pari_strdup(GSTR(gel(e,2)));
    1618             :   case e_TYPE:
    1619        9928 :     return pari_sprintf("incorrect type in %Ps (%s).",
    1620        9928 :                         gel(e,2), type_name(typ(gel(e,3))));
    1621             :   case e_USER:
    1622          14 :     return pari_sprint0("user error: ", gel(e,2), f_RAW);
    1623             :   case e_VAR:
    1624             :     {
    1625         280 :       GEN x = gel(e,3), y = gel(e,4);
    1626         840 :       return pari_sprintf("inconsistent variables in %Ps, %Ps != %Ps.",
    1627         840 :                           gel(e,2), pol_x(varn(x)), pol_x(varn(y)));
    1628             :     }
    1629             :   }
    1630             :   return NULL; /*LCOV_EXCL_LINE*/
    1631             : }
    1632             : 
    1633             : static int
    1634        9336 : pari_err_display(GEN err)
    1635             : {
    1636        9336 :   long numerr=err_get_num(err);
    1637        9336 :   err_init();
    1638        9336 :   if (numerr==e_SYNTAX)
    1639             :   {
    1640          93 :     const char *msg = GSTR(gel(err,2));
    1641          93 :     const char *s     = (const char *) gmael(err,3,1);
    1642          93 :     const char *entry = (const char *) gmael(err,3,2);
    1643          93 :     print_errcontext(pariErr, msg, s, entry);
    1644             :   }
    1645             :   else
    1646             :   {
    1647             :     char *s;
    1648        9243 :     closure_err(0);
    1649        9243 :     err_init_msg(numerr==e_USER);
    1650        9243 :     s = pari_err2str(err); pariErr->puts(s); pari_free(s);
    1651        9243 :     if (numerr==e_NOTFUNC)
    1652             :     {
    1653        2723 :       GEN fun = gel(err,2);
    1654        2723 :       if (gequalX(fun))
    1655             :       {
    1656        2723 :         entree *ep = varentries[varn(fun)];
    1657        2723 :         const char *t = ep->name;
    1658        2723 :         if (cb_pari_whatnow) cb_pari_whatnow(pariErr,t,1);
    1659             :       }
    1660             :     }
    1661             :   }
    1662        9322 :   out_term_color(pariErr, c_NONE);
    1663        9322 :   pariErr->flush(); return 0;
    1664             : }
    1665             : 
    1666             : void
    1667       44079 : pari_err(int numerr, ...)
    1668             : {
    1669             :   va_list ap;
    1670             :   GEN E;
    1671             : 
    1672       44079 :   va_start(ap,numerr);
    1673             : 
    1674       44079 :   if (numerr)
    1675       44063 :     E = pari_err2GEN(numerr,ap);
    1676             :   else
    1677             :   {
    1678          16 :     E = va_arg(ap,GEN);
    1679          16 :     numerr = err_get_num(E);
    1680             :   }
    1681       44079 :   global_err_data = E;
    1682       44079 :   if (*iferr_env) longjmp(*iferr_env, numerr);
    1683        9338 :   mt_err_recover(numerr);
    1684        9336 :   va_end(ap);
    1685       18658 :   if (cb_pari_err_handle &&
    1686        9336 :       cb_pari_err_handle(E)) return;
    1687       18635 :   if (cb_pari_handle_exception &&
    1688        9320 :       cb_pari_handle_exception(numerr)) return;
    1689        9315 :   err_recover(numerr);
    1690             : }
    1691             : 
    1692             : GEN
    1693       69471 : pari_err_last(void) { return global_err_data; }
    1694             : 
    1695             : const char *
    1696       26947 : numerr_name(long numerr)
    1697             : {
    1698       26947 :   switch ((enum err_list) numerr)
    1699             :   {
    1700           0 :   case e_ALARM:    return "e_ALARM";
    1701           0 :   case e_ARCH:     return "e_ARCH";
    1702           0 :   case e_BUG:      return "e_BUG";
    1703           0 :   case e_COMPONENT: return "e_COMPONENT";
    1704           0 :   case e_CONSTPOL: return "e_CONSTPOL";
    1705           0 :   case e_COPRIME:  return "e_COPRIME";
    1706           0 :   case e_DIM:      return "e_DIM";
    1707          56 :   case e_DOMAIN:   return "e_DOMAIN";
    1708           0 :   case e_FILE:     return "e_FILE";
    1709           0 :   case e_FILEDESC: return "e_FILEDESC";
    1710           7 :   case e_FLAG:     return "e_FLAG";
    1711          28 :   case e_IMPL:     return "e_IMPL";
    1712       19093 :   case e_INV:      return "e_INV";
    1713           0 :   case e_IRREDPOL: return "e_IRREDPOL";
    1714           0 :   case e_MAXPRIME: return "e_MAXPRIME";
    1715           0 :   case e_MEM:      return "e_MEM";
    1716           0 :   case e_MISC:     return "e_MISC";
    1717           0 :   case e_MODULUS:  return "e_MODULUS";
    1718           0 :   case e_NONE:     return "e_NONE";
    1719           0 :   case e_NOTFUNC:  return "e_NOTFUNC";
    1720           0 :   case e_OP:       return "e_OP";
    1721           0 :   case e_OVERFLOW: return "e_OVERFLOW";
    1722           0 :   case e_PACKAGE:  return "e_PACKAGE";
    1723           0 :   case e_PREC:     return "e_PREC";
    1724           0 :   case e_PRIME:    return "e_PRIME";
    1725          49 :   case e_PRIORITY: return "e_PRIORITY";
    1726           0 :   case e_ROOTS0:   return "e_ROOTS0";
    1727           0 :   case e_SQRTN:    return "e_SQRTN";
    1728           0 :   case e_STACK:    return "e_STACK";
    1729           0 :   case e_SYNTAX:   return "e_SYNTAX";
    1730           0 :   case e_STACKTHREAD:   return "e_STACKTHREAD";
    1731           0 :   case e_TYPE2:    return "e_TYPE2";
    1732        7714 :   case e_TYPE:     return "e_TYPE";
    1733           0 :   case e_USER:     return "e_USER";
    1734           0 :   case e_VAR:      return "e_VAR";
    1735             :   }
    1736           0 :   return "invalid error number";
    1737             : }
    1738             : 
    1739             : long
    1740          21 : name_numerr(const char *s)
    1741             : {
    1742          21 :   if (!strcmp(s,"e_ALARM"))    return e_ALARM;
    1743          21 :   if (!strcmp(s,"e_ARCH"))     return e_ARCH;
    1744          21 :   if (!strcmp(s,"e_BUG"))      return e_BUG;
    1745          21 :   if (!strcmp(s,"e_COMPONENT")) return e_COMPONENT;
    1746          21 :   if (!strcmp(s,"e_CONSTPOL")) return e_CONSTPOL;
    1747          21 :   if (!strcmp(s,"e_COPRIME"))  return e_COPRIME;
    1748          21 :   if (!strcmp(s,"e_DIM"))      return e_DIM;
    1749          21 :   if (!strcmp(s,"e_DOMAIN"))   return e_DOMAIN;
    1750          21 :   if (!strcmp(s,"e_FILE"))     return e_FILE;
    1751          21 :   if (!strcmp(s,"e_FILEDESC")) return e_FILEDESC;
    1752          21 :   if (!strcmp(s,"e_FLAG"))     return e_FLAG;
    1753          21 :   if (!strcmp(s,"e_IMPL"))     return e_IMPL;
    1754          21 :   if (!strcmp(s,"e_INV"))      return e_INV;
    1755           0 :   if (!strcmp(s,"e_IRREDPOL")) return e_IRREDPOL;
    1756           0 :   if (!strcmp(s,"e_MAXPRIME")) return e_MAXPRIME;
    1757           0 :   if (!strcmp(s,"e_MEM"))      return e_MEM;
    1758           0 :   if (!strcmp(s,"e_MISC"))     return e_MISC;
    1759           0 :   if (!strcmp(s,"e_MODULUS"))  return e_MODULUS;
    1760           0 :   if (!strcmp(s,"e_NONE"))     return e_NONE;
    1761           0 :   if (!strcmp(s,"e_NOTFUNC"))  return e_NOTFUNC;
    1762           0 :   if (!strcmp(s,"e_OP"))       return e_OP;
    1763           0 :   if (!strcmp(s,"e_OVERFLOW")) return e_OVERFLOW;
    1764           0 :   if (!strcmp(s,"e_PACKAGE"))  return e_PACKAGE;
    1765           0 :   if (!strcmp(s,"e_PREC"))     return e_PREC;
    1766           0 :   if (!strcmp(s,"e_PRIME"))    return e_PRIME;
    1767           0 :   if (!strcmp(s,"e_PRIORITY")) return e_PRIORITY;
    1768           0 :   if (!strcmp(s,"e_ROOTS0"))   return e_ROOTS0;
    1769           0 :   if (!strcmp(s,"e_SQRTN"))    return e_SQRTN;
    1770           0 :   if (!strcmp(s,"e_STACK"))    return e_STACK;
    1771           0 :   if (!strcmp(s,"e_SYNTAX"))   return e_SYNTAX;
    1772           0 :   if (!strcmp(s,"e_TYPE"))     return e_TYPE;
    1773           0 :   if (!strcmp(s,"e_TYPE2"))    return e_TYPE2;
    1774           0 :   if (!strcmp(s,"e_USER"))     return e_USER;
    1775           0 :   if (!strcmp(s,"e_VAR"))      return e_VAR;
    1776           0 :   pari_err(e_MISC,"unknown error name");
    1777             :   return -1; /* LCOV_EXCL_LINE */
    1778             : }
    1779             : 
    1780             : GEN
    1781       26947 : errname(GEN err)
    1782             : {
    1783       26947 :   if (typ(err)!=t_ERROR) pari_err_TYPE("errname",err);
    1784       26947 :   return strtoGENstr(numerr_name(err_get_num(err)));
    1785             : }
    1786             : 
    1787             : /* Try f (trapping error e), recover using r (break_loop, if NULL) */
    1788             : GEN
    1789          21 : trap0(const char *e, GEN r, GEN f)
    1790             : {
    1791          21 :   long numerr = CATCH_ALL;
    1792             :   GEN x;
    1793          21 :   if (!e || !*e) numerr = CATCH_ALL;
    1794          21 :   else numerr = name_numerr(e);
    1795          21 :   if (!f) {
    1796           0 :     pari_warn(warner,"default handlers are no longer supported --> ignored");
    1797           0 :     return gnil;
    1798             :   }
    1799          21 :   x = closure_trapgen(f, numerr);
    1800          14 :   if (x == (GEN)1L) x = r? closure_evalgen(r): gnil;
    1801          14 :   return x;
    1802             : }
    1803             : 
    1804             : /*******************************************************************/
    1805             : /*                                                                */
    1806             : /*                       CLONING & COPY                            */
    1807             : /*                  Replicate an existing GEN                      */
    1808             : /*                                                                 */
    1809             : /*******************************************************************/
    1810             : /* lontyp[tx] = 0 (non recursive type) or number of codewords for type tx */
    1811             : const  long lontyp[] = { 0,0,0,1,1,2,1,2,1,1, 2,2,0,1,1,1,1,1,1,1, 2,0,0,2,2,1 };
    1812             : 
    1813             : static GEN
    1814        1481 : list_internal_copy(GEN z, long nmax)
    1815             : {
    1816             :   long i, l;
    1817             :   GEN a;
    1818        1481 :   if (!z) return NULL;
    1819        1036 :   l = lg(z);
    1820        1036 :   a = newblock(nmax+1);
    1821        1036 :   for (i = 1; i < l; i++) gel(a,i) = gel(z,i)? gclone(gel(z,i)): gen_0;
    1822        1036 :   a[0] = z[0]; return a;
    1823             : }
    1824             : 
    1825             : static void
    1826        1481 : listassign(GEN x, GEN y)
    1827             : {
    1828        1481 :   long nmax = list_nmax(x);
    1829        1481 :   GEN L = list_data(x);
    1830        1481 :   if (!nmax && L) nmax = lg(L) + 32; /* not malloc'ed yet */
    1831        1481 :   y[1] = evaltyp(list_typ(x))|evallg(nmax);
    1832        1481 :   list_data(y) = list_internal_copy(L, nmax);
    1833        1481 : }
    1834             : 
    1835             : /* transform a non-malloced list (e.g. from gtolist or gtomap) to a malloced
    1836             :  * list suitable for listput */
    1837             : GEN
    1838           0 : listinit(GEN x)
    1839             : {
    1840           0 :   GEN y = cgetg(3, t_LIST);
    1841           0 :   listassign(x, y); return y;
    1842             : }
    1843             : 
    1844             : /* copy list on the PARI stack */
    1845             : GEN
    1846         543 : listcopy(GEN x)
    1847             : {
    1848         543 :   GEN y = mklist(), L = list_data(x);
    1849         543 :   if (L) list_data(y) = gcopy(L);
    1850         543 :   y[1] = evaltyp(list_typ(x));
    1851         543 :   return y;
    1852             : }
    1853             : 
    1854             : GEN
    1855  3576130400 : gcopy(GEN x)
    1856             : {
    1857  3576130400 :   long tx = typ(x), lx, i;
    1858             :   GEN y;
    1859  3576130400 :   switch(tx)
    1860             :   { /* non recursive types */
    1861  3177237799 :     case t_INT: return signe(x)? icopy(x): gen_0;
    1862             :     case t_REAL:
    1863             :     case t_STR:
    1864   198806162 :     case t_VECSMALL: return leafcopy(x);
    1865             :     /* one more special case */
    1866         543 :     case t_LIST: return listcopy(x);
    1867             :   }
    1868   200085896 :   y = cgetg_copy(x, &lx);
    1869   200085780 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1870   200085780 :   for (; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
    1871   200085962 :   return y;
    1872             : }
    1873             : 
    1874             : /* as gcopy, but truncate to the first lx components if recursive type
    1875             :  * [ leaves use their own lg ]. No checks. */
    1876             : GEN
    1877         938 : gcopy_lg(GEN x, long lx)
    1878             : {
    1879         938 :   long tx = typ(x), i;
    1880             :   GEN y;
    1881         938 :   switch(tx)
    1882             :   { /* non recursive types */
    1883           0 :     case t_INT: return signe(x)? icopy(x): gen_0;
    1884             :     case t_REAL:
    1885             :     case t_STR:
    1886           0 :     case t_VECSMALL: return leafcopy(x);
    1887             :     /* one more special case */
    1888           0 :     case t_LIST: return listcopy(x);
    1889             :   }
    1890         938 :   y = cgetg(lx, tx);
    1891         938 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1892         938 :   for (; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
    1893         938 :   return y;
    1894             : }
    1895             : 
    1896             : /* cf cgetg_copy: "allocate" (by updating first codeword only) for subsequent
    1897             :  * copy of x, as if avma = *AVMA */
    1898             : INLINE GEN
    1899   388385245 : cgetg_copy_avma(GEN x, long *plx, pari_sp *AVMA) {
    1900             :   GEN z;
    1901   388385245 :   *plx = lg(x);
    1902   388385245 :   z = ((GEN)*AVMA) - *plx;
    1903   388385245 :   z[0] = x[0] & (TYPBITS|LGBITS);
    1904   388385245 :   *AVMA = (pari_sp)z; return z;
    1905             : }
    1906             : INLINE GEN
    1907         441 : cgetlist_avma(pari_sp *AVMA)
    1908             : {
    1909         441 :   GEN y = ((GEN)*AVMA) - 3;
    1910         441 :   y[0] = _evallg(3) | evaltyp(t_LIST);
    1911         441 :   *AVMA = (pari_sp)y; return y;
    1912             : }
    1913             : 
    1914             : /* copy x as if avma = *AVMA, update *AVMA */
    1915             : GEN
    1916  2832251500 : gcopy_avma(GEN x, pari_sp *AVMA)
    1917             : {
    1918  2832251500 :   long i, lx, tx = typ(x);
    1919             :   GEN y;
    1920             : 
    1921  2832251500 :   switch(typ(x))
    1922             :   { /* non recursive types */
    1923             :     case t_INT:
    1924  2692258054 :       if (lgefint(x) == 2) return gen_0;
    1925  2209419425 :       *AVMA = (pari_sp)icopy_avma(x, *AVMA);
    1926  2209419465 :       return (GEN)*AVMA;
    1927             :     case t_REAL: case t_STR: case t_VECSMALL:
    1928    30741190 :       *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
    1929    30741190 :       return (GEN)*AVMA;
    1930             : 
    1931             :     /* one more special case */
    1932             :     case t_LIST:
    1933         441 :       y = cgetlist_avma(AVMA);
    1934         441 :       listassign(x, y); return y;
    1935             : 
    1936             :   }
    1937   109251815 :   y = cgetg_copy_avma(x, &lx, AVMA);
    1938   109251738 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1939   109251738 :   for (; i<lx; i++) gel(y,i) = gcopy_avma(gel(x,i), AVMA);
    1940   109251881 :   return y;
    1941             : }
    1942             : 
    1943             : /* [copy_bin/bin_copy:] same as gcopy_avma but use NULL to code an exact 0, and
    1944             :  * make shallow copies of finalized t_LISTs */
    1945             : static GEN
    1946  1239807347 : gcopy_av0(GEN x, pari_sp *AVMA)
    1947             : {
    1948  1239807347 :   long i, lx, tx = typ(x);
    1949             :   GEN y;
    1950             : 
    1951  1239807347 :   switch(tx)
    1952             :   { /* non recursive types */
    1953             :     case t_INT:
    1954   805353174 :       if (!signe(x)) return NULL; /* special marker */
    1955   453534228 :       *AVMA = (pari_sp)icopy_avma(x, *AVMA);
    1956   453534371 :       return (GEN)*AVMA;
    1957             :     case t_LIST:
    1958          49 :       if (list_data(x) && !list_nmax(x)) break; /* not finalized, need copy */
    1959             :       /* else finalized: shallow copy */
    1960             :     case t_REAL: case t_STR: case t_VECSMALL:
    1961   155322140 :       *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
    1962   155322235 :       return (GEN)*AVMA;
    1963             :   }
    1964   279132033 :   y = cgetg_copy_avma(x, &lx, AVMA);
    1965   279132449 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1966   279132449 :   for (; i<lx; i++) gel(y,i) = gcopy_av0(gel(x,i), AVMA);
    1967   279134120 :   return y;
    1968             : }
    1969             : 
    1970             : INLINE GEN
    1971           0 : icopy_avma_canon(GEN x, pari_sp AVMA)
    1972             : {
    1973           0 :   long i, lx = lgefint(x);
    1974           0 :   GEN y = ((GEN)AVMA) - lx;
    1975           0 :   y[0] = evaltyp(t_INT)|evallg(lx); /* kills isclone */
    1976           0 :   y[1] = x[1]; x = int_MSW(x);
    1977           0 :   for (i=2; i<lx; i++, x = int_precW(x)) y[i] = *x;
    1978           0 :   return y;
    1979             : }
    1980             : 
    1981             : /* [copy_bin_canon:] same as gcopy_av0, but copy integers in
    1982             :  * canonical (native kernel) form and make a full copy of t_LISTs */
    1983             : static GEN
    1984           0 : gcopy_av0_canon(GEN x, pari_sp *AVMA)
    1985             : {
    1986           0 :   long i, lx, tx = typ(x);
    1987             :   GEN y;
    1988             : 
    1989           0 :   switch(tx)
    1990             :   { /* non recursive types */
    1991             :     case t_INT:
    1992           0 :       if (!signe(x)) return NULL; /* special marker */
    1993           0 :       *AVMA = (pari_sp)icopy_avma_canon(x, *AVMA);
    1994           0 :       return (GEN)*AVMA;
    1995             :     case t_REAL: case t_STR: case t_VECSMALL:
    1996           0 :       *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
    1997           0 :       return (GEN)*AVMA;
    1998             : 
    1999             :     /* one more special case */
    2000             :     case t_LIST:
    2001             :     {
    2002           0 :       long t = list_typ(x);
    2003           0 :       GEN y = cgetlist_avma(AVMA), z = list_data(x);
    2004           0 :       if (z) {
    2005           0 :         list_data(y) = gcopy_av0_canon(z, AVMA);
    2006           0 :         y[1] = evaltyp(t)|evallg(lg(z)-1);
    2007             :       } else {
    2008           0 :         list_data(y) = NULL;
    2009           0 :         y[1] = evaltyp(t);
    2010             :       }
    2011           0 :       return y;
    2012             :     }
    2013             :   }
    2014           0 :   y = cgetg_copy_avma(x, &lx, AVMA);
    2015           0 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    2016           0 :   for (; i<lx; i++) gel(y,i) = gcopy_av0_canon(gel(x,i), AVMA);
    2017           0 :   return y;
    2018             : }
    2019             : 
    2020             : /* [copy_bin/bin_copy:] size (number of words) required for
    2021             :  * gcopy_av0_canon(x) */
    2022             : static long
    2023           0 : taille0_canon(GEN x)
    2024             : {
    2025           0 :   long i,n,lx, tx = typ(x);
    2026           0 :   switch(tx)
    2027             :   { /* non recursive types */
    2028           0 :     case t_INT: return signe(x)? lgefint(x): 0;
    2029             :     case t_REAL:
    2030             :     case t_STR:
    2031           0 :     case t_VECSMALL: return lg(x);
    2032             : 
    2033             :     /* one more special case */
    2034             :     case t_LIST:
    2035             :     {
    2036           0 :       GEN L = list_data(x);
    2037           0 :       return L? 3 + taille0_canon(L): 3;
    2038             :     }
    2039             :   }
    2040           0 :   n = lx = lg(x);
    2041           0 :   for (i=lontyp[tx]; i<lx; i++) n += taille0_canon(gel(x,i));
    2042           0 :   return n;
    2043             : }
    2044             : 
    2045             : /* [copy_bin/bin_copy:] size (number of words) required for gcopy_av0(x) */
    2046             : static long
    2047  1239815599 : taille0(GEN x)
    2048             : {
    2049  1239815599 :   long i,n,lx, tx = typ(x);
    2050  1239815599 :   switch(tx)
    2051             :   { /* non recursive types */
    2052             :     case t_INT:
    2053   805361064 :       lx = lgefint(x);
    2054   805361064 :       return lx == 2? 0: lx;
    2055             :     case t_LIST:
    2056             :     {
    2057          49 :       GEN L = list_data(x);
    2058          49 :       if (L && !list_nmax(x)) break; /* not finalized, deep copy */
    2059             :     }
    2060             :     /* else finalized: shallow */
    2061             :     case t_REAL:
    2062             :     case t_STR:
    2063             :     case t_VECSMALL:
    2064   155322754 :       return lg(x);
    2065             :   }
    2066   279131781 :   n = lx = lg(x);
    2067   279131781 :   for (i=lontyp[tx]; i<lx; i++) n += taille0(gel(x,i));
    2068   279134546 :   return n;
    2069             : }
    2070             : 
    2071             : static long
    2072  2901660416 : gsizeclone_i(GEN x)
    2073             : {
    2074  2901660416 :   long i,n,lx, tx = typ(x);
    2075  2901660416 :   switch(tx)
    2076             :   { /* non recursive types */
    2077  2692833597 :     case t_INT: lx = lgefint(x); return lx == 2? 0: lx;;
    2078             :     case t_REAL:
    2079             :     case t_STR:
    2080    34779777 :     case t_VECSMALL: return lg(x);
    2081             : 
    2082        1481 :     case t_LIST: return 3;
    2083             :     default:
    2084   174045561 :       n = lx = lg(x);
    2085   174045561 :       for (i=lontyp[tx]; i<lx; i++) n += gsizeclone_i(gel(x,i));
    2086   174045560 :       return n;
    2087             :   }
    2088             : }
    2089             : 
    2090             : /* #words needed to clone x; t_LIST is a special case since list_data() is
    2091             :  * malloc'ed later, in list_internal_copy() */
    2092             : static long
    2093   207399953 : gsizeclone(GEN x) { return (typ(x) == t_INT)? lgefint(x): gsizeclone_i(x); }
    2094             : 
    2095             : long
    2096         154 : gsizeword(GEN x)
    2097             : {
    2098             :   GEN L;
    2099         154 :   if (typ(x) != t_LIST) return gsizeclone(x);
    2100             :   /* For t_LIST, return the actual list size, gsizeclone() is always 3 */
    2101           0 :   L = list_data(x);
    2102           0 :   return L? 3 + gsizeclone(L): 3;
    2103             : }
    2104             : long
    2105         154 : gsizebyte(GEN x) { return gsizeword(x) * sizeof(long); }
    2106             : 
    2107             : /* return a clone of x structured as a gcopy */
    2108             : GENbin*
    2109    65145513 : copy_bin(GEN x)
    2110             : {
    2111    65145513 :   long t = taille0(x);
    2112    65146192 :   GENbin *p = (GENbin*)pari_malloc(sizeof(GENbin) + t*sizeof(long));
    2113    65147048 :   pari_sp AVMA = (pari_sp)(GENbinbase(p) + t);
    2114    65146724 :   p->rebase = &shiftaddress;
    2115    65146724 :   p->len = t;
    2116    65146724 :   p->x   = gcopy_av0(x, &AVMA);
    2117    65146339 :   p->base= (GEN)AVMA; return p;
    2118             : }
    2119             : 
    2120             : /* same, writing t_INT in canonical native form */
    2121             : GENbin*
    2122           0 : copy_bin_canon(GEN x)
    2123             : {
    2124           0 :   long t = taille0_canon(x);
    2125           0 :   GENbin *p = (GENbin*)pari_malloc(sizeof(GENbin) + t*sizeof(long));
    2126           0 :   pari_sp AVMA = (pari_sp)(GENbinbase(p) + t);
    2127           0 :   p->rebase = &shiftaddress_canon;
    2128           0 :   p->len = t;
    2129           0 :   p->x   = gcopy_av0_canon(x, &AVMA);
    2130           0 :   p->base= (GEN)AVMA; return p;
    2131             : }
    2132             : 
    2133             : GEN
    2134   207399803 : gclone(GEN x)
    2135             : {
    2136   207399803 :   long i,lx,tx = typ(x), t = gsizeclone(x);
    2137   207399804 :   GEN y = newblock(t);
    2138   207399819 :   switch(tx)
    2139             :   { /* non recursive types */
    2140             :     case t_INT:
    2141   140206349 :       lx = lgefint(x);
    2142   140206349 :       y[0] = evaltyp(t_INT)|evallg(lx);
    2143   140206344 :       for (i=1; i<lx; i++) y[i] = x[i];
    2144   140206344 :       break;
    2145             :     case t_REAL:
    2146             :     case t_STR:
    2147             :     case t_VECSMALL:
    2148     2936954 :       lx = lg(x);
    2149     2936954 :       for (i=0; i<lx; i++) y[i] = x[i];
    2150     2936954 :       break;
    2151             : 
    2152             :     /* one more special case */
    2153             :     case t_LIST:
    2154        1040 :       y[0] = evaltyp(t_LIST)|_evallg(3);
    2155        1040 :       listassign(x, y);
    2156        1040 :       break;
    2157             :     default: {
    2158    64255476 :       pari_sp AVMA = (pari_sp)(y + t);
    2159    64255476 :       lx = lg(x);
    2160    64255476 :       y[0] = x[0];
    2161    64255476 :       if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    2162    64255476 :       for (; i<lx; i++) gel(y,i) = gcopy_avma(gel(x,i), &AVMA);
    2163             :     }
    2164             :   }
    2165   207399811 :   setisclone(y); return y;
    2166             : }
    2167             : 
    2168             : void
    2169   887992488 : shiftaddress(GEN x, long dec)
    2170             : {
    2171   887992488 :   long i, lx, tx = typ(x);
    2172   887992488 :   if (is_recursive_t(tx))
    2173             :   {
    2174   279134730 :     if (tx == t_LIST)
    2175             :     {
    2176          49 :       if (!list_data(x) || list_nmax(x)) return; /* empty or finalized */
    2177             :       /* not finalized, update pointers  */
    2178             :     }
    2179   279134688 :     lx = lg(x);
    2180  1453806958 :     for (i=lontyp[tx]; i<lx; i++) {
    2181  1174672189 :       if (!x[i]) gel(x,i) = gen_0;
    2182             :       else
    2183             :       {
    2184   822853988 :         x[i] += dec;
    2185   822853988 :         shiftaddress(gel(x,i), dec);
    2186             :       }
    2187             :     }
    2188             :   }
    2189             : }
    2190             : 
    2191             : void
    2192           0 : shiftaddress_canon(GEN x, long dec)
    2193             : {
    2194           0 :   long i, lx, tx = typ(x);
    2195           0 :   switch(tx)
    2196             :   { /* non recursive types */
    2197             :     case t_INT: {
    2198             :       GEN y;
    2199           0 :       lx = lgefint(x); if (lx <= 3) return;
    2200           0 :       y = x + 2;
    2201           0 :       x = int_MSW(x);  if (x == y) return;
    2202           0 :       while (x > y) { lswap(*x, *y); x = int_precW(x); y++; }
    2203           0 :       break;
    2204             :     }
    2205             :     case t_REAL:
    2206             :     case t_STR:
    2207             :     case t_VECSMALL:
    2208           0 :       break;
    2209             : 
    2210             :     /* one more special case */
    2211             :     case t_LIST: {
    2212           0 :       GEN Lx = list_data(x);
    2213           0 :       if (Lx) {
    2214           0 :         pari_sp av = avma;
    2215           0 :         GEN L = (GEN)((long)Lx+dec);
    2216           0 :         shiftaddress_canon(L, dec);
    2217           0 :         list_data(x) = list_internal_copy(L, lg(L)); set_avma(av);
    2218             :       }
    2219           0 :       break;
    2220             :     }
    2221             :     default:
    2222           0 :       lx = lg(x);
    2223           0 :       for (i=lontyp[tx]; i<lx; i++) {
    2224           0 :         if (!x[i]) gel(x,i) = gen_0;
    2225             :         else
    2226             :         {
    2227           0 :           x[i] += dec;
    2228           0 :           shiftaddress_canon(gel(x,i), dec);
    2229             :         }
    2230             :       }
    2231             :   }
    2232             : }
    2233             : 
    2234             : /********************************************************************/
    2235             : /**                                                                **/
    2236             : /**                INSERT DYNAMIC OBJECT IN STRUCTURE              **/
    2237             : /**                                                                **/
    2238             : /********************************************************************/
    2239             : GEN
    2240          35 : obj_reinit(GEN S)
    2241             : {
    2242          35 :   GEN s, T = leafcopy(S);
    2243          35 :   long a = lg(T)-1;
    2244          35 :   s = gel(T,a);
    2245          35 :   gel(T,a) = zerovec(lg(s)-1);
    2246          35 :   return T;
    2247             : }
    2248             : 
    2249             : GEN
    2250     1213813 : obj_init(long d, long n)
    2251             : {
    2252     1213813 :   GEN S = cgetg(d+2, t_VEC);
    2253     1213813 :   gel(S, d+1) = zerovec(n);
    2254     1213814 :   return S;
    2255             : }
    2256             : /* insert O in S [last position] at position K, return it */
    2257             : GEN
    2258     1131746 : obj_insert(GEN S, long K, GEN O)
    2259     1131746 : { return obj_insert_shallow(S, K, gclone(O)); }
    2260             : /* as obj_insert. WITHOUT cloning (for libpari, when creating a *new* obj
    2261             :  * from an existing one) */
    2262             : GEN
    2263     1135974 : obj_insert_shallow(GEN S, long K, GEN O)
    2264             : {
    2265     1135974 :   GEN o, v = gel(S, lg(S)-1);
    2266     1135974 :   if (typ(v) != t_VEC) pari_err_TYPE("obj_insert", S);
    2267     1135974 :   o = gel(v,K);
    2268     1135974 :   gel(v,K) = O; /*SIGINT: before unclone(o)*/
    2269     1135974 :   clone_unlock(o); return gel(v,K);
    2270             : }
    2271             : 
    2272             : /* Does S [last position] contain data at position K ? Return it, or NULL */
    2273             : GEN
    2274     2715930 : obj_check(GEN S, long K)
    2275             : {
    2276     2715930 :   GEN O, v = gel(S, lg(S)-1);
    2277     2715930 :   if (typ(v) != t_VEC || K >= lg(v)) pari_err_TYPE("obj_check", S);
    2278     2715930 :   O = gel(v,K); return isintzero(O)? NULL: O;
    2279             : }
    2280             : 
    2281             : GEN
    2282      897921 : obj_checkbuild(GEN S, long tag, GEN (*build)(GEN))
    2283             : {
    2284      897921 :   GEN O = obj_check(S, tag);
    2285      897921 :   if (!O)
    2286      663313 :   { pari_sp av = avma; O = obj_insert(S, tag, build(S)); set_avma(av); }
    2287      897914 :   return O;
    2288             : }
    2289             : 
    2290             : GEN
    2291      105395 : obj_checkbuild_prec(GEN S, long tag, GEN (*build)(GEN,long),
    2292             :   long (*pr)(GEN), long prec)
    2293             : {
    2294      105395 :   pari_sp av = avma;
    2295      105395 :   GEN w = obj_check(S, tag);
    2296      105395 :   if (!w || pr(w) < prec) w = obj_insert(S, tag, build(S, prec));
    2297      105395 :   set_avma(av); return gcopy(w);
    2298             : }
    2299             : GEN
    2300       12281 : obj_checkbuild_realprec(GEN S, long tag, GEN (*build)(GEN,long), long prec)
    2301       12281 : { return obj_checkbuild_prec(S,tag,build,gprecision,prec); }
    2302             : GEN
    2303         497 : obj_checkbuild_padicprec(GEN S, long tag, GEN (*build)(GEN,long), long prec)
    2304         497 : { return obj_checkbuild_prec(S,tag,build,padicprec_relative,prec); }
    2305             : 
    2306             : /* Reset S [last position], freeing all clones */
    2307             : void
    2308       12572 : obj_free(GEN S)
    2309             : {
    2310       12572 :   GEN v = gel(S, lg(S)-1);
    2311             :   long i;
    2312       12572 :   if (typ(v) != t_VEC) pari_err_TYPE("obj_free", S);
    2313       68558 :   for (i = 1; i < lg(v); i++)
    2314             :   {
    2315       55986 :     GEN o = gel(v,i);
    2316       55986 :     gel(v,i) = gen_0;
    2317       55986 :     gunclone_deep(o);
    2318             :   }
    2319       12572 : }
    2320             : 
    2321             : /*******************************************************************/
    2322             : /*                                                                 */
    2323             : /*                         STACK MANAGEMENT                        */
    2324             : /*                                                                 */
    2325             : /*******************************************************************/
    2326             : INLINE void
    2327  2240697825 : dec_gerepile(pari_sp *x, pari_sp av0, pari_sp av, pari_sp tetpil, size_t dec)
    2328             : {
    2329  2240697825 :   if (*x < av && *x >= av0)
    2330             :   { /* update address if in stack */
    2331  1849042537 :     if (*x < tetpil) *x += dec;
    2332           0 :     else pari_err_BUG("gerepile, significant pointers lost");
    2333             :   }
    2334  2240697825 : }
    2335             : 
    2336             : void
    2337      189300 : gerepileallsp(pari_sp av, pari_sp tetpil, int n, ...)
    2338             : {
    2339      189300 :   const pari_sp av0 = avma;
    2340      189300 :   const size_t dec = av-tetpil;
    2341             :   int i;
    2342      189300 :   va_list a; va_start(a, n);
    2343      189300 :   (void)gerepile(av,tetpil,NULL);
    2344      189300 :   for (i=0; i<n; i++) dec_gerepile((pari_sp*)va_arg(a,GEN*), av0,av,tetpil,dec);
    2345      189300 :   va_end(a);
    2346      189300 : }
    2347             : 
    2348             : /* Takes an array of pointers to GENs, of length n.
    2349             :  * Cleans up the stack between av and tetpil, updating those GENs. */
    2350             : void
    2351     6009636 : gerepilemanysp(pari_sp av, pari_sp tetpil, GEN* gptr[], int n)
    2352             : {
    2353     6009636 :   const pari_sp av0 = avma;
    2354     6009636 :   const size_t dec = av-tetpil;
    2355             :   int i;
    2356     6009636 :   (void)gerepile(av,tetpil,NULL);
    2357     6009636 :   for (i=0; i<n; i++) dec_gerepile((pari_sp*)gptr[i], av0, av, tetpil, dec);
    2358     6009636 : }
    2359             : 
    2360             : /* Takes an array of GENs (cast to longs), of length n.
    2361             :  * Cleans up the stack between av and tetpil, updating those GENs. */
    2362             : void
    2363   152136501 : gerepilecoeffssp(pari_sp av, pari_sp tetpil, long *g, int n)
    2364             : {
    2365   152136501 :   const pari_sp av0 = avma;
    2366   152136501 :   const size_t dec = av-tetpil;
    2367             :   int i;
    2368   152136501 :   (void)gerepile(av,tetpil,NULL);
    2369   152136501 :   for (i=0; i<n; i++,g++) dec_gerepile((pari_sp*)g, av0, av, tetpil, dec);
    2370   152136501 : }
    2371             : 
    2372             : static int
    2373           0 : dochk_gerepileupto(GEN av, GEN x)
    2374             : {
    2375             :   long i,lx,tx;
    2376           0 :   if (!isonstack(x)) return 1;
    2377           0 :   if (x > av)
    2378             :   {
    2379           0 :     pari_warn(warner,"bad object %Ps",x);
    2380           0 :     return 0;
    2381             :   }
    2382           0 :   tx = typ(x);
    2383           0 :   if (! is_recursive_t(tx)) return 1;
    2384             : 
    2385           0 :   lx = lg(x);
    2386           0 :   for (i=lontyp[tx]; i<lx; i++)
    2387           0 :     if (!dochk_gerepileupto(av, gel(x,i)))
    2388             :     {
    2389           0 :       pari_warn(warner,"bad component %ld in object %Ps",i,x);
    2390           0 :       return 0;
    2391             :     }
    2392           0 :   return 1;
    2393             : }
    2394             : /* check that x and all its components are out of stack, or have been
    2395             :  * created after av */
    2396             : int
    2397           0 : chk_gerepileupto(GEN x) { return dochk_gerepileupto(x, x); }
    2398             : 
    2399             : /* print stack between avma & av */
    2400             : void
    2401           0 : dbg_gerepile(pari_sp av)
    2402             : {
    2403           0 :   GEN x = (GEN)avma;
    2404           0 :   while (x < (GEN)av)
    2405             :   {
    2406           0 :     const long tx = typ(x), lx = lg(x);
    2407             :     GEN *a;
    2408             : 
    2409           0 :     pari_printf(" [%ld] %Ps:", x - (GEN)avma, x);
    2410           0 :     if (! is_recursive_t(tx)) { pari_putc('\n'); x += lx; continue; }
    2411           0 :     a = (GEN*)x + lontyp[tx]; x += lx;
    2412           0 :     for (  ; a < (GEN*)x; a++)
    2413             :     {
    2414           0 :       if (*a == gen_0)
    2415           0 :         pari_puts("  gen_0");
    2416           0 :       else if (*a == gen_1)
    2417           0 :         pari_puts("  gen_1");
    2418           0 :       else if (*a == gen_m1)
    2419           0 :         pari_puts("  gen_m1");
    2420           0 :       else if (*a == gen_2)
    2421           0 :         pari_puts("  gen_2");
    2422           0 :       else if (*a == gen_m2)
    2423           0 :         pari_puts("  gen_m2");
    2424           0 :       else if (*a == ghalf)
    2425           0 :         pari_puts("  ghalf");
    2426           0 :       else if (isclone(*a))
    2427           0 :         pari_printf("  %Ps (clone)", *a);
    2428             :       else
    2429           0 :         pari_printf("  %Ps [%ld]", *a, *a - (GEN)avma);
    2430           0 :       if (a+1 < (GEN*)x) pari_putc(',');
    2431             :     }
    2432           0 :     pari_printf("\n");
    2433             :   }
    2434           0 : }
    2435             : void
    2436           0 : dbg_gerepileupto(GEN q)
    2437             : {
    2438           0 :   err_printf("%Ps:\n", q);
    2439           0 :   dbg_gerepile((pari_sp) (q+lg(q)));
    2440           0 : }
    2441             : 
    2442             : GEN
    2443   521790073 : gerepile(pari_sp av, pari_sp tetpil, GEN q)
    2444             : {
    2445   521790073 :   const size_t dec = av - tetpil;
    2446   521790073 :   const pari_sp av0 = avma;
    2447             :   GEN x, a;
    2448             : 
    2449   521790073 :   if (dec == 0) return q;
    2450   442864542 :   if ((long)dec < 0) pari_err(e_MISC,"lbot>ltop in gerepile");
    2451             : 
    2452             :   /* dec_gerepile(&q, av0, av, tetpil, dec), saving 1 comparison */
    2453   442871979 :   if (q >= (GEN)av0 && q < (GEN)tetpil)
    2454   288362588 :     q = (GEN) (((pari_sp)q) + dec);
    2455             : 
    2456   442871979 :   for (x = (GEN)av, a = (GEN)tetpil; a > (GEN)av0; ) *--x = *--a;
    2457   442871979 :   set_avma((pari_sp)x);
    2458  3084975541 :   while (x < (GEN)av)
    2459             :   {
    2460  2199238412 :     const long tx = typ(x), lx = lg(x);
    2461             : 
    2462  2199238412 :     if (! is_recursive_t(tx)) { x += lx; continue; }
    2463   508604277 :     a = x + lontyp[tx]; x += lx;
    2464   508604277 :     for (  ; a < x; a++) dec_gerepile((pari_sp*)a, av0, av, tetpil, dec);
    2465             :   }
    2466   442869280 :   return q;
    2467             : }
    2468             : 
    2469             : void
    2470           0 : fill_stack(void)
    2471             : {
    2472           0 :   GEN x = ((GEN)pari_mainstack->bot);
    2473           0 :   while (x < (GEN)avma) *x++ = 0xfefefefeUL;
    2474           0 : }
    2475             : 
    2476             : void
    2477           0 : debug_stack(void)
    2478             : {
    2479           0 :   pari_sp top = pari_mainstack->top, bot = pari_mainstack->bot;
    2480             :   GEN z;
    2481           0 :   err_printf("bot=0x%lx\ttop=0x%lx\tavma=0x%lx\n", bot, top, avma);
    2482           0 :   for (z = ((GEN)top)-1; z >= (GEN)avma; z--)
    2483           0 :     err_printf("%p:\t0x%lx\t%lu\n",z,*z,*z);
    2484           0 : }
    2485             : 
    2486             : void
    2487           0 : setdebugvar(long n) { DEBUGVAR=n; }
    2488             : 
    2489             : long
    2490           0 : getdebugvar(void) { return DEBUGVAR; }
    2491             : 
    2492             : long
    2493           7 : getstack(void) { return pari_mainstack->top-avma; }
    2494             : 
    2495             : /*******************************************************************/
    2496             : /*                                                                 */
    2497             : /*                               timer_delay                             */
    2498             : /*                                                                 */
    2499             : /*******************************************************************/
    2500             : 
    2501             : #if defined(USE_CLOCK_GETTIME)
    2502             : #if defined(_POSIX_THREAD_CPUTIME)
    2503             : static THREAD clockid_t time_type = CLOCK_THREAD_CPUTIME_ID;
    2504             : #else
    2505             : static const THREAD clockid_t time_type = CLOCK_PROCESS_CPUTIME_ID;
    2506             : #endif
    2507             : static void
    2508             : pari_init_timer(void)
    2509             : {
    2510             : #if defined(_POSIX_THREAD_CPUTIME)
    2511             :   time_type = CLOCK_PROCESS_CPUTIME_ID;
    2512             : #endif
    2513             : }
    2514             : 
    2515             : void
    2516             : timer_start(pari_timer *T)
    2517             : {
    2518             :   struct timespec t;
    2519             :   clock_gettime(time_type,&t);
    2520             :   T->us = t.tv_nsec / 1000;
    2521             :   T->s  = t.tv_sec;
    2522             : }
    2523             : #elif defined(USE_GETRUSAGE)
    2524             : #ifdef RUSAGE_THREAD
    2525             : static THREAD int rusage_type = RUSAGE_THREAD;
    2526             : #else
    2527             : static const THREAD int rusage_type = RUSAGE_SELF;
    2528             : #endif /*RUSAGE_THREAD*/
    2529             : static void
    2530        1566 : pari_init_timer(void)
    2531             : {
    2532             : #ifdef RUSAGE_THREAD
    2533        1566 :   rusage_type = RUSAGE_SELF;
    2534             : #endif
    2535        1566 : }
    2536             : 
    2537             : void
    2538      237408 : timer_start(pari_timer *T)
    2539             : {
    2540             :   struct rusage r;
    2541      237408 :   getrusage(rusage_type,&r);
    2542      237409 :   T->us = r.ru_utime.tv_usec;
    2543      237409 :   T->s  = r.ru_utime.tv_sec;
    2544      237409 : }
    2545             : #elif defined(USE_FTIME)
    2546             : 
    2547             : static void
    2548             : pari_init_timer(void) { }
    2549             : 
    2550             : void
    2551             : timer_start(pari_timer *T)
    2552             : {
    2553             :   struct timeb t;
    2554             :   ftime(&t);
    2555             :   T->us = ((long)t.millitm) * 1000;
    2556             :   T->s  = t.time;
    2557             : }
    2558             : 
    2559             : #else
    2560             : 
    2561             : static void
    2562             : _get_time(pari_timer *T, long Ticks, long TickPerSecond)
    2563             : {
    2564             :   T->us = (long) ((Ticks % TickPerSecond) * (1000000. / TickPerSecond));
    2565             :   T->s  = Ticks / TickPerSecond;
    2566             : }
    2567             : 
    2568             : # ifdef USE_TIMES
    2569             : static void
    2570             : pari_init_timer(void) { }
    2571             : 
    2572             : void
    2573             : timer_start(pari_timer *T)
    2574             : {
    2575             : # ifdef _SC_CLK_TCK
    2576             :   long tck = sysconf(_SC_CLK_TCK);
    2577             : # else
    2578             :   long tck = CLK_TCK;
    2579             : # endif
    2580             :   struct tms t; times(&t);
    2581             :   _get_time(T, t.tms_utime, tck);
    2582             : }
    2583             : # elif defined(_WIN32)
    2584             : static void
    2585             : pari_init_timer(void) { }
    2586             : 
    2587             : void
    2588             : timer_start(pari_timer *T)
    2589             : { _get_time(T, win32_timer(), 1000); }
    2590             : # else
    2591             : #  include <time.h>
    2592             : #  ifndef CLOCKS_PER_SEC
    2593             : #   define CLOCKS_PER_SEC 1000000 /* may be false on YOUR system */
    2594             : #  endif
    2595             : static void
    2596             : pari_init_timer(void) { }
    2597             : 
    2598             : void
    2599             : timer_start(pari_timer *T)
    2600             : { _get_time(T, clock(), CLOCKS_PER_SEC); }
    2601             : # endif
    2602             : #endif
    2603             : 
    2604             : /* round microseconds to milliseconds */
    2605             : static long
    2606      151188 : rndus(long x) { return (x + 500) / 1000; }
    2607             : static long
    2608      151188 : timer_aux(pari_timer *T, pari_timer *U, void (*settime)(pari_timer *))
    2609             : {
    2610      151188 :   long s = T->s, us = T->us;
    2611      151188 :   settime(U); return 1000 * (U->s - s) + rndus(U->us - us);
    2612             : }
    2613             : 
    2614             : /* return delay, set timer checkpoint */
    2615             : long
    2616       76349 : timer_delay(pari_timer *T) { return timer_aux(T, T, &timer_start); }
    2617             : /* return delay, don't set checkpoint */
    2618             : long
    2619        1568 : timer_get(pari_timer *T) {pari_timer t; return timer_aux(T, &t, &timer_start);}
    2620             : 
    2621             : static void
    2622           0 : timer_vprintf(pari_timer *T, const char *format, va_list args)
    2623             : {
    2624           0 :   out_puts(pariErr, "Time ");
    2625           0 :   out_vprintf(pariErr, format,args);
    2626           0 :   out_printf(pariErr, ": %ld\n", timer_delay(T));
    2627           0 :   pariErr->flush();
    2628           0 : }
    2629             : void
    2630           0 : timer_printf(pari_timer *T, const char *format, ...)
    2631             : {
    2632           0 :   va_list args; va_start(args, format);
    2633           0 :   timer_vprintf(T, format, args);
    2634           0 :   va_end(args);
    2635           0 : }
    2636             : 
    2637             : long
    2638           0 : timer(void)  { static THREAD pari_timer T; return timer_delay(&T);}
    2639             : long
    2640        3078 : gettime(void)  { static THREAD pari_timer T; return timer_delay(&T);}
    2641             : 
    2642             : static THREAD pari_timer timer2_T, abstimer_T;
    2643             : long
    2644           0 : timer2(void) {  return timer_delay(&timer2_T);}
    2645             : void
    2646           0 : msgtimer(const char *format, ...)
    2647             : {
    2648           0 :   va_list args; va_start(args, format);
    2649           0 :   timer_vprintf(&timer2_T, format, args);
    2650           0 :   va_end(args);
    2651           0 : }
    2652             : long
    2653        1566 : getabstime(void)  { return timer_get(&abstimer_T);}
    2654             : 
    2655             : void
    2656      172688 : walltimer_start(pari_timer *ti)
    2657             : {
    2658             : #if defined(USE_CLOCK_GETTIME)
    2659             :   struct timespec t;
    2660             :   if (!clock_gettime(CLOCK_REALTIME,&t))
    2661             :   { ti->s = t.tv_sec; ti->us = rndus(t.tv_nsec); return; }
    2662             : #elif defined(USE_GETTIMEOFDAY)
    2663             :   struct timeval tv;
    2664      172688 :   if (!gettimeofday(&tv, NULL))
    2665      172688 :   {  ti->s = tv.tv_sec; ti->us = tv.tv_usec; return; }
    2666             : #elif defined(USE_FTIMEFORWALLTIME)
    2667             :   struct timeb tp;
    2668             :   if (!ftime(&tp))
    2669             :   { ti->s = tp.time; ti->us = tp.millitm*1000; return; }
    2670             : #endif
    2671           0 :   timer_start(ti);
    2672             : }
    2673             : /* return delay, set timer checkpoint */
    2674             : long
    2675       73271 : walltimer_delay(pari_timer *T) { return timer_aux(T, T, &walltimer_start); }
    2676             : /* return delay, don't set checkpoint */
    2677             : long
    2678           0 : walltimer_get(pari_timer *T)
    2679             : {
    2680             :   pari_timer t;
    2681           0 :   return timer_aux(T, &t, &walltimer_start);
    2682             : }
    2683             : 
    2684             : static GEN
    2685           0 : timetoi(ulong s, ulong m)
    2686             : {
    2687           0 :   pari_sp av = avma;
    2688           0 :   return gerepileuptoint(av, addiu(muluu(s, 1000), m));
    2689             : }
    2690             : GEN
    2691           0 : getwalltime(void)
    2692             : {
    2693             :   pari_timer ti;
    2694           0 :   walltimer_start(&ti);
    2695           0 :   return timetoi(ti.s, rndus(ti.us));
    2696             : }
    2697             : 
    2698             : /*******************************************************************/
    2699             : /*                                                                 */
    2700             : /*                   FUNCTIONS KNOWN TO THE ANALYZER               */
    2701             : /*                                                                 */
    2702             : /*******************************************************************/
    2703             : GEN
    2704           7 : pari_version(void)
    2705             : {
    2706           7 :   const ulong mask = (1UL<<PARI_VERSION_SHIFT) - 1;
    2707           7 :   ulong major, minor, patch, n = paricfg_version_code;
    2708           7 :   patch = n & mask; n >>= PARI_VERSION_SHIFT;
    2709           7 :   minor = n & mask; n >>= PARI_VERSION_SHIFT;
    2710           7 :   major = n;
    2711           7 :   if (*paricfg_vcsversion) {
    2712           7 :     const char *ver = paricfg_vcsversion;
    2713           7 :     const char *s = strchr(ver, '-');
    2714             :     char t[8];
    2715           7 :     const long len = s-ver;
    2716             :     GEN v;
    2717           7 :     if (!s || len > 6) pari_err_BUG("pari_version()"); /* paranoia */
    2718           7 :     memcpy(t, ver, len); t[len] = 0;
    2719           7 :     v = cgetg(6, t_VEC);
    2720           7 :     gel(v,1) = utoi(major);
    2721           7 :     gel(v,2) = utoi(minor);
    2722           7 :     gel(v,3) = utoi(patch);
    2723           7 :     gel(v,4) = stoi( atoi(t) );
    2724           7 :     gel(v,5) = strtoGENstr(s+1);
    2725           7 :     return v;
    2726             :   } else {
    2727           0 :     GEN v = cgetg(4, t_VEC);
    2728           0 :     gel(v,1) = utoi(major);
    2729           0 :     gel(v,2) = utoi(minor);
    2730           0 :     gel(v,3) = utoi(patch);
    2731           0 :     return v;
    2732             :   }
    2733             : }
    2734             : 
    2735             : /* List of GP functions: generated from the description system.
    2736             :  * Format (struct entree) :
    2737             :  *   char *name   : name (under GP).
    2738             :  *   ulong valence: (EpNEW, EpALIAS,EpVAR, EpINSTALL)|EpSTATIC
    2739             :  *   void *value  : For PREDEFINED FUNCTIONS: C function to call.
    2740             :  *                  For USER FUNCTIONS: pointer to defining data (block) =
    2741             :  *                   entree*: NULL, list of entree (arguments), NULL
    2742             :  *                   char*  : function text
    2743             :  *   long menu    : which help section do we belong to
    2744             :  *                   1: Standard monadic or dyadic OPERATORS
    2745             :  *                   2: CONVERSIONS and similar elementary functions
    2746             :  *                   3: functions related to COMBINATORICS
    2747             :  *                   4: TRANSCENDENTAL functions, etc.
    2748             :  *   char *code   : GP prototype, aka Parser Code (see libpari's manual)
    2749             :  *                  if NULL, use valence instead.
    2750             :  *   char *help   : short help text (init to NULL).
    2751             :  *   void *pvalue : push_val history.
    2752             :  *   long arity   : maximum number of arguments.
    2753             :  *   entree *next : next entree (init to NULL, used in hashing code). */
    2754             : #include "init.h"
    2755             : #include "default.h"

Generated by: LCOV version 1.13