This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch, libfortran, 2/3] Introduce a ssize member function


Hi,

the attached patch introduces a new "member function" ssize in order
to determine the size of a unit. The benefit is that with unbuffered
IO, when getting the size of a file we can replace 3 lseek() calls
with one fstat() call, and also that for unseekable file we get a size
of 0 rather than -1 (which would indicate that the syscall failed).

Regtested on x86_64-unknown-linux-gnu, Ok for trunk?

2011-10-18  Janne Blomqvist  <jb@gcc.gnu.org>

	* io/unix.h (struct stream): Add size function pointer.
	(ssize): New inline function.
	(file_length): Remove prototype.
	* io/unix.c (raw_size): New function.
	(raw_init): Initialize st.size pointer.
	(buf_size): New function.
	(buf_init): Initialize st.size pointer.
	(open_internal): Likewise.
	(open_internal4): Likewise.
	(file_length): Remove function.
	* io/file_pos.c (st_rewind): Use ssize instead of file_length.
	* io/open.c (test_endfile): Likewise.
	* io/transfer.c (data_transfer_init): Likewise.
	(next_record_r): Likewise.
	(next_record_w): Likewise.
	* io/unit.c (update_position): Likewise.


-- 
Janne Blomqvist
diff --git a/libgfortran/io/file_pos.c b/libgfortran/io/file_pos.c
index 2caf601..c8ecc3a 100644
--- a/libgfortran/io/file_pos.c
+++ b/libgfortran/io/file_pos.c
@@ -408,7 +408,7 @@ st_rewind (st_parameter_filepos *fpp)
 	    generate_error (&fpp->common, LIBERROR_OS, NULL);
 
 	  /* Set this for compatibilty with g77 for /dev/null.  */
-	  if (file_length (u->s) == 0)
+	  if (ssize (u->s) == 0)
 	    u->endfile = AT_ENDFILE;
 	  else
 	    {
diff --git a/libgfortran/io/open.c b/libgfortran/io/open.c
index b26d14d..0102b9c 100644
--- a/libgfortran/io/open.c
+++ b/libgfortran/io/open.c
@@ -153,7 +153,7 @@ static const st_option async_opt[] =
 static void
 test_endfile (gfc_unit * u)
 {
-  if (u->endfile == NO_ENDFILE && file_length (u->s) == stell (u->s))
+  if (u->endfile == NO_ENDFILE && ssize (u->s) == stell (u->s))
     u->endfile = AT_ENDFILE;
 }
 
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index 1e054f8..26263ae 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -2627,7 +2627,7 @@ data_transfer_init (st_parameter_dt *dtp, int read_flag)
 	 a partial record needs to exist.  */
 
       if (dtp->u.p.mode == READING && (dtp->rec - 1)
-	  * dtp->u.p.current_unit->recl >= file_length (dtp->u.p.current_unit->s))
+	  * dtp->u.p.current_unit->recl >= ssize (dtp->u.p.current_unit->s))
 	{
 	  generate_error (&dtp->common, LIBERROR_BAD_OPTION,
 			  "Non-existing record number");
@@ -2944,7 +2944,7 @@ next_record_r (st_parameter_dt *dtp, int done)
 	    {
 	      bytes_left = (int) dtp->u.p.current_unit->bytes_left;
 	      bytes_left = min_off (bytes_left, 
-		      file_length (dtp->u.p.current_unit->s)
+		      ssize (dtp->u.p.current_unit->s)
 		      - stell (dtp->u.p.current_unit->s));
 	      if (sseek (dtp->u.p.current_unit->s, 
 			 bytes_left, SEEK_CUR) < 0)
@@ -3309,7 +3309,7 @@ next_record_w (st_parameter_dt *dtp, int done)
 	    {
 	      dtp->u.p.current_unit->strm_pos += len;
 	      if (dtp->u.p.current_unit->strm_pos
-		  < file_length (dtp->u.p.current_unit->s))
+		  < ssize (dtp->u.p.current_unit->s))
 		unit_truncate (dtp->u.p.current_unit,
                                dtp->u.p.current_unit->strm_pos - 1,
                                &dtp->common);
diff --git a/libgfortran/io/unit.c b/libgfortran/io/unit.c
index d2fb6d0..1d36214 100644
--- a/libgfortran/io/unit.c
+++ b/libgfortran/io/unit.c
@@ -719,7 +719,7 @@ update_position (gfc_unit *u)
     return;
   else if (cur == 0)
     u->flags.position = POSITION_REWIND;
-  else if (file_length (u->s) == cur)
+  else if (ssize (u->s) == cur)
     u->flags.position = POSITION_APPEND;
   else
     u->flags.position = POSITION_ASIS;
diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c
index 00f7c72..c87be13 100644
--- a/libgfortran/io/unix.c
+++ b/libgfortran/io/unix.c
@@ -332,6 +332,16 @@ raw_tell (unix_stream * s)
   return lseek (s->fd, 0, SEEK_CUR);
 }
 
+static gfc_offset
+raw_size (unix_stream * s)
+{
+  struct stat statbuf;
+  int ret = fstat (s->fd, &statbuf);
+  if (ret == -1)
+    return ret;
+  return statbuf.st_size;
+}
+
 static int
 raw_truncate (unix_stream * s, gfc_offset length)
 {
@@ -398,6 +408,7 @@ raw_init (unix_stream * s)
   s->st.write = (void *) raw_write;
   s->st.seek = (void *) raw_seek;
   s->st.tell = (void *) raw_tell;
+  s->st.size = (void *) raw_size;
   s->st.trunc = (void *) raw_truncate;
   s->st.close = (void *) raw_close;
   s->st.flush = (void *) raw_flush;
@@ -584,6 +595,12 @@ buf_tell (unix_stream * s)
   return buf_seek (s, 0, SEEK_CUR);
 }
 
+static gfc_offset
+buf_size (unix_stream * s)
+{
+  return s->file_length;
+}
+
 static int
 buf_truncate (unix_stream * s, gfc_offset length)
 {
@@ -613,6 +630,7 @@ buf_init (unix_stream * s)
   s->st.write = (void *) buf_write;
   s->st.seek = (void *) buf_seek;
   s->st.tell = (void *) buf_tell;
+  s->st.size = (void *) buf_size;
   s->st.trunc = (void *) buf_truncate;
   s->st.close = (void *) buf_close;
   s->st.flush = (void *) buf_flush;
@@ -884,6 +902,9 @@ open_internal (char *base, int length, gfc_offset offset)
   s->st.close = (void *) mem_close;
   s->st.seek = (void *) mem_seek;
   s->st.tell = (void *) mem_tell;
+  /* buf_size is not a typo, we just reuse an identical
+     implementation.  */
+  s->st.size = (void *) buf_size;
   s->st.trunc = (void *) mem_truncate;
   s->st.read = (void *) mem_read;
   s->st.write = (void *) mem_write;
@@ -912,6 +933,9 @@ open_internal4 (char *base, int length, gfc_offset offset)
   s->st.close = (void *) mem_close;
   s->st.seek = (void *) mem_seek;
   s->st.tell = (void *) mem_tell;
+  /* buf_size is not a typo, we just reuse an identical
+     implementation.  */
+  s->st.size = (void *) buf_size;
   s->st.trunc = (void *) mem_truncate;
   s->st.read = (void *) mem_read4;
   s->st.write = (void *) mem_write4;
@@ -1740,21 +1764,6 @@ inquire_readwrite (const char *string, int len)
 }
 
 
-/* file_length()-- Return the file length in bytes, -1 if unknown */
-
-gfc_offset
-file_length (stream * s)
-{
-  gfc_offset curr, end;
-  curr = stell (s);
-  if (curr == -1)
-    return curr;
-  end = sseek (s, 0, SEEK_END);
-  sseek (s, curr, SEEK_SET);
-  return end;
-}
-
-
 int
 stream_isatty (stream *s)
 {
diff --git a/libgfortran/io/unix.h b/libgfortran/io/unix.h
index 08c83e4..52f3e0c 100644
--- a/libgfortran/io/unix.h
+++ b/libgfortran/io/unix.h
@@ -35,6 +35,7 @@ struct stream
   ssize_t (*write) (struct stream *, const void *, ssize_t);
   gfc_offset (*seek) (struct stream *, gfc_offset, int);
   gfc_offset (*tell) (struct stream *);
+  gfc_offset (*size) (struct stream *);
   /* Avoid keyword truncate due to AIX namespace collision.  */
   int (*trunc) (struct stream *, gfc_offset);
   int (*flush) (struct stream *);
@@ -67,6 +68,12 @@ stell (stream * s)
   return s->tell (s);
 }
 
+static inline gfc_offset
+ssize (stream * s)
+{
+  return s->size (s);
+}
+
 static inline int
 struncate (stream * s, gfc_offset length)
 {
@@ -155,9 +162,6 @@ internal_proto(inquire_write);
 extern const char *inquire_readwrite (const char *, int);
 internal_proto(inquire_readwrite);
 
-extern gfc_offset file_length (stream *);
-internal_proto(file_length);
-
 extern void flush_if_preconnected (stream *);
 internal_proto(flush_if_preconnected);
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]