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

Generated by: LCOV version 1.13