ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcollate.c
(Generate patch)

Comparing ray/src/util/rcollate.c (file contents):
Revision 2.19 by greg, Thu Sep 18 00:12:42 2014 UTC vs.
Revision 2.29 by greg, Sat Oct 20 15:02:51 2018 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include "platform.h"
12   #include "rtio.h"
13   #include "resolu.h"
14 < #ifdef _WIN32
15 < #undef ftello
16 < #define ftello  ftell
17 < #undef ssize_t
18 < #define ssize_t size_t
14 > #if defined(_WIN32) || defined(_WIN64)
15 >  #undef ftello
16 >  #define       ftello  ftell
17 >  #undef ssize_t
18 >  #define ssize_t       size_t
19   #else
20 < #include <sys/mman.h>
20 >  #include <sys/mman.h>
21   #endif
22  
23 #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
24 #undef getc
25 #undef putc
26 #define getc    getc_unlocked
27 #define putc    putc_unlocked
28 #endif
29
23   typedef struct {
24 +        void    *mapped;        /* memory-mapped pointer */
25          void    *base;          /* pointer to base memory */
26          size_t  len;            /* allocated memory length */
33        int     mapped;         /* memory-mapped file? */
27   } MEMLOAD;              /* file loaded/mapped into memory */
28  
29   typedef struct {
# Line 49 | Line 42 | free_load(MEMLOAD *mp)
42                  return;
43   #ifdef MAP_FILE
44          if (mp->mapped)
45 <                munmap(mp->base, mp->len);
45 >                munmap(mp->mapped, mp->len);
46          else
47   #endif
48                  free(mp->base);
49 +        mp->mapped = NULL;
50          mp->base = NULL;
51          mp->len = 0;
52   }
53  
60 /* load a file into memory */
61 static int
62 load_file(MEMLOAD *mp, FILE *fp)
63 {
64        int     fd;
65        off_t   skip, flen;
66
67        if (mp == NULL)
68                return(-1);
69        mp->base = NULL;
70        mp->len = 0;
71        mp->mapped = 0;
72        if (fp == NULL)
73                return(-1);
74        fd = fileno(fp);
75        skip = ftello(fp);
76        flen = lseek(fd, 0, SEEK_END);
77        if (flen <= skip)
78                return((int)(flen - skip));
79        mp->len = (size_t)(flen - skip);
80 #ifdef MAP_FILE
81        if (mp->len > 1L<<20) {         /* map file if > 1 MByte */
82                mp->base = mmap(NULL, mp->len, PROT_READ, MAP_PRIVATE, fd, skip);
83                if (mp->base != MAP_FAILED) {
84                        mp->mapped = 1;
85                        return(1);      /* mmap() success */
86                }
87                mp->base = NULL;        /* fall back to reading it in... */
88        }
89 #endif
90        if (lseek(fd, skip, SEEK_SET) != skip ||
91                        (mp->base = malloc(mp->len)) == NULL) {
92                mp->len = 0;
93                return(-1);
94        }
95        if (read(fd, (char *)mp->base, mp->len) != mp->len) {
96                free_load(mp);
97                return(-1);
98        }
99        return(1);
100 }
101
54   /* load memory from an input stream, starting from current position */
55   static int
56   load_stream(MEMLOAD *mp, FILE *fp)
# Line 109 | Line 61 | load_stream(MEMLOAD *mp, FILE *fp)
61  
62          if (mp == NULL)
63                  return(-1);
64 +        mp->mapped = NULL;
65          mp->base = NULL;
66          mp->len = 0;
114        mp->mapped = 0;
67          if (fp == NULL)
68                  return(-1);
69          while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) {
70                  if (!alloced)
71 <                        mp->base = malloc(nr);
71 >                        mp->base = malloc(alloced = nr);
72                  else if (mp->len+nr > alloced)
73                          mp->base = realloc(mp->base,
74                                  alloced = alloced*(2+(nr==sizeof(buf)))/2+nr);
# Line 134 | Line 86 | load_stream(MEMLOAD *mp, FILE *fp)
86          return(mp->len > 0);
87   }
88  
89 + /* load a file into memory */
90 + static int
91 + load_file(MEMLOAD *mp, FILE *fp)
92 + {
93 +        int     fd;
94 +        off_t   skip, flen, fpos;
95 +
96 + #if defined(_WIN32) || defined(_WIN64)
97 +                                /* too difficult to fix this */
98 +        return load_stream(mp, fp);
99 + #endif
100 +        if (mp == NULL)
101 +                return(-1);
102 +        mp->mapped = NULL;
103 +        mp->base = NULL;
104 +        mp->len = 0;
105 +        if (fp == NULL)
106 +                return(-1);
107 +        fd = fileno(fp);
108 +        skip = ftello(fp);
109 +        flen = lseek(fd, 0, SEEK_END);
110 +        if (flen <= skip)
111 +                return((int)(flen - skip));
112 +        mp->len = (size_t)(flen - skip);
113 + #ifdef MAP_FILE
114 +        if (mp->len > 1L<<20) {         /* map file if > 1 MByte */
115 +                mp->mapped = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0);
116 +                if (mp->mapped != MAP_FAILED) {
117 +                        mp->base = (char *)mp->mapped + skip;
118 +                        return(1);      /* mmap() success */
119 +                }
120 +                mp->mapped = NULL;      /* else fall back to reading it in... */
121 +        }
122 + #endif
123 +        if (lseek(fd, skip, SEEK_SET) != skip ||
124 +                        (mp->base = malloc(mp->len)) == NULL) {
125 +                mp->len = 0;
126 +                return(-1);
127 +        }
128 +        fpos = skip;
129 +        while (fpos < flen) {           /* read() fails if n > 2 GBytes */
130 +                ssize_t nread = read(fd, (char *)mp->base+(fpos-skip),
131 +                                (flen-fpos < 1L<<24) ? flen-fpos : 1L<<24);
132 +                if (nread <= 0) {
133 +                        free_load(mp);
134 +                        return(-1);
135 +                }
136 +                fpos += nread;
137 +        }
138 +        return(1);
139 + }
140 +
141   /* free a record index */
142   #define free_records(rp)        free(rp)
143  
# Line 141 | Line 145 | load_stream(MEMLOAD *mp, FILE *fp)
145   static RECINDEX *
146   index_records(const MEMLOAD *mp, int nw_rec)
147   {
148 +        int             nall = 0;
149          RECINDEX        *rp;
150          char            *cp, *mend;
151          int             n;
# Line 149 | Line 154 | index_records(const MEMLOAD *mp, int nw_rec)
154                  return(NULL);
155          if (nw_rec <= 0)
156                  return(NULL);
157 <        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + mp->len/(2*nw_rec)*sizeof(char *));
157 >        nall = 1000;
158 >        rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *));
159          if (rp == NULL)
160                  return(NULL);
161          rp->nw_rec = nw_rec;
# Line 161 | Line 167 | index_records(const MEMLOAD *mp, int nw_rec)
167                          ++cp;
168                  if (cp >= mend)
169                          break;
170 +                if (rp->nrecs >= nall) {
171 +                        nall += nall>>1;        /* get more record space */
172 +                        rp = (RECINDEX *)realloc(rp,
173 +                                        sizeof(RECINDEX) + nall*sizeof(char *));
174 +                        if (rp == NULL)
175 +                                return(NULL);
176 +                }
177                  rp->rec[rp->nrecs++] = cp;      /* point to first non-white */
178                  n = rp->nw_rec;
179                  while (++cp < mend)             /* find end of record */
# Line 356 | Line 369 | do_transpose(const MEMLOAD *mp)
369                          print_record(rp, j*ni_columns + i);
370                          putc(tabEOL[j >= no_columns-1], stdout);
371                  } else {                        /* binary output */
372 <                        fwrite((char *)mp->base +
373 <                                        (n_comp*comp_size)*(j*ni_columns + i),
374 <                                        n_comp*comp_size, 1, stdout);
372 >                        putbinary((char *)mp->base +
373 >                                (size_t)(n_comp*comp_size)*(j*ni_columns + i),
374 >                                        comp_size, n_comp, stdout);
375                  }
376              if (ferror(stdout)) {
377                  fprintf(stderr, "Error writing to stdout\n");
# Line 435 | Line 448 | done:
448   static int
449   headline(char *s, void *p)
450   {
451 <        static char     fmt[32];
451 >        static char     fmt[MAXFMTLEN];
452          int             n;
453  
454          if (formatval(fmt, s)) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines