1 /* Copyright (C) 2000  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 /* This file contains memory and I/O management definitions       */
16 
17 typedef struct {
18   long s, us;
19 } pari_timer;
20 
21 typedef struct pari_str {
22   char *string; /* start of the output buffer */
23   char *end;    /* end of the output buffer */
24   char *cur;   /* current writing place in the output buffer */
25   size_t size; /* buffer size */
26   int use_stack; /* use stack_malloc instead of malloc ? */
27 } pari_str;
28 
29 typedef unsigned char *byteptr;
30 typedef ulong pari_sp;
31 
32 struct pari_sieve
33 {
34   ulong start, end, maxpos;
35   ulong c, q;
36   unsigned char* sieve;
37 };
38 
39 /* iterator over primes */
40 typedef struct {
41   int strategy; /* 1 to 4 */
42   GEN bb; /* iterate through primes <= bb */
43   ulong c, q; /* congruent to c (mod q) */
44 
45   /* strategy 1: private prime table */
46   byteptr d; /* diffptr + n */
47   ulong p; /* current p = n-th prime */
48   ulong b; /* min(bb, ULONG_MAX) */
49 
50   /* strategy 2: sieve, use p */
51   struct pari_sieve *psieve;
52   unsigned char *sieve, *isieve;
53   ulong cache[9]; /* look-ahead primes already computed */
54   ulong chunk; /* # of odd integers in sieve */
55   ulong a, end, sieveb; /* [a,end] interval currently being sieved,
56                          * end <= sieveb = min(bb, maxprime^2, ULONG_MAX) */
57   ulong pos, maxpos; /* current cell and max cell */
58 
59   /* strategy 3: unextprime, use p */
60 
61   /* strategy 4: nextprime */
62   GEN pp;
63 } forprime_t;
64 
65 typedef struct {
66   int first;
67   GEN b, n, p;
68   forprime_t T;
69 } forcomposite_t;
70 
71 typedef struct forvec_t {
72   long first;
73   GEN *a, *m, *M; /* current n-uplet, minima, Maxima */
74   long n; /* length */
75   GEN (*next)(struct forvec_t *);
76 } forvec_t;
77 
78 /* Iterate over partitions */
79 typedef struct
80 {
81   long k;
82   long amax, amin, nmin, nmax, strip;
83   GEN v;
84 } forpart_t;
85 
86 /* Iterate over permutations */
87 typedef struct
88 {
89   long k, first;
90   GEN v;
91 } forperm_t;
92 
93 /* Iterate over subsets */
94 typedef struct
95 {
96   long n, k, all, first;
97   GEN v;
98 } forsubset_t;
99 
100 /* Plot engines */
101 typedef struct PARI_plot {
102   void (*draw)(struct PARI_plot *T, GEN w, GEN x, GEN y);
103   long width;
104   long height;
105   long hunit;
106   long vunit;
107   long fwidth;
108   long fheight;
109   long dwidth;
110   long dheight;
111 } PARI_plot;
112 
113 /* binary I/O */
114 typedef struct GENbin {
115   size_t len; /* gsizeword(x) */
116   GEN x; /* binary copy of x */
117   GEN base; /* base address of p->x */
118   void (*rebase)(GEN,long);
119 } GENbin;
120 
121 struct pari_mainstack
122 {
123   pari_sp top, bot, vbot;
124   size_t size, rsize, vsize, memused;
125 };
126 
127 typedef struct pariFILE {
128   FILE *file;
129   int type;
130   const char *name;
131   struct pariFILE* prev;
132   struct pariFILE* next;
133 } pariFILE;
134 /* pariFILE.type */
135 enum { mf_IN  = 1, mf_PIPE = 2, mf_FALSE = 4, mf_OUT = 8, mf_PERM = 16 };
136 
137 typedef struct entree {
138   const char *name;
139   ulong valence;
140   void *value;
141   long menu;
142   const char *code;
143   const char *help;
144   void *pvalue;
145   long arity;
146   ulong hash;
147   struct entree *next;
148 } entree;
149 
150 struct pari_parsestate
151 {
152   long node;
153   int once;
154   long discarded;
155   const char *lex_start;
156   GEN lasterror;
157 };
158 
159 struct pari_compilestate
160 {
161   long opcode, operand, accesslex, data, localvars, frames, dbginfo;
162   long offset, nblex;
163   const char *dbgstart;
164 };
165 
166 struct pari_mtstate
167 {
168   long pending_threads;
169   long is_thread;
170   long trace_level;
171 };
172 
173 struct pari_evalstate
174 {
175   pari_sp avma;
176   long sp;
177   long rp;
178   long var;
179   long lvars;
180   long locks;
181   long prec;
182   long trace;
183   struct pari_mtstate mt;
184   struct pari_compilestate comp;
185 };
186 
187 struct pari_varstate
188 {
189   long nvar, max_avail, min_priority, max_priority;
190 };
191 
192 struct pari_filestate
193 {
194   pariFILE *file;
195   long serial;
196 };
197 
198 struct gp_context
199 {
200   long listloc;
201   long prettyp;
202   struct pari_varstate var;
203   struct pari_evalstate eval;
204   struct pari_parsestate parse;
205   struct pari_filestate file;
206   jmp_buf *iferr_env;
207   GEN err_data;
208 };
209 
210 extern THREAD struct pari_mainstack *pari_mainstack;
211 
212 struct pari_global_state
213 {
214   long bitprec;
215   GEN primetab;
216   GEN seadata;
217   long *varpriority;
218   struct pari_varstate varstate;
219 };
220 
221 struct pari_thread
222 {
223   struct pari_mainstack st;
224   struct pari_global_state gs;
225   GEN data;
226 };
227 
228 struct mt_state
229 {
230   GEN worker;
231   GEN pending;
232   long workid;
233 };
234 
235 struct pari_mt
236 {
237   struct mt_state mt;
238   GEN (*get)(struct mt_state *mt, long *workid, long *pending);
239   void (*submit)(struct mt_state *mt, long workid, GEN work);
240   void (*end)(void);
241 };
242 
243 struct parfor_iter
244 {
245   long pending;
246   GEN worker;
247   struct pari_mt pt;
248 };
249 
250 typedef struct
251 {
252   GEN a, b;
253   struct parfor_iter iter;
254 } parfor_t;
255 
256 typedef struct
257 {
258   GEN x, W;
259   long i, l;
260   struct parfor_iter iter;
261 } parforeach_t;
262 
263 typedef struct
264 {
265   GEN v;
266   forprime_t forprime;
267   struct parfor_iter iter;
268 } parforprime_t;
269 
270 typedef struct
271 {
272   GEN v;
273   forvec_t forvec;
274   struct parfor_iter iter;
275 } parforvec_t;
276 
277 typedef struct PariOUT {
278   void (*putch)(char);
279   void (*puts)(const char*);
280   void (*flush)(void);
281 } PariOUT;
282 
283 /* hashtables */
284 typedef struct hashentry {
285   void *key, *val;
286   ulong hash; /* hash(key) */
287   struct hashentry *next;
288 } hashentry;
289 
290 typedef struct hashtable {
291   ulong len; /* table length */
292   hashentry **table; /* the table */
293   ulong nb, maxnb; /* number of entries stored and max nb before enlarging */
294   ulong pindex; /* prime index */
295   ulong (*hash) (void *k); /* hash function */
296   int (*eq) (void *k1, void *k2); /* equality test */
297   int use_stack; /* use stack_malloc instead of malloc ? */
298 } hashtable;
299 
300 typedef struct {
301   void **data;
302   long n;
303   long alloc;
304   size_t size;
305 } pari_stack;
306 
307 /* GP_DATA */
308 typedef struct {
309   GEN z; /* result */
310   time_t t; /* time to obtain result */
311   time_t r; /* realtime to obtain result */
312 } gp_hist_cell;
313 typedef struct {
314   gp_hist_cell *v; /* array of previous results, FIFO */
315   size_t size; /* # res */
316   ulong total; /* # of results computed since big bang */
317 } gp_hist; /* history */
318 
319 typedef struct {
320   pariFILE *file;
321   char *cmd;
322 } gp_pp; /* prettyprinter */
323 
324 typedef struct {
325   char *PATH;
326   char **dirs;
327 } gp_path; /* path */
328 typedef struct {
329   char format; /* e,f,g */
330   long sigd;   /* -1 (all) or number of significant digits printed */
331   int sp;      /* 0 = suppress whitespace from output */
332   int prettyp; /* output style: raw, prettyprint, etc */
333   int TeXstyle;
334 } pariout_t; /* output format */
335 
336 enum { gpd_QUIET=1, gpd_TEST=2, gpd_EMACS=256, gpd_TEXMACS=512};
337 enum { DO_MATCHED_INSERT = 2, DO_ARGS_COMPLETE = 4 };
338 typedef struct {
339   gp_hist *hist;
340   gp_pp *pp;
341   gp_path *path, *sopath;
342   pariout_t *fmt;
343   ulong lim_lines, flags, linewrap, readline_state, echo;
344   int breakloop, recover, use_readline;
345   char *help, *histfile, *prompt, *prompt_cont, *prompt_comment;
346   GEN colormap, graphcolors, plothsizes;
347 
348   int secure, simplify, strictmatch, strictargs, chrono;
349   pari_timer *T, *Tw;
350   ulong primelimit; /* deprecated */
351   ulong threadsizemax, threadsize;
352 } gp_data;
353 extern gp_data *GP_DATA;
354 
355 /* Common global variables: */
356 
357 extern PariOUT *pariOut, *pariErr;
358 extern FILE    *pari_outfile, *pari_logfile, *pari_infile, *pari_errfile;
359 extern ulong    pari_logstyle;
360 
361 enum pari_logstyles {
362     logstyle_none,        /* 0 */
363     logstyle_plain,        /* 1 */
364     logstyle_color,        /* 2 */
365     logstyle_TeX         /* 3 */
366 };
367 
368 enum { c_ERR, c_HIST, c_PROMPT, c_INPUT, c_OUTPUT, c_HELP, c_TIME, c_LAST,
369        c_NONE = 0xffffUL };
370 
371 enum { TEXSTYLE_PAREN=2, TEXSTYLE_BREAK=4 };
372 
373 extern THREAD pari_sp avma;
374 #define DISABLE_MEMUSED (size_t)-1
375 extern byteptr diffptr;
376 extern char *current_psfile, *pari_datadir;
377 
378 #define gcopyifstack(x,y)  STMT_START {pari_sp _t=(pari_sp)(x); \
379   (y)=(_t>=pari_mainstack->bot &&_t<pari_mainstack->top)? \
380       gcopy((GEN)_t): (GEN)_t;} STMT_END
381 #define copyifstack(x,y)  STMT_START {pari_sp _t=(pari_sp)(x); \
382   (y)=(_t>=pari_mainstack->bot &&_t<pari_mainstack->top)? \
383       gcopy((GEN)_t): (GEN)_t;} STMT_END
384 #define icopyifstack(x,y) STMT_START {pari_sp _t=(pari_sp)(x); \
385   (y)=(_t>=pari_mainstack->bot &&_t<pari_mainstack->top)? \
386       icopy((GEN)_t): (GEN)_t;} STMT_END
387 
388 /* Define this to thoroughly check "random" garbage collecting */
389 #ifdef DEBUG_LOWSTACK
390 #  define low_stack(x,l) 1
391 #  define gc_needed(av,n) 1
392 #else
393 #  define low_stack(x,l) ((void)(x),avma < (pari_sp)(l))
394 #  define gc_needed(av,n) (avma < (pari_sp)stack_lim(av,n))
395 #endif
396 
397 #define stack_lim(av,n) (pari_mainstack->bot+(((av)-pari_mainstack->bot)>>(n)))
398 
399 #ifndef SIG_IGN
400 #  define SIG_IGN (void(*)())1
401 #endif
402 #ifndef SIGINT
403 #  define SIGINT 2
404 #endif
405