unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Using pread
@ 2013-02-01  8:05 Dmitry Antipov
  2013-02-01  8:18 ` Paul Eggert
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Antipov @ 2013-02-01  8:05 UTC (permalink / raw)
  To: Emacs development discussions; +Cc: Paul Eggert

[-- Attachment #1: Type: text/plain, Size: 173 bytes --]

Can we assume that all supported systems has pread(2)? If yes, we can optimize
read + lseek in some cases (and even if not, it's easy to emulate it in emacs_pread).

Dmitry

[-- Attachment #2: pread.patch --]
[-- Type: text/plain, Size: 2451 bytes --]

=== modified file 'src/fileio.c'
--- src/fileio.c	2013-02-01 06:30:51 +0000
+++ src/fileio.c	2013-02-01 07:40:44 +0000
@@ -3703,17 +3703,14 @@
 	      int nread;
 
 	      if (st.st_size <= (1024 * 4))
-		nread = emacs_read (fd, read_buf, 1024 * 4);
+		nread = emacs_pread (fd, read_buf, 1024 * 4, 0);
 	      else
 		{
-		  nread = emacs_read (fd, read_buf, 1024);
+		  nread = emacs_pread (fd, read_buf, 1024, 0);
 		  if (nread == 1024)
 		    {
-		      int ntail;
-		      if (lseek (fd, - (1024 * 3), SEEK_END) < 0)
-			report_file_error ("Setting file position",
-					   Fcons (orig_filename, Qnil));
-		      ntail = emacs_read (fd, read_buf + nread, 1024 * 3);
+		      int ntail = emacs_pread (fd, read_buf + nread, 1024 * 3,
+					       st.st_size - 1024 * 3);
 		      nread = ntail < 0 ? ntail : nread + ntail;
 		    }
 		}
@@ -3753,11 +3750,6 @@
 		  /* Discard the unwind protect for recovering the
                      current buffer.  */
 		  specpdl_ptr--;
-
-		  /* Rewind the file for the actual read done later.  */
-		  if (lseek (fd, 0, SEEK_SET) < 0)
-		    report_file_error ("Setting file position",
-				       Fcons (orig_filename, Qnil));
 		}
 	    }
 

=== modified file 'src/lisp.h'
--- src/lisp.h	2013-02-01 06:30:51 +0000
+++ src/lisp.h	2013-02-01 07:33:29 +0000
@@ -3566,6 +3566,7 @@
 extern int emacs_open (const char *, int, int);
 extern int emacs_close (int);
 extern ptrdiff_t emacs_read (int, char *, ptrdiff_t);
+extern ptrdiff_t emacs_pread (int, char *, ptrdiff_t, ptrdiff_t);
 extern ptrdiff_t emacs_write (int, const char *, ptrdiff_t);
 
 extern void unlock_all_files (void);

=== modified file 'src/sysdep.c'
--- src/sysdep.c	2013-02-01 06:30:51 +0000
+++ src/sysdep.c	2013-02-01 07:35:36 +0000
@@ -2209,6 +2209,21 @@
   return (rtnval);
 }
 
+/* Read from FILEDESC to a buffer BUF with size NBYTES at offset OFFSET,
+   retrying if interrupted.  Return the number of bytes read, which might
+   be less than NBYTES.  On error, set errno and return -1.  */
+
+ptrdiff_t
+emacs_pread (int fildes, char *buf, ptrdiff_t nbytes, ptrdiff_t offset)
+{
+  register ssize_t rtnval;
+
+  while ((rtnval = pread (fildes, buf, nbytes, offset)) == -1
+	 && (errno == EINTR))
+    QUIT;
+  return rtnval;
+}
+
 /* Write to FILEDES from a buffer BUF with size NBYTE, retrying if interrupted
    or if a partial write occurs.  Return the number of bytes written, setting
    errno if this is less than NBYTE.  */


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-02-02  8:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-01  8:05 Using pread Dmitry Antipov
2013-02-01  8:18 ` Paul Eggert
2013-02-01  8:34   ` Dmitry Antipov
2013-02-01 10:33     ` Eli Zaretskii
2013-02-02  1:47     ` Paul Eggert
2013-02-02  8:09       ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).