forked from timbunce/devel-nytprof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHandle.xs
1565 lines (1327 loc) · 41.3 KB
/
FileHandle.xs
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
/* vim: ts=8 sw=4 expandtab:
* ************************************************************************
* This file is part of the Devel::NYTProf package.
* See http://metacpan.org/release/Devel-NYTProf/
* For Copyright see lib/Devel/NYTProf.pm
* For contribution history see repository logs.
* ************************************************************************
*/
#define PERL_NO_GET_CONTEXT /* we want efficiency */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#if defined(PERL_IMPLICIT_SYS) && !defined(NO_XSLOCKS)
# ifndef fgets
# define fgets PerlSIO_fgets
# endif
#endif
#include "FileHandle.h"
#include "NYTProf.h"
#define NEED_sv_2pvbyte
#include "ppport.h"
#ifdef HAS_ZLIB
# include <zlib.h>
#endif
#define NYTP_FILE_STDIO 0
#define NYTP_FILE_DEFLATE 1
#define NYTP_FILE_INFLATE 2
/* to help find places in NYTProf.xs where we don't save/restore errno */
#if 0
#define ERRNO_PROBE errno=__LINE__
#else
#define ERRNO_PROBE (void)0
#endif
/* During profiling the large buffer collects the raw data until full.
* Then flush_output zips it into the small buffer and writes it to disk.
* A scale factor of ~90 makes the large buffer usually almost fill the small
* one when zipped (so calls to flush_output() almost always trigger one fwrite()).
* We use a lower number to save some memory as there's little performance
* impact either way.
*/
#define NYTP_FILE_SMALL_BUFFER_SIZE 4096
#define NYTP_FILE_LARGE_BUFFER_SIZE (NYTP_FILE_SMALL_BUFFER_SIZE * 40)
#ifdef HAS_ZLIB
# define FILE_STATE(f) ((f)->state)
#else
# define FILE_STATE(f) NYTP_FILE_STDIO
#endif
#if defined(PERL_IMPLICIT_CONTEXT) && ! defined(tTHX)
# define tTHX PerlInterpreter*
#endif
struct NYTP_file_t {
FILE *file;
#ifdef PERL_IMPLICIT_CONTEXT
tTHX aTHX; /* on 5.8 and older, pTHX contains a "register" which is not
compatible with a struct def, so use something else */
#endif
#ifdef HAS_ZLIB
unsigned char state;
bool stdio_at_eof;
bool zlib_at_eof;
/* For input only, the position we are in large_buffer. */
unsigned int count;
z_stream zs;
unsigned char small_buffer[NYTP_FILE_SMALL_BUFFER_SIZE];
unsigned char large_buffer[NYTP_FILE_LARGE_BUFFER_SIZE];
#endif
};
/* unlike dTHX which contains a function call, and therefore can never be
optimized away, even if return value isn't used, the below will optimize away
if NO_XSLOCKS is defined and PerlIO is not being used (i.e. native C lib
IO is being used on Win32 )*/
#ifdef PERL_IMPLICIT_CONTEXT
# define dNFTHX(x) dTHXa((x)->aTHX)
#else
# define dNFTHX(x) dNOOP
#endif
/* XXX The proper return value would be Off_t */
long
NYTP_tell(NYTP_file file) {
ERRNO_PROBE;
#ifdef HAS_ZLIB
/* This has to work with compressed files as it's used in the croaking
routine. */
if (FILE_STATE(file) != NYTP_FILE_STDIO) {
return FILE_STATE(file) == NYTP_FILE_INFLATE
? file->zs.total_out : file->zs.total_in;
}
#endif
{
dNFTHX(file);
return (long)ftell(file->file);
}
}
#ifdef HAS_ZLIB
const char *
NYTP_type_of_offset(NYTP_file file) {
switch (FILE_STATE(file)) {
case NYTP_FILE_STDIO:
return "";
case NYTP_FILE_DEFLATE:
return " in compressed output data";
break;
case NYTP_FILE_INFLATE:
return " in compressed input data";
break;
default:
return Perl_form_nocontext(" in stream in unknown state %d",
FILE_STATE(file));
}
}
#endif
#ifdef HAS_ZLIB
# define CROAK_IF_NOT_STDIO(file, where) \
STMT_START { \
if (FILE_STATE(file) != NYTP_FILE_STDIO) { \
compressed_io_croak((file), (where)); \
} \
} STMT_END
#else
# define CROAK_IF_NOT_STDIO(file, where)
#endif
#ifdef HAS_ZLIB
#ifdef HASATTRIBUTE_NORETURN
__attribute__noreturn__
#endif
static void
compressed_io_croak(NYTP_file file, const char *function) {
const char *what;
switch (FILE_STATE(file)) {
case NYTP_FILE_STDIO:
what = "stdio";
break;
case NYTP_FILE_DEFLATE:
what = "compressed output";
break;
case NYTP_FILE_INFLATE:
what = "compressed input";
break;
default:
croak("Can't use function %s() on a stream of type %d at offset %ld",
function, FILE_STATE(file), NYTP_tell(file));
}
croak("Can't use function %s() on a %s stream at offset %ld", function,
what, NYTP_tell(file));
}
void
NYTP_start_deflate(NYTP_file file, int compression_level) {
int status;
ERRNO_PROBE;
CROAK_IF_NOT_STDIO(file, "NYTP_start_deflate");
FILE_STATE(file) = NYTP_FILE_DEFLATE;
file->zs.next_in = (Bytef *) file->large_buffer;
file->zs.avail_in = 0;
file->zs.next_out = (Bytef *) file->small_buffer;
file->zs.avail_out = NYTP_FILE_SMALL_BUFFER_SIZE;
file->zs.zalloc = (alloc_func) 0;
file->zs.zfree = (free_func) 0;
file->zs.opaque = 0;
status = deflateInit2(&(file->zs), compression_level, Z_DEFLATED,
15 /* windowBits */,
9 /* memLevel */, Z_DEFAULT_STRATEGY);
if (status != Z_OK) {
croak("deflateInit2 failed, error %d (%s)", status, file->zs.msg);
}
}
void
NYTP_start_inflate(NYTP_file file) {
int status;
ERRNO_PROBE;
CROAK_IF_NOT_STDIO(file, "NYTP_start_inflate");
FILE_STATE(file) = NYTP_FILE_INFLATE;
file->zs.next_in = (Bytef *) file->small_buffer;
file->zs.avail_in = 0;
file->zs.next_out = (Bytef *) file->large_buffer;
file->zs.avail_out = NYTP_FILE_LARGE_BUFFER_SIZE;
file->zs.zalloc = (alloc_func) 0;
file->zs.zfree = (free_func) 0;
file->zs.opaque = 0;
status = inflateInit2(&(file->zs), 15);
if (status != Z_OK) {
croak("inflateInit2 failed, error %d (%s)", status, file->zs.msg);
}
}
#endif
NYTP_file
NYTP_open(const char *name, const char *mode) {
dTHX;
FILE *raw_file = fopen(name, mode);
NYTP_file file;
ERRNO_PROBE;
if (!raw_file)
return NULL;
/* MS libc has 4096 as default, this is too slow for GB size profiling data */
if (setvbuf(raw_file, NULL, _IOFBF, 16384))
return NULL;
Newx(file, 1, struct NYTP_file_t);
file->file = raw_file;
#ifdef PERL_IMPLICIT_CONTEXT
file->aTHX = aTHX;
#endif
#ifdef HAS_ZLIB
file->state = NYTP_FILE_STDIO;
file->count = 0;
file->stdio_at_eof = FALSE;
file->zlib_at_eof = FALSE;
file->zs.msg = (char *)"[Oops. zlib hasn't updated this error string]";
#endif
return file;
}
#ifdef HAS_ZLIB
static void
grab_input(NYTP_file ifile) {
dNFTHX(ifile);
ERRNO_PROBE;
ifile->count = 0;
ifile->zs.next_out = (Bytef *) ifile->large_buffer;
ifile->zs.avail_out = NYTP_FILE_LARGE_BUFFER_SIZE;
#ifdef DEBUG_INFLATE
fprintf(stderr, "grab_input enter\n");
#endif
while (1) {
int status;
if (ifile->zs.avail_in == 0 && !ifile->stdio_at_eof) {
size_t got = fread(ifile->small_buffer, 1,
NYTP_FILE_SMALL_BUFFER_SIZE, ifile->file);
if (got == 0) {
if (!feof(ifile->file)) {
int eno = errno;
croak("grab_input failed: %d (%s)", eno, strerror(eno));
}
ifile->stdio_at_eof = TRUE;
}
ifile->zs.avail_in = got;
ifile->zs.next_in = (Bytef *) ifile->small_buffer;
}
#ifdef DEBUG_INFLATE
fprintf(stderr, "grab_input predef next_in= %p avail_in= %08x\n"
" next_out=%p avail_out=%08x"
" eof=%d,%d\n", ifile->zs.next_in, ifile->zs.avail_in,
ifile->zs.next_out, ifile->zs.avail_out, ifile->stdio_at_eof,
ifile->zlib_at_eof);
#endif
status = inflate(&(ifile->zs), Z_NO_FLUSH);
#ifdef DEBUG_INFLATE
fprintf(stderr, "grab_input postdef next_in= %p avail_in= %08x\n"
" next_out=%p avail_out=%08x "
"status=%d\n", ifile->zs.next_in, ifile->zs.avail_in,
ifile->zs.next_out, ifile->zs.avail_out, status);
#endif
if (!(status == Z_OK || status == Z_STREAM_END)) {
if (ifile->stdio_at_eof)
croak("Profile data incomplete, inflate error %d (%s) at end of input file,"
" perhaps the process didn't exit cleanly or the file has been truncated "
" (refer to TROUBLESHOOTING in the documentation)\n",
status, ifile->zs.msg);
croak("Error reading file: inflate failed, error %d (%s) at offset %ld in input file",
status, ifile->zs.msg, (long)ftell(ifile->file));
}
if (ifile->zs.avail_out == 0 || status == Z_STREAM_END) {
if (status == Z_STREAM_END) {
ifile->zlib_at_eof = TRUE;
}
return;
}
}
}
#endif
size_t
NYTP_read_unchecked(NYTP_file ifile, void *buffer, size_t len) {
dNFTHX(ifile);
#ifdef HAS_ZLIB
size_t result = 0;
#endif
ERRNO_PROBE;
if (FILE_STATE(ifile) == NYTP_FILE_STDIO) {
return fread(buffer, 1, len, ifile->file);
}
#ifdef HAS_ZLIB
else if (FILE_STATE(ifile) != NYTP_FILE_INFLATE) {
compressed_io_croak(ifile, "NYTP_read");
return 0;
}
while (1) {
unsigned char *p = ifile->large_buffer + ifile->count;
int remaining = ((unsigned char *) ifile->zs.next_out) - p;
if (remaining >= len) {
Copy(p, buffer, len, unsigned char);
ifile->count += len;
result += len;
return result;
}
Copy(p, buffer, remaining, unsigned char);
ifile->count = NYTP_FILE_LARGE_BUFFER_SIZE;
result += remaining;
len -= remaining;
buffer = (void *)(remaining + (char *)buffer);
if (ifile->zlib_at_eof)
return result;
grab_input(ifile);
}
#endif
}
size_t
NYTP_read(NYTP_file ifile, void *buffer, size_t len, const char *what) {
size_t got = NYTP_read_unchecked(ifile, buffer, len);
if (got != len) {
croak("Profile format error whilst reading %s at %ld%s: expected %ld got %ld, %s (see TROUBLESHOOTING in docs)",
what, NYTP_tell(ifile), NYTP_type_of_offset(ifile), (long)len, (long)got,
(NYTP_eof(ifile)) ? "end of file" : NYTP_fstrerror(ifile));
}
return len;
}
/* This isn't exactly fgets. It will resize the buffer as needed, and returns
a pointer to one beyond the read data (usually the terminating '\0'), or
NULL if it hit error/EOF */
char *
NYTP_gets(NYTP_file ifile, char **buffer_p, size_t *len_p) {
char *buffer = *buffer_p;
size_t len = *len_p;
size_t prev_len = 0;
ERRNO_PROBE;
#ifdef HAS_ZLIB
if (FILE_STATE(ifile) == NYTP_FILE_INFLATE) {
while (1) {
const unsigned char *const p = ifile->large_buffer + ifile->count;
const unsigned int remaining = ((unsigned char *) ifile->zs.next_out) - p;
unsigned char *const nl = (unsigned char *)memchr(p, '\n', remaining);
size_t got;
size_t want;
size_t extra;
if (nl) {
want = nl + 1 - p;
extra = want + 1; /* 1 more to add a \0 */
} else {
want = extra = remaining;
}
if (extra > len - prev_len) {
prev_len = len;
len += extra;
buffer = (char *)saferealloc(buffer, len);
}
got = NYTP_read_unchecked(ifile, buffer + prev_len, want);
if (got != want)
croak("NYTP_gets unexpected short read. got %lu, expected %lu\n",
(unsigned long)got, (unsigned long)want);
if (nl) {
buffer[prev_len + want] = '\0';
*buffer_p = buffer;
*len_p = len;
return buffer + prev_len + want;
}
if (ifile->zlib_at_eof) {
*buffer_p = buffer;
*len_p = len;
return NULL;
}
grab_input(ifile);
}
}
#endif
CROAK_IF_NOT_STDIO(ifile, "NYTP_gets");
{
dNFTHX(ifile);
while(fgets(buffer + prev_len, (int)(len - prev_len), ifile->file)) {
/* We know that there are no '\0' bytes in the part we've already
read, so don't bother running strlen() over that part. */
char *end = buffer + prev_len + strlen(buffer + prev_len);
if (end[-1] == '\n') {
*buffer_p = buffer;
*len_p = len;
return end;
}
prev_len = len - 1; /* -1 to take off the '\0' at the end */
len *= 2;
buffer = (char *)saferealloc(buffer, len);
}
}
*buffer_p = buffer;
*len_p = len;
return NULL;
}
#ifdef HAS_ZLIB
/* Cheat, by telling zlib about a reduced amount of available output space,
such that our next write of the (slightly underused) output buffer will
align the underlying file pointer back with the size of our output buffer
(and hopefully the underlying OS block writes). */
static void
sync_avail_out_to_ftell(NYTP_file ofile) {
dNFTHX(ofile);
const long result = ftell(ofile->file);
const unsigned long where = result < 0 ? 0 : result;
ERRNO_PROBE;
ofile->zs.avail_out =
NYTP_FILE_SMALL_BUFFER_SIZE - where % NYTP_FILE_SMALL_BUFFER_SIZE;
#ifdef DEBUG_DEFLATE
fprintf(stderr, "sync_avail_out_to_ftell pos=%ld, avail_out=%lu\n",
result, (unsigned long) ofile->zs.avail_out);
#endif
}
/* flush has values as described for "allowed flush values" in zlib.h */
static void
flush_output(NYTP_file ofile, int flush) {
dNFTHX(ofile);
ERRNO_PROBE;
ofile->zs.next_in = (Bytef *) ofile->large_buffer;
#ifdef DEBUG_DEFLATE
fprintf(stderr, "flush_output enter flush = %d\n", flush);
#endif
while (1) {
int status;
#ifdef DEBUG_DEFLATE
fprintf(stderr, "flush_output predef next_in= %p avail_in= %08x\n"
" next_out=%p avail_out=%08x"
" flush=%d\n", ofile->zs.next_in, ofile->zs.avail_in,
ofile->zs.next_out, ofile->zs.avail_out, flush);
#endif
status = deflate(&(ofile->zs), flush);
/* workaround for RT#50851 */
if (status == Z_BUF_ERROR && flush != Z_NO_FLUSH
&& !ofile->zs.avail_in && ofile->zs.avail_out)
status = Z_OK;
#ifdef DEBUG_DEFLATE
fprintf(stderr, "flush_output postdef next_in= %p avail_in= %08x\n"
" next_out=%p avail_out=%08x "
"status=%d\n", ofile->zs.next_in, ofile->zs.avail_in,
ofile->zs.next_out, ofile->zs.avail_out, status);
#endif
if (status == Z_OK || status == Z_STREAM_END) {
if (ofile->zs.avail_out == 0 || flush != Z_NO_FLUSH) {
int terminate
= ofile->zs.avail_in == 0 && ofile->zs.avail_out > 0;
size_t avail = ((unsigned char *) ofile->zs.next_out)
- ofile->small_buffer;
const unsigned char *where = ofile->small_buffer;
while (avail > 0) {
size_t count = fwrite(where, 1, avail, ofile->file);
if (count > 0) {
where += count;
avail -= count;
} else {
int eno = errno;
croak("fwrite in flush error %d: %s", eno,
strerror(eno));
}
}
ofile->zs.next_out = (Bytef *) ofile->small_buffer;
ofile->zs.avail_out = NYTP_FILE_SMALL_BUFFER_SIZE;
if (terminate) {
ofile->zs.avail_in = 0;
if (flush == Z_SYNC_FLUSH) {
sync_avail_out_to_ftell(ofile);
}
return;
}
} else {
ofile->zs.avail_in = 0;
return;
}
} else {
croak("deflate(%ld,%d) failed, error %d (%s) in pid %d",
(long)ofile->zs.avail_in, flush, status, ofile->zs.msg, getpid());
}
}
}
#endif
size_t
NYTP_write(NYTP_file ofile, const void *buffer, size_t len) {
#ifdef HAS_ZLIB
size_t result = 0;
#endif
ERRNO_PROBE;
if (FILE_STATE(ofile) == NYTP_FILE_STDIO) {
/* fwrite with len==0 is problematic */
/* http://www.opengroup.org/platform/resolutions/bwg98-007.html */
if (len == 0)
return len;
{
dNFTHX(ofile);
if (fwrite(buffer, 1, len, ofile->file) < 1) {
int eno = errno;
croak("fwrite error %d writing %ld bytes to fd%d: %s",
eno, (long)len, fileno(ofile->file), strerror(eno));
}
}
return len;
}
#ifdef HAS_ZLIB
else if (FILE_STATE(ofile) != NYTP_FILE_DEFLATE) {
compressed_io_croak(ofile, "NYTP_write");
return 0;
}
while (1) {
int remaining = NYTP_FILE_LARGE_BUFFER_SIZE - ofile->zs.avail_in;
unsigned char *p = ofile->large_buffer + ofile->zs.avail_in;
if (remaining >= len) {
Copy(buffer, p, len, unsigned char);
ofile->zs.avail_in += len;
result += len;
return result;
} else {
/* Copy what we can, then flush the buffer. Lather, rinse, repeat.
*/
Copy(buffer, p, remaining, unsigned char);
ofile->zs.avail_in = NYTP_FILE_LARGE_BUFFER_SIZE;
result += remaining;
len -= remaining;
buffer = (void *)(remaining + (char *)buffer);
flush_output(ofile, Z_NO_FLUSH);
}
}
#endif
}
int
NYTP_printf(NYTP_file ofile, const char *format, ...) {
int retval;
va_list args;
ERRNO_PROBE;
CROAK_IF_NOT_STDIO(ofile, "NYTP_printf");
va_start(args, format);
{
dNFTHX(ofile);
retval = vfprintf(ofile->file, format, args);
}
va_end(args);
return retval;
}
int
NYTP_flush(NYTP_file file) {
ERRNO_PROBE;
#ifdef HAS_ZLIB
if (FILE_STATE(file) == NYTP_FILE_DEFLATE) {
flush_output(file, Z_SYNC_FLUSH);
}
#endif
{
dNFTHX(file);
return fflush(file->file);
}
}
int
NYTP_eof(NYTP_file ifile) {
ERRNO_PROBE;
#ifdef HAS_ZLIB
if (FILE_STATE(ifile) == NYTP_FILE_INFLATE) {
return ifile->zlib_at_eof;
}
#endif
{
dNFTHX(ifile);
return feof(ifile->file);
}
}
const char *
NYTP_fstrerror(NYTP_file file) {
#ifdef HAS_ZLIB
if (FILE_STATE(file) == NYTP_FILE_DEFLATE || FILE_STATE(file) == NYTP_FILE_INFLATE) {
return file->zs.msg;
}
#endif
{
dNFTHX(file);
return strerror(errno);
}
}
int
NYTP_close(NYTP_file file, int discard) {
FILE *raw_file = file->file;
int result;
dNFTHX(file);
ERRNO_PROBE;
#ifdef HAS_ZLIB
if (!discard && FILE_STATE(file) == NYTP_FILE_DEFLATE) {
const double ratio = file->zs.total_in / (double) file->zs.total_out;
flush_output(file, Z_FINISH);
fprintf(raw_file, "#\n"
"# Compressed %lu bytes to %lu, ratio %f:1, data shrunk by %f%%\n",
(long)file->zs.total_in, (long)file->zs.total_out, ratio,
100 * (1 - 1 / ratio));
}
if (FILE_STATE(file) == NYTP_FILE_DEFLATE) {
int status = deflateEnd(&(file->zs));
if (status != Z_OK) {
if (discard && status == Z_DATA_ERROR) {
/* deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
stream state was inconsistent, Z_DATA_ERROR if the stream
was freed prematurely (some input or output was discarded).
*/
} else {
croak("deflateEnd failed, error %d (%s) in %d", status,
file->zs.msg, getpid());
}
}
}
else if (FILE_STATE(file) == NYTP_FILE_INFLATE) {
int err = inflateEnd(&(file->zs));
if (err != Z_OK) {
croak("inflateEnd failed, error %d (%s)", err, file->zs.msg);
}
}
#endif
Safefree(file);
result = ferror(raw_file) ? errno : 0;
if (discard) {
/* close the underlying fd first so any buffered data gets discarded
* when fclose is called below */
close(fileno(raw_file));
}
if (result || discard) {
/* Something has already gone wrong, so try to preserve its error */
fclose(raw_file);
return result;
}
return fclose(raw_file) == 0 ? 0 : errno;
}
/* ====== Low-level element I/O functions ====== */
/**
* Output an integer in bytes, optionally preceded by a tag. Use the special tag
* NYTP_TAG_NO_TAG to suppress the tag output. A wrapper macro output_u32(fh, i)
* does this for you.
* "In bytes" means output the number in binary, using the least number of bytes
* possible. All numbers are positive. Use sign slot as a marker
*/
static size_t
output_tag_u32(NYTP_file file, unsigned char tag, U32 i)
{
U8 buffer[6];
U8 *p = buffer;
if (tag != NYTP_TAG_NO_TAG)
*p++ = tag;
/* general case. handles all integers */
if (i < 0x80) { /* < 8 bits */
*p++ = (U8)i;
}
else if (i < 0x4000) { /* < 16 bits */
*p++ = (U8)((i >> 8) | 0x80);
*p++ = (U8)i;
}
else if (i < 0x200000) { /* < 24 bits */
*p++ = (U8)((i >> 16) | 0xC0);
*p++ = (U8)(i >> 8);
*p++ = (U8)i;
}
else if (i < 0x10000000) { /* < 32 bits */
*p++ = (U8)((i >> 24) | 0xE0);
*p++ = (U8)(i >> 16);
*p++ = (U8)(i >> 8);
*p++ = (U8)i;
}
else { /* need all the bytes. */
*p++ = 0xFF;
*p++ = (U8)(i >> 24);
*p++ = (U8)(i >> 16);
*p++ = (U8)(i >> 8);
*p++ = (U8)i;
}
return NYTP_write(file, buffer, p - buffer);
}
static size_t
output_tag_i32(NYTP_file file, unsigned char tag, I32 i)
{
return output_tag_u32(file, tag, *( (U32*) &i ) );
}
#define output_u32(fh, i) output_tag_u32((fh), NYTP_TAG_NO_TAG, (i))
#define output_i32(fh, i) output_tag_i32((fh), NYTP_TAG_NO_TAG, (i))
/**
* Read an integer by decompressing the next 1 to 4 bytes of binary into a 32-
* bit integer. See output_int() for the compression details.
*/
U32
read_u32(NYTP_file ifile)
{
unsigned char d;
U32 newint;
NYTP_read(ifile, &d, sizeof(d), "integer prefix");
if (d < 0x80) { /* < 8 bits */
newint = d;
}
else {
unsigned char buffer[4];
unsigned char *p = buffer;
unsigned int length;
if (d < 0xC0) { /* < 16 bits */
newint = d & 0x7F;
length = 1;
}
else if (d < 0xE0) { /* < 24 bits */
newint = d & 0x1F;
length = 2;
}
else if (d < 0xFF) { /* < 32 bits */
newint = d & 0xF;
length = 3;
}
else { /* d == 0xFF */ /* = 32 bits */
newint = 0;
length = 4;
}
NYTP_read(ifile, buffer, length, "integer");
while (length--) {
newint <<= 8;
newint |= *p++;
}
}
return newint;
}
I32
read_i32(NYTP_file ifile) {
U32 u = read_u32(ifile);
return *( (I32*)&u );
}
static size_t
output_str(NYTP_file file, const char *str, I32 len) { /* negative len signifies utf8 */
unsigned char tag = NYTP_TAG_STRING;
size_t retval;
size_t total;
if (len < 0) {
tag = NYTP_TAG_STRING_UTF8;
len = -len;
}
total = retval = output_tag_u32(file, tag, len);
if (retval <= 0)
return retval;
if (len) {
total += retval = NYTP_write(file, str, len);
if (retval <= 0)
return retval;
}
return total;
}
/**
* Output a double precision float via a simple binary write of the memory.
* (Minor portbility issues are seen as less important than speed and space.)
*/
size_t
output_nv(NYTP_file file, NV nv)
{
return NYTP_write(file, (unsigned char *)&nv, sizeof(NV));
}
/**
* Read an NV by simple byte copy to memory
*/
NV
read_nv(NYTP_file ifile)
{
NV nv;
/* no error checking on the assumption that a later token read will
* detect the error/eof condition
*/
NYTP_read(ifile, (unsigned char *)&nv, sizeof(NV), "float");
return nv;
}
/* ====== Higher-level protocol I/O functions ====== */
size_t
NYTP_write_header(NYTP_file ofile, U32 major, U32 minor)
{
return NYTP_printf(ofile, "NYTProf %u %u\n", major, minor);
}
size_t
NYTP_write_comment(NYTP_file ofile, const char *format, ...) {
size_t retval;
size_t retval2;
va_list args;
ERRNO_PROBE;
retval = NYTP_write(ofile, "#", 1);
if (retval != 1)
return retval;
va_start(args, format);
if(strEQ(format, "%s")) {
const char * const s = va_arg(args, char*);
STRLEN len = strlen(s);
retval = NYTP_write(ofile, s, len);
} else {
CROAK_IF_NOT_STDIO(ofile, "NYTP_printf");
{
dNFTHX(ofile);
retval = vfprintf(ofile->file, format, args);
}
}
va_end(args);
retval2 = NYTP_write(ofile, "\n", 1);
if (retval2 != 1)
return retval2;
return retval + 2;
}
static size_t
NYTP_write_plain_kv(NYTP_file ofile, const char prefix,
const char *key, size_t key_len,
const char *value, size_t value_len)
{
size_t total;
size_t retval;
total = retval = NYTP_write(ofile, &prefix, 1);
if (retval != 1)
return retval;
total += retval = NYTP_write(ofile, key, key_len);
if (retval != key_len)
return retval;
total += retval = NYTP_write(ofile, "=", 1);
if (retval != 1)
return retval;
total += retval = NYTP_write(ofile, value, value_len);
if (retval != value_len)
return retval;
total += retval = NYTP_write(ofile, "\n", 1);
if (retval != 1)
return retval;
return total;
}
size_t
NYTP_write_attribute_string(NYTP_file ofile,
const char *key, size_t key_len,
const char *value, size_t value_len)
{
return NYTP_write_plain_kv(ofile, ':', key, key_len, value, value_len);
}
#ifndef CHAR_BIT
# define CHAR_BIT 8
#endif
#define LOG_2_OVER_LOG_10 0.30103
size_t
NYTP_write_attribute_unsigned(NYTP_file ofile, const char *key,
size_t key_len, unsigned long value)
{
/* 3: 1 for rounding errors, 1 for the '\0' */
char buffer[(int)(sizeof (unsigned long) * CHAR_BIT * LOG_2_OVER_LOG_10 + 3)];
const size_t len = my_snprintf(buffer, sizeof(buffer), "%lu", value);
return NYTP_write_attribute_string(ofile, key, key_len, buffer, len);
}
size_t
NYTP_write_attribute_signed(NYTP_file ofile, const char *key,
size_t key_len, long value)
{
/* 3: 1 for rounding errors, 1 for the sign, 1 for the '\0' */
char buffer[(int)(sizeof (long) * CHAR_BIT * LOG_2_OVER_LOG_10 + 3)];
const size_t len = my_snprintf(buffer, sizeof(buffer), "%ld", value);
return NYTP_write_attribute_string(ofile, key, key_len, buffer, len);
}
size_t
NYTP_write_attribute_nv(NYTP_file ofile, const char *key,
size_t key_len, NV value)
{
char buffer[NV_DIG+20]; /* see Perl_sv_2pv_flags */
const size_t len = my_snprintf(buffer, sizeof(buffer), "%" NVgf, value);
return NYTP_write_attribute_string(ofile, key, key_len, buffer, len);
}
/* options */
size_t
NYTP_write_option_pv(NYTP_file ofile,
const char *key,
const char *value, size_t value_len)
{
return NYTP_write_plain_kv(ofile, '!', key, strlen(key), value, value_len);
}
size_t
NYTP_write_option_iv(NYTP_file ofile, const char *key, IV value)
{
/* 3: 1 for rounding errors, 1 for the sign, 1 for the '\0' */
char buffer[(int)(sizeof (IV) * CHAR_BIT * LOG_2_OVER_LOG_10 + 3)];
const size_t len = my_snprintf(buffer, sizeof(buffer), "%" IVdf, value);
return NYTP_write_option_pv(ofile, key, buffer, len);
}
/* other */
#ifdef HAS_ZLIB