File: FileHandle.xs

package info (click to toggle)
libdevel-nytprof-perl 4.04-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,124 kB
  • ctags: 1,330
  • sloc: perl: 4,392; ansic: 92; makefile: 19
file content (1312 lines) | stat: -rw-r--r-- 36,297 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
/* vim: ts=8 sw=4 expandtab:
 * ************************************************************************
 * This file is part of the Devel::NYTProf package.
 * See http://search.cpan.org/dist/Devel-NYTProf/
 * For Copyright see lib/Devel/NYTProf.pm
 * For contribution history see repository logs.
 * ************************************************************************
 */

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#if defined(PERL_IMPLICIT_SYS)
#  ifndef fgets
#    define fgets PerlSIO_fgets
#  endif
#endif

#include "FileHandle.h"
#include "NYTProf.h"

#define NEED_newRV_noinc
#define NEED_sv_2pvbyte
#define NEED_my_snprintf
#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

struct NYTP_file_t {
    FILE *file;
#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
};

/* 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
    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) {
    FILE *raw_file = fopen(name, mode);
    NYTP_file file;
    ERRNO_PROBE;

    if (!raw_file)
        return NULL;

    Newx(file, 1, struct NYTP_file_t);
    file->file = raw_file;

#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) {
    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)) {
                    dTHX;
                    croak("grab_input failed: %d (%s)", errno, strerror(errno));
                }
                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) {
#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",
              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");

    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) {
    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) {
    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 {
                        dTHX;
                        croak("fwrite in flush error %d: %s", errno,
                              strerror(errno));
                    }
                }
                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;
        if (fwrite(buffer, 1, len, ofile->file) < 1) {
            dTHX;
            croak("fwrite error %d writing %ld bytes to fd%d: %s",
                errno, (long)len, fileno(ofile->file), strerror(errno));
        }
        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);
    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
    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
    return feof(ifile->file);
}

const char *
NYTP_fstrerror(NYTP_file file) {
    dTHX;
#ifdef HAS_ZLIB
    if (FILE_STATE(file) == NYTP_FILE_DEFLATE || FILE_STATE(file) == NYTP_FILE_INFLATE) {
        return file->zs.msg;
    }
#endif
    return strerror(errno);
}

int
NYTP_close(NYTP_file file, int discard) {
    FILE *raw_file = file->file;
    int result;
    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",
                file->zs.total_in, 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;
}

/**
 * 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_int(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_int(NYTP_file file, unsigned char tag, unsigned int 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) {                        /* < 15 bits */
        *p++ = (U8)((i >> 8) | 0x80);
        *p++ = (U8)i;
    }
    else if (i < 0x200000) {                      /* < 22 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);
}

#define     output_int(fh, i)   output_tag_int((fh), NYTP_TAG_NO_TAG, (unsigned int)(i))

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_int(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));
}

size_t
NYTP_write_header(NYTP_file ofile, unsigned int major, unsigned int 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");
        retval = vfprintf(ofile->file, format, args);
    }
    va_end(args);

    retval2 = NYTP_write(ofile, "\n", 1);
    if (retval2 != 1)
        return retval2;

    return retval + 2;
}

size_t
NYTP_write_attribute_string(NYTP_file ofile,
                            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, ":", 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;
}

#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);
}

#ifdef HAS_ZLIB

size_t
NYTP_start_deflate_write_tag_comment(NYTP_file ofile, int compression_level) {
    const unsigned char tag = NYTP_TAG_START_DEFLATE;
    size_t total;
    size_t retval;

    total = retval = NYTP_write_comment(ofile, "Compressed at level %d with zlib %s",
                                        compression_level, zlibVersion());

    if (retval < 1)
        return retval;

    total += retval = NYTP_write(ofile, &tag, sizeof(tag));
    if (retval < 1)
        return retval;

    NYTP_start_deflate(ofile, compression_level);

    return total;
}

#endif

size_t
NYTP_write_process_start(NYTP_file ofile, unsigned int pid, unsigned int ppid,
                         NV time_of_day)
{
    size_t total;
    size_t retval;

    total = retval = output_tag_int(ofile, NYTP_TAG_PID_START, pid);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, ppid);
    if (retval < 1)
        return retval;

    total += retval = output_nv(ofile, time_of_day);
    if (retval < 1)
        return retval;

    return total;
}

size_t
NYTP_write_process_end(NYTP_file ofile, unsigned int pid, NV time_of_day)
{
    size_t total;
    size_t retval;

    total = retval = output_tag_int(ofile, NYTP_TAG_PID_END, pid);
    if (retval < 1)
        return retval;

    total += retval = output_nv(ofile, time_of_day);
    if (retval < 1)
        return retval;

    return total;
}

size_t
NYTP_write_sawampersand(NYTP_file ofile, unsigned int fid, unsigned int line)
{
    size_t total;
    size_t retval;

    total += retval = NYTP_write_attribute_unsigned(ofile, STR_WITH_LEN("sawampersand_fid"),  fid);
    if (retval < 1)
        return retval;

    total += retval = NYTP_write_attribute_unsigned(ofile, STR_WITH_LEN("sawampersand_line"), line);
    if (retval < 1)
        return retval;

    return total;
}

size_t
NYTP_write_new_fid(NYTP_file ofile, unsigned int id, unsigned int eval_fid,
                   unsigned int eval_line_num, unsigned int flags,
                   unsigned int size, unsigned int mtime,
                   const char *name, I32 len)
{
    size_t total;
    size_t retval;

    total = retval = output_tag_int(ofile, NYTP_TAG_NEW_FID, id);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, eval_fid);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, eval_line_num);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, flags);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, size);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, mtime);
    if (retval < 1)
        return retval;

    total += retval = output_str(ofile, name, len);
    if (retval < 1)
        return retval;

    return total;
}

static size_t
write_time_common(NYTP_file ofile, unsigned char tag, unsigned int elapsed,
                  unsigned int fid, unsigned int line)
{
    size_t total;
    size_t retval;

    total = retval = output_tag_int(ofile, tag, elapsed);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, fid);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, line);
    if (retval < 1)
        return retval;

    return total;
}

size_t
NYTP_write_time_block(NYTP_file ofile, unsigned int elapsed, unsigned int fid,
                      unsigned int line, unsigned int last_block_line,
                      unsigned int last_sub_line)
{
    size_t total;
    size_t retval;

    total = retval = write_time_common(ofile, NYTP_TAG_TIME_BLOCK, elapsed,
                                       fid, line);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, last_block_line);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, last_sub_line);
    if (retval < 1)
        return retval;

    return total;
}

size_t
NYTP_write_time_line(NYTP_file ofile, unsigned int elapsed, unsigned int fid,
                     unsigned int line)
{
    return write_time_common(ofile, NYTP_TAG_TIME_LINE, elapsed, fid, line);
}

size_t
NYTP_write_sub_info(NYTP_file ofile, unsigned int fid,
                    const char *name, I32 len,
                    unsigned int first_line, unsigned int last_line)
{
    size_t total;
    size_t retval;

    total = retval = output_tag_int(ofile, NYTP_TAG_SUB_INFO, fid);
    if (retval < 1)
        return retval;

    total += retval = output_str(ofile, name, (I32)len);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, first_line);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, last_line);
    if (retval < 1)
        return retval;

    if (retval < 1)
        return retval;

    return total;
}

size_t
NYTP_write_sub_callers(NYTP_file ofile, unsigned int fid, unsigned int line,
                       const char *caller_name, I32 caller_name_len,
                       unsigned int count, NV incl_rtime, NV excl_rtime,
                       NV reci_rtime, unsigned int depth,
                       const char *called_name, I32 called_name_len)
{
    size_t total;
    size_t retval;

    total = retval = output_tag_int(ofile, NYTP_TAG_SUB_CALLERS, fid);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, line);
    if (retval < 1)
        return retval;

    total += retval = output_str(ofile, caller_name, caller_name_len);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, count);
    if (retval < 1)
        return retval;

    total += retval = output_nv(ofile, incl_rtime);
    if (retval < 1)
        return retval;

    total += retval = output_nv(ofile, excl_rtime);
    if (retval < 1)
        return retval;

    total += retval = output_nv(ofile, reci_rtime);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, depth);
    if (retval < 1)
        return retval;

    total += retval = output_str(ofile, called_name, called_name_len);
    if (retval < 1)
        return retval;

    return total;
}

size_t
NYTP_write_src_line(NYTP_file ofile, unsigned int fid,
                    unsigned int line, const char *text, I32 text_len)
{
    size_t total;
    size_t retval;

    total = retval = output_tag_int(ofile, NYTP_TAG_SRC_LINE, fid);
    if (retval < 1)
        return retval;

    total += retval = output_int(ofile, line);
    if (retval < 1)
        return retval;

    total += retval = output_str(ofile, text, text_len);
    if (retval < 1)
        return retval;

    return total;
}

size_t
NYTP_write_discount(NYTP_file ofile)
{
    const unsigned char tag = NYTP_TAG_DISCOUNT;
    return NYTP_write(ofile, &tag, sizeof(tag));
}


MODULE = Devel::NYTProf::FileHandle     PACKAGE = Devel::NYTProf::FileHandle    PREFIX = NYTP_

PROTOTYPES: DISABLE

void
open(pathname, mode)
char *pathname
char *mode
    PREINIT:
        NYTP_file fh = NYTP_open(pathname, mode);
        SV *object;
    PPCODE:
        if(!fh)
            XSRETURN(0);
        object = newSV(0);
        sv_usepvn(object, (char *) fh, sizeof(struct NYTP_file_t));
        ST(0) = sv_bless(sv_2mortal(newRV_noinc(object)), gv_stashpvs("Devel::NYTProf::FileHandle", GV_ADD));
        XSRETURN(1);

int
DESTROY(handle)
NYTP_file handle
    ALIAS:
        close = 1
    PREINIT:
        SV *guts;
    CODE:
	guts = SvRV(ST(0));
        if (ix == ix) {
            /* Unused argument.  */
        }
        RETVAL = NYTP_close(handle, 0);
        SvPV_set(guts, NULL);
        SvLEN_set(guts, 0);
    OUTPUT:
        RETVAL

size_t
write(handle, string)
NYTP_file handle
SV *string
    PREINIT:
        STRLEN len;
        char *p;
    CODE:
        p = SvPVbyte(string, len);
        RETVAL = NYTP_write(handle, p, len);
    OUTPUT:
        RETVAL

#ifdef HAS_ZLIB

void
NYTP_start_deflate(handle, compression_level = 6)
NYTP_file handle
int compression_level

void
NYTP_start_deflate_write_tag_comment(handle, compression_level = 6)
NYTP_file handle
int compression_level

#endif

size_t
NYTP_write_comment(handle, comment)
NYTP_file handle
char *comment
    CODE:
        RETVAL = NYTP_write_comment(handle, "%s", comment);
    OUTPUT:
        RETVAL

size_t
NYTP_write_attribute(handle, key, value)
NYTP_file handle
SV *key
SV *value
    PREINIT:
        STRLEN key_len;
        const char *const key_p = SvPVbyte(key, key_len);
        STRLEN value_len;
        const char *const value_p = SvPVbyte(value, value_len);
    CODE:
        RETVAL = NYTP_write_attribute_string(handle, key_p, key_len, value_p, value_len);
    OUTPUT:
        RETVAL

size_t
NYTP_write_process_start(handle, pid, ppid, time_of_day)
NYTP_file handle
unsigned int pid
unsigned int ppid
NV time_of_day

size_t
NYTP_write_process_end(handle, pid, time_of_day)
NYTP_file handle
unsigned int pid
NV time_of_day

size_t
NYTP_write_new_fid(handle, id, eval_fid, eval_line_num, flags, size, mtime, name)
NYTP_file handle
unsigned int id
unsigned int eval_fid
int eval_line_num
unsigned int flags
unsigned int size
unsigned int mtime
SV *name
    PREINIT:
        STRLEN len;
        const char *const p = SvPV(name, len);
    CODE:
        RETVAL = NYTP_write_new_fid(handle, id, eval_fid, eval_line_num,
                                    flags, size, mtime, p,
                                    SvUTF8(name) ? -(I32)len : (I32)len );
    OUTPUT:
        RETVAL

size_t
NYTP_write_time_block(handle, elapsed, fid, line, last_block_line, last_sub_line)
NYTP_file handle
unsigned int elapsed
unsigned int fid
unsigned int line
unsigned int last_block_line
unsigned int last_sub_line

size_t
NYTP_write_time_line(handle, elapsed, fid, line)
NYTP_file handle
unsigned int elapsed
unsigned int fid
unsigned int line

size_t
NYTP_write_sub_info(handle, fid, name, first_line, last_line)
NYTP_file handle
unsigned int fid
SV *name
unsigned int first_line
unsigned int last_line
    PREINIT:
        STRLEN len;
        const char *const p = SvPV(name, len);
    CODE:
        RETVAL = NYTP_write_sub_info(handle, fid,
                                     p, SvUTF8(name) ? -(I32)len : (I32)len,
                                     first_line, last_line);
    OUTPUT:
        RETVAL

size_t
NYTP_write_sub_callers(handle, fid, line, caller, count, incl_rtime, excl_rtime, reci_rtime, depth, called_sub)
NYTP_file handle
unsigned int fid
unsigned int line
SV *caller
unsigned int count
NV incl_rtime
NV excl_rtime
NV reci_rtime
unsigned int depth
SV *called_sub
    PREINIT:
        STRLEN caller_len;
        const char *const caller_p = SvPV(caller, caller_len);
        STRLEN called_len;
        const char *const called_p = SvPV(called_sub, called_len);
    CODE:
        RETVAL = NYTP_write_sub_callers(handle, fid, line, caller_p,
                                        SvUTF8(caller) ? -(I32)caller_len : (I32)caller_len,
                                        count, incl_rtime, excl_rtime,
                                        reci_rtime, depth, called_p,
                                        SvUTF8(called_sub) ? -(I32)called_len : (I32)called_len);
    OUTPUT:
        RETVAL

size_t
NYTP_write_src_line(handle, fid,  line, text)
NYTP_file handle
unsigned int fid
unsigned int line
SV *text
    PREINIT:
        STRLEN len;
        const char *const p = SvPV(text, len);
    CODE:
        RETVAL = NYTP_write_src_line(handle, fid, line,
                                     p, SvUTF8(text) ? -(I32)len : (I32)len);
    OUTPUT:
        RETVAL

size_t
NYTP_write_discount(handle)
NYTP_file handle

size_t
NYTP_write_header(handle, major, minor)
NYTP_file handle
unsigned int major
unsigned int minor