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 337690 : pari_init_blocks(void)
149 : {
150 337690 : next_block = 0; cur_block = NULL; root_block = NULL;
151 337690 : }
152 :
153 : static void
154 329450 : pari_close_blocks(void)
155 : {
156 2209234 : while (cur_block) killblock(cur_block);
157 335970 : }
158 :
159 : static long
160 11450243371 : blockheight(GEN bl) { return bl? bl_height(bl): 0; }
161 :
162 : static long
163 2749754691 : blockbalance(GEN bl)
164 2749754691 : { return bl ? blockheight(bl_left(bl)) - blockheight(bl_right(bl)): 0; }
165 :
166 : static void
167 2975380787 : fix_height(GEN bl)
168 2975380787 : { bl_height(bl) = maxss(blockheight(bl_left(bl)), blockheight(bl_right(bl)))+1; }
169 :
170 : static GEN
171 59992891 : bl_rotright(GEN y)
172 : {
173 59992891 : GEN x = bl_left(y), t = bl_right(x);
174 59992891 : bl_right(x) = y;
175 59992891 : bl_left(y) = t;
176 59992891 : fix_height(y);
177 59992880 : fix_height(x);
178 59992802 : return x;
179 : }
180 :
181 : static GEN
182 63096950 : bl_rotleft(GEN x)
183 : {
184 63096950 : GEN y = bl_right(x), t = bl_left(y);
185 63096950 : bl_left(y) = x;
186 63096950 : bl_right(x) = t;
187 63096950 : fix_height(x);
188 63097260 : fix_height(y);
189 63097161 : return y;
190 : }
191 :
192 : static GEN
193 1667085435 : blockinsert(GEN x, GEN bl, long *d)
194 : {
195 : long b, c;
196 1667085435 : if (!bl)
197 : {
198 232327048 : bl_left(x)=NULL; bl_right(x)=NULL;
199 232327048 : bl_height(x)=1; return x;
200 : }
201 1434758387 : c = cmpuu((ulong)x, (ulong)bl);
202 1434761703 : if (c < 0)
203 581957588 : bl_left(bl) = blockinsert(x, bl_left(bl), d);
204 852804115 : else if (c > 0)
205 852804115 : bl_right(bl) = blockinsert(x, bl_right(bl), d);
206 0 : else return bl; /* ??? Already exist in the tree ? */
207 1434743520 : fix_height(bl);
208 1434715689 : b = blockbalance(bl);
209 1434728810 : if (b > 1)
210 : {
211 32088567 : if (*d > 0) bl_left(bl) = bl_rotleft(bl_left(bl));
212 32088590 : return bl_rotright(bl);
213 : }
214 1402640243 : if (b < -1)
215 : {
216 30934101 : if (*d < 0) bl_right(bl) = bl_rotright(bl_right(bl));
217 30934100 : return bl_rotleft(bl);
218 : }
219 1371706142 : *d = c; return bl;
220 : }
221 :
222 : static GEN
223 1526997350 : blockdelete(GEN x, GEN bl)
224 : {
225 : long b;
226 1526997350 : if (!bl) return NULL; /* ??? Do not exist in the tree */
227 1526997350 : if (x < bl)
228 537057064 : bl_left(bl) = blockdelete(x, bl_left(bl));
229 989940286 : else if (x > bl)
230 706207642 : bl_right(bl) = blockdelete(x, bl_right(bl));
231 : else
232 : {
233 283732644 : if (!bl_left(bl) && !bl_right(bl)) return NULL;
234 94194182 : else if (!bl_left(bl)) return bl_right(bl);
235 72974746 : else if (!bl_right(bl)) return bl_left(bl);
236 : else
237 : {
238 51405437 : GEN r = bl_right(bl);
239 74508747 : while (bl_left(r)) r = bl_left(r);
240 51405437 : bl_right(r) = blockdelete(r, bl_right(bl));
241 51412518 : bl_left(r) = bl_left(bl);
242 51412518 : bl = r;
243 : }
244 : }
245 1294657860 : fix_height(bl);
246 1294636801 : b = blockbalance(bl);
247 1294635497 : if (b > 1)
248 : {
249 14481197 : if (blockbalance(bl_left(bl)) >= 0) return bl_rotright(bl);
250 : else
251 1984992 : { bl_left(bl) = bl_rotleft(bl_left(bl)); return bl_rotright(bl); }
252 : }
253 1280154300 : if (b < -1)
254 : {
255 5998350 : if (blockbalance(bl_right(bl)) <= 0) return bl_rotleft(bl);
256 : else
257 1985436 : { bl_right(bl) = bl_rotright(bl_right(bl)); return bl_rotleft(bl); }
258 : }
259 1274155950 : return bl;
260 : }
261 :
262 : /* If x is a component of a block, return the latter. Else return NULL */
263 : static GEN
264 233455753 : is_in_block(GEN x)
265 : {
266 233455753 : GEN bl = root_block;
267 897469505 : while (bl)
268 : {
269 893969862 : if (x >= bl && x < bl + bl_size(bl)) return bl;
270 664013752 : bl = x < bl ? bl_left(bl): bl_right(bl);
271 : }
272 3499643 : 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 768927633 : clonesearch(GEN x)
278 : {
279 768927633 : if (isclone(x)) return x;
280 594456552 : if (!isonstack(x) && !is_universal_constant(x))
281 : {
282 233010561 : x = is_in_block(x);
283 233011161 : if (x && isclone(x)) return x;
284 : }
285 364444410 : return NULL;
286 : }
287 :
288 : void
289 384632933 : clone_lock(GEN x)
290 : {
291 384632933 : GEN y = clonesearch(x);
292 384307140 : if (y)
293 : {
294 201852091 : if (DEBUGMEM > 2)
295 0 : err_printf("locking block no %ld: %08lx from %08lx\n", bl_num(y), y, x);
296 201852091 : ++bl_refc(y);
297 : }
298 384307140 : }
299 :
300 : void
301 325388858 : clone_unlock(GEN x)
302 : {
303 325388858 : GEN y = clonesearch(x);
304 325282042 : if (y)
305 : {
306 150171092 : if (DEBUGMEM > 2)
307 0 : err_printf("unlocking block no %ld: %08lx from %08lx\n", bl_num(y), y, x);
308 150171092 : gunclone(y);
309 : }
310 325282042 : }
311 :
312 : void
313 59316263 : clone_unlock_deep(GEN x)
314 : {
315 59316263 : GEN y = clonesearch(x);
316 59316263 : if (y)
317 : {
318 52125884 : if (DEBUGMEM > 2)
319 0 : err_printf("unlocking deep block no %ld: %08lx from %08lx\n", bl_num(y), y, x);
320 52125884 : gunclone_deep(y);
321 : }
322 59316263 : }
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 232325011 : newblock(size_t n)
336 : {
337 232325011 : long d = 0;
338 : long *x;
339 232325011 : BLOCK_SIGINT_START
340 232330139 : x = (long *) pari_malloc((n + BL_HEAD)*sizeof(long)) + BL_HEAD;
341 :
342 232331648 : bl_size(x) = n;
343 232331648 : bl_refc(x) = 1;
344 232331648 : bl_next(x) = NULL;
345 232331648 : bl_prev(x) = cur_block;
346 232331648 : bl_num(x) = next_block++;
347 232331648 : if (cur_block) bl_next(cur_block) = x;
348 232331648 : root_block = blockinsert(x, root_block, &d);
349 232327403 : if (DEBUGMEM > 2)
350 0 : err_printf("new block, size %6lu (no %ld): %08lx\n", n, next_block-1, x);
351 232327403 : cur_block = x;
352 232327403 : BLOCK_SIGINT_END
353 232332218 : return cur_block;
354 : }
355 :
356 : GEN
357 37881 : gcloneref(GEN x)
358 : {
359 37881 : if (isclone(x)) { ++bl_refc(x); return x; }
360 37363 : else return gclone(x);
361 : }
362 :
363 : void
364 0 : gclone_refc(GEN x) { ++bl_refc(x); }
365 :
366 : void
367 382487862 : gunclone(GEN x)
368 : {
369 382487862 : if (--bl_refc(x) > 0) return;
370 232316672 : BLOCK_SIGINT_START;
371 232326348 : root_block = blockdelete(x, root_block);
372 232307728 : if (bl_next(x)) bl_prev(bl_next(x)) = bl_prev(x);
373 : else
374 : {
375 38301818 : cur_block = bl_prev(x);
376 38301818 : next_block = bl_num(x);
377 : }
378 232307728 : if (bl_prev(x)) bl_next(bl_prev(x)) = bl_next(x);
379 232307728 : if (DEBUGMEM > 2)
380 0 : err_printf("killing block (no %ld): %08lx\n", bl_num(x), x);
381 232307728 : free((void*)bl_base(x)); /* pari_free not needed: we already block */
382 232307728 : BLOCK_SIGINT_END;
383 : }
384 :
385 : static void
386 128228866 : vec_gunclone_deep(GEN x)
387 : {
388 128228866 : long i, l = lg(x);
389 3126330903 : for (i = 1; i < l; i++) gunclone_deep(gel(x,i));
390 128228859 : }
391 : /* Recursively look for clones in the container and kill them. Then kill
392 : * container if clone. */
393 : void
394 3252208796 : gunclone_deep(GEN x)
395 : {
396 : GEN v;
397 3252208796 : if (isclone(x) && bl_refc(x) > 1) { --bl_refc(x); return; }
398 3200081862 : BLOCK_SIGINT_START;
399 3200081864 : switch(typ(x))
400 : {
401 128227487 : case t_VEC: case t_COL: case t_MAT:
402 128227487 : vec_gunclone_deep(x);
403 128227482 : break;
404 6175 : case t_LIST:
405 6175 : if ((v = list_data(x))) { vec_gunclone_deep(v); gunclone(v); }
406 6175 : break;
407 : }
408 3200081859 : if (isclone(x)) gunclone(x);
409 3200081790 : BLOCK_SIGINT_END;
410 : }
411 :
412 : int
413 387240 : pop_entree_block(entree *ep, long loc)
414 : {
415 387240 : GEN x = (GEN)ep->value;
416 387240 : if (bl_num(x) < loc) return 0; /* older */
417 455 : if (DEBUGMEM>2)
418 0 : err_printf("popping %s (block no %ld)\n", ep->name, bl_num(x));
419 455 : gunclone_deep(x); return 1;
420 : }
421 :
422 : /***************************************************************************
423 : ** **
424 : ** Export **
425 : ** **
426 : ***************************************************************************/
427 :
428 : static hashtable *export_hash;
429 : static void
430 1900 : pari_init_export(void)
431 : {
432 1900 : export_hash = hash_create_str(1,0);
433 1900 : }
434 : static void
435 1890 : pari_close_export(void)
436 : {
437 1890 : hash_destroy(export_hash);
438 1890 : }
439 :
440 : /* Exported values are blocks, but do not have the clone bit set so that they
441 : * are not affected by clone_lock and ensure_nb, etc. */
442 :
443 : void
444 59 : export_add(const char *str, GEN val)
445 : {
446 : hashentry *h;
447 59 : val = gclone(val); unsetisclone(val);
448 59 : h = hash_search(export_hash, (void*) str);
449 59 : if (h)
450 : {
451 21 : GEN v = (GEN)h->val;
452 21 : h->val = val;
453 21 : setisclone(v); gunclone(v);
454 : }
455 : else
456 38 : hash_insert(export_hash,(void*)str, (void*) val);
457 59 : }
458 :
459 : void
460 24 : export_del(const char *str)
461 : {
462 24 : hashentry *h = hash_remove(export_hash,(void*)str);
463 24 : if (h)
464 : {
465 24 : GEN v = (GEN)h->val;
466 24 : setisclone(v); gunclone(v);
467 24 : pari_free(h);
468 : }
469 24 : }
470 :
471 : GEN
472 1498 : export_get(const char *str)
473 : {
474 1498 : return hash_haskey_GEN(export_hash,(void*)str);
475 : }
476 :
477 : void
478 6 : unexportall(void)
479 : {
480 6 : pari_sp av = avma;
481 6 : GEN keys = hash_keys(export_hash);
482 6 : long i, l = lg(keys);
483 24 : for (i = 1; i < l; i++) mt_export_del((const char *)keys[i]);
484 6 : set_avma(av);
485 6 : }
486 :
487 : void
488 6 : exportall(void)
489 : {
490 : long i;
491 816 : for (i = 0; i < functions_tblsz; i++)
492 : {
493 : entree *ep;
494 9090 : for (ep = functions_hash[i]; ep; ep = ep->next)
495 8280 : if (EpVALENCE(ep)==EpVAR) mt_export_add(ep->name, (GEN)ep->value);
496 : }
497 6 : }
498 :
499 : /*********************************************************************/
500 : /* */
501 : /* C STACK SIZE CONTROL */
502 : /* */
503 : /*********************************************************************/
504 : /* Avoid core dump on deep recursion. Adapted Perl code by Dominic Dunlop */
505 : THREAD void *PARI_stack_limit = NULL;
506 :
507 : #ifdef STACK_CHECK
508 :
509 : # ifdef __EMX__ /* Emulate */
510 : void
511 : pari_stackcheck_init(void *pari_stack_base)
512 : {
513 : if (!pari_stack_base) { PARI_stack_limit = NULL; return; }
514 : PARI_stack_limit = get_stack(1./16, 32*1024);
515 : }
516 : # elif _WIN32
517 : void
518 : pari_stackcheck_init(void *pari_stack_base)
519 : {
520 : ulong size = 1UL << 21;
521 : if (!pari_stack_base) { PARI_stack_limit = NULL; return; }
522 : if (size > (ulong)pari_stack_base)
523 : PARI_stack_limit = (void*)(((ulong)pari_stack_base) / 16);
524 : else
525 : PARI_stack_limit = (void*)((ulong)pari_stack_base - (size/16)*15);
526 : }
527 : # else /* !__EMX__ && !_WIN32 */
528 : /* Set PARI_stack_limit to (a little above) the lowest safe address that can be
529 : * used on the stack. Leave PARI_stack_limit at its initial value (NULL) to
530 : * show no check should be made [init failed]. Assume stack grows downward. */
531 : void
532 339542 : pari_stackcheck_init(void *pari_stack_base)
533 : {
534 : struct rlimit rip;
535 : ulong size;
536 339542 : if (!pari_stack_base) { PARI_stack_limit = NULL; return; }
537 339542 : if (getrlimit(RLIMIT_STACK, &rip)) return;
538 339712 : size = rip.rlim_cur;
539 339712 : if (size == (ulong)RLIM_INFINITY || size > (ulong)pari_stack_base)
540 0 : PARI_stack_limit = (void*)(((ulong)pari_stack_base) / 16);
541 : else
542 339739 : PARI_stack_limit = (void*)((ulong)pari_stack_base - (size/16)*15);
543 : }
544 : # endif /* !__EMX__ */
545 :
546 : #else
547 : void
548 : pari_stackcheck_init(void *pari_stack_base)
549 : {
550 : (void) pari_stack_base; PARI_stack_limit = NULL;
551 : }
552 : #endif /* STACK_CHECK */
553 :
554 : /*******************************************************************/
555 : /* HEAP TRAVERSAL */
556 : /*******************************************************************/
557 : struct getheap_t { long n, l; };
558 : /* x is a block, not necessarily a clone [x[0] may not be set] */
559 : static void
560 6664 : f_getheap(GEN x, void *D)
561 : {
562 6664 : struct getheap_t *T = (struct getheap_t*)D;
563 6664 : T->n++;
564 6664 : T->l += bl_size(x) + BL_HEAD;
565 6664 : }
566 : GEN
567 84 : getheap(void)
568 : {
569 84 : struct getheap_t T = { 0, 0 };
570 84 : traverseheap(&f_getheap, &T); return mkvec2s(T.n, T.l);
571 : }
572 :
573 : static void
574 13412 : traverseheap_r(GEN bl, void(*f)(GEN, void *), void *data)
575 : {
576 13412 : if (!bl) return;
577 6664 : traverseheap_r(bl_left(bl), f, data);
578 6664 : traverseheap_r(bl_right(bl), f, data);
579 6664 : f(bl, data);
580 : }
581 :
582 : void
583 84 : traverseheap( void(*f)(GEN, void *), void *data)
584 : {
585 84 : traverseheap_r(root_block,f, 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 20840 : os_signal(int sig, pari_sighandler_t f)
660 : {
661 : #ifdef HAS_SIGACTION
662 : struct sigaction sa, oldsa;
663 :
664 20840 : sa.sa_handler = f;
665 20840 : sigemptyset(&sa.sa_mask);
666 20840 : sa.sa_flags = SA_NODEFER;
667 :
668 20840 : if (sigaction(sig, &sa, &oldsa)) return NULL;
669 20840 : return oldsa.sa_handler;
670 : #else
671 : return signal(sig,f);
672 : #endif
673 : }
674 :
675 : void
676 4 : 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 4 : 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 4 : case SIGPIPE:
722 : {
723 4 : pariFILE *f = GP_DATA->pp->file;
724 4 : 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 3790 : pari_sig_init(void (*f)(int))
741 : {
742 : #ifdef SIGBUS
743 3790 : (void)os_signal(SIGBUS,f);
744 : #endif
745 : #ifdef SIGFPE
746 3790 : (void)os_signal(SIGFPE,f);
747 : #endif
748 : #ifdef SIGINT
749 3790 : (void)os_signal(SIGINT,f);
750 : #endif
751 : #ifdef SIGBREAK
752 : (void)os_signal(SIGBREAK,f);
753 : #endif
754 : #ifdef SIGPIPE
755 3790 : (void)os_signal(SIGPIPE,f);
756 : #endif
757 : #ifdef SIGSEGV
758 3790 : (void)os_signal(SIGSEGV,f);
759 : #endif
760 3790 : }
761 :
762 : /*********************************************************************/
763 : /* STACK AND UNIVERSAL CONSTANTS */
764 : /*********************************************************************/
765 : static void
766 1900 : init_universal_constants(void)
767 : {
768 1900 : gen_0 = (GEN)readonly_constants;
769 1900 : gnil = (GEN)readonly_constants+2;
770 1900 : gen_1 = (GEN)readonly_constants+4;
771 1900 : gen_2 = (GEN)readonly_constants+7;
772 1900 : gen_m1 = (GEN)readonly_constants+10;
773 1900 : gen_m2 = (GEN)readonly_constants+13;
774 1900 : err_e_STACK = (GEN)readonly_constants+16;
775 1900 : ghalf = (GEN)readonly_constants+18;
776 1900 : }
777 :
778 : static void
779 338068 : pari_init_errcatch(void)
780 : {
781 338068 : iferr_env = NULL;
782 338068 : global_err_data = NULL;
783 338068 : }
784 :
785 : void
786 1928 : setalldebug(long n)
787 : {
788 1928 : long i, l = numberof(pari_DEBUGLEVEL_ptr);
789 117608 : for (i = 0; i < l; i++) *pari_DEBUGLEVEL_ptr[i] = n;
790 1928 : }
791 :
792 : /*********************************************************************/
793 : /* INIT DEFAULTS */
794 : /*********************************************************************/
795 : void
796 1900 : pari_init_defaults(void)
797 : {
798 : long i;
799 1900 : initout(1);
800 :
801 1900 : precreal = 128;
802 1900 : precdl = 16;
803 1900 : DEBUGLEVEL = 0;
804 1900 : setalldebug(0);
805 1900 : DEBUGMEM = 1;
806 1900 : disable_color = 1;
807 1900 : pari_logstyle = logstyle_none;
808 :
809 1900 : current_psfile = pari_strdup("pari.ps");
810 1900 : current_logfile= pari_strdup("pari.log");
811 1900 : pari_logfile = NULL;
812 :
813 1900 : pari_datadir = os_getenv("GP_DATA_DIR");
814 1900 : 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 1900 : pari_datadir = pari_strdup(paricfg_datadir);
822 : }
823 0 : else pari_datadir= pari_strdup(pari_datadir);
824 15200 : for (i=0; i<c_LAST; i++) gp_colors[i] = c_NONE;
825 1900 : }
826 :
827 : /*********************************************************************/
828 : /* FUNCTION HASHTABLES, MODULES */
829 : /*********************************************************************/
830 : extern entree functions_basic[], functions_default[];
831 : static void
832 1900 : pari_init_functions(void)
833 : {
834 1900 : pari_stack_init(&s_MODULES, sizeof(*MODULES),(void**)&MODULES);
835 1900 : pari_stack_pushp(&s_MODULES,functions_basic);
836 1900 : functions_hash = (entree**) pari_calloc(sizeof(entree*)*functions_tblsz);
837 1900 : pari_fill_hashtable(functions_hash, functions_basic);
838 1900 : defaults_hash = (entree**) pari_calloc(sizeof(entree*)*functions_tblsz);
839 1900 : pari_add_defaults_module(functions_default);
840 1900 : }
841 :
842 : void
843 1890 : pari_add_module(entree *ep)
844 : {
845 1890 : pari_fill_hashtable(functions_hash, ep);
846 1890 : pari_stack_pushp(&s_MODULES, ep);
847 1890 : }
848 :
849 : void
850 1900 : pari_add_defaults_module(entree *ep)
851 1900 : { 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 338382 : 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 338382 : BLOCK_SIGINT_START;
872 338382 : b = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
873 338382 : BLOCK_SIGINT_END;
874 338382 : if (b == MAP_FAILED) return NULL;
875 338382 : BLOCK_SIGINT_START;
876 338382 : 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 338382 : b = mmap(NULL, size, PROT_READ|PROT_WRITE,
882 : MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
883 338382 : BLOCK_SIGINT_END;
884 338382 : if (b == MAP_FAILED) return NULL;
885 338382 : return b;
886 : }
887 :
888 : static void
889 338372 : pari_mainstack_mfree(void *s, size_t size)
890 : {
891 338372 : BLOCK_SIGINT_START;
892 338372 : munmap(s, size);
893 338372 : BLOCK_SIGINT_END;
894 338372 : }
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 458351 : pari_mainstack_mreset(pari_sp from, pari_sp to)
911 : {
912 458351 : size_t s = to - from;
913 : void *addr, *res;
914 458351 : 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 458351 : pari_mainstack_mextend(pari_sp from, pari_sp to)
929 : {
930 458351 : size_t s = to - from;
931 : int ret;
932 458351 : BLOCK_SIGINT_START;
933 458351 : ret = mprotect((void*)from, s, PROT_READ|PROT_WRITE);
934 458351 : BLOCK_SIGINT_END;
935 458351 : 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 458351 : pari_mainstack_setsize(struct pari_mainstack *st, size_t size)
944 : {
945 458351 : pari_sp newbot = st->top - size;
946 : /* Align newbot to pagesize */
947 458351 : pari_sp alignbot = newbot & ~(pari_sp)(PARI_STACK_ALIGN - 1);
948 458351 : 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 458351 : pari_mainstack_mreset(st->vbot, alignbot);
957 458351 : st->bot = newbot;
958 458351 : st->size = size;
959 458351 : 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 676754 : fix_size(size_t a)
990 : {
991 676754 : size_t ps = PARI_STACK_ALIGN;
992 676754 : size_t b = a & ~(ps - 1); /* Align */
993 676754 : if (b < a && b < ~(ps - 1)) b += ps;
994 676754 : if (b < MIN_STACK) b = MIN_STACK;
995 676754 : return b;
996 : }
997 :
998 : static void
999 338382 : pari_mainstack_alloc(int numerr, struct pari_mainstack *st, size_t rsize, size_t vsize)
1000 : {
1001 338382 : size_t sizemax = vsize ? vsize: rsize, s = fix_size(sizemax);
1002 : for (;;)
1003 : {
1004 338382 : st->vbot = (pari_sp)pari_mainstack_malloc(s);
1005 338382 : 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 338382 : st->vsize = vsize ? s: 0;
1011 338382 : st->rsize = minuu(rsize, s);
1012 338382 : st->top = st->vbot+s;
1013 338382 : 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 338382 : st->memused = 0;
1019 338382 : }
1020 :
1021 : static void
1022 338372 : pari_mainstack_free(struct pari_mainstack *st)
1023 : {
1024 338372 : pari_mainstack_mfree((void*)st->vbot, st->vsize ? st->vsize : fix_size(st->rsize));
1025 338372 : st->top = st->bot = st->vbot = 0;
1026 338372 : st->size = st->vsize = 0;
1027 338372 : }
1028 :
1029 : static void
1030 438 : pari_mainstack_resize(struct pari_mainstack *st, size_t rsize, size_t vsize)
1031 : {
1032 438 : BLOCK_SIGINT_START;
1033 438 : pari_mainstack_free(st);
1034 438 : pari_mainstack_alloc(warnstack, st, rsize, vsize);
1035 438 : BLOCK_SIGINT_END;
1036 438 : }
1037 :
1038 : static void
1039 337787 : pari_mainstack_use(struct pari_mainstack *st)
1040 : {
1041 337787 : pari_mainstack = st;
1042 337787 : avma = st->top; /* don't use set_avma */
1043 337787 : }
1044 :
1045 : static void
1046 1900 : paristack_alloc(size_t rsize, size_t vsize)
1047 : {
1048 1900 : pari_mainstack_alloc(warnstack, pari_mainstack, rsize, vsize);
1049 1900 : pari_mainstack_use(pari_mainstack);
1050 1900 : }
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 445 : paristack_newrsize(ulong newsize)
1078 : {
1079 445 : size_t s, vsize = pari_mainstack->vsize;
1080 445 : if (!newsize) newsize = pari_mainstack->rsize << 1;
1081 445 : if (newsize != pari_mainstack->rsize)
1082 438 : pari_mainstack_resize(pari_mainstack, newsize, vsize);
1083 445 : evalstate_reset();
1084 445 : s = pari_mainstack->rsize;
1085 445 : if (DEBUGMEM)
1086 445 : pari_warn(warner,"new stack size = %lu (%.3f Mbytes)", s, s/1048576.);
1087 445 : pari_init_errcatch();
1088 445 : 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 119969 : parivstack_reset(void)
1113 : {
1114 119969 : pari_mainstack_setsize(pari_mainstack, pari_mainstack->rsize);
1115 119969 : if (avma < pari_mainstack->bot)
1116 0 : pari_err_BUG("parivstack_reset [avma < bot]");
1117 119969 : }
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 14 : new_chunk_resize(size_t x)
1123 : {
1124 14 : if (pari_mainstack->vsize==0
1125 14 : || 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 335652 : pari_thread_set_global(struct pari_global_state *gs)
1139 : {
1140 335652 : setdebugvar(gs->debugvar);
1141 335660 : push_localbitprec(gs->bitprec);
1142 335848 : pari_set_primetab(gs->primetab);
1143 335662 : pari_set_seadata(gs->seadata);
1144 335688 : pari_set_varstate(gs->varpriority, &gs->varstate);
1145 331847 : }
1146 :
1147 : static void
1148 336044 : pari_thread_get_global(struct pari_global_state *gs)
1149 : {
1150 336044 : gs->debugvar = getdebugvar();
1151 336044 : gs->bitprec = get_localbitprec();
1152 336044 : gs->primetab = primetab;
1153 336044 : gs->seadata = pari_get_seadata();
1154 336044 : varstate_save(&gs->varstate);
1155 336044 : gs->varpriority = varpriority;
1156 336044 : }
1157 :
1158 : void
1159 336044 : pari_thread_alloc(struct pari_thread *t, size_t s, GEN arg)
1160 : {
1161 336044 : pari_mainstack_alloc(warnstackthread, &t->st,s,0);
1162 336044 : pari_thread_get_global(&t->gs);
1163 336044 : t->data = arg;
1164 336044 : }
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 336044 : pari_thread_free(struct pari_thread *t)
1179 : {
1180 336044 : pari_mainstack_free(&t->st);
1181 336044 : }
1182 :
1183 : void
1184 337660 : pari_thread_init(void)
1185 : {
1186 : long var;
1187 337660 : pari_stackcheck_init((void*)&var);
1188 337633 : pari_init_blocks();
1189 337620 : pari_init_errcatch();
1190 337518 : pari_init_rand();
1191 337830 : pari_init_floats();
1192 337798 : pari_init_hgm();
1193 337723 : pari_init_parser();
1194 337851 : pari_init_compiler();
1195 337748 : pari_init_evaluator();
1196 337619 : pari_init_files();
1197 337618 : pari_init_ellcondfile();
1198 337584 : }
1199 :
1200 : void
1201 336229 : pari_thread_close(void)
1202 : {
1203 336229 : pari_thread_close_files();
1204 330556 : pari_close_evaluator();
1205 336001 : pari_close_compiler();
1206 329502 : pari_close_parser();
1207 336507 : pari_close_floats();
1208 330036 : pari_close_hgm();
1209 329070 : pari_close_blocks();
1210 335726 : }
1211 :
1212 : GEN
1213 335950 : pari_thread_start(struct pari_thread *t)
1214 : {
1215 335950 : pari_mainstack_use(&t->st);
1216 335794 : pari_thread_init();
1217 335684 : pari_thread_set_global(&t->gs);
1218 332093 : mt_thread_init();
1219 332207 : 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 1900 : pari_init_opts(size_t parisize, ulong maxprime, ulong init_opts)
1242 : {
1243 : ulong u;
1244 :
1245 1900 : pari_mt_nbthreads = 0;
1246 1900 : cb_pari_quit = dflt_pari_quit;
1247 1900 : cb_pari_init_histfile = NULL;
1248 1900 : cb_pari_get_line_interactive = NULL;
1249 1900 : cb_pari_fgets_interactive = NULL;
1250 1900 : cb_pari_whatnow = NULL;
1251 1900 : cb_pari_handle_exception = NULL;
1252 1900 : cb_pari_err_handle = pari_err_display;
1253 1900 : cb_pari_pre_recover = NULL;
1254 1900 : cb_pari_break_loop = NULL;
1255 1900 : cb_pari_is_interactive = NULL;
1256 1900 : cb_pari_start_output = NULL;
1257 1900 : cb_pari_sigint = dflt_sigint_fun;
1258 1900 : cb_pari_long_help = NULL;
1259 1900 : if (init_opts&INIT_JMPm) cb_pari_err_recover = dflt_err_recover;
1260 :
1261 1900 : pari_stackcheck_init(&u);
1262 1900 : pari_init_homedir();
1263 1900 : 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 1900 : pari_mainstack = (struct pari_mainstack *) malloc(sizeof(*pari_mainstack));
1270 1900 : paristack_alloc(parisize, 0);
1271 1900 : init_universal_constants();
1272 1900 : pari_PRIMES = NULL;
1273 1900 : if (!(init_opts&INIT_noPRIMEm))
1274 : {
1275 0 : GP_DATA->primelimit = maxprime;
1276 0 : pari_init_primes(GP_DATA->primelimit);
1277 : }
1278 1900 : if (!(init_opts&INIT_noINTGMPm)) pari_kernel_init();
1279 1900 : pari_init_graphics();
1280 1900 : pari_thread_init();
1281 1900 : pari_set_primetab(NULL);
1282 1900 : pari_set_seadata(NULL);
1283 1900 : pari_init_functions();
1284 1900 : pari_init_export();
1285 1900 : pari_var_init();
1286 1900 : pari_init_timer();
1287 1900 : pari_init_buffers();
1288 1900 : (void)getabstime();
1289 1900 : try_to_restore = 1;
1290 1900 : if (!(init_opts&INIT_noIMTm)) pari_mt_init();
1291 1900 : if ((init_opts&INIT_SIGm)) pari_sig_init(pari_sighandler);
1292 1900 : }
1293 :
1294 : void
1295 0 : pari_init(size_t parisize, ulong maxprime)
1296 0 : { pari_init_opts(parisize, maxprime, INIT_JMPm | INIT_SIGm | INIT_DFTm); }
1297 :
1298 : void
1299 1890 : pari_close_opts(ulong init_opts)
1300 : {
1301 : long i;
1302 :
1303 1890 : BLOCK_SIGINT_START;
1304 1890 : if ((init_opts&INIT_SIGm)) pari_sig_init(SIG_DFL);
1305 1890 : if (!(init_opts&INIT_noIMTm)) pari_mt_close();
1306 :
1307 1890 : pari_var_close(); /* must come before destruction of functions_hash */
1308 257040 : for (i = 0; i < functions_tblsz; i++)
1309 : {
1310 255150 : entree *ep = functions_hash[i];
1311 2891758 : while (ep) {
1312 2636608 : entree *EP = ep->next;
1313 2636608 : if (!EpSTATIC(ep)) { freeep(ep); free(ep); }
1314 2636608 : ep = EP;
1315 : }
1316 : }
1317 1890 : pari_close_mf();
1318 1890 : pari_thread_close();
1319 1890 : pari_close_export();
1320 1890 : pari_close_files();
1321 1890 : pari_close_homedir();
1322 1890 : if (!(init_opts&INIT_noINTGMPm)) pari_kernel_close();
1323 :
1324 1890 : free((void*)functions_hash);
1325 1890 : free((void*)defaults_hash);
1326 1890 : if (pari_PRIMES) pari_close_primes();
1327 1890 : free(current_logfile);
1328 1890 : free(current_psfile);
1329 1890 : pari_mainstack_free(pari_mainstack);
1330 1890 : free((void*)pari_mainstack);
1331 1890 : pari_stack_delete(&s_MODULES);
1332 1890 : if (pari_datadir) free(pari_datadir);
1333 1890 : if (init_opts&INIT_DFTm)
1334 : { /* delete GP_DATA */
1335 1890 : pari_close_paths();
1336 1890 : if (GP_DATA->hist->v) free((void*)GP_DATA->hist->v);
1337 1890 : if (GP_DATA->pp->cmd) free((void*)GP_DATA->pp->cmd);
1338 1890 : if (GP_DATA->help) free((void*)GP_DATA->help);
1339 1890 : if (GP_DATA->plothsizes) free((void*)GP_DATA->plothsizes);
1340 1890 : if (GP_DATA->colormap) pari_free(GP_DATA->colormap);
1341 1890 : if (GP_DATA->graphcolors) pari_free(GP_DATA->graphcolors);
1342 1890 : free((void*)GP_DATA->prompt);
1343 1890 : free((void*)GP_DATA->prompt_cont);
1344 1890 : free((void*)GP_DATA->histfile);
1345 : }
1346 1890 : BLOCK_SIGINT_END;
1347 1890 : }
1348 :
1349 : void
1350 1890 : pari_close(void)
1351 1890 : { pari_close_opts(INIT_JMPm | INIT_SIGm | INIT_DFTm); }
1352 :
1353 : /*******************************************************************/
1354 : /* */
1355 : /* ERROR RECOVERY */
1356 : /* */
1357 : /*******************************************************************/
1358 : void
1359 144145 : gp_context_save(struct gp_context* rec)
1360 : {
1361 144145 : rec->prettyp = GP_DATA->fmt->prettyp;
1362 144145 : rec->listloc = next_block;
1363 144145 : rec->iferr_env = iferr_env;
1364 144145 : rec->err_data = global_err_data;
1365 144145 : varstate_save(&rec->var);
1366 144145 : evalstate_save(&rec->eval);
1367 144145 : parsestate_save(&rec->parse);
1368 144145 : filestate_save(&rec->file);
1369 144145 : }
1370 :
1371 : void
1372 13012 : gp_context_restore(struct gp_context* rec)
1373 : {
1374 : long i;
1375 :
1376 13012 : if (!try_to_restore) return;
1377 : /* disable gp_context_restore() and SIGINT */
1378 13012 : BLOCK_SIGINT_START
1379 13012 : try_to_restore = 0;
1380 13012 : if (DEBUGMEM>2) err_printf("entering recover(), loc = %ld\n", rec->listloc);
1381 13012 : evalstate_restore(&rec->eval);
1382 13012 : parsestate_restore(&rec->parse);
1383 13012 : filestate_restore(&rec->file);
1384 13012 : global_err_data = rec->err_data;
1385 13012 : iferr_env = rec->iferr_env;
1386 13012 : GP_DATA->fmt->prettyp = rec->prettyp;
1387 :
1388 1769632 : for (i = 0; i < functions_tblsz; i++)
1389 : {
1390 1756620 : entree *ep = functions_hash[i];
1391 20785308 : while (ep)
1392 : {
1393 19028688 : entree *EP = ep->next;
1394 19028688 : switch(EpVALENCE(ep))
1395 : {
1396 420926 : case EpVAR:
1397 421381 : while (pop_val_if_newer(ep,rec->listloc)) /* empty */;
1398 420926 : break;
1399 703238 : case EpNEW: break;
1400 : }
1401 19028688 : ep = EP;
1402 : }
1403 : }
1404 13012 : varstate_restore(&rec->var);
1405 13012 : try_to_restore = 1;
1406 13012 : BLOCK_SIGINT_END
1407 13012 : if (DEBUGMEM>2) err_printf("leaving recover()\n");
1408 : }
1409 :
1410 : static void
1411 12937 : err_recover(long numerr)
1412 : {
1413 12937 : if (cb_pari_pre_recover)
1414 12937 : cb_pari_pre_recover(numerr);
1415 0 : evalstate_reset();
1416 0 : killallfiles();
1417 0 : pari_init_errcatch();
1418 0 : cb_pari_err_recover(numerr);
1419 0 : }
1420 :
1421 : static void
1422 13718 : err_init(void)
1423 : {
1424 : /* make sure pari_err msg starts at the beginning of line */
1425 13718 : if (!pari_last_was_newline()) pari_putc('\n');
1426 13718 : pariOut->flush();
1427 13718 : pariErr->flush();
1428 13718 : out_term_color(pariErr, c_ERR);
1429 13718 : }
1430 :
1431 : static void
1432 13578 : err_init_msg(int user)
1433 : {
1434 : const char *gp_function_name;
1435 13578 : out_puts(pariErr, " *** ");
1436 13578 : if (!user && (gp_function_name = closure_func_err()))
1437 9900 : out_printf(pariErr, "%s: ", gp_function_name);
1438 : else
1439 3678 : out_puts(pariErr, " ");
1440 13578 : }
1441 :
1442 : void
1443 760 : pari_warn(int numerr, ...)
1444 : {
1445 : char *ch1;
1446 : va_list ap;
1447 :
1448 760 : va_start(ap,numerr);
1449 :
1450 760 : err_init();
1451 760 : err_init_msg(numerr==warnuser || numerr==warnstack);
1452 760 : switch (numerr)
1453 : {
1454 7 : case warnuser:
1455 7 : out_puts(pariErr, "user warning: ");
1456 7 : out_print1(pariErr, NULL, va_arg(ap, GEN), f_RAW);
1457 7 : break;
1458 :
1459 0 : case warnmem:
1460 0 : out_puts(pariErr, "collecting garbage in "); ch1=va_arg(ap, char*);
1461 0 : out_vprintf(pariErr, ch1,ap); out_putc(pariErr, '.');
1462 0 : break;
1463 :
1464 753 : case warner:
1465 753 : out_puts(pariErr, "Warning: "); ch1=va_arg(ap, char*);
1466 753 : out_vprintf(pariErr, ch1,ap); out_putc(pariErr, '.');
1467 753 : break;
1468 :
1469 0 : case warnprec:
1470 0 : out_vprintf(pariErr, "Warning: increasing prec in %s; new prec = %ld",
1471 : ap);
1472 0 : break;
1473 :
1474 0 : case warnfile:
1475 0 : out_puts(pariErr, "Warning: failed to "),
1476 0 : ch1 = va_arg(ap, char*);
1477 0 : out_printf(pariErr, "%s: %s", ch1, va_arg(ap, char*));
1478 0 : break;
1479 :
1480 0 : case warnstack:
1481 : case warnstackthread:
1482 : {
1483 0 : ulong s = va_arg(ap, ulong);
1484 : char buf[128];
1485 0 : const char * stk = numerr == warnstackthread
1486 0 : || mt_is_thread() ? "thread": "PARI";
1487 0 : sprintf(buf,"Warning: not enough memory, new %s stack %lu", stk, s);
1488 0 : out_puts(pariErr,buf);
1489 0 : break;
1490 : }
1491 : }
1492 760 : va_end(ap);
1493 760 : out_term_color(pariErr, c_NONE);
1494 760 : out_putc(pariErr, '\n');
1495 760 : pariErr->flush();
1496 760 : }
1497 : void
1498 0 : pari_sigint(const char *time_s)
1499 : {
1500 0 : int recover=0;
1501 0 : BLOCK_SIGALRM_START
1502 0 : err_init();
1503 0 : mt_break_recover();
1504 0 : closure_err(0);
1505 0 : err_init_msg(0);
1506 0 : out_puts(pariErr, "user interrupt after ");
1507 0 : out_puts(pariErr, time_s);
1508 0 : out_term_color(pariErr, c_NONE);
1509 0 : pariErr->flush();
1510 0 : if (cb_pari_handle_exception)
1511 0 : recover = cb_pari_handle_exception(-1);
1512 0 : if (!recover && !block)
1513 0 : PARI_SIGINT_pending = 0;
1514 0 : BLOCK_SIGINT_END
1515 0 : if (!recover) err_recover(e_MISC);
1516 0 : }
1517 :
1518 : #define retmkerr2(x,y)\
1519 : do { GEN _v = cgetg(3, t_ERROR);\
1520 : _v[1] = (x);\
1521 : gel(_v,2) = (y); return _v; } while(0)
1522 : #define retmkerr3(x,y,z)\
1523 : do { GEN _v = cgetg(4, t_ERROR);\
1524 : _v[1] = (x);\
1525 : gel(_v,2) = (y);\
1526 : gel(_v,3) = (z); return _v; } while(0)
1527 : #define retmkerr4(x,y,z,t)\
1528 : do { GEN _v = cgetg(5, t_ERROR);\
1529 : _v[1] = (x);\
1530 : gel(_v,2) = (y);\
1531 : gel(_v,3) = (z);\
1532 : gel(_v,4) = (t); return _v; } while(0)
1533 : #define retmkerr5(x,y,z,t,u)\
1534 : do { GEN _v = cgetg(6, t_ERROR);\
1535 : _v[1] = (x);\
1536 : gel(_v,2) = (y);\
1537 : gel(_v,3) = (z);\
1538 : gel(_v,4) = (t);\
1539 : gel(_v,5) = (u); return _v; } while(0)
1540 : #define retmkerr6(x,y,z,t,u,v)\
1541 : do { GEN _v = cgetg(7, t_ERROR);\
1542 : _v[1] = (x);\
1543 : gel(_v,2) = (y);\
1544 : gel(_v,3) = (z);\
1545 : gel(_v,4) = (t);\
1546 : gel(_v,5) = (u);\
1547 : gel(_v,6) = (v); return _v; } while(0)
1548 :
1549 : static GEN
1550 56697 : pari_err2GEN(long numerr, va_list ap)
1551 : {
1552 56697 : switch ((enum err_list) numerr)
1553 : {
1554 140 : case e_SYNTAX:
1555 : {
1556 140 : const char *msg = va_arg(ap, char*);
1557 140 : const char *s = va_arg(ap,char *);
1558 140 : const char *entry = va_arg(ap,char *);
1559 140 : retmkerr3(numerr,strtoGENstr(msg), mkvecsmall2((long)s,(long)entry));
1560 : }
1561 390 : case e_MISC: case e_ALARM:
1562 : {
1563 390 : const char *ch1 = va_arg(ap, char*);
1564 390 : retmkerr2(numerr, gvsprintf(ch1,ap));
1565 : }
1566 2723 : case e_NOTFUNC:
1567 : case e_USER:
1568 2723 : retmkerr2(numerr,va_arg(ap, GEN));
1569 0 : case e_FILE:
1570 : {
1571 0 : const char *f = va_arg(ap, const char*);
1572 0 : retmkerr3(numerr, strtoGENstr(f), strtoGENstr(va_arg(ap, char*)));
1573 : }
1574 36 : case e_FILEDESC:
1575 : {
1576 36 : const char *f = va_arg(ap, const char*);
1577 36 : retmkerr3(numerr, strtoGENstr(f), stoi(va_arg(ap, long)));
1578 : }
1579 1854 : case e_OVERFLOW:
1580 : case e_IMPL:
1581 : case e_DIM:
1582 : case e_CONSTPOL:
1583 : case e_ROOTS0:
1584 : case e_FLAG:
1585 : case e_PREC:
1586 : case e_BUG:
1587 : case e_ARCH:
1588 : case e_PACKAGE:
1589 1854 : retmkerr2(numerr, strtoGENstr(va_arg(ap, char*)));
1590 1687 : case e_MODULUS:
1591 : case e_VAR:
1592 : {
1593 1687 : const char *f = va_arg(ap, const char*);
1594 1687 : GEN x = va_arg(ap, GEN);
1595 1687 : GEN y = va_arg(ap, GEN);
1596 1687 : retmkerr4(numerr, strtoGENstr(f), x,y);
1597 : }
1598 42647 : case e_INV:
1599 : case e_IRREDPOL:
1600 : case e_PRIME:
1601 : case e_SQRTN:
1602 : case e_TYPE:
1603 : {
1604 42647 : const char *f = va_arg(ap, const char*);
1605 42647 : GEN x = va_arg(ap, GEN);
1606 42647 : retmkerr3(numerr, strtoGENstr(f), x);
1607 : }
1608 3983 : case e_COPRIME: case e_OP: case e_TYPE2:
1609 : {
1610 3983 : const char *f = va_arg(ap, const char*);
1611 3983 : GEN x = va_arg(ap, GEN);
1612 3983 : GEN y = va_arg(ap, GEN);
1613 3983 : retmkerr4(numerr,strtoGENstr(f),x,y);
1614 : }
1615 214 : case e_COMPONENT:
1616 : {
1617 214 : const char *f= va_arg(ap, const char *);
1618 214 : const char *op = va_arg(ap, const char *);
1619 214 : GEN l = va_arg(ap, GEN);
1620 214 : GEN x = va_arg(ap, GEN);
1621 214 : retmkerr5(numerr,strtoGENstr(f),strtoGENstr(op),l,x);
1622 : }
1623 2771 : case e_DOMAIN:
1624 : {
1625 2771 : const char *f = va_arg(ap, const char*);
1626 2771 : const char *v = va_arg(ap, const char *);
1627 2771 : const char *op = va_arg(ap, const char *);
1628 2771 : GEN l = va_arg(ap, GEN);
1629 2771 : GEN x = va_arg(ap, GEN);
1630 2771 : retmkerr6(numerr,strtoGENstr(f),strtoGENstr(v),strtoGENstr(op),l,x);
1631 : }
1632 238 : case e_PRIORITY:
1633 : {
1634 238 : const char *f = va_arg(ap, const char*);
1635 238 : GEN x = va_arg(ap, GEN);
1636 238 : const char *op = va_arg(ap, const char *);
1637 238 : long v = va_arg(ap, long);
1638 238 : retmkerr5(numerr,strtoGENstr(f),x,strtoGENstr(op),stoi(v));
1639 : }
1640 0 : case e_MAXPRIME:
1641 0 : retmkerr2(numerr, utoi(va_arg(ap, ulong)));
1642 14 : case e_STACK:
1643 14 : return err_e_STACK;
1644 0 : case e_STACKTHREAD:
1645 0 : retmkerr3(numerr, utoi(va_arg(ap, ulong)), utoi(va_arg(ap, ulong)));
1646 0 : default:
1647 0 : return mkerr(numerr);
1648 : }
1649 : }
1650 :
1651 : static char *
1652 7308 : type_dim(GEN x)
1653 : {
1654 7308 : char *v = stack_malloc(64);
1655 7308 : switch(typ(x))
1656 : {
1657 133 : case t_MAT:
1658 : {
1659 133 : long l = lg(x), r = (l == 1)? 1: lgcols(x);
1660 133 : sprintf(v, "t_MAT (%ld x %ld)", r-1,l-1);
1661 133 : break;
1662 : }
1663 133 : case t_COL:
1664 133 : sprintf(v, "t_COL (%ld elts)", lg(x)-1);
1665 133 : break;
1666 252 : case t_VEC:
1667 252 : sprintf(v, "t_VEC (%ld elts)", lg(x)-1);
1668 252 : break;
1669 6790 : default:
1670 6790 : v = (char*)type_name(typ(x));
1671 : }
1672 7308 : return v;
1673 : }
1674 :
1675 : static char *
1676 3689 : gdisplay(GEN x)
1677 : {
1678 3689 : char *s = GENtostr_raw(x);
1679 3689 : if (strlen(s) < 1600) return s;
1680 35 : if (! GP_DATA->breakloop) return (char*)"(...)";
1681 0 : return stack_sprintf("\n *** (...) Huge %s omitted; you can access it via dbg_err()", type_name(typ(x)));
1682 : }
1683 :
1684 : char *
1685 21743 : pari_err2str(GEN e)
1686 : {
1687 21743 : long numerr = err_get_num(e);
1688 21743 : switch ((enum err_list) numerr)
1689 : {
1690 0 : case e_ALARM:
1691 0 : return pari_sprintf("alarm interrupt after %Ps.",gel(e,2));
1692 378 : case e_MISC:
1693 378 : return pari_sprintf("%Ps.",gel(e,2));
1694 :
1695 0 : case e_ARCH:
1696 0 : return pari_sprintf("sorry, '%Ps' not available on this system.",gel(e,2));
1697 14 : case e_BUG:
1698 14 : return pari_sprintf("bug in %Ps, please report.",gel(e,2));
1699 21 : case e_CONSTPOL:
1700 21 : return pari_sprintf("constant polynomial in %Ps.", gel(e,2));
1701 84 : case e_COPRIME:
1702 252 : return pari_sprintf("elements not coprime in %Ps:\n %s\n %s",
1703 84 : gel(e,2), gdisplay(gel(e,3)), gdisplay(gel(e,4)));
1704 718 : case e_DIM:
1705 718 : return pari_sprintf("inconsistent dimensions in %Ps.", gel(e,2));
1706 0 : case e_FILE:
1707 0 : return pari_sprintf("error opening %Ps: `%Ps'.", gel(e,2), gel(e,3));
1708 36 : case e_FILEDESC:
1709 36 : return pari_sprintf("invalid file descriptor in %Ps [%Ps]", gel(e,2), gel(e,3));
1710 91 : case e_FLAG:
1711 91 : return pari_sprintf("invalid flag in %Ps.", gel(e,2));
1712 490 : case e_IMPL:
1713 490 : return pari_sprintf("sorry, %Ps is not yet implemented.", gel(e,2));
1714 0 : case e_PACKAGE:
1715 0 : return pari_sprintf("package %Ps is required, please install it.", gel(e,2));
1716 644 : case e_INV:
1717 644 : return pari_sprintf("impossible inverse in %Ps: %s.", gel(e,2),
1718 644 : gdisplay(gel(e,3)));
1719 63 : case e_IRREDPOL:
1720 126 : return pari_sprintf("not an irreducible polynomial in %Ps: %s.",
1721 63 : gel(e,2), gdisplay(gel(e,3)));
1722 0 : case e_MAXPRIME:
1723 : {
1724 0 : const char * msg = "not enough precomputed primes";
1725 0 : ulong c = itou(gel(e,2));
1726 0 : if (c) return pari_sprintf("%s, need primelimit ~ %lu.",msg, c);
1727 0 : else return pari_strdup(msg);
1728 : }
1729 0 : case e_MEM:
1730 0 : return pari_strdup("not enough memory");
1731 1316 : case e_MODULUS:
1732 : {
1733 1316 : GEN x = gel(e,3), y = gel(e,4);
1734 1316 : return pari_sprintf("inconsistent moduli in %Ps: %s != %s",
1735 1316 : gel(e,2), gdisplay(x), gdisplay(y));
1736 : }
1737 0 : case e_NONE: return NULL;
1738 2709 : case e_NOTFUNC:
1739 2709 : return pari_strdup("not a function in function call");
1740 3654 : case e_OP: case e_TYPE2:
1741 : {
1742 3654 : pari_sp av = avma;
1743 : char *v;
1744 3654 : const char *f, *op = GSTR(gel(e,2));
1745 3654 : const char *what = numerr == e_OP? "inconsistent": "forbidden";
1746 3654 : GEN x = gel(e,3);
1747 3654 : GEN y = gel(e,4);
1748 3654 : switch(*op)
1749 : {
1750 14 : case '+': f = "addition"; break;
1751 175 : case '*': f = "multiplication"; break;
1752 2744 : case '/': case '%': case '\\': f = "division"; break;
1753 0 : case '=': op = "-->"; f = "assignment"; break;
1754 721 : default: f = op; op = ","; break;
1755 : }
1756 3654 : v = pari_sprintf("%s %s %s %s %s.", what,f,type_dim(x),op,type_dim(y));
1757 3654 : set_avma(av); return v;
1758 : }
1759 214 : case e_COMPONENT:
1760 : {
1761 214 : const char *f= GSTR(gel(e,2));
1762 214 : const char *op= GSTR(gel(e,3));
1763 214 : GEN l = gel(e,4);
1764 214 : if (!*f)
1765 154 : return pari_sprintf("nonexistent component: index %s %Ps",op,l);
1766 60 : return pari_sprintf("nonexistent component in %s: index %s %Ps",f,op,l);
1767 : }
1768 2661 : case e_DOMAIN:
1769 : {
1770 2661 : const char *f = GSTR(gel(e,2));
1771 2661 : const char *v = GSTR(gel(e,3));
1772 2661 : const char *op= GSTR(gel(e,4));
1773 2661 : GEN l = gel(e,5);
1774 2661 : if (!*op)
1775 42 : return pari_sprintf("domain error in %s: %s out of range",f,v);
1776 2619 : return pari_sprintf("domain error in %s: %s %s %Ps",f,v,op,l);
1777 : }
1778 189 : case e_PRIORITY:
1779 : {
1780 189 : const char *f = GSTR(gel(e,2));
1781 189 : long vx = gvar(gel(e,3));
1782 189 : const char *op= GSTR(gel(e,4));
1783 189 : long v = itos(gel(e,5));
1784 189 : return pari_sprintf("incorrect priority in %s: variable %Ps %s %Ps",f,
1785 : pol_x(vx), op, pol_x(v));
1786 : }
1787 161 : case e_OVERFLOW:
1788 161 : return pari_sprintf("overflow in %Ps.", gel(e,2));
1789 231 : case e_PREC:
1790 231 : return pari_sprintf("precision too low in %Ps.", gel(e,2));
1791 98 : case e_PRIME:
1792 196 : return pari_sprintf("not a prime number in %Ps: %s.",
1793 98 : gel(e,2), gdisplay(gel(e,3)));
1794 63 : case e_ROOTS0:
1795 63 : return pari_sprintf("zero polynomial in %Ps.", gel(e,2));
1796 84 : case e_SQRTN:
1797 168 : return pari_sprintf("not an n-th power residue in %Ps: %s.",
1798 84 : gel(e,2), gdisplay(gel(e,3)));
1799 14 : case e_STACK:
1800 : case e_STACKTHREAD:
1801 : {
1802 14 : const char *what = numerr == e_STACK? "PARI": "thread";
1803 14 : const char *var = numerr == e_STACK? "parisizemax": "threadsizemax";
1804 14 : size_t s = numerr == e_STACK? pari_mainstack->vsize: GP_DATA->threadsizemax;
1805 14 : char *hint = (char *) pari_malloc(512*sizeof(char));
1806 14 : char *buf = (char *) pari_malloc(512*sizeof(char));
1807 14 : if (s)
1808 0 : sprintf(hint,"you can increase '%s' using default()", var);
1809 : else
1810 : {
1811 14 : s = (numerr != e_STACK || !GP_DATA->threadsize)? pari_mainstack->rsize
1812 28 : : GP_DATA->threadsize;
1813 14 : sprintf(hint,"set '%s' to a nonzero value in your GPRC", var);
1814 : }
1815 14 : sprintf(buf, "the %s stack overflows !\n"
1816 : " current stack size: %lu (%.3f Mbytes)\n [hint] %s\n",
1817 14 : what, (ulong)s, (double)s/1048576., hint);
1818 14 : return buf;
1819 : }
1820 0 : case e_SYNTAX:
1821 0 : return pari_strdup(GSTR(gel(e,2)));
1822 7425 : case e_TYPE:
1823 14850 : return pari_sprintf("incorrect type in %Ps (%s).",
1824 7425 : gel(e,2), type_name(typ(gel(e,3))));
1825 14 : case e_USER:
1826 14 : return pari_sprint0("user error: ", gel(e,2), f_RAW);
1827 371 : case e_VAR:
1828 : {
1829 371 : GEN x = gel(e,3), y = gel(e,4);
1830 1113 : return pari_sprintf("inconsistent variables in %Ps, %Ps != %Ps.",
1831 371 : gel(e,2), pol_x(varn(x)), pol_x(varn(y)));
1832 : }
1833 : }
1834 : return NULL; /*LCOV_EXCL_LINE*/
1835 : }
1836 :
1837 : int
1838 12958 : pari_err_display(GEN err)
1839 : {
1840 12958 : long numerr=err_get_num(err);
1841 12958 : err_init();
1842 12958 : if (numerr==e_SYNTAX)
1843 : {
1844 140 : const char *msg = GSTR(gel(err,2));
1845 140 : const char *s = (const char *) gmael(err,3,1);
1846 140 : const char *entry = (const char *) gmael(err,3,2);
1847 140 : print_errcontext(pariErr, msg, s, entry);
1848 : }
1849 : else
1850 : {
1851 : char *s;
1852 12818 : closure_err(0);
1853 12818 : err_init_msg(numerr==e_USER);
1854 12818 : s = pari_err2str(err); pariErr->puts(s); pari_free(s);
1855 12818 : if (numerr==e_NOTFUNC)
1856 : {
1857 2709 : GEN fun = gel(err,2);
1858 2709 : if (gequalX(fun))
1859 : {
1860 2709 : entree *ep = varentries[varn(fun)];
1861 2709 : const char *t = ep->name;
1862 2709 : if (cb_pari_whatnow) cb_pari_whatnow(pariErr,t,1);
1863 : }
1864 : }
1865 : }
1866 12944 : out_term_color(pariErr, c_NONE);
1867 12944 : pariErr->flush(); return 0;
1868 : }
1869 :
1870 : void
1871 56709 : pari_err(int numerr, ...)
1872 : {
1873 : va_list ap;
1874 : GEN E;
1875 :
1876 56709 : va_start(ap,numerr);
1877 :
1878 56709 : if (numerr)
1879 56697 : E = pari_err2GEN(numerr,ap);
1880 : else
1881 : {
1882 12 : E = va_arg(ap,GEN);
1883 12 : numerr = err_get_num(E);
1884 : }
1885 56707 : global_err_data = E;
1886 56707 : if (*iferr_env) longjmp(*iferr_env, numerr);
1887 12967 : mt_err_recover(numerr);
1888 12958 : va_end(ap);
1889 25902 : if (cb_pari_err_handle &&
1890 12958 : cb_pari_err_handle(E)) return;
1891 25879 : if (cb_pari_handle_exception &&
1892 12942 : cb_pari_handle_exception(numerr)) return;
1893 12937 : err_recover(numerr);
1894 : }
1895 :
1896 : GEN
1897 87494 : pari_err_last(void) { return global_err_data; }
1898 :
1899 : const char *
1900 27109 : numerr_name(long numerr)
1901 : {
1902 27109 : switch ((enum err_list) numerr)
1903 : {
1904 0 : case e_ALARM: return "e_ALARM";
1905 0 : case e_ARCH: return "e_ARCH";
1906 0 : case e_BUG: return "e_BUG";
1907 0 : case e_COMPONENT: return "e_COMPONENT";
1908 0 : case e_CONSTPOL: return "e_CONSTPOL";
1909 0 : case e_COPRIME: return "e_COPRIME";
1910 0 : case e_DIM: return "e_DIM";
1911 56 : case e_DOMAIN: return "e_DOMAIN";
1912 0 : case e_FILE: return "e_FILE";
1913 0 : case e_FILEDESC: return "e_FILEDESC";
1914 7 : case e_FLAG: return "e_FLAG";
1915 49 : case e_IMPL: return "e_IMPL";
1916 19101 : case e_INV: return "e_INV";
1917 0 : case e_IRREDPOL: return "e_IRREDPOL";
1918 0 : case e_MAXPRIME: return "e_MAXPRIME";
1919 0 : case e_MEM: return "e_MEM";
1920 0 : case e_MISC: return "e_MISC";
1921 0 : case e_MODULUS: return "e_MODULUS";
1922 0 : case e_NONE: return "e_NONE";
1923 0 : case e_NOTFUNC: return "e_NOTFUNC";
1924 0 : case e_OP: return "e_OP";
1925 0 : case e_OVERFLOW: return "e_OVERFLOW";
1926 0 : case e_PACKAGE: return "e_PACKAGE";
1927 0 : case e_PREC: return "e_PREC";
1928 0 : case e_PRIME: return "e_PRIME";
1929 49 : case e_PRIORITY: return "e_PRIORITY";
1930 0 : case e_ROOTS0: return "e_ROOTS0";
1931 0 : case e_SQRTN: return "e_SQRTN";
1932 0 : case e_STACK: return "e_STACK";
1933 0 : case e_SYNTAX: return "e_SYNTAX";
1934 0 : case e_STACKTHREAD: return "e_STACKTHREAD";
1935 0 : case e_TYPE2: return "e_TYPE2";
1936 7847 : case e_TYPE: return "e_TYPE";
1937 0 : case e_USER: return "e_USER";
1938 0 : case e_VAR: return "e_VAR";
1939 : }
1940 0 : return "invalid error number";
1941 : }
1942 :
1943 : long
1944 0 : name_numerr(const char *s)
1945 : {
1946 0 : if (!strcmp(s,"e_ALARM")) return e_ALARM;
1947 0 : if (!strcmp(s,"e_ARCH")) return e_ARCH;
1948 0 : if (!strcmp(s,"e_BUG")) return e_BUG;
1949 0 : if (!strcmp(s,"e_COMPONENT")) return e_COMPONENT;
1950 0 : if (!strcmp(s,"e_CONSTPOL")) return e_CONSTPOL;
1951 0 : if (!strcmp(s,"e_COPRIME")) return e_COPRIME;
1952 0 : if (!strcmp(s,"e_DIM")) return e_DIM;
1953 0 : if (!strcmp(s,"e_DOMAIN")) return e_DOMAIN;
1954 0 : if (!strcmp(s,"e_FILE")) return e_FILE;
1955 0 : if (!strcmp(s,"e_FILEDESC")) return e_FILEDESC;
1956 0 : if (!strcmp(s,"e_FLAG")) return e_FLAG;
1957 0 : if (!strcmp(s,"e_IMPL")) return e_IMPL;
1958 0 : if (!strcmp(s,"e_INV")) return e_INV;
1959 0 : if (!strcmp(s,"e_IRREDPOL")) return e_IRREDPOL;
1960 0 : if (!strcmp(s,"e_MAXPRIME")) return e_MAXPRIME;
1961 0 : if (!strcmp(s,"e_MEM")) return e_MEM;
1962 0 : if (!strcmp(s,"e_MISC")) return e_MISC;
1963 0 : if (!strcmp(s,"e_MODULUS")) return e_MODULUS;
1964 0 : if (!strcmp(s,"e_NONE")) return e_NONE;
1965 0 : if (!strcmp(s,"e_NOTFUNC")) return e_NOTFUNC;
1966 0 : if (!strcmp(s,"e_OP")) return e_OP;
1967 0 : if (!strcmp(s,"e_OVERFLOW")) return e_OVERFLOW;
1968 0 : if (!strcmp(s,"e_PACKAGE")) return e_PACKAGE;
1969 0 : if (!strcmp(s,"e_PREC")) return e_PREC;
1970 0 : if (!strcmp(s,"e_PRIME")) return e_PRIME;
1971 0 : if (!strcmp(s,"e_PRIORITY")) return e_PRIORITY;
1972 0 : if (!strcmp(s,"e_ROOTS0")) return e_ROOTS0;
1973 0 : if (!strcmp(s,"e_SQRTN")) return e_SQRTN;
1974 0 : if (!strcmp(s,"e_STACK")) return e_STACK;
1975 0 : if (!strcmp(s,"e_SYNTAX")) return e_SYNTAX;
1976 0 : if (!strcmp(s,"e_TYPE")) return e_TYPE;
1977 0 : if (!strcmp(s,"e_TYPE2")) return e_TYPE2;
1978 0 : if (!strcmp(s,"e_USER")) return e_USER;
1979 0 : if (!strcmp(s,"e_VAR")) return e_VAR;
1980 0 : pari_err(e_MISC,"unknown error name");
1981 : return -1; /* LCOV_EXCL_LINE */
1982 : }
1983 :
1984 : GEN
1985 27109 : errname(GEN err)
1986 : {
1987 27109 : if (typ(err)!=t_ERROR) pari_err_TYPE("errname",err);
1988 27109 : return strtoGENstr(numerr_name(err_get_num(err)));
1989 : }
1990 :
1991 : /*******************************************************************/
1992 : /* */
1993 : /* CLONING & COPY */
1994 : /* Replicate an existing GEN */
1995 : /* */
1996 : /*******************************************************************/
1997 : /* lontyp[tx] = 0 (non recursive type) or number of codewords for type tx */
1998 : 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 };
1999 :
2000 : static GEN
2001 7019 : list_internal_copy(GEN z, long nmax)
2002 : {
2003 : long i, l;
2004 : GEN a;
2005 7019 : if (!z) return NULL;
2006 1336 : l = lg(z);
2007 1336 : a = newblock(nmax+1);
2008 50018 : for (i = 1; i < l; i++) gel(a,i) = gel(z,i)? gclone(gel(z,i)): gen_0;
2009 1336 : a[0] = z[0]; setisclone(a); return a;
2010 : }
2011 :
2012 : static void
2013 7019 : listassign(GEN x, GEN y)
2014 : {
2015 7019 : long nmax = list_nmax(x);
2016 7019 : GEN L = list_data(x);
2017 7019 : if (!nmax && L) nmax = lg(L) + 32; /* not malloc'ed yet */
2018 7019 : y[1] = evaltyp(list_typ(x))|evallg(nmax);
2019 7019 : list_data(y) = list_internal_copy(L, nmax);
2020 7019 : }
2021 :
2022 : /* transform a non-malloced list (e.g. from gtolist or gtomap) to a malloced
2023 : * list suitable for listput */
2024 : GEN
2025 0 : listinit(GEN x)
2026 : {
2027 0 : GEN y = cgetg(3, t_LIST);
2028 0 : listassign(x, y); return y;
2029 : }
2030 :
2031 : /* copy list on the PARI stack */
2032 : GEN
2033 730 : listcopy(GEN x)
2034 : {
2035 730 : GEN y = mklist(), L = list_data(x);
2036 730 : if (L) list_data(y) = gcopy(L);
2037 730 : y[1] = evaltyp(list_typ(x));
2038 730 : return y;
2039 : }
2040 :
2041 : GEN
2042 4724748718 : gcopy(GEN x)
2043 : {
2044 4724748718 : long tx = typ(x), lx, i;
2045 : GEN y;
2046 4724748718 : switch(tx)
2047 : { /* non recursive types */
2048 3908144307 : case t_INT: return signe(x)? icopy(x): gen_0;
2049 484786731 : case t_REAL:
2050 : case t_STR:
2051 484786731 : case t_VECSMALL: return leafcopy(x);
2052 : /* one more special case */
2053 730 : case t_LIST: return listcopy(x);
2054 : }
2055 331816950 : y = cgetg_copy(x, &lx);
2056 485388433 : for(i = 1; i < lontyp[tx]; i++) y[i] = x[i];
2057 1364206526 : for (; i < lx; i++) gel(y,i) = gcopy(gel(x,i));
2058 332017666 : return y;
2059 : }
2060 :
2061 : /* as gcopy, but truncate to the first lx components if recursive type
2062 : * [ leaves use their own lg ]. No checks. */
2063 : GEN
2064 742 : gcopy_lg(GEN x, long lx)
2065 : {
2066 742 : long tx = typ(x), i;
2067 : GEN y;
2068 742 : switch(tx)
2069 : { /* non recursive types */
2070 0 : case t_INT: return signe(x)? icopy(x): gen_0;
2071 0 : case t_REAL:
2072 : case t_STR:
2073 0 : case t_VECSMALL: return leafcopy(x);
2074 : /* one more special case */
2075 0 : case t_LIST: return listcopy(x);
2076 : }
2077 742 : y = cgetg(lx, tx);
2078 742 : for(i = 1; i < lontyp[tx]; i++) y[i] = x[i];
2079 2051 : for (; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
2080 742 : return y;
2081 : }
2082 :
2083 : /* cf cgetg_copy: "allocate" (by updating first codeword only) for subsequent
2084 : * copy of x, as if avma = *AVMA */
2085 : INLINE GEN
2086 1015253542 : cgetg_copy_avma(GEN x, long *plx, pari_sp *AVMA) {
2087 : GEN z;
2088 1015253542 : *plx = lg(x);
2089 1015253542 : z = ((GEN)*AVMA) - *plx;
2090 1015253542 : z[0] = x[0] & (TYPBITS|LGBITS);
2091 1015253542 : *AVMA = (pari_sp)z; return z;
2092 : }
2093 : INLINE GEN
2094 620 : cgetlist_avma(pari_sp *AVMA)
2095 : {
2096 620 : GEN y = ((GEN)*AVMA) - 3;
2097 620 : y[0] = _evallg(3) | evaltyp(t_LIST);
2098 620 : *AVMA = (pari_sp)y; return y;
2099 : }
2100 :
2101 : /* copy x as if avma = *AVMA, update *AVMA */
2102 : GEN
2103 3281794669 : gcopy_avma(GEN x, pari_sp *AVMA)
2104 : {
2105 3281794669 : long i, lx, tx = typ(x);
2106 : GEN y;
2107 :
2108 3281794669 : switch(typ(x))
2109 : { /* non recursive types */
2110 3073291234 : case t_INT:
2111 3073291234 : if (lgefint(x) == 2) return gen_0;
2112 2555608069 : *AVMA = (pari_sp)icopy_avma(x, *AVMA);
2113 2555608259 : return (GEN)*AVMA;
2114 63650325 : case t_REAL: case t_STR: case t_VECSMALL:
2115 63650325 : *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
2116 63649880 : return (GEN)*AVMA;
2117 :
2118 : /* one more special case */
2119 616 : case t_LIST:
2120 616 : y = cgetlist_avma(AVMA);
2121 616 : listassign(x, y); return y;
2122 :
2123 : }
2124 144852494 : y = cgetg_copy_avma(x, &lx, AVMA);
2125 170239953 : for(i = 1; i < lontyp[tx]; i++) y[i] = x[i];
2126 698117889 : for (; i<lx; i++) gel(y,i) = gcopy_avma(gel(x,i), AVMA);
2127 144894588 : return y;
2128 : }
2129 :
2130 : /* [copy_bin/bin_copy:] same as gcopy_avma but use NULL to code an exact 0, and
2131 : * make shallow copies of finalized t_LISTs */
2132 : static GEN
2133 3935080708 : gcopy_av0(GEN x, pari_sp *AVMA)
2134 : {
2135 3935080708 : long i, lx, tx = typ(x);
2136 : GEN y;
2137 :
2138 3935080708 : switch(tx)
2139 : { /* non recursive types */
2140 2598699201 : case t_INT:
2141 2598699201 : if (!signe(x)) return NULL; /* special marker */
2142 1709832573 : *AVMA = (pari_sp)icopy_avma(x, *AVMA);
2143 1710081583 : return (GEN)*AVMA;
2144 49 : case t_LIST:
2145 49 : if (list_data(x) && !list_nmax(x)) break; /* not finalized, need copy */
2146 : /* else finalized: shallow copy */
2147 : case t_REAL: case t_STR: case t_VECSMALL:
2148 466731928 : *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
2149 466729562 : return (GEN)*AVMA;
2150 : }
2151 869649579 : y = cgetg_copy_avma(x, &lx, AVMA);
2152 974256708 : for(i = 1; i < lontyp[tx]; i++) y[i] = x[i];
2153 4323928160 : for (; i<lx; i++) gel(y,i) = gcopy_av0(gel(x,i), AVMA);
2154 870232239 : return y;
2155 : }
2156 :
2157 : INLINE GEN
2158 12 : icopy_avma_canon(GEN x, pari_sp AVMA)
2159 : {
2160 12 : long i, lx = lgefint(x);
2161 12 : GEN y = ((GEN)AVMA) - lx;
2162 12 : y[0] = evaltyp(t_INT)|_evallg(lx); /* kills isclone */
2163 12 : y[1] = x[1]; x = int_MSW(x);
2164 24 : for (i=2; i<lx; i++, x = int_precW(x)) y[i] = *x;
2165 12 : return y;
2166 : }
2167 :
2168 : /* [copy_bin_canon:] same as gcopy_av0, but copy integers in
2169 : * canonical (native kernel) form and make a full copy of t_LISTs */
2170 : static GEN
2171 32 : gcopy_av0_canon(GEN x, pari_sp *AVMA)
2172 : {
2173 32 : long i, lx, tx = typ(x);
2174 : GEN y;
2175 :
2176 32 : switch(tx)
2177 : { /* non recursive types */
2178 20 : case t_INT:
2179 20 : if (!signe(x)) return NULL; /* special marker */
2180 12 : *AVMA = (pari_sp)icopy_avma_canon(x, *AVMA);
2181 12 : return (GEN)*AVMA;
2182 0 : case t_REAL: case t_STR: case t_VECSMALL:
2183 0 : *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
2184 0 : return (GEN)*AVMA;
2185 :
2186 : /* one more special case */
2187 4 : case t_LIST:
2188 : {
2189 4 : long t = list_typ(x);
2190 4 : GEN y = cgetlist_avma(AVMA), z = list_data(x);
2191 4 : if (z) {
2192 0 : list_data(y) = gcopy_av0_canon(z, AVMA);
2193 0 : y[1] = evaltyp(t)|_evallg(lg(z)-1);
2194 : } else {
2195 4 : list_data(y) = NULL;
2196 4 : y[1] = evaltyp(t);
2197 : }
2198 4 : return y;
2199 : }
2200 : }
2201 8 : y = cgetg_copy_avma(x, &lx, AVMA);
2202 16 : for(i = 1; i < lontyp[tx]; i++) y[i] = x[i];
2203 24 : for (; i<lx; i++) gel(y,i) = gcopy_av0_canon(gel(x,i), AVMA);
2204 8 : return y;
2205 : }
2206 :
2207 : /* [copy_bin/bin_copy:] size (number of words) required for
2208 : * gcopy_av0_canon(x) */
2209 : static long
2210 32 : taille0_canon(GEN x)
2211 : {
2212 32 : long i,n,lx, tx = typ(x);
2213 32 : switch(tx)
2214 : { /* non recursive types */
2215 20 : case t_INT: return signe(x)? lgefint(x): 0;
2216 0 : case t_REAL:
2217 : case t_STR:
2218 0 : case t_VECSMALL: return lg(x);
2219 :
2220 : /* one more special case */
2221 4 : case t_LIST:
2222 : {
2223 4 : GEN L = list_data(x);
2224 4 : return L? 3 + taille0_canon(L): 3;
2225 : }
2226 : }
2227 8 : n = lx = lg(x);
2228 24 : for (i=lontyp[tx]; i<lx; i++) n += taille0_canon(gel(x,i));
2229 8 : return n;
2230 : }
2231 :
2232 : /* [copy_bin/bin_copy:] size (number of words) required for gcopy_av0(x) */
2233 : static long
2234 3935575967 : taille0(GEN x)
2235 : {
2236 3935575967 : long i,n,lx, tx = typ(x);
2237 3935575967 : switch(tx)
2238 : { /* non recursive types */
2239 2599027794 : case t_INT:
2240 2599027794 : lx = lgefint(x);
2241 2599027794 : return lx == 2? 0: lx;
2242 49 : case t_LIST:
2243 : {
2244 49 : GEN L = list_data(x);
2245 49 : if (L && !list_nmax(x)) break; /* not finalized, deep copy */
2246 : }
2247 : /* else finalized: shallow */
2248 : case t_REAL:
2249 : case t_STR:
2250 : case t_VECSMALL:
2251 466740915 : return lg(x);
2252 : }
2253 869807258 : n = lx = lg(x);
2254 4324073660 : for (i=lontyp[tx]; i<lx; i++) n += taille0(gel(x,i));
2255 869892217 : return n;
2256 : }
2257 :
2258 : static long
2259 3368909746 : gsizeclone_i(GEN x)
2260 : {
2261 3368909746 : long i,n,lx, tx = typ(x);
2262 3368909746 : switch(tx)
2263 : { /* non recursive types */
2264 3073295209 : case t_INT: lx = lgefint(x); return lx == 2? 0: lx;;
2265 76158841 : case t_REAL:
2266 : case t_STR:
2267 76158841 : case t_VECSMALL: return lg(x);
2268 :
2269 7019 : case t_LIST: return 3;
2270 219448677 : default:
2271 219448677 : n = lx = lg(x);
2272 3501284980 : for (i=lontyp[tx]; i<lx; i++) n += gsizeclone_i(gel(x,i));
2273 219449147 : return n;
2274 : }
2275 : }
2276 :
2277 : /* #words needed to clone x; t_LIST is a special case since list_data() is
2278 : * malloc'ed later, in list_internal_copy() */
2279 : static long
2280 231553754 : gsizeclone(GEN x) { return (typ(x) == t_INT)? lgefint(x): gsizeclone_i(x); }
2281 :
2282 : long
2283 2214450 : gsizeword(GEN x)
2284 : {
2285 2214450 : long i, n, lx, tx = typ(x);
2286 2214450 : switch(tx)
2287 : { /* non recursive types */
2288 1676416 : case t_INT:
2289 : case t_REAL:
2290 : case t_STR:
2291 1676416 : case t_VECSMALL: return lg(x);
2292 :
2293 7 : case t_LIST:
2294 7 : x = list_data(x);
2295 7 : return x? 3 + gsizeword(x): 3;
2296 :
2297 538027 : default:
2298 538027 : n = lx = lg(x);
2299 2752295 : for (i=lontyp[tx]; i<lx; i++) n += gsizeword(gel(x,i));
2300 538027 : return n;
2301 : }
2302 : }
2303 : long
2304 175 : gsizebyte(GEN x) { return gsizeword(x) * sizeof(long); }
2305 :
2306 : /* return a clone of x structured as a gcopy */
2307 : GENbin*
2308 481621884 : copy_bin(GEN x)
2309 : {
2310 481621884 : long t = taille0(x);
2311 481663517 : GENbin *p = (GENbin*)pari_malloc(sizeof(GENbin) + t*sizeof(long));
2312 481717125 : pari_sp AVMA = (pari_sp)(GENbinbase(p) + t);
2313 481705879 : p->rebase = &shiftaddress;
2314 481705879 : p->len = t;
2315 481705879 : p->x = gcopy_av0(x, &AVMA);
2316 481652839 : p->base= (GEN)AVMA; return p;
2317 : }
2318 :
2319 : /* same, writing t_INT in canonical native form */
2320 : GENbin*
2321 16 : copy_bin_canon(GEN x)
2322 : {
2323 16 : long t = taille0_canon(x);
2324 16 : GENbin *p = (GENbin*)pari_malloc(sizeof(GENbin) + t*sizeof(long));
2325 16 : pari_sp AVMA = (pari_sp)(GENbinbase(p) + t);
2326 16 : p->rebase = &shiftaddress_canon;
2327 16 : p->len = t;
2328 16 : p->x = gcopy_av0_canon(x, &AVMA);
2329 16 : p->base= (GEN)AVMA; return p;
2330 : }
2331 :
2332 : GEN
2333 231553454 : gclone(GEN x)
2334 : {
2335 231553454 : long i,lx,tx = typ(x), t = gsizeclone(x);
2336 231553113 : GEN y = newblock(t);
2337 231560203 : switch(tx)
2338 : { /* non recursive types */
2339 144477515 : case t_INT:
2340 144477515 : lx = lgefint(x);
2341 144477515 : y[0] = evaltyp(t_INT)|_evallg(lx);
2342 876084427 : for (i=1; i<lx; i++) y[i] = x[i];
2343 144477515 : break;
2344 12499862 : case t_REAL:
2345 : case t_STR:
2346 : case t_VECSMALL:
2347 12499862 : lx = lg(x);
2348 133740294 : for (i=0; i<lx; i++) y[i] = x[i];
2349 12499862 : break;
2350 :
2351 : /* one more special case */
2352 6403 : case t_LIST:
2353 6403 : y[0] = evaltyp(t_LIST)|_evallg(3);
2354 6403 : listassign(x, y);
2355 6403 : break;
2356 74576423 : default: {
2357 74576423 : pari_sp AVMA = (pari_sp)(y + t);
2358 74576423 : lx = lg(x);
2359 151806600 : for(i = 0; i < lontyp[tx]; i++) y[i] = x[i];
2360 2803148486 : for (; i<lx; i++) gel(y,i) = gcopy_avma(gel(x,i), &AVMA);
2361 : }
2362 : }
2363 231510862 : setisclone(y); return y;
2364 : }
2365 :
2366 : void
2367 3047117348 : shiftaddress(GEN x, long dec)
2368 : {
2369 3047117348 : long i, lx, tx = typ(x);
2370 3047117348 : if (is_recursive_t(tx))
2371 : {
2372 870525652 : if (tx == t_LIST)
2373 : {
2374 49 : if (!list_data(x) || list_nmax(x)) return; /* empty or finalized */
2375 : /* not finalized, update pointers */
2376 : }
2377 870525610 : lx = lg(x);
2378 4324916366 : for (i=lontyp[tx]; i<lx; i++) {
2379 3454704530 : if (!x[i]) gel(x,i) = gen_0;
2380 : else
2381 : {
2382 2594804665 : x[i] += dec;
2383 2594804665 : shiftaddress(gel(x,i), dec);
2384 : }
2385 : }
2386 : }
2387 : }
2388 :
2389 : void
2390 24 : shiftaddress_canon(GEN x, long dec)
2391 : {
2392 24 : long i, lx, tx = typ(x);
2393 24 : switch(tx)
2394 : { /* non recursive types */
2395 12 : case t_INT: {
2396 : GEN y;
2397 12 : lx = lgefint(x); if (lx <= 3) return;
2398 0 : y = x + 2;
2399 0 : x = int_MSW(x); if (x == y) return;
2400 0 : while (x > y) { lswap(*x, *y); x = int_precW(x); y++; }
2401 0 : break;
2402 : }
2403 0 : case t_REAL:
2404 : case t_STR:
2405 : case t_VECSMALL:
2406 0 : break;
2407 :
2408 : /* one more special case */
2409 4 : case t_LIST: {
2410 4 : GEN Lx = list_data(x);
2411 4 : if (Lx) {
2412 0 : GEN L = (GEN)((long)Lx+dec);
2413 0 : shiftaddress_canon(L, dec);
2414 0 : list_data(x) = gcopy(L);
2415 : }
2416 4 : break;
2417 : }
2418 8 : default: /* Fall through */
2419 8 : lx = lg(x);
2420 24 : for (i=lontyp[tx]; i<lx; i++) {
2421 16 : if (!x[i]) gel(x,i) = gen_0;
2422 : else
2423 : {
2424 8 : x[i] += dec;
2425 8 : shiftaddress_canon(gel(x,i), dec);
2426 : }
2427 : }
2428 : }
2429 : }
2430 :
2431 : /********************************************************************/
2432 : /** **/
2433 : /** INSERT DYNAMIC OBJECT IN STRUCTURE **/
2434 : /** **/
2435 : /********************************************************************/
2436 : GEN
2437 42 : obj_reinit(GEN S)
2438 : {
2439 42 : GEN s, T = leafcopy(S);
2440 42 : long a = lg(T)-1;
2441 42 : s = gel(T,a);
2442 42 : gel(T,a) = zerovec(lg(s)-1);
2443 42 : return T;
2444 : }
2445 :
2446 : GEN
2447 1623235 : obj_init(long d, long n)
2448 : {
2449 1623235 : GEN S = cgetg(d+2, t_VEC);
2450 1623235 : gel(S, d+1) = zerovec(n);
2451 1623235 : return S;
2452 : }
2453 :
2454 : GEN
2455 1749614 : obj_insert(GEN S, long K, GEN O)
2456 : {
2457 1749614 : GEN o, v = veclast(S);
2458 1749614 : if (typ(v) != t_VEC) pari_err_TYPE("obj_insert", S);
2459 1749614 : O = gclone(O);
2460 1749614 : if (!isonstack(v) && !is_universal_constant(v) && !is_in_block(v))
2461 : {
2462 0 : if (DEBUGLEVEL) pari_warn(warner,"trying to update parent object");
2463 0 : return O;
2464 : }
2465 1749614 : o = gel(v,K); gel(v,K) = O; /* before unclone(o) in case of SIGINT */
2466 1749614 : if (isclone(o)) gunclone(o);
2467 1749614 : return gel(v,K);
2468 : }
2469 :
2470 : /* as obj_insert. WITHOUT cloning (for libpari, when creating a *new* obj
2471 : * from an existing one) */
2472 : GEN
2473 172956 : obj_insert_shallow(GEN S, long K, GEN O)
2474 : {
2475 172956 : GEN v = veclast(S);
2476 172956 : if (typ(v) != t_VEC) pari_err_TYPE("obj_insert", S);
2477 172956 : gel(v,K) = O; return gel(v,K);
2478 : }
2479 :
2480 : /* Does S [last position] contain data at position K ? Return it, or NULL */
2481 : GEN
2482 4532913 : obj_check(GEN S, long K)
2483 : {
2484 4532913 : GEN O, v = veclast(S);
2485 4532910 : if (typ(v) != t_VEC || K >= lg(v)) pari_err_TYPE("obj_check", S);
2486 4532910 : O = gel(v,K); return isintzero(O)? NULL: O;
2487 : }
2488 :
2489 : GEN
2490 1032415 : obj_checkbuild(GEN S, long tag, GEN (*build)(GEN))
2491 : {
2492 1032415 : GEN O = obj_check(S, tag);
2493 1032415 : if (!O)
2494 896615 : { pari_sp av = avma; O = obj_insert(S, tag, build(S)); set_avma(av); }
2495 1032408 : return O;
2496 : }
2497 :
2498 : GEN
2499 541947 : obj_checkbuild_prec(GEN S, long tag, GEN (*build)(GEN,long),
2500 : long (*pr)(GEN), long prec)
2501 : {
2502 541947 : pari_sp av = avma;
2503 541947 : GEN w = obj_check(S, tag);
2504 541947 : if (!w || pr(w) < prec) w = obj_insert(S, tag, build(S, prec));
2505 541947 : set_avma(av); return gcopy(w);
2506 : }
2507 : GEN
2508 398834 : obj_checkbuild_realprec(GEN S, long tag, GEN (*build)(GEN,long), long prec)
2509 398834 : { return obj_checkbuild_prec(S,tag,build,gprecision,prec); }
2510 : GEN
2511 539 : obj_checkbuild_padicprec(GEN S, long tag, GEN (*build)(GEN,long), long prec)
2512 539 : { return obj_checkbuild_prec(S,tag,build,padicprec_relative,prec); }
2513 :
2514 : /* Reset S [last position], freeing all clones */
2515 : void
2516 186683 : obj_free(GEN S)
2517 : {
2518 186683 : GEN v = veclast(S);
2519 : long i;
2520 186683 : if (typ(v) != t_VEC) pari_err_TYPE("obj_free", S);
2521 1207948 : for (i = 1; i < lg(v); i++)
2522 : {
2523 1021265 : GEN o = gel(v,i);
2524 1021265 : gel(v,i) = gen_0;
2525 1021265 : gunclone_deep(o);
2526 : }
2527 186683 : }
2528 :
2529 : /*******************************************************************/
2530 : /* */
2531 : /* STACK MANAGEMENT */
2532 : /* */
2533 : /*******************************************************************/
2534 : INLINE void
2535 5990450466 : gc_dec(pari_sp *x, pari_sp av0, pari_sp av, pari_sp tetpil, size_t dec)
2536 : {
2537 5990450466 : if (*x < av && *x >= av0)
2538 : { /* update address if in stack */
2539 5084123656 : if (*x < tetpil) *x += dec;
2540 0 : else pari_err_BUG("gc, significant pointers lost");
2541 : }
2542 5990478655 : }
2543 :
2544 : GEN
2545 27837787 : gc_all_unsafe(pari_sp av, pari_sp tetpil, int n, ...)
2546 : {
2547 27837787 : pari_sp av0 = avma;
2548 27837787 : size_t dec = gc_stack_update(av, tetpil);
2549 : GEN *pz;
2550 : int i;
2551 : va_list a;
2552 27837619 : if (n <= 0) return NULL;
2553 27837619 : va_start(a, n);
2554 27837619 : pz = va_arg(a,GEN*);
2555 27837707 : gc_dec((pari_sp*)pz, av0,av,tetpil,dec);
2556 66343164 : for (i=1; i<n; i++) gc_dec((pari_sp*)va_arg(a,GEN*), av0,av,tetpil,dec);
2557 27837682 : va_end(a); return *pz;
2558 : }
2559 :
2560 : /* Takes a slice g[0..n-1] of GENs. Cleans up the stack between av and
2561 : * tetpil, updating those GENs. */
2562 : void
2563 323655348 : gc_slice_unsafe(pari_sp av, pari_sp tetpil, GEN g, int n)
2564 : {
2565 323655348 : pari_sp av0 = avma;
2566 323655348 : size_t dec = gc_stack_update(av, tetpil);
2567 : int i;
2568 971078897 : for (i=0; i<n; i++,g++) gc_dec((pari_sp*)g, av0, av, tetpil, dec);
2569 323693627 : }
2570 :
2571 : static int
2572 0 : dochk_gc_upto(GEN av, GEN x)
2573 : {
2574 : long i,lx,tx;
2575 0 : if (!isonstack(x)) return 1;
2576 0 : if (x > av)
2577 : {
2578 0 : pari_warn(warner,"bad object %Ps",x);
2579 0 : return 0;
2580 : }
2581 0 : tx = typ(x);
2582 0 : if (! is_recursive_t(tx)) return 1;
2583 :
2584 0 : lx = lg(x);
2585 0 : for (i=lontyp[tx]; i<lx; i++)
2586 0 : if (!dochk_gc_upto(av, gel(x,i)))
2587 : {
2588 0 : pari_warn(warner,"bad component %ld in object %Ps",i,x);
2589 0 : return 0;
2590 : }
2591 0 : return 1;
2592 : }
2593 : /* check that x and all its components are out of stack, or have been
2594 : * created after av */
2595 : int
2596 0 : chk_gc_upto(GEN x) { return dochk_gc_upto(x, x); }
2597 :
2598 : /* print stack between avma & av */
2599 : void
2600 0 : dbg_stack(pari_sp av)
2601 : {
2602 0 : GEN x = (GEN)avma;
2603 0 : while (x < (GEN)av)
2604 : {
2605 0 : const long tx = typ(x), lx = lg(x);
2606 : GEN *a;
2607 :
2608 0 : pari_printf(" [%ld] %Ps:", x - (GEN)avma, x);
2609 0 : if (! is_recursive_t(tx)) { pari_putc('\n'); x += lx; continue; }
2610 0 : a = (GEN*)x + lontyp[tx]; x += lx;
2611 0 : for ( ; a < (GEN*)x; a++)
2612 : {
2613 0 : if (*a == gen_0)
2614 0 : pari_puts(" gen_0");
2615 0 : else if (*a == gen_1)
2616 0 : pari_puts(" gen_1");
2617 0 : else if (*a == gen_m1)
2618 0 : pari_puts(" gen_m1");
2619 0 : else if (*a == gen_2)
2620 0 : pari_puts(" gen_2");
2621 0 : else if (*a == gen_m2)
2622 0 : pari_puts(" gen_m2");
2623 0 : else if (*a == ghalf)
2624 0 : pari_puts(" ghalf");
2625 0 : else if (isclone(*a))
2626 0 : pari_printf(" %Ps (clone)", *a);
2627 : else
2628 0 : pari_printf(" %Ps [%ld]", *a, *a - (GEN)avma);
2629 0 : if (a+1 < (GEN*)x) pari_putc(',');
2630 : }
2631 0 : pari_printf("\n");
2632 : }
2633 0 : }
2634 : void
2635 0 : dbg_gc_upto(GEN q)
2636 : {
2637 0 : err_printf("%Ps:\n", q);
2638 0 : dbg_stack((pari_sp) (q+lg(q)));
2639 0 : }
2640 :
2641 : /* OBSOLETE: kept for backward compatibility */
2642 : GEN
2643 0 : gerepile(pari_sp av, pari_sp tetpil, GEN q)
2644 : {
2645 0 : pari_sp av0 = avma, Q = (pari_sp)q;
2646 0 : size_t dec = gc_stack_update(av, tetpil);
2647 0 : if (Q >= av0 && Q < tetpil) Q += dec;
2648 0 : return (GEN)Q;
2649 : }
2650 :
2651 : size_t
2652 1241699978 : gc_stack_update(pari_sp av, pari_sp tetpil)
2653 : {
2654 1241699978 : const pari_sp av0 = avma;
2655 1241699978 : size_t dec = av - tetpil;
2656 : GEN x, a;
2657 :
2658 1241699978 : if (dec == 0) return 0;
2659 1033408993 : if ((long)dec < 0) pari_err(e_MISC,"lbot>ltop in gc");
2660 31379227005 : for (x = (GEN)av, a = (GEN)tetpil; a > (GEN)av0; ) *--x = *--a;
2661 1033837435 : set_avma((pari_sp)x);
2662 6896149535 : while (x < (GEN)av)
2663 : {
2664 5861965690 : const long tx = typ(x), lx = lg(x);
2665 5861965690 : if (! is_recursive_t(tx)) { x += lx; continue; }
2666 1279883279 : a = x + lontyp[tx]; x += lx;
2667 6557573768 : for ( ; a < x; a++) gc_dec((pari_sp*)a, av0, av, tetpil, dec);
2668 : }
2669 1034183845 : return dec;
2670 : }
2671 :
2672 : void
2673 0 : dbg_fill_stack(void)
2674 : {
2675 : #ifdef LONG_IS_64BIT
2676 0 : const long JUNK = 0xBADC0FFEE0DDF00D;
2677 : #else
2678 0 : const long JUNK = 0xDEADBEEF;
2679 : #endif
2680 0 : GEN x = ((GEN)pari_mainstack->bot);
2681 0 : while (x < (GEN)avma) *x++ = JUNK;
2682 0 : }
2683 :
2684 : void
2685 0 : debug_stack(void)
2686 : {
2687 0 : pari_sp top = pari_mainstack->top, bot = pari_mainstack->bot;
2688 : GEN z;
2689 0 : err_printf("bot=0x%lx\ttop=0x%lx\tavma=0x%lx\n", bot, top, avma);
2690 0 : for (z = ((GEN)top)-1; z >= (GEN)avma; z--)
2691 0 : err_printf("%p:\t0x%lx\t%lu\n",z,*z,*z);
2692 0 : }
2693 :
2694 : void
2695 335701 : setdebugvar(long n) { DEBUGVAR=n; }
2696 :
2697 : long
2698 336044 : getdebugvar(void) { return DEBUGVAR; }
2699 :
2700 : long
2701 7 : getstack(void) { return pari_mainstack->top-avma; }
2702 :
2703 : /*******************************************************************/
2704 : /* */
2705 : /* timer_delay */
2706 : /* */
2707 : /*******************************************************************/
2708 :
2709 : #if defined(USE_CLOCK_GETTIME)
2710 : #if defined(_POSIX_THREAD_CPUTIME)
2711 : static THREAD clockid_t time_type = CLOCK_THREAD_CPUTIME_ID;
2712 : #else
2713 : static const THREAD clockid_t time_type = CLOCK_PROCESS_CPUTIME_ID;
2714 : #endif
2715 : static void
2716 : pari_init_timer(void)
2717 : {
2718 : #if defined(_POSIX_THREAD_CPUTIME)
2719 : time_type = CLOCK_PROCESS_CPUTIME_ID;
2720 : #endif
2721 : }
2722 :
2723 : void
2724 : timer_start(pari_timer *T)
2725 : {
2726 : struct timespec t;
2727 : clock_gettime(time_type,&t);
2728 : T->us = t.tv_nsec / 1000;
2729 : T->s = t.tv_sec;
2730 : }
2731 : #elif defined(USE_GETRUSAGE)
2732 : #ifdef RUSAGE_THREAD
2733 : static THREAD int rusage_type = RUSAGE_THREAD;
2734 : #else
2735 : static const THREAD int rusage_type = RUSAGE_SELF;
2736 : #endif /*RUSAGE_THREAD*/
2737 : static void
2738 1900 : pari_init_timer(void)
2739 : {
2740 : #ifdef RUSAGE_THREAD
2741 1900 : rusage_type = RUSAGE_SELF;
2742 : #endif
2743 1900 : }
2744 :
2745 : void
2746 354604 : timer_start(pari_timer *T)
2747 : {
2748 : struct rusage r;
2749 354604 : getrusage(rusage_type,&r);
2750 354604 : T->us = r.ru_utime.tv_usec;
2751 354604 : T->s = r.ru_utime.tv_sec;
2752 354604 : }
2753 : #elif defined(USE_FTIME)
2754 :
2755 : static void
2756 : pari_init_timer(void) { }
2757 :
2758 : void
2759 : timer_start(pari_timer *T)
2760 : {
2761 : struct timeb t;
2762 : ftime(&t);
2763 : T->us = ((long)t.millitm) * 1000;
2764 : T->s = t.time;
2765 : }
2766 :
2767 : #else
2768 :
2769 : static void
2770 : _get_time(pari_timer *T, long Ticks, long TickPerSecond)
2771 : {
2772 : T->us = (long) ((Ticks % TickPerSecond) * (1000000. / TickPerSecond));
2773 : T->s = Ticks / TickPerSecond;
2774 : }
2775 :
2776 : # ifdef USE_TIMES
2777 : static void
2778 : pari_init_timer(void) { }
2779 :
2780 : void
2781 : timer_start(pari_timer *T)
2782 : {
2783 : # ifdef _SC_CLK_TCK
2784 : long tck = sysconf(_SC_CLK_TCK);
2785 : # else
2786 : long tck = CLK_TCK;
2787 : # endif
2788 : struct tms t; times(&t);
2789 : _get_time(T, t.tms_utime, tck);
2790 : }
2791 : # elif defined(_WIN32)
2792 : static void
2793 : pari_init_timer(void) { }
2794 :
2795 : void
2796 : timer_start(pari_timer *T)
2797 : { _get_time(T, win32_timer(), 1000); }
2798 : # else
2799 : # include <time.h>
2800 : # ifndef CLOCKS_PER_SEC
2801 : # define CLOCKS_PER_SEC 1000000 /* may be false on YOUR system */
2802 : # endif
2803 : static void
2804 : pari_init_timer(void) { }
2805 :
2806 : void
2807 : timer_start(pari_timer *T)
2808 : { _get_time(T, clock(), CLOCKS_PER_SEC); }
2809 : # endif
2810 : #endif
2811 :
2812 : /* round microseconds to milliseconds */
2813 : static long
2814 218836 : rndus(long x) { return (x + 500) / 1000; }
2815 : static long
2816 218828 : timer_aux(pari_timer *T, pari_timer *U, void (*settime)(pari_timer *))
2817 : {
2818 218828 : long s = T->s, us = T->us;
2819 218828 : settime(U); return 1000 * (U->s - s) + rndus(U->us - us);
2820 : }
2821 :
2822 : /* return delay, set timer checkpoint */
2823 : long
2824 110314 : timer_delay(pari_timer *T) { return timer_aux(T, T, &timer_start); }
2825 : /* return delay, don't set checkpoint */
2826 : long
2827 1918 : timer_get(pari_timer *T) {pari_timer t; return timer_aux(T, &t, &timer_start);}
2828 :
2829 : static void
2830 0 : timer_vprintf(pari_timer *T, const char *format, va_list args)
2831 : {
2832 0 : out_puts(pariErr, "Time ");
2833 0 : out_vprintf(pariErr, format,args);
2834 0 : out_printf(pariErr, ": %ld\n", timer_delay(T));
2835 0 : pariErr->flush();
2836 0 : }
2837 : void
2838 0 : timer_printf(pari_timer *T, const char *format, ...)
2839 : {
2840 0 : va_list args; va_start(args, format);
2841 0 : timer_vprintf(T, format, args);
2842 0 : va_end(args);
2843 0 : }
2844 :
2845 : long
2846 0 : timer(void) { static THREAD pari_timer T; return timer_delay(&T);}
2847 : long
2848 3718 : gettime(void) { static THREAD pari_timer T; return timer_delay(&T);}
2849 :
2850 : static THREAD pari_timer timer2_T, abstimer_T;
2851 : long
2852 0 : timer2(void) { return timer_delay(&timer2_T);}
2853 : void
2854 0 : msgtimer(const char *format, ...)
2855 : {
2856 0 : va_list args; va_start(args, format);
2857 0 : timer_vprintf(&timer2_T, format, args);
2858 0 : va_end(args);
2859 0 : }
2860 : long
2861 1912 : getabstime(void) { return timer_get(&abstimer_T);}
2862 :
2863 : void
2864 250087 : walltimer_start(pari_timer *ti)
2865 : {
2866 : #if defined(USE_CLOCK_GETTIME)
2867 : struct timespec t;
2868 : if (!clock_gettime(CLOCK_REALTIME,&t))
2869 : { ti->s = t.tv_sec; ti->us = rndus(t.tv_nsec); return; }
2870 : #elif defined(USE_GETTIMEOFDAY)
2871 : struct timeval tv;
2872 250087 : if (!gettimeofday(&tv, NULL))
2873 250087 : { ti->s = tv.tv_sec; ti->us = tv.tv_usec; return; }
2874 : #elif defined(USE_FTIMEFORWALLTIME)
2875 : struct timeb tp;
2876 : if (!ftime(&tp))
2877 : { ti->s = tp.time; ti->us = tp.millitm*1000; return; }
2878 : #endif
2879 0 : timer_start(ti);
2880 : }
2881 : /* return delay, set timer checkpoint */
2882 : long
2883 106596 : walltimer_delay(pari_timer *T) { return timer_aux(T, T, &walltimer_start); }
2884 : /* return delay, don't set checkpoint */
2885 : long
2886 0 : walltimer_get(pari_timer *T)
2887 : {
2888 : pari_timer t;
2889 0 : return timer_aux(T, &t, &walltimer_start);
2890 : }
2891 :
2892 : static GEN
2893 8 : timetoi(ulong s, ulong m)
2894 : {
2895 8 : pari_sp av = avma;
2896 8 : return gc_INT(av, addiu(muluu(s, 1000), m));
2897 : }
2898 : GEN
2899 8 : getwalltime(void)
2900 : {
2901 : pari_timer ti;
2902 8 : walltimer_start(&ti);
2903 8 : return timetoi(ti.s, rndus(ti.us));
2904 : }
2905 :
2906 : /*******************************************************************/
2907 : /* */
2908 : /* FUNCTIONS KNOWN TO THE ANALYZER */
2909 : /* */
2910 : /*******************************************************************/
2911 :
2912 : GEN
2913 127 : setdebug(const char *s, long n)
2914 : {
2915 127 : long i, l = numberof(pari_DEBUGLEVEL_str);
2916 : GEN V, V1, V2;
2917 127 : if (s)
2918 : {
2919 120 : if (n > 20)
2920 0 : pari_err_DOMAIN("setdebug", "n", ">", utoipos(20), stoi(n));
2921 2276 : for (i = 0; i < l; i++)
2922 2248 : if (!strcmp(s, pari_DEBUGLEVEL_str[i])) break;
2923 120 : if (i == l)
2924 28 : pari_err_DOMAIN("setdebug", s, "not a valid",
2925 : strtoGENstr("debug domain"), strtoGENstr(s));
2926 92 : if (n >= 0) { *pari_DEBUGLEVEL_ptr[i] = n; return gnil; }
2927 42 : return stoi(*pari_DEBUGLEVEL_ptr[i]);
2928 : }
2929 7 : V = cgetg(3,t_MAT);
2930 7 : V1 = gel(V,1) = cgetg(l+1, t_COL);
2931 7 : V2 = gel(V,2) = cgetg(l+1, t_COL);
2932 427 : for (i = 0; i < l; i++)
2933 : {
2934 420 : gel(V1, i+1) = strtoGENstr(pari_DEBUGLEVEL_str[i]);
2935 420 : gel(V2, i+1) = stoi(*pari_DEBUGLEVEL_ptr[i]);
2936 : }
2937 7 : return V;
2938 : }
2939 :
2940 : GEN
2941 7 : pari_version(void)
2942 : {
2943 7 : const ulong mask = (1UL<<PARI_VERSION_SHIFT) - 1;
2944 7 : ulong major, minor, patch, n = paricfg_version_code;
2945 7 : patch = n & mask; n >>= PARI_VERSION_SHIFT;
2946 7 : minor = n & mask; n >>= PARI_VERSION_SHIFT;
2947 7 : major = n;
2948 7 : if (*paricfg_vcsversion) {
2949 7 : const char *ver = paricfg_vcsversion;
2950 7 : const char *s = strchr(ver, '-');
2951 : char t[8];
2952 7 : const long len = s-ver;
2953 : GEN v;
2954 7 : if (!s || len > 6) pari_err_BUG("pari_version()"); /* paranoia */
2955 7 : memcpy(t, ver, len); t[len] = 0;
2956 7 : v = cgetg(6, t_VEC);
2957 7 : gel(v,1) = utoi(major);
2958 7 : gel(v,2) = utoi(minor);
2959 7 : gel(v,3) = utoi(patch);
2960 7 : gel(v,4) = stoi( atoi(t) );
2961 7 : gel(v,5) = strtoGENstr(s+1);
2962 7 : return v;
2963 : } else {
2964 0 : GEN v = cgetg(4, t_VEC);
2965 0 : gel(v,1) = utoi(major);
2966 0 : gel(v,2) = utoi(minor);
2967 0 : gel(v,3) = utoi(patch);
2968 0 : return v;
2969 : }
2970 : }
2971 :
2972 : /* List of GP functions: generated from the description system.
2973 : * Format (struct entree) :
2974 : * char *name : name (under GP).
2975 : * ulong valence: (EpNEW, EpALIAS,EpVAR, EpINSTALL)|EpSTATIC
2976 : * void *value : For PREDEFINED FUNCTIONS: C function to call.
2977 : * For USER FUNCTIONS: pointer to defining data (block) =
2978 : * entree*: NULL, list of entree (arguments), NULL
2979 : * char* : function text
2980 : * long menu : which help section do we belong to
2981 : * 1: Standard monadic or dyadic OPERATORS
2982 : * 2: CONVERSIONS and similar elementary functions
2983 : * 3: functions related to COMBINATORICS
2984 : * 4: TRANSCENDENTAL functions, etc.
2985 : * char *code : GP prototype, aka Parser Code (see libpari's manual)
2986 : * if NULL, use valence instead.
2987 : * char *help : short help text (init to NULL).
2988 : * void *pvalue : push_val history.
2989 : * long arity : maximum number of arguments.
2990 : * entree *next : next entree (init to NULL, used in hashing code). */
2991 : #include "init.h"
2992 : #include "default.h"
|