HPCToolkit
hpcio.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // General and helper functions for reading/writing a HPC data
54 // files from/to a binary file.
55 //
56 // These routines *must not* allocate dynamic memory; if such memory
57 // is needed, callbacks to the user's allocator should be used.
58 //
59 // Description:
60 // [The set of functions, macros, etc. defined in the file]
61 //
62 //***************************************************************************
63 
64 //************************* System Include Files ****************************
65 
66 #ifndef _POSIX_SOURCE
67 # define _POSIX_SOURCE // fdopen()
68 #endif
69 #ifndef _SVID_SOURCE
70 # define _SVID_SOURCE // fputc_unlocked()
71 #endif
72 
73 #include <stdlib.h>
74 #include <string.h>
75 #include <stdio.h> // fdopen(), fputc_unlocked()
76 
77 #include <limits.h>
78 #include <unistd.h>
79 #include <fcntl.h>
80 #include <errno.h>
81 #include <sys/stat.h>
82 
83 
84 //*************************** User Include Files ****************************
85 
86 #include "hpcio.h"
87 
88 
89 
90 //*************************** Forward Declarations **************************
91 
92 
93 
94 //***************************************************************************
95 // interface operations
96 //***************************************************************************
97 
98 // See header for interface information.
99 FILE*
100 hpcio_fopen_w(const char* fnm, int overwrite)
101 {
102  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
103  int fd;
104  FILE* fs = NULL; // default return value
105 
106  if (overwrite == 0) {
107  // Open file for writing; fail if the file already exists.
108  fd = open(fnm, O_WRONLY | O_CREAT | O_EXCL, mode);
109  }
110  else if (overwrite == 1) {
111  // Open file for writing; truncate if the file already exists.
112  fd = open(fnm, O_WRONLY | O_CREAT | O_TRUNC, mode);
113  }
114  else if (overwrite == 2) {
115  // Options specific to /dev/null.
116  fd = open(fnm, O_WRONLY);
117  }
118  else {
119  return NULL; // blech
120  }
121 
122  if (fd != -1 ) {
123  // open succeeded. create a buffered stream since we
124  // will perform many small writes.
125  fs = fdopen(fd, "w");
126  }
127 
128  return fs;
129 }
130 
131 
132 // See header for interface information.
133 FILE*
134 hpcio_fopen_r(const char* fnm)
135 {
136  FILE* fs = fopen(fnm, "r");
137  return fs;
138 }
139 
140 
141 // See header for interface information.
142 FILE*
143 hpcio_fopen_rw(const char* fnm)
144 {
145  FILE* fs = fopen(fnm, "r+");
146  return fs;
147 }
148 
149 
150 // See header for interface information.
151 int
152 hpcio_fclose(FILE* fs)
153 {
154  if (fs) {
155  if (fclose(fs) == EOF) {
156  return 1;
157  }
158  }
159  return 0;
160 }
161 
162 
163 //***************************************************************************
164 // Read and write x bytes in little/big endian format. See header for
165 // interface information.
166 //
167 // Example: Read/Write a 4 byte value into/from a program value:
168 // program value: B3 B2 B1 B0 (actual memory order depends on system)
169 //
170 // little-endian file: B0 B1 B2 B3
171 // big-endian file: B3 B2 B1 B0
172 //***************************************************************************
173 
174 size_t
175 hpcio_le2_fread(uint16_t* val, FILE* fs)
176 {
177  uint16_t v = 0; // local copy of val
178  int shift = 0, num_read = 0, c;
179 
180  for (shift = 0; shift < 16; shift += 8) {
181  if ( (c = fgetc(fs)) == EOF ) { break; }
182  num_read++;
183  v = (uint16_t)(v | ((c & 0xff) << shift)); // 0, 8
184  }
185 
186  *val = v;
187  return num_read;
188 }
189 
190 
191 size_t
192 hpcio_le4_fread(uint32_t* val, FILE* fs)
193 {
194  uint32_t v = 0; // local copy of val
195  int shift = 0, num_read = 0, c;
196 
197  for (shift = 0; shift < 32; shift += 8) {
198  if ( (c = fgetc(fs)) == EOF ) { break; }
199  num_read++;
200  v |= ((uint32_t)(c & 0xff) << shift); // 0, 8, 16, 24
201  }
202 
203  *val = v;
204  return num_read;
205 }
206 
207 
208 size_t
209 hpcio_le8_fread(uint64_t* val, FILE* fs)
210 {
211  uint64_t v = 0; // local copy of val
212  int shift = 0, num_read = 0, c;
213 
214  for (shift = 0; shift < 64; shift += 8) {
215  if ( (c = fgetc(fs)) == EOF ) { break; }
216  num_read++;
217  v |= ((uint64_t)(c & 0xff) << shift); // 0, 8, 16, 24... 56
218  }
219 
220  *val = v;
221  return num_read;
222 }
223 
224 
225 //***************************************************************************
226 
227 size_t
228 hpcio_le2_fwrite(uint16_t* val, FILE* fs)
229 {
230  uint16_t v = *val; // local copy of val
231  int shift = 0, num_write = 0, c;
232 
233  for (shift = 0; shift < 16; shift += 8) {
234  c = fputc( ((v >> shift) & 0xff) , fs);
235  if (c == EOF) { break; }
236  num_write++;
237  }
238  return num_write;
239 }
240 
241 
242 size_t
243 hpcio_le4_fwrite(uint32_t* val, FILE* fs)
244 {
245  uint32_t v = *val; // local copy of val
246  int shift = 0, num_write = 0, c;
247 
248  for (shift = 0; shift < 32; shift += 8) {
249  c = fputc( ((v >> shift) & 0xff) , fs);
250  if (c == EOF) { break; }
251  num_write++;
252  }
253  return num_write;
254 }
255 
256 
257 size_t
258 hpcio_le8_fwrite(uint64_t* val, FILE* fs)
259 {
260  uint64_t v = *val; // local copy of val
261  int shift = 0, num_write = 0, c;
262 
263  for (shift = 0; shift < 64; shift += 8) {
264  c = fputc( ((v >> shift) & 0xff) , fs);
265  if (c == EOF) { break; }
266  num_write++;
267  }
268  return num_write;
269 }
270 
271 
272 //***************************************************************************
273 // Big endian
274 //***************************************************************************
275 
276 size_t
277 hpcio_be2_fread(uint16_t* val, FILE* fs)
278 {
279  uint16_t v = 0; // local copy of val
280  int shift = 0, num_read = 0, c;
281 
282  for (shift = 8; shift >= 0; shift -= 8) {
283  if ( (c = fgetc(fs)) == EOF ) { break; }
284  num_read++;
285  v = (uint16_t)(v | ((c & 0xff) << shift)); // 8, 0
286  }
287 
288  *val = v;
289  return num_read;
290 }
291 
292 
293 size_t
294 hpcio_be4_fread(uint32_t* val, FILE* fs)
295 {
296  uint32_t v = 0; // local copy of val
297  int shift = 0, num_read = 0, c;
298 
299  for (shift = 24; shift >= 0; shift -= 8) {
300  if ( (c = fgetc(fs)) == EOF ) { break; }
301  num_read++;
302  v |= ((uint32_t)(c & 0xff) << shift); // 24, 16, 8, 0
303  }
304 
305  *val = v;
306  return num_read;
307 }
308 
309 
310 size_t
311 hpcio_be8_fread(uint64_t* val, FILE* fs)
312 {
313  uint64_t v = 0; // local copy of val
314  int shift = 0, num_read = 0, c;
315 
316  for (shift = 56; shift >= 0; shift -= 8) {
317  if ( (c = fgetc(fs)) == EOF ) { break; }
318  num_read++;
319  v |= ((uint64_t)(c & 0xff) << shift); // 56, 48, 40, ... 0
320  }
321 
322  *val = v;
323  return num_read;
324 }
325 
326 
327 size_t
328 hpcio_beX_fread(uint8_t* val, size_t size, FILE* fs)
329 {
330  size_t num_read = 0;
331 
332  for (uint i = 0; i < size; ++i) {
333  int c = fgetc(fs);
334  if (c == EOF) {
335  break;
336  }
337  val[i] = (uint8_t) c;
338  num_read++;
339  }
340 
341  return num_read;
342 }
343 
344 
345 //***************************************************************************
346 
347 size_t
348 hpcio_be2_fwrite(uint16_t* val, FILE* fs)
349 {
350  uint16_t v = *val; // local copy of val
351  int shift = 0, num_write = 0, c;
352 
353  for (shift = 8; shift >= 0; shift -= 8) {
354  c = fputc( ((v >> shift) & 0xff) , fs);
355  if (c == EOF) { break; }
356  num_write++;
357  }
358  return num_write;
359 }
360 
361 
362 size_t
363 hpcio_be4_fwrite(uint32_t* val, FILE* fs)
364 {
365  uint32_t v = *val; // local copy of val
366  int shift = 0, num_write = 0, c;
367 
368  for (shift = 24; shift >= 0; shift -= 8) {
369  c = fputc( ((v >> shift) & 0xff) , fs);
370  if (c == EOF) { break; }
371  num_write++;
372  }
373  return num_write;
374 }
375 
376 
377 size_t
378 hpcio_be8_fwrite(uint64_t* val, FILE* fs)
379 {
380  uint64_t v = *val; // local copy of val
381  int shift = 0, num_write = 0, c;
382 
383  for (shift = 56; shift >= 0; shift -= 8) {
384  c = fputc( ((v >> shift) & 0xff) , fs);
385  if (c == EOF) { break; }
386  num_write++;
387  }
388  return num_write;
389 }
390 
391 
392 size_t
393 hpcio_beX_fwrite(uint8_t* val, size_t size, FILE* fs)
394 {
395  size_t num_write = 0;
396 
397  for (uint i = 0; i < size; ++i) {
398  int c = fputc(val[i], fs);
399  if (c == EOF) {
400  break;
401  }
402  num_write++;
403  }
404 
405  return num_write;
406 }
407 
408 
409 //***************************************************************************
410 //
411 //***************************************************************************
412 
413 #if 0
414 #define BIG_ENDIAN 0
415 #define LITTLE_ENDIAN 1
416 
417 // hpcio_get_endianness: Return endianness of current architecture.
418 int
419 hpcio_get_endianness()
420 {
421  uint16_t val = 0x0001;
422  char* bite = (char*) &val;
423  if (bite[0] == 0x00) {
424  return BIG_ENDIAN; // 'bite' points to most significant byte
425  }
426  else { // bite[0] == 0x01
427  return LITTLE_ENDIAN; // 'bite' points to least significant byte
428  }
429 }
430 #endif
size_t hpcio_be2_fread(uint16_t *val, FILE *fs)
Definition: hpcio.c:277
size_t hpcio_le4_fwrite(uint32_t *val, FILE *fs)
Definition: hpcio.c:243
size_t hpcio_le2_fwrite(uint16_t *val, FILE *fs)
Definition: hpcio.c:228
size_t hpcio_be2_fwrite(uint16_t *val, FILE *fs)
Definition: hpcio.c:348
size_t hpcio_le8_fread(uint64_t *val, FILE *fs)
Definition: hpcio.c:209
size_t hpcio_be8_fwrite(uint64_t *val, FILE *fs)
Definition: hpcio.c:378
size_t hpcio_le2_fread(uint16_t *val, FILE *fs)
Definition: hpcio.c:175
unsigned int uint
Definition: uint.h:124
size_t hpcio_be4_fwrite(uint32_t *val, FILE *fs)
Definition: hpcio.c:363
#define NULL
Definition: ElfHelper.cpp:85
FILE * hpcio_fopen_r(const char *fnm)
Definition: hpcio.c:134
size_t hpcio_le8_fwrite(uint64_t *val, FILE *fs)
Definition: hpcio.c:258
size_t hpcio_le4_fread(uint32_t *val, FILE *fs)
Definition: hpcio.c:192
size_t hpcio_be4_fread(uint32_t *val, FILE *fs)
Definition: hpcio.c:294
int hpcio_fclose(FILE *fs)
Definition: hpcio.c:152
size_t hpcio_be8_fread(uint64_t *val, FILE *fs)
Definition: hpcio.c:311
FILE * hpcio_fopen_rw(const char *fnm)
Definition: hpcio.c:143
size_t hpcio_beX_fread(uint8_t *val, size_t size, FILE *fs)
Definition: hpcio.c:328
FILE * hpcio_fopen_w(const char *fnm, int overwrite)
Definition: hpcio.c:100
size_t hpcio_beX_fwrite(uint8_t *val, size_t size, FILE *fs)
Definition: hpcio.c:393