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