1 % $Id: psout.w 616 2008-07-09 14:05:55Z taco $
3 % Copyright 2008 Taco Hoekwater.
5 % This program is free software: you can redistribute it and/or modify
6 % it under the terms of the GNU General Public License as published by
7 % the Free Software Foundation, either version 2 of the License, or
8 % (at your option) any later version.
10 % This program is distributed in the hope that it will be useful,
11 % but WITHOUT ANY WARRANTY; without even the implied warranty of
12 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 % GNU General Public License for more details.
15 % You should have received a copy of the GNU General Public License
16 % along with this program. If not, see <http://www.gnu.org/licenses/>.
18 % TeX is a trademark of the American Mathematical Society.
19 % METAFONT is a trademark of Addison-Wesley Publishing Company.
20 % PostScript is a trademark of Adobe Systems Incorporated.
22 % Here is TeX material that gets inserted after \input webmac
24 \def\title{MetaPost MEM reading and writing}
25 \def\topofcontents{\hsize 5.5in
26 \vglue -30pt plus 1fil minus 1.5in
27 \def\?##1]{\hbox to 1in{\hfil##1.\ }}
29 \def\botofcontents{\vskip 0pt plus 1fil minus 1.5in}
33 @ As usual, need true and false.
37 @d null 0 /* the null pointer */
38 @d incr(A) (A)=(A)+1 /* increase a variable by unity */
39 @d decr(A) (A)=(A)-1 /* decrease a variable by unity */
40 @d qo(A) (A) /* to read eight bits from a quarterword */
41 @d qi(A) (quarterword)(A) /* to store eight bits in a quarterword */
42 @d max_quarterword 0x3FFF /* largest allowable value in a |quarterword| */
43 @d max_halfword 0xFFFFFFF /* largest allowable value in a |halfword| */
44 @d max_str_ref 127 /* ``infinite'' number of references */
54 @ @c void mp_store_mem_file (MP mp) {
55 integer k; /* all-purpose index */
56 pointer p,q; /* all-purpose pointers */
57 integer x; /* something to dump */
58 four_quarters w; /* four ASCII codes */
60 @<Create the |mem_ident|, open the mem file,
61 and inform the user that dumping has begun@>;
62 @<Dump constants for consistency check@>;
63 @<Dump the string pool@>;
64 @<Dump the dynamic memory@>;
65 @<Dump the table of equivalents and the hash table@>;
66 @<Dump a few more things and the closing check word@>;
67 @<Close the mem file@>;
70 @ Corresponding to the procedure that dumps a mem file, we also have a function
71 that reads~one~in. The function returns |false| if the dumped mem is
72 incompatible with the present \MP\ table sizes, etc.
74 @d too_small(A) { wake_up_terminal;
75 wterm_ln("---! Must increase the "); wterm((A));
76 @.Must increase the x@>
81 boolean mp_load_mem_file (MP mp) {
82 integer k; /* all-purpose index */
83 pointer p,q; /* all-purpose pointers */
84 integer x; /* something undumped */
85 str_number s; /* some temporary string */
86 four_quarters w; /* four ASCII codes */
88 @<Undump the string pool@>;
89 @<Undump the dynamic memory@>;
90 @<Undump the table of equivalents and the hash table@>;
91 @<Undump a few more things and the closing check word@>;
92 return true; /* it worked! */
95 wterm_ln("(Fatal mem file error; I'm stymied)\n");
96 @.Fatal mem file error@>
100 @ Mem files consist of |memory_word| items, and we use the following
101 macros to dump words of different types:
103 @d dump_wd(A) { WW=(A); (mp->write_binary_file)(mp,mp->mem_file,&WW,sizeof(WW)); }
104 @d dump_int(A) { int cint=(A); (mp->write_binary_file)(mp,mp->mem_file,&cint,sizeof(cint)); }
105 @d dump_hh(A) { WW.hh=(A); (mp->write_binary_file)(mp,mp->mem_file,&WW,sizeof(WW)); }
106 @d dump_qqqq(A) { WW.qqqq=(A); (mp->write_binary_file)(mp,mp->mem_file,&WW,sizeof(WW)); }
107 @d dump_string(A) { dump_int((int)(strlen(A)+1));
108 (mp->write_binary_file)(mp,mp->mem_file,A,strlen(A)+1); }
110 @ The inverse macros are slightly more complicated, since we need to check
111 the range of the values we are reading in. We say `|undump(a)(b)(x)|' to
112 read an integer value |x| that is supposed to be in the range |a<=x<=b|.
115 size_t wanted = sizeof(A);
117 (mp->read_binary_file)(mp, mp->mem_file,&A_ptr,&wanted);
118 if (wanted!=sizeof(A)) goto OFF_BASE;
122 size_t wanted = sizeof(A);
124 (mp->read_binary_file)(mp, mp->mem_file,&A_ptr,&wanted);
125 if (wanted!=sizeof(A)) goto OFF_BASE;
128 @d undump_wd(A) { mgetw(WW); A=WW; }
129 @d undump_int(A) { int cint; mgeti(cint); A=cint; }
130 @d undump_hh(A) { mgetw(WW); A=WW.hh; }
131 @d undump_qqqq(A) { mgetw(WW); A=WW.qqqq; }
132 @d undump_strings(A,B,C) {
133 undump_int(x); if ( (x<(A)) || (x>(B)) ) goto OFF_BASE; else C=str(x); }
134 @d undump(A,B,C) { undump_int(x);
135 if ( (x<(A)) || (x>(int)(B)) ) goto OFF_BASE; else C=x; }
136 @d undump_size(A,B,C,D) { undump_int(x);
137 if (x<(A)) goto OFF_BASE;
138 if (x>(B)) too_small((C)); else D=x; }
139 @d undump_string(A) {
144 the_wanted = (size_t)XX;
145 the_string = mp_xmalloc(mp,(size_t)XX,1);
146 (mp->read_binary_file)(mp,mp->mem_file,&the_string,&the_wanted);
147 A = (char *)the_string;
148 if (the_wanted!=(size_t)XX) goto OFF_BASE;
151 @ The next few sections of the program should make it clear how we use the
154 @<Dump constants for consistency check@>=
155 x = metapost_magic; dump_int(x);
156 dump_int(mp->mem_top);
157 dump_int((integer)mp->hash_size);
158 dump_int(mp->hash_prime)
159 dump_int(mp->param_size);
160 dump_int(mp->max_in_open);
162 @ Sections of a \.{WEB} program that are ``commented out'' still contribute
163 strings to the string pool; therefore \.{INIMP} and \MP\ will have
164 the same strings. (And it is, of course, a good thing that they do.)
169 boolean mp_undump_constants (MP mp) {
172 if (x!=metapost_magic) return false;
173 undump_int(x); mp->mem_top = x;
174 undump_int(x); mp->hash_size = (unsigned)x;
175 undump_int(x); mp->hash_prime = x;
176 undump_int(x); mp->param_size = x;
177 undump_int(x); mp->max_in_open = x;
183 @ We do string pool compaction to avoid dumping unused strings.
186 w.b0=qi(mp->str_pool[k]); w.b1=qi(mp->str_pool[k+1]);
187 w.b2=qi(mp->str_pool[k+2]); w.b3=qi(mp->str_pool[k+3]);
190 @<Dump the string pool@>=
191 mp_do_compaction(mp, mp->pool_size);
192 dump_int(mp->pool_ptr);
193 dump_int(mp->max_str_ptr);
194 dump_int(mp->str_ptr);
196 while ( (mp->next_str[k]==k+1) && (k<=mp->max_str_ptr) )
199 while ( k<=mp->max_str_ptr ) {
200 dump_int(mp->next_str[k]); incr(k);
204 dump_int(mp->str_start[k]); /* TODO: valgrind warning here */
205 if ( k==mp->str_ptr ) {
212 while (k+4<mp->pool_ptr ) {
213 dump_four_ASCII; k=k+4;
215 k=mp->pool_ptr-4; dump_four_ASCII;
216 mp_print_ln(mp); mp_print(mp, "at most "); mp_print_int(mp, mp->max_str_ptr);
217 mp_print(mp, " strings of total length ");
218 mp_print_int(mp, mp->pool_ptr)
220 @ @d undump_four_ASCII
222 mp->str_pool[k]=(ASCII_code)qo(w.b0); mp->str_pool[k+1]=(ASCII_code)qo(w.b1);
223 mp->str_pool[k+2]=(ASCII_code)qo(w.b2); mp->str_pool[k+3]=(ASCII_code)qo(w.b3)
225 @<Undump the string pool@>=
226 undump_int(mp->pool_ptr);
227 mp_reallocate_pool(mp, mp->pool_ptr) ;
228 undump_int(mp->max_str_ptr);
229 mp_reallocate_strings (mp,mp->max_str_ptr) ;
230 undump(0,mp->max_str_ptr,mp->str_ptr);
231 undump(0,mp->max_str_ptr+1,s);
234 for (k=s;k<=mp->max_str_ptr;k++)
235 undump(s+1,mp->max_str_ptr+1,mp->next_str[k]);
239 undump(0,mp->pool_ptr,mp->str_start[k]);
240 if ( k==mp->str_ptr ) break;
241 mp->str_ref[k]=max_str_ref;
242 incr(mp->fixed_str_use);
243 mp->last_fixed_str=k; k=mp->next_str[k];
246 while ( k+4<mp->pool_ptr ) {
247 undump_four_ASCII; k=k+4;
249 k=mp->pool_ptr-4; undump_four_ASCII;
250 mp->init_str_use=mp->fixed_str_use; mp->init_pool_ptr=mp->pool_ptr;
251 mp->max_pool_ptr=mp->pool_ptr;
252 mp->strs_used_up=mp->fixed_str_use;
253 mp->pool_in_use=mp->str_start[mp->str_ptr]; mp->strs_in_use=mp->fixed_str_use;
254 mp->max_pl_used=mp->pool_in_use; mp->max_strs_used=mp->strs_in_use;
255 mp->pact_count=0; mp->pact_chars=0; mp->pact_strs=0;
257 @ By sorting the list of available spaces in the variable-size portion of
258 |mem|, we are usually able to get by without having to dump very much
259 of the dynamic memory.
261 We recompute |var_used| and |dyn_used|, so that \.{INIMP} dumps valid
262 information even when it has not been gathering statistics.
264 @<Dump the dynamic memory@>=
265 mp_sort_avail(mp); mp->var_used=0;
266 dump_int(mp->lo_mem_max); dump_int(mp->rover);
267 p=0; q=mp->rover; x=0;
269 for (k=p;k<= q+1;k++)
271 x=x+q+2-p; mp->var_used=mp->var_used+q-p;
272 p=q+node_size(q); q=rmp_link(q);
273 } while (q!=mp->rover);
274 mp->var_used=mp->var_used+mp->lo_mem_max-p;
275 mp->dyn_used=mp->mem_end+1-mp->hi_mem_min;
276 for (k=p;k<= mp->lo_mem_max;k++ )
278 x=x+mp->lo_mem_max+1-p;
279 dump_int(mp->hi_mem_min); dump_int(mp->avail);
280 for (k=mp->hi_mem_min;k<=mp->mem_end;k++ )
282 x=x+mp->mem_end+1-mp->hi_mem_min;
285 decr(mp->dyn_used); p=mp_link(p);
287 dump_int(mp->var_used); dump_int(mp->dyn_used);
288 mp_print_ln(mp); mp_print_int(mp, x);
289 mp_print(mp, " memory locations dumped; current usage is ");
290 mp_print_int(mp, mp->var_used); mp_print_char(mp, xord('&')); mp_print_int(mp, mp->dyn_used)
292 @ @<Undump the dynamic memory@>=
293 undump(lo_mem_stat_max+1000,hi_mem_stat_min-1,mp->lo_mem_max);
294 undump(lo_mem_stat_max+1,mp->lo_mem_max,mp->rover);
297 for (k=p;k<= q+1; k++)
298 undump_wd(mp->mem[k]);
300 if ( (p>mp->lo_mem_max)||((q>=rmp_link(q))&&(rmp_link(q)!=mp->rover)) )
303 } while (q!=mp->rover);
304 for (k=p;k<=mp->lo_mem_max;k++ )
305 undump_wd(mp->mem[k]);
306 undump(mp->lo_mem_max+1,hi_mem_stat_min,mp->hi_mem_min);
307 undump(null,mp->mem_top,mp->avail); mp->mem_end=mp->mem_top;
308 mp->last_pending=spec_head;
309 for (k=mp->hi_mem_min;k<= mp->mem_end;k++)
310 undump_wd(mp->mem[k]);
311 undump_int(mp->var_used); undump_int(mp->dyn_used)
313 @ A different scheme is used to compress the hash table, since its lower region
314 is usually sparse. When |text(p)<>0| for |p<=hash_used|, we output three
315 words: |p|, |hash[p]|, and |eqtb[p]|. The hash table is, of course, densely
316 packed for |p>=hash_used|, so the remaining entries are output in~a~block.
318 @<Dump the table of equivalents and the hash table@>=
319 dump_int(mp->hash_used);
320 mp->st_count=frozen_inaccessible-1-mp->hash_used;
321 for (p=1;p<=mp->hash_used;p++) {
323 dump_int(p); dump_hh(mp->hash[p]); dump_hh(mp->eqtb[p]); incr(mp->st_count);
326 for (p=mp->hash_used+1;p<=(int)hash_end;p++) {
327 dump_hh(mp->hash[p]); dump_hh(mp->eqtb[p]);
329 dump_int(mp->st_count);
330 mp_print_ln(mp); mp_print_int(mp, mp->st_count); mp_print(mp, " symbolic tokens")
332 @ @<Undump the table of equivalents and the hash table@>=
333 undump(1,frozen_inaccessible,mp->hash_used);
336 undump(p+1,mp->hash_used,p);
337 undump_hh(mp->hash[p]); undump_hh(mp->eqtb[p]);
338 } while (p!=mp->hash_used);
339 for (p=mp->hash_used+1;p<=(int)hash_end;p++ ) {
340 undump_hh(mp->hash[p]); undump_hh(mp->eqtb[p]);
342 undump_int(mp->st_count)
344 @ We have already printed a lot of statistics, so we set |mp_tracing_stats:=0|
345 to prevent them appearing again.
347 @<Dump a few more things and the closing check word@>=
348 dump_int(mp->max_internal);
349 dump_int(mp->int_ptr);
350 for (k=1;k<= mp->int_ptr;k++ ) {
351 dump_int(mp->internal[k]);
352 dump_string(mp->int_name[k]);
354 dump_int(mp->start_sym);
355 dump_int(mp->interaction);
356 dump_string(mp->mem_ident);
357 dump_int(mp->bg_loc); dump_int(mp->eg_loc); dump_int(mp->serial_no); dump_int(69073);
358 mp->internal[mp_tracing_stats]=0
360 @ @<Undump a few more things and the closing check word@>=
362 if (x>mp->max_internal) mp_grow_internals(mp,x);
363 undump_int(mp->int_ptr);
364 for (k=1;k<= mp->int_ptr;k++) {
365 undump_int(mp->internal[k]);
366 undump_string(mp->int_name[k]);
368 undump(0,frozen_inaccessible,mp->start_sym);
369 if (mp->interaction==mp_unspecified_mode) {
370 undump(mp_unspecified_mode,mp_error_stop_mode,mp->interaction);
372 undump(mp_unspecified_mode,mp_error_stop_mode,x);
374 undump_string(mp->mem_ident);
375 undump(1,hash_end,mp->bg_loc);
376 undump(1,hash_end,mp->eg_loc);
377 undump_int(mp->serial_no);
379 if (x!=69073) goto OFF_BASE
381 @ @<Create the |mem_ident|...@>=
383 char *tmp = mp_xmalloc(mp,11,1);
384 mp_xfree(mp->mem_ident);
385 mp->mem_ident = mp_xmalloc(mp,256,1);
386 mp_snprintf(tmp,11,"%04d.%02d.%02d",
387 (int)mp_round_unscaled(mp, mp->internal[mp_year]),
388 (int)mp_round_unscaled(mp, mp->internal[mp_month]),
389 (int)mp_round_unscaled(mp, mp->internal[mp_day]));
390 mp_snprintf(mp->mem_ident,256," (mem=%s %s)",mp->job_name, tmp);
392 mp_pack_job_name(mp, ".mem");
393 while (! mp_w_open_out(mp, &mp->mem_file) )
394 mp_prompt_file_name(mp, "mem file name", ".mem");
395 mp_print_nl(mp, "Beginning to dump on file ");
396 @.Beginning to dump...@>
397 mp_print(mp, mp->name_of_file);
398 mp_print_nl(mp, mp->mem_ident);
401 @ @<Close the mem file@>=
402 (mp->close_file)(mp,mp->mem_file)