[m-rev.] for review: always use 64-bit offsets with fseek() and ftell()

Julien Fischer jfischer at opturion.com
Mon Oct 7 16:47:14 AEDT 2019


For review by anyone.

This builds on my change from Friday.

-------------------------------

Always use 64-bit offsets with fseek() and ftell().

Most sane systems use 64-bit offsets with those functions; Windows does not.

runtime/mercury_file.h:
    Define macros MR_fseek and MR_ftell that expand to the name of the
    fseek or ftell function that uses 64-bit offsets.

runtime/mercury_deep_profiling.c:
library/io.m:
     Use the new macros and always use 64-bit offsets.

Julien.

diff --git a/library/io.m b/library/io.m
index 2938d48..553c986 100644
--- a/library/io.m
+++ b/library/io.m
@@ -9414,7 +9414,7 @@ seek64_binary_output(binary_output_stream(Stream), Whence, Offset, !IO) :-

      // XXX check if the stream is seekable.
      if (MR_IS_FILE_STREAM(*Stream)) {
-        if (fseek(MR_file(*Stream), Off, seek_flags[Flag]) < 0) {
+        if (MR_fseek(MR_file(*Stream), Off, seek_flags[Flag]) < 0) {
              Error = errno;
          } else {
              Error = 0;
@@ -9489,7 +9489,7 @@ binary_output_stream_offset64(binary_output_stream(Stream), Offset, !IO) :-
  "
      // XXX should check if the stream is tellable
      if (MR_IS_FILE_STREAM(*Stream)) {
-        Offset = ftell(MR_file(*Stream));
+        Offset = MR_ftell(MR_file(*Stream));
          if (Offset < 0) {
              Error = errno;
          } else {
diff --git a/runtime/mercury_deep_profiling.c b/runtime/mercury_deep_profiling.c
index c8a5798..1d270bb 100644
--- a/runtime/mercury_deep_profiling.c
+++ b/runtime/mercury_deep_profiling.c
@@ -22,10 +22,12 @@
  #include "mercury_runtime_util.h"
  #include "mercury_deep_profiling.h"
  #include "mercury_deep_profiling_hand.h"
+#include "mercury_file.h"

  #ifdef MR_DEEP_PROFILING

  #include <stdio.h>
+#include <stdint.h>
  #include <errno.h>

  #ifdef  MR_EXEC_TRACE
@@ -373,7 +375,7 @@ MR_write_out_profiling_tree(void)
      FILE                    *check_fp;
      int                     ticks_per_sec;
      unsigned                num_call_seqs;
-    long                    table_sizes_offset;
+    int64_t                 table_sizes_offset;
      char                    errbuf[MR_STRERROR_BUF_SIZE];

  #ifdef MR_DEEP_PROFILING_STATISTICS
@@ -409,7 +411,7 @@ MR_write_out_profiling_tree(void)
      MR_write_out_deep_flags(deep_fp, MR_FALSE);

      // We overwrite these zeros after seeking back to table_sizes_offset.
-    table_sizes_offset = ftell(deep_fp);
+    table_sizes_offset = MR_ftell(deep_fp);
      if (table_sizes_offset == -1) {
          MR_deep_data_output_error("ftell failed for ",
              MR_MDPROF_DATA_FILENAME);
@@ -464,7 +466,7 @@ MR_write_out_profiling_tree(void)
          MR_address_of_write_out_proc_statics != NULL);
      (*MR_address_of_write_out_proc_statics)(deep_fp, procrep_fp);

-    if (fseek(deep_fp, table_sizes_offset, SEEK_SET) != 0) {
+    if (MR_fseek(deep_fp, table_sizes_offset, SEEK_SET) != 0) {
          MR_deep_data_output_error("cannot seek to header of",
              MR_MDPROF_DATA_FILENAME);
      }
diff --git a/runtime/mercury_file.h b/runtime/mercury_file.h
index 460bd35..3af07bd 100644
--- a/runtime/mercury_file.h
+++ b/runtime/mercury_file.h
@@ -9,6 +9,22 @@

  #include "mercury_library_types.h"

+// MR_fseek and MR_ftell expand to the name of the versions of fseek()
+// and ftell() that use 64-bit offsets.
+//
+#if defined(MR_WIN32) && !defined(MR_CYGWIN)
+    #if defined(MR_MINGW)
+       #define MR_fseek fseeko64
+       #define MR_ftell ftello64
+    #else
+       #define MR_fseek _fseeki64
+       #define MR_ftell _ftelli64
+    #endif
+#else
+   #define MR_fseek fseek
+   #define MR_ftell ftell
+#endif
+
  // Initialise a MercuryFile structure to use the C stdlib FILE *type.

  void MR_mercuryfile_init(FILE *file, int line_number, MercuryFile *mf);


More information about the reviews mailing list