-
Notifications
You must be signed in to change notification settings - Fork 988
/
prim5.c
2444 lines (2127 loc) · 79.7 KB
/
prim5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* prim5.c
* Copyright 1984-2017 Cisco Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "system.h"
#include "sort.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <ctype.h>
#include <math.h>
#if defined(__GNU__) /* Hurd */
#include <sys/resource.h>
#endif
/* locally defined functions */
static INT s_errno(void);
static IBOOL s_addr_in_heap(uptr x);
static IBOOL s_ptr_in_heap(ptr x);
static ptr s_generation(ptr x);
static iptr s_fxmul(iptr x, iptr y);
static iptr s_fxdiv(iptr x, iptr y);
static ptr s_trunc_rem(ptr x, ptr y);
static ptr s_fltofx(ptr x);
static ptr s_weak_pairp(ptr p);
static ptr s_ephemeron_cons(ptr car, ptr cdr);
static ptr s_ephemeron_pairp(ptr p);
static ptr s_box_immobile(ptr p);
static ptr s_make_immobile_vector(uptr len, ptr fill);
static ptr s_make_immobile_bytevector(uptr len);
static ptr s_make_reference_bytevector(uptr len);
static ptr s_make_immobile_reference_bytevector(uptr len);
static ptr s_reference_bytevectorp(ptr p);
static ptr s_reference_star_address_object(ptr p);
static ptr s_bytevector_reference_star_ref(ptr p, uptr offset);
static ptr s_oblist(void);
static ptr s_bigoddp(ptr n);
static ptr s_float(ptr x);
static ptr s_decode_float(ptr x);
#ifdef segment_t2_bits
static void s_show_info(FILE *out);
#endif
static void s_show_chunks(FILE *out, ptr sorted_chunks);
static ptr sort_chunks(ptr ls, uptr n);
static ptr merge_chunks(ptr ls1, ptr ls2);
static ptr sorted_chunk_list(void);
static void s_showalloc(IBOOL show_dump, const char *outfn);
static ptr s_system(const char *s);
static ptr s_process(char *s, IBOOL stderrp);
static I32 s_chdir(const char *inpath);
#if defined(GETWD) || defined(__GNU__) /* Hurd */
static char *s_getwd(void);
#endif
static ptr s_set_code_byte(ptr p, ptr n, ptr x);
static ptr s_set_code_word(ptr p, ptr n, ptr x);
static ptr s_set_code_long(ptr p, ptr n, ptr x);
static void s_set_code_long2(ptr p, ptr n, ptr h, ptr l);
static ptr s_set_code_quad(ptr p, ptr n, ptr x);
static ptr s_set_reloc(ptr p, ptr n, ptr e);
static ptr s_flush_instruction_cache(void);
static ptr s_make_code(iptr flags, iptr free, ptr name, ptr arity_mark, iptr n, ptr info, ptr pinfos);
static ptr s_make_reloc_table(ptr codeobj, ptr n);
static ptr s_make_closure(ptr offset, ptr codeobj);
static ptr s_fxrandom(ptr n);
static ptr s_flrandom(ptr x);
static U32 s_random_seed(void);
static void s_set_random_seed(U32 x);
static ptr s_intern(ptr x);
static ptr s_intern2(ptr x, ptr n);
static ptr s_strings_to_gensym(ptr pname_str, ptr uname_str);
static ptr s_intern3(ptr x, ptr n, ptr m);
static ptr s_delete_file(const char *inpath);
static ptr s_delete_directory(const char *inpath);
static ptr s_rename_file(const char *inpath1, const char *inpath2);
static ptr s_mkdir(const char *inpath, INT mode);
static ptr s_chmod(const char *inpath, INT mode);
static ptr s_getmod(const char *inpath, IBOOL followp);
static ptr s_path_atime(const char *inpath, IBOOL followp);
static ptr s_path_ctime(const char *inpath, IBOOL followp);
static ptr s_path_mtime(const char *inpath, IBOOL followp);
static ptr s_fd_atime(INT fd);
static ptr s_fd_ctime(INT fd);
static ptr s_fd_mtime(INT fd);
static IBOOL s_fd_regularp(INT fd);
static void s_nanosleep(ptr sec, ptr nsec);
static ptr s_set_collect_trip_bytes(ptr n);
static void c_exit(I32 status);
static ptr s_get_reloc(ptr co, IBOOL with_offsets);
#ifdef PTHREADS
static s_thread_rv_t s_backdoor_thread_start(void *p);
static iptr s_backdoor_thread(ptr p);
static ptr s_threads(void);
static void s_mutex_acquire(ptr m);
static ptr s_mutex_acquire_noblock(ptr m);
static void s_mutex_release(ptr m);
static void s_condition_broadcast(ptr c);
static void s_condition_signal(ptr c);
static void s_condition_free(ptr c);
static IBOOL s_condition_wait(ptr c, ptr m, ptr t);
static void s_thread_preserve_ownership(ptr tc);
#endif
static void s_byte_copy(ptr src, iptr srcoff, ptr dst, iptr dstoff, iptr cnt);
static void s_ptr_copy(ptr src, iptr srcoff, ptr dst, iptr dstoff, iptr cnt);
static ptr s_tlv(ptr x);
static void s_stlv(ptr x, ptr v);
static void s_test_schlib(void);
static void s_breakhere(ptr x);
static IBOOL s_interactivep(void);
static IBOOL s_same_devicep(INT fd1, INT fd2);
static uptr s_malloc(iptr n);
static void s_free(uptr n);
#ifdef FEATURE_ICONV
static ptr s_iconv_open(const char *tocode, const char *fromcode);
static void s_iconv_close(uptr cd);
static ptr s_iconv_from_string(uptr cd, ptr in, uptr i, uptr iend, ptr out, uptr o, uptr oend);
static ptr s_iconv_to_string(uptr cd, ptr in, uptr i, uptr iend, ptr out, uptr o, uptr oend);
#endif
#ifdef WIN32
static ptr s_multibytetowidechar(unsigned cp, ptr inbv);
static ptr s_widechartomultibyte(unsigned cp, ptr inbv);
#endif
#ifdef PORTABLE_BYTECODE
static ptr s_separatorchar();
#endif
static ptr s_profile_counters(void);
static ptr s_profile_release_counters(void);
#ifdef WIN32
# define WIN32_UNUSED UNUSED
#else
# define WIN32_UNUSED
#endif
#define require(test,who,msg,arg) if (!(test)) S_error1(who, msg, arg)
ptr S_strerror(INT errnum) {
ptr p; char *msg;
tc_mutex_acquire();
#ifdef WIN32
msg = Swide_to_utf8(_wcserror(errnum));
if (msg == NULL)
p = Sfalse;
else {
p = Sstring_utf8(msg, -1);
free(msg);
}
#else
p = (msg = strerror(errnum)) == NULL ? Sfalse : Sstring_utf8(msg, -1);
#endif
tc_mutex_release();
return p;
}
static INT s_errno(void) {
return errno;
}
static IBOOL s_addr_in_heap(uptr x) {
return MaybeSegInfo(addr_get_segment(x)) != NULL;
}
static IBOOL s_ptr_in_heap(ptr x) {
return MaybeSegInfo(ptr_get_segment(x)) != NULL;
}
static ptr s_generation(ptr x) {
seginfo *si = MaybeSegInfo(ptr_get_segment(x));
return si == NULL ? Sfalse : FIX(si->generation);
}
static iptr s_fxmul(iptr x, iptr y) {
return x * y;
}
static iptr s_fxdiv(iptr x, iptr y) {
return x / y;
}
static ptr s_trunc_rem(ptr x, ptr y) {
ptr q, r;
S_trunc_rem(get_thread_context(), x, y, &q, &r);
return Scons(q, r);
}
static ptr s_fltofx(ptr x) {
return FIX((iptr)FLODAT(x));
}
static ptr s_weak_pairp(ptr p) {
seginfo *si;
return Spairp(p) && (si = MaybeSegInfo(ptr_get_segment(p))) != NULL && si->space == space_weakpair ? Strue : Sfalse;
}
static ptr s_ephemeron_cons(ptr car, ptr cdr) {
ptr p;
p = S_ephemeron_cons_in(0, car, cdr);
return p;
}
static ptr s_ephemeron_pairp(ptr p) {
seginfo *si;
return Spairp(p) && (si = MaybeSegInfo(ptr_get_segment(p))) != NULL && si->space == space_ephemeron ? Strue : Sfalse;
}
static ptr s_box_immobile(ptr p) {
ptr b = S_box2(p, 1);
S_immobilize_object(b);
return b;
}
static ptr s_make_immobile_bytevector(uptr len) {
ptr b = S_bytevector2(get_thread_context(), len, space_immobile_data);
S_immobilize_object(b);
return b;
}
static ptr s_make_immobile_vector(uptr len, ptr fill) {
ptr tc = get_thread_context();
ptr v;
uptr i;
v = S_vector_in(tc, space_immobile_impure, 0, len);
S_immobilize_object(v);
for (i = 0; i < len; i++)
INITVECTIT(v, i) = fill;
if (!(len & 0x1)) {
/* pad, since we're not going to copy on a GC */
INITVECTIT(v, len) = FIX(0);
}
return v;
}
static ptr s_make_reference_bytevector(uptr len) {
ptr b = S_bytevector2(get_thread_context(), len, space_reference_array);
/* In case of a dirty sweep at the current allocation site, we need
to clear any padding bytes, either internal or for alignment */
len = (len + ptr_bytes - 1) >> log2_ptr_bytes;
#ifdef bytevector_pad_disp
*(ptr *)TO_VOIDP((uptr)b+bytevector_pad_disp) = FIX(0);
if (len & 1) len++;
#else
if (!(len & 1)) len++;
#endif
memset(&BVIT(b, 0), 0, len << log2_ptr_bytes);
return b;
}
static ptr s_make_immobile_reference_bytevector(uptr len) {
ptr b = s_make_reference_bytevector(len);
S_immobilize_object(b);
return b;
}
static ptr s_reference_bytevectorp(ptr p) {
seginfo *si;
return (si = MaybeSegInfo(ptr_get_segment(p))) != NULL && si->space == space_reference_array ? Strue : Sfalse;
}
static ptr s_reference_star_address_object(ptr p) {
if (p == (ptr)0)
return Sfalse;
else if (MaybeSegInfo(addr_get_segment(p)))
return (ptr)((uptr)p - reference_disp);
else
return Sunsigned((uptr)p);
}
static ptr s_bytevector_reference_star_ref(ptr p, uptr offset) {
return s_reference_star_address_object(*(ptr *)&BVIT(p, offset));
}
static ptr s_oblist() {
ptr ls = Snil;
iptr idx = S_G.oblist_length;
bucket *b;
while (idx-- != 0) {
for (b = S_G.oblist[idx]; b != NULL; b = b->next) {
ls = Scons(b->sym, ls);
}
}
return ls;
}
static ptr s_bigoddp(ptr n) {
return Sboolean(BIGIT(n, BIGLEN(n) - 1) & 1); /* last bigit */;
}
static ptr s_float(ptr x) {
return Sflonum(S_floatify(x));
}
static ptr s_decode_float(ptr x) {
require(Sflonump(x),"decode-float","~s is not a float",x);
return S_decode_float(FLODAT(x));
}
#define FMTBUFSIZE 120
#define CHUNKADDRLT(x, y) (((chunkinfo *)TO_VOIDP(Scar(x)))->addr < ((chunkinfo *)TO_VOIDP(Scar(y)))->addr)
mkmergesort(sort_chunks, merge_chunks, ptr, Snil, CHUNKADDRLT, INITCDR)
static ptr sorted_chunk_list(void) {
chunkinfo *chunk; INT i, n = 0; ptr ls = Snil;
for (i = PARTIAL_CHUNK_POOLS; i >= -1; i -= 1) {
for (chunk = (i == -1) ? S_chunks_full : S_chunks[i]; chunk != NULL; chunk = chunk->next) {
ls = Scons(TO_PTR(chunk), ls);
n += 1;
}
for (chunk = (i == -1) ? S_code_chunks_full : S_code_chunks[i]; chunk != NULL; chunk = chunk->next) {
ls = Scons(TO_PTR(chunk), ls);
n += 1;
}
}
return sort_chunks(ls, n);
}
#ifdef __MINGW32__
# include <inttypes.h>
# define PHtx "%" PRIxPTR
# define Ptd "%" PRIdPTR
#else
# define PHtx "%#tx"
# define Ptd "%td"
#endif
#ifdef segment_t2_bits
static void s_show_info(FILE *out) {
void *max_addr = 0;
INT addrwidth;
const char *addrtitle = "address";
char fmtbuf[FMTBUFSIZE];
uptr i2;
#ifdef segment_t3_bits
INT byteswidth;
uptr i3;
for (i3 = 0; i3 < SEGMENT_T3_SIZE; i3 += 1) {
t2table *t2t = S_segment_info[i3];
if (t2t != NULL) {
if ((void *)t2t > max_addr) max_addr = (void *)t2t;
for (i2 = 0; i2 < SEGMENT_T2_SIZE; i2 += 1) {
t1table *t1t = t2t->t2[i2];
if (t1t != NULL) {
if ((void *)t1t > max_addr) max_addr = (void *)t1t;
}
}
}
}
addrwidth = snprintf(fmtbuf, FMTBUFSIZE, ""PHtx"", (ptrdiff_t)max_addr);
if (addrwidth < (INT)strlen(addrtitle)) addrwidth = (INT)strlen(addrtitle);
byteswidth = snprintf(fmtbuf, FMTBUFSIZE, ""PHtx"", (ptrdiff_t)(sizeof(t1table) > sizeof(t2table) ? sizeof(t1table) : sizeof(t2table)));
snprintf(fmtbuf, FMTBUFSIZE, "%%s %%-%ds %%-%ds\n\n", addrwidth, byteswidth);
fprintf(out, fmtbuf, "level", addrtitle, "bytes");
snprintf(fmtbuf, FMTBUFSIZE, "%%-5d %%#0%dtx %%#0%dtx\n", addrwidth, byteswidth);
for (i3 = 0; i3 < SEGMENT_T3_SIZE; i3 += 1) {
t2table *t2t = S_segment_info[i3];
if (t2t != NULL) {
fprintf(out, fmtbuf, 2, t2t, sizeof(t2table));
for (i2 = 0; i2 < SEGMENT_T2_SIZE; i2 += 1) {
t1table *t1t = t2t->t2[i2];
if (t1t != NULL) {
fprintf(out, fmtbuf, 1, (ptrdiff_t)t1t, (ptrdiff_t)sizeof(t1table));
}
}
}
}
#else
for (i2 = 0; i2 < SEGMENT_T2_SIZE; i2 += 1) {
t1table *t1t = S_segment_info[i2];
if (t1t != NULL) {
if ((void *)t1t > max_addr) max_addr = (void *)t1t;
}
}
addrwidth = 1 + snprintf(fmtbuf, FMTBUFSIZE, ""PHtx"", (ptrdiff_t)max_addr);
if (addrwidth < (INT)strlen(addrtitle) + 1) addrwidth = (INT)strlen(addrtitle) + 1;
snprintf(fmtbuf, FMTBUFSIZE, "%%s %%-%ds %%s\n\n", addrwidth);
fprintf(out, fmtbuf, "level", addrtitle, "bytes");
snprintf(fmtbuf, FMTBUFSIZE, "%%-5d %%#0%dtx %"PHtx"\n", (ptrdiff_t)addrwidth);
for (i2 = 0; i2 < SEGMENT_T2_SIZE; i2 += 1) {
t1table *t1t = S_segment_info[i2];
if (t1t != NULL) {
fprintf(out, fmtbuf, 1, (ptrdiff_t)t1t, (ptrdiff_t)sizeof(t1table));
}
}
#endif
}
#endif
static void s_show_chunks(FILE *out, ptr sorted_chunks) {
char fmtbuf[FMTBUFSIZE];
chunkinfo *chunk;
void *max_addr = 0;
void *max_header_addr = 0;
iptr max_segs = 0;
INT addrwidth, byteswidth, headeraddrwidth, headerbyteswidth, segswidth, headerwidth;
const char *addrtitle = "address", *bytestitle = "bytes", *headertitle = "(+ header)";
ptr ls;
for (ls = sorted_chunks; ls != Snil; ls = Scdr(ls)) {
chunk = TO_VOIDP(Scar(ls));
max_addr = chunk->addr;
if (chunk->segs > max_segs) max_segs = chunk->segs;
if ((void *)chunk > max_header_addr) max_header_addr = (void *)chunk;
}
addrwidth = (INT)snprintf(fmtbuf, FMTBUFSIZE, ""PHtx"", (ptrdiff_t)max_addr);
if (addrwidth < (INT)strlen(addrtitle)) addrwidth = (INT)strlen(addrtitle);
byteswidth = (INT)snprintf(fmtbuf, FMTBUFSIZE, ""PHtx"", (ptrdiff_t)(max_segs * bytes_per_segment));
if (byteswidth < (INT)strlen(bytestitle)) byteswidth = (INT)strlen(bytestitle);
headerbyteswidth = (INT)snprintf(fmtbuf, FMTBUFSIZE, ""PHtx"", (ptrdiff_t)(sizeof(chunkinfo) + sizeof(seginfo) * max_segs));
headeraddrwidth = (INT)snprintf(fmtbuf, FMTBUFSIZE, ""PHtx"", (ptrdiff_t)max_header_addr);
segswidth = (INT)snprintf(fmtbuf, FMTBUFSIZE, ""Ptd"", (ptrdiff_t)max_segs);
headerwidth = headerbyteswidth + headeraddrwidth + 13;
snprintf(fmtbuf, FMTBUFSIZE, "%%-%ds %%-%ds %%-%ds %%s\n\n", addrwidth, byteswidth, headerwidth);
fprintf(out, fmtbuf, addrtitle, bytestitle, headertitle, "segments used");
snprintf(fmtbuf, FMTBUFSIZE, "%%#0%dtx %%#0%dtx (+ %%#0%dtx bytes @ %%#0%dtx) %%%dtd of %%%dtd\n",
addrwidth, byteswidth, headerbyteswidth, headeraddrwidth, segswidth, segswidth);
for (ls = sorted_chunks; ls != Snil; ls = Scdr(ls)) {
chunk = TO_VOIDP(Scar(ls));
fprintf(out, fmtbuf, (ptrdiff_t)chunk->addr, (ptrdiff_t)chunk->bytes,
(ptrdiff_t)(sizeof(chunkinfo) + sizeof(seginfo) * chunk->segs),
(ptrdiff_t)chunk, (ptrdiff_t)chunk->nused_segs, (ptrdiff_t)chunk->segs);
}
}
#define space_bogus (max_space + 1)
#define space_total (space_bogus + 1)
#define generation_total (static_generation + 1)
#define INCRGEN(g) (g = g == S_G.max_nonstatic_generation ? static_generation : g+1)
static void s_showalloc(IBOOL show_dump, const char *outfn) {
FILE *out;
iptr count[generation_total+1][space_total+1];
uptr bytes[generation_total+1][space_total+1];
int i, column_size[generation_total+1];
char fmtbuf[FMTBUFSIZE];
static char *spacename[space_total+1] = { alloc_space_names, "bogus", "total" };
static char spacechar[space_total+1] = { alloc_space_chars, '?', 't' };
chunkinfo *chunk; seginfo *si; ISPC s; IGEN g;
ptr sorted_chunks;
ptr tc = get_thread_context();
tc_mutex_acquire();
alloc_mutex_acquire();
if (outfn == NULL) {
out = stderr;
} else {
#ifdef WIN32
wchar_t *outfnw = Sutf8_to_wide(outfn);
out = _wfopen(outfnw, L"w");
free(outfnw);
#else
out = fopen(outfn, "w");
#endif
if (out == NULL) {
ptr msg = S_strerror(errno);
if (msg != Sfalse) {
tc_mutex_release();
S_error2("fopen", "open of ~s failed: ~a", Sstring_utf8(outfn, -1), msg);
} else {
tc_mutex_release();
S_error1("fopen", "open of ~s failed", Sstring_utf8(outfn, -1));
}
}
}
for (g = 0; g <= generation_total; INCRGEN(g))
for (s = 0; s <= space_total; s++)
count[g][s] = bytes[g][s] = 0;
for (g = 0; g <= static_generation; INCRGEN(g)) {
for (s = 0; s <= max_real_space; s++) {
/* add in bytes previously recorded */
bytes[g][s] += S_G.bytes_of_space[g][s];
/* add in bytes in active segments */
if (THREAD_GC(tc)->next_loc[g][s] != FIX(0))
bytes[g][s] += (uptr)THREAD_GC(tc)->next_loc[g][s] - (uptr)THREAD_GC(tc)->base_loc[g][s];
}
}
for (g = 0; g <= static_generation; INCRGEN(g)) {
for (s = 0; s <= max_real_space; s++) {
for (si = S_G.occupied_segments[g][s]; si != NULL; si = si->next) {
count[g][s] += 1;
}
}
}
for (g = 0; g < generation_total; INCRGEN(g)) {
for (s = 0; s < space_total; s++) {
count[g][space_total] += count[g][s];
count[generation_total][s] += count[g][s];
count[generation_total][space_total] += count[g][s];
bytes[g][space_total] += bytes[g][s];
bytes[generation_total][s] += bytes[g][s];
bytes[generation_total][space_total] += bytes[g][s];
}
}
for (g = 0; g <= generation_total; INCRGEN(g)) {
if (count[g][space_total] != 0) {
int n = 1 + snprintf(fmtbuf, FMTBUFSIZE, ""Ptd"", (ptrdiff_t)count[g][space_total]);
column_size[g] = n < 8 ? 8 : n;
}
}
fprintf(out, "Segments per space & generation:\n\n");
fprintf(out, "%8s", "");
for (g = 0; g <= generation_total; INCRGEN(g)) {
if (count[g][space_total] != 0) {
if (g == generation_total) {
/* coverity[uninit_use] */
snprintf(fmtbuf, FMTBUFSIZE, "%%%ds", column_size[g]);
fprintf(out, fmtbuf, "total");
} else if (g == static_generation) {
/* coverity[uninit_use] */
snprintf(fmtbuf, FMTBUFSIZE, "%%%ds", column_size[g]);
fprintf(out, fmtbuf, "static");
} else {
/* coverity[uninit_use] */
snprintf(fmtbuf, FMTBUFSIZE, "%%%dd", column_size[g]);
fprintf(out, fmtbuf, g);
}
}
}
fprintf(out, "\n");
for (s = 0; s <= space_total; s++) {
if (s != space_empty) {
if (count[generation_total][s] != 0) {
fprintf(out, "%7s:", spacename[s]);
for (g = 0; g <= generation_total; INCRGEN(g)) {
if (count[g][space_total] != 0) {
/* coverity[uninit_use] */
snprintf(fmtbuf, FMTBUFSIZE, "%%%dtd", column_size[g]);
fprintf(out, fmtbuf, (ptrdiff_t)(count[g][s]));
}
}
fprintf(out, "\n");
fprintf(out, "%8s", "");
for (g = 0; g <= generation_total; INCRGEN(g)) {
if (count[g][space_total] != 0) {
if (count[g][s] != 0 && s <= max_real_space) {
/* coverity[uninit_use] */
snprintf(fmtbuf, FMTBUFSIZE, "%%%dd%%%%", column_size[g] - 1);
fprintf(out, fmtbuf,
(int)(((double)bytes[g][s] /
((double)count[g][s] * bytes_per_segment)) * 100.0));
} else {
/* coverity[uninit_use] */
snprintf(fmtbuf, FMTBUFSIZE, "%%%ds", column_size[g]);
fprintf(out, fmtbuf, "");
}
}
}
fprintf(out, "\n");
}
}
}
fprintf(out, "segment size = "PHtx" bytes. percentages show the portion actually occupied.\n", (ptrdiff_t)bytes_per_segment);
fprintf(out, ""Ptd" segments are presently reserved for future allocation or collection.\n", (ptrdiff_t)S_G.number_of_empty_segments);
fprintf(out, "\nMemory chunks obtained and not returned to the O/S:\n\n");
sorted_chunks = sorted_chunk_list();
s_show_chunks(out, sorted_chunks);
#ifdef segment_t2_bits
fprintf(out, "\nDynamic memory occupied by segment info table:\n\n");
s_show_info(out);
#endif
fprintf(out, "\nAdditional memory might be used by C libraries and programs in the\nsame address space.\n");
if (show_dump) {
iptr max_seg = 0;
int segwidth, segsperline;
iptr next_base = 0;
int segsprinted = 0;
char spaceline[100], genline[100];
ptr ls;
for (ls = sorted_chunks; ls != Snil; ls = Scdr(ls)) {
iptr last_seg;
chunk = TO_VOIDP(Scar(ls));
last_seg = chunk->base + chunk->segs;
if (last_seg > max_seg) max_seg = last_seg;
}
segwidth = snprintf(fmtbuf, FMTBUFSIZE, ""PHtx" ", (ptrdiff_t)max_seg);
segsperline = (99 - segwidth) & ~0xf;
snprintf(fmtbuf, FMTBUFSIZE, " %%-%ds", segwidth);
snprintf(genline, 100, fmtbuf, "");
fprintf(out, "\nMap of occupied segments:\n");
for (ls = sorted_chunks; ls != Snil; ls = Scdr(ls)) {
seginfo *si;
chunk = TO_VOIDP(Scar(ls));
if (chunk->base != next_base && segsprinted != 0) {
for (;;) {
if (segsprinted == segsperline) {
fprintf(out, "\n%s", spaceline);
fprintf(out, "\n%s", genline);
break;
}
if (next_base == chunk->base) break;
spaceline[segwidth+segsprinted] = ' ';
genline[segwidth+segsprinted] = ' ';
segsprinted += 1;
next_base += 1;
}
}
if (chunk->base > next_base && next_base != 0) {
fprintf(out, "\n-------- skipping "Ptd" segments --------", (ptrdiff_t)(chunk->base - next_base));
}
for (i = 0; i < chunk->segs; i += 1) {
if (segsprinted >= segsperline) segsprinted = 0;
if (segsprinted == 0) {
if (i != 0) {
fprintf(out, "\n%s", spaceline);
fprintf(out, "\n%s", genline);
}
snprintf(fmtbuf, FMTBUFSIZE, "%%#0%dtx ", segwidth - 1);
snprintf(spaceline, 100, fmtbuf, (ptrdiff_t)(chunk->base + i));
segsprinted = 0;
}
si = &chunk->sis[i];
s = si->space;
if (s < 0 || s > max_space) s = space_bogus;
spaceline[segwidth+segsprinted] = spacechar[s];
g = si->generation;
genline[segwidth+segsprinted] =
(s == space_empty) ? '.' :
(g < 10) ? '0' + g :
(g < 36) ? 'A' + g - 10 :
(g == static_generation) ? '*' : '+';
segsprinted += 1;
}
next_base = chunk->base + chunk->segs;
}
if (segsprinted != 0) {
spaceline[segwidth+segsprinted] = 0;
genline[segwidth+segsprinted] = 0;
fprintf(out, "\n%s", spaceline);
fprintf(out, "\n%s", genline);
}
fprintf(out, "\n\nSpaces:");
for (s = 0; s < space_total; s += 1)
fprintf(out, "%s%c = %s", s % 5 == 0 ? "\n " : "\t",
spacechar[s], spacename[s]);
fprintf(out, "\n\nGenerations:\n 0-9: 0<=g<=9; A-Z: 10<=g<=35; +: g>=36; *: g=static; .: empty\n\n");
}
if (outfn == NULL) {
fflush(out);
} else {
fclose(out);
}
alloc_mutex_release();
tc_mutex_release();
}
#include <signal.h>
#ifdef WIN32
#include <io.h>
#include <process.h>
#include <fcntl.h>
#include <direct.h>
#include <malloc.h>
#else /* WIN32 */
#include <sys/param.h>
#include <sys/wait.h>
#endif /* WIN32 */
static ptr s_system(const char *s) {
INT status;
#ifdef PTHREADS
ptr tc = get_thread_context();
char *s_arg = NULL;
#endif
#ifdef PTHREADS
if (DISABLECOUNT(tc) == FIX(0)) {
/* copy `s` in case a GC happens */
uptr len = strlen(s) + 1;
s_arg = malloc(len);
if (s_arg == NULL)
S_error("system", "malloc failed");
memcpy(s_arg, s, len);
deactivate_thread(tc);
s = s_arg;
} else
s_arg = NULL;
#endif
status = SYSTEM(s);
#ifdef PTHREADS
if (DISABLECOUNT(tc) == FIX(0)) {
reactivate_thread(tc);
free(s_arg);
}
#endif
if ((status == -1) && (errno != 0)) {
ptr msg = S_strerror(errno);
if (msg != Sfalse)
S_error1("system", "~a", msg);
else
S_error("system", "subprocess execution failed");
}
#ifdef WIN32
return Sinteger(status);
#else
if (WIFEXITED(status)) return Sinteger(WEXITSTATUS(status));
if (WIFSIGNALED(status)) return Sinteger(-WTERMSIG(status));
S_error("system", "cannot determine subprocess exit status");
return 0 /* not reached */;
#endif /* WIN32 */
}
static ptr s_process(char *s, IBOOL stderrp) {
INT ifd = -1, ofd = -1, efd = -1, child = -1;
#ifdef WIN32
HANDLE hToRead, hToWrite, hFromRead, hFromWrite, hFromReadErr, hFromWriteErr, hProcess;
STARTUPINFOW si = {0};
PROCESS_INFORMATION pi;
char *comspec;
char *buffer;
wchar_t* bufferw;
/* Create non-inheritable pipes, important to eliminate zombee children
* when the parent sides are closed. */
if (!CreatePipe(&hToRead, &hToWrite, NULL, 0))
S_error("process", "cannot open pipes");
if (!CreatePipe(&hFromRead, &hFromWrite, NULL, 0)) {
CloseHandle(hToRead);
CloseHandle(hToWrite);
S_error("process", "cannot open pipes");
}
if (stderrp && !CreatePipe(&hFromReadErr, &hFromWriteErr, NULL, 0)) {
CloseHandle(hToRead);
CloseHandle(hToWrite);
CloseHandle(hFromRead);
CloseHandle(hFromWrite);
S_error("process", "cannot open pipes");
}
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
hProcess = GetCurrentProcess();
/* Duplicate the ToRead handle so that the child can inherit it. */
if (!DuplicateHandle(hProcess, hToRead, hProcess, &si.hStdInput,
GENERIC_READ, TRUE, 0)) {
CloseHandle(hToRead);
CloseHandle(hToWrite);
CloseHandle(hFromRead);
CloseHandle(hFromWrite);
if (stderrp) {
CloseHandle(hFromReadErr);
CloseHandle(hFromWriteErr);
}
S_error("process", "cannot open pipes");
}
CloseHandle(hToRead);
/* Duplicate the FromWrite handle so that the child can inherit it. */
if (!DuplicateHandle(hProcess, hFromWrite, hProcess, &si.hStdOutput,
GENERIC_WRITE, TRUE, 0)) {
CloseHandle(si.hStdInput);
CloseHandle(hToWrite);
CloseHandle(hFromRead);
CloseHandle(hFromWrite);
if (stderrp) {
CloseHandle(hFromReadErr);
CloseHandle(hFromWriteErr);
}
S_error("process", "cannot open pipes");
}
CloseHandle(hFromWrite);
if (stderrp) {
/* Duplicate the FromWrite handle so that the child can inherit it. */
if (!DuplicateHandle(hProcess, hFromWriteErr, hProcess, &si.hStdError,
GENERIC_WRITE, TRUE, 0)) {
CloseHandle(si.hStdInput);
CloseHandle(hToWrite);
CloseHandle(hFromRead);
CloseHandle(hFromWrite);
CloseHandle(hFromReadErr);
CloseHandle(hFromWriteErr);
S_error("process", "cannot open pipes");
}
CloseHandle(hFromWriteErr);
} else {
si.hStdError = si.hStdOutput;
}
if ((comspec = Sgetenv("COMSPEC"))) {
size_t n = strlen(comspec) + strlen(s) + 7;
buffer = (char *)_alloca(n);
snprintf(buffer, n, "\"%s\" /c %s", comspec, s);
free(comspec);
} else
buffer = s;
bufferw = Sutf8_to_wide(buffer);
if (!CreateProcessW(NULL, bufferw, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
free(bufferw);
CloseHandle(si.hStdInput);
CloseHandle(hToWrite);
CloseHandle(hFromRead);
CloseHandle(si.hStdOutput);
if (stderrp) {
CloseHandle(hFromReadErr);
CloseHandle(si.hStdError);
}
S_error("process", "cannot spawn subprocess");
}
free(bufferw);
CloseHandle(si.hStdInput);
CloseHandle(si.hStdOutput);
if (stderrp) {
CloseHandle(si.hStdError);
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
ifd = _open_osfhandle((intptr_t)hFromRead, 0);
ofd = _open_osfhandle((intptr_t)hToWrite, 0);
if (stderrp) {
efd = _open_osfhandle((intptr_t)hFromReadErr, 0);
}
child = pi.dwProcessId;
#else /* WIN32 */
INT tofds[2], fromfds[2], errfds[2];
struct sigaction act, oint_act;
if (pipe(tofds)) S_error("process","cannot open pipes");
if (pipe(fromfds)) {
CLOSE(tofds[0]); CLOSE(tofds[1]);
S_error("process","cannot open pipes");
}
if (stderrp) {
if (pipe(errfds)) {
CLOSE(tofds[0]); CLOSE(tofds[1]);
CLOSE(fromfds[0]); CLOSE(fromfds[1]);
S_error("process","cannot open pipes");
}
}
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = SIG_IGN;
sigaction(SIGINT, &act, &oint_act);
if ((child = fork()) == 0) {
/* child does this: */
CLOSE(0); if (dup(tofds[0]) != 0) _exit(1);
CLOSE(1); if (dup(fromfds[1]) != 1) _exit(1);
CLOSE(2); if (dup(stderrp ? errfds[1] : 1) != 2) _exit(1);
#ifndef __GNU__ /* Hurd */
{INT i; for (i = 3; i < NOFILE; i++) (void)CLOSE(i);}
#else /* __GNU__ Hurd: no NOFILE */
{
INT i;
struct rlimit rlim;
getrlimit(RLIMIT_NOFILE, &rlim);
for (i = 3; i < rlim.rlim_cur; i++) {
(void)CLOSE(i);
}
}
#endif /* __GNU__ Hurd */
execl("/bin/sh", "/bin/sh", "-c", s, NULL);
_exit(1) /* only if execl fails */;
/*NOTREACHED*/
} else {
/* parent does this: */
CLOSE(tofds[0]); CLOSE(fromfds[1]); if (stderrp) CLOSE(errfds[1]);
if (child < 0) {
CLOSE(tofds[1]); CLOSE(fromfds[0]); if (stderrp) CLOSE(errfds[0]);
sigaction(SIGINT, &oint_act, (struct sigaction *)0);
S_error("process", "cannot fork subprocess");
/*NOTREACHED*/
} else {
ifd = fromfds[0];
ofd = tofds[1];
if (stderrp) efd = errfds[0];
sigaction(SIGINT, &oint_act, (struct sigaction *)0);
S_register_child_process(child);
}
}
#endif /* WIN32 */
if (stderrp)
return LIST4(FIX(ifd), FIX(efd), FIX(ofd), FIX(child));
else
return LIST3(FIX(ifd), FIX(ofd), FIX(child));
}
static I32 s_chdir(const char *inpath) {
char *path;
I32 status;
path = S_malloc_pathname(inpath);
#ifdef EINTR
while ((status = CHDIR(path)) != 0 && errno == EINTR) ;
#else /* EINTR */
status = CHDIR(path);
#endif /* EINTR */
free(path);
return status;
}
#ifdef GETWD
static char *s_getwd() {
return GETWD(TO_VOIDP(&BVIT(S_bytevector(PATH_MAX), 0)));
}
#elif defined(__GNU__) /* Hurd: no PATH_MAX */
static char *s_getwd() {
char *path;
size_t len;
ptr bv;
path = getcwd(NULL, 0);
if (NULL == path) {
return NULL;
} else {
len = strlen(path);
bv = S_bytevector(len);
memcpy(TO_VOIDP(&BVIT(bv, 0)), path, len);
free(path);
return TO_VOIDP(&BVIT(bv, 0));
}
}
#endif /* GETWD */
static ptr s_set_code_byte(ptr p, ptr n, ptr x) {
I8 *a;
ptr tc = get_thread_context();
a = (I8 *)TO_VOIDP((uptr)p + UNFIX(n));
S_thread_start_code_write(tc, 0, 0, TO_VOIDP(a), sizeof(I8));
*a = (I8)UNFIX(x);
S_thread_end_code_write(tc, 0, 0, TO_VOIDP(a), sizeof(I8));
return Svoid;
}
static ptr s_set_code_word(ptr p, ptr n, ptr x) {
I16 *a;
ptr tc = get_thread_context();
a = (I16 *)TO_VOIDP((uptr)p + UNFIX(n));
S_thread_start_code_write(tc, 0, 0, TO_VOIDP(a), sizeof(I16));
*a = (I16)UNFIX(x);
S_thread_end_code_write(tc, 0, 0, TO_VOIDP(a), sizeof(I16));
return Svoid;
}