bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Optimal buffer size for copy


From: neillm
Subject: Re: Optimal buffer size for copy
Date: Thu, 6 Nov 2003 13:19:19 -0600
User-agent: Mutt/1.5.4i

Hello,

On Wed, Nov 05, 2003 at 04:35:50PM -0800, Paul Eggert wrote:
> I'd put it into the lib directory, so that the code can be shared.
> Also, it should take and return size_t, not unsigned int.

Below is a proposed patch that addresses these issues.  Again, your
comments are appreciated.


Best regards,
-Neill.



diff -N -P -r -u --exclude=Makefile.in --exclude=Makefile --exclude='conf*' 
--exclude='auto*' --exclude='*doc*' coreutils-5.0.91/aclocal.m4 
coreutils-5.0.91-patched/aclocal.m4
--- coreutils-5.0.91/aclocal.m4 2003-09-05 07:26:49.000000000 -0500
+++ coreutils-5.0.91-patched/aclocal.m4 2003-11-06 12:28:22.000000000 -0600
@@ -1,4 +1,4 @@
-# generated automatically by aclocal 1.7.6b -*- Autoconf -*-
+# generated automatically by aclocal 1.7.8 -*- Autoconf -*-
 
 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
 # Free Software Foundation, Inc.
@@ -163,7 +163,7 @@
 # Call AM_AUTOMAKE_VERSION so it can be traced.
 # This function is AC_REQUIREd by AC_INIT_AUTOMAKE.
 AC_DEFUN([AM_SET_CURRENT_AUTOMAKE_VERSION],
-        [AM_AUTOMAKE_VERSION([1.7.6b])])
+        [AM_AUTOMAKE_VERSION([1.7.8])])
 
 # Helper functions for option handling.                    -*- Autoconf -*-
 
diff -N -P -r -u --exclude=Makefile.in --exclude=Makefile --exclude='conf*' 
--exclude='auto*' --exclude='*doc*' coreutils-5.0.91/lib/gcd-lcm.c 
coreutils-5.0.91-patched/lib/gcd-lcm.c
--- coreutils-5.0.91/lib/gcd-lcm.c      1969-12-31 18:00:00.000000000 -0600
+++ coreutils-5.0.91-patched/lib/gcd-lcm.c      2003-11-06 10:47:04.000000000 
-0600
@@ -0,0 +1,53 @@
+/* gcd-lcm.c - gcd and lcm routines
+
+   Copyright (C) 2003 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+   Written by Paul Eggert.
+*/
+
+#include <sys/types.h>
+
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t) -1)
+#endif
+
+/* Compute the greatest common denominator of U and V
+   using Euclid's algorithm.  */
+
+size_t
+gcd (size_t u, size_t v)
+{
+  size_t t;
+  while (v != 0)
+    {
+      t = u % v;
+      u = v;
+      v = t;
+    }
+  return u;
+}
+
+/* Compute the least common multiple of U and V.  */
+
+size_t
+lcm (size_t u, size_t v)
+{
+  size_t t = gcd (u, v);
+  if ((t == 0) || (u > (SIZE_MAX / v)))
+    return 0;
+  return u * v / t;
+}
diff -N -P -r -u --exclude=Makefile.in --exclude=Makefile --exclude='conf*' 
--exclude='auto*' --exclude='*doc*' coreutils-5.0.91/lib/gcd-lcm.h 
coreutils-5.0.91-patched/lib/gcd-lcm.h
--- coreutils-5.0.91/lib/gcd-lcm.h      1969-12-31 18:00:00.000000000 -0600
+++ coreutils-5.0.91-patched/lib/gcd-lcm.h      2003-11-06 10:47:04.000000000 
-0600
@@ -0,0 +1,23 @@
+/* gcd-lcm.c - gcd and lcm routines
+
+   Copyright (C) 2003 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+   Written by Paul Eggert.
+*/
+
+size_t gcd (size_t u, size_t v);
+size_t lcm (size_t u, size_t v);
diff -N -P -r -u --exclude=Makefile.in --exclude=Makefile --exclude='conf*' 
--exclude='auto*' --exclude='*doc*' coreutils-5.0.91/lib/Makefile.am 
coreutils-5.0.91-patched/lib/Makefile.am
--- coreutils-5.0.91/lib/Makefile.am    2003-08-17 02:51:23.000000000 -0500
+++ coreutils-5.0.91-patched/lib/Makefile.am    2003-11-06 12:24:06.000000000 
-0600
@@ -61,6 +61,7 @@
   ftw_.h \
   full-read.c full-read.h \
   full-write.c full-write.h \
+  gcd-lcm.c gcd-lcm.h \
   getline.h \
   getpagesize.h \
   gettime.c \
diff -N -P -r -u --exclude=Makefile.in --exclude=Makefile --exclude='conf*' 
--exclude='auto*' --exclude='*doc*' coreutils-5.0.91/src/copy.c 
coreutils-5.0.91-patched/src/copy.c
--- coreutils-5.0.91/src/copy.c 2003-08-30 10:57:32.000000000 -0500
+++ coreutils-5.0.91-patched/src/copy.c 2003-11-06 12:42:08.000000000 -0600
@@ -42,6 +42,7 @@
 #include "same.h"
 #include "utimens.h"
 #include "xreadlink.h"
+#include "gcd-lcm.h"
 
 #define DO_CHOWN(Chown, File, New_uid, New_gid)                                
\
   (Chown (File, New_uid, New_gid)                                      \
@@ -205,7 +206,7 @@
          struct stat const *src_sb)
 {
   char *buf;
-  int buf_size;
+  size_t buf_size;
   int dest_desc;
   int source_desc;
   struct stat sb;
@@ -216,6 +217,8 @@
   off_t n_read_total = 0;
   int last_write_made_hole = 0;
   int make_holes = (x->sparse_mode == SPARSE_ALWAYS);
+  size_t max_block_size = 0;
+  size_t min_block_size = 0;
 
   source_desc = open (src_path, O_RDONLY);
   if (source_desc < 0)
@@ -285,7 +288,23 @@
       goto close_src_and_dst_desc;
     }
 
-  buf_size = ST_BLKSIZE (sb);
+  if (ST_BLKSIZE (sb) < ST_BLKSIZE (src_open_sb))
+    {
+      min_block_size = ST_BLKSIZE (sb);
+      max_block_size = ST_BLKSIZE (src_open_sb);
+    }
+  else
+    {
+      min_block_size = ST_BLKSIZE (src_open_sb);
+      max_block_size = ST_BLKSIZE (sb);
+    }
+
+  buf_size = lcm (min_block_size, max_block_size);
+  if (buf_size == 0)
+    {
+      buf_size = (((max_block_size % min_block_size) == 0) ?
+                  max_block_size : min_block_size);
+    }
 
 #if HAVE_STRUCT_STAT_ST_BLOCKS
   if (x->sparse_mode == SPARSE_AUTO && S_ISREG (sb.st_mode))
diff -N -P -r -u --exclude=Makefile.in --exclude=Makefile --exclude='conf*' 
--exclude='auto*' --exclude='*doc*' coreutils-5.0.91/src/od.c 
coreutils-5.0.91-patched/src/od.c
--- coreutils-5.0.91/src/od.c   2003-07-23 02:26:48.000000000 -0500
+++ coreutils-5.0.91-patched/src/od.c   2003-11-06 10:47:14.000000000 -0600
@@ -27,6 +27,7 @@
 #include "error.h"
 #include "posixver.h"
 #include "xstrtol.h"
+#include "gcd-lcm.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "od"
@@ -371,33 +372,6 @@
   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
-/* Compute the greatest common denominator of U and V
-   using Euclid's algorithm.  */
-
-static unsigned int
-gcd (unsigned int u, unsigned int v)
-{
-  unsigned int t;
-  while (v != 0)
-    {
-      t = u % v;
-      u = v;
-      v = t;
-    }
-  return u;
-}
-
-/* Compute the least common multiple of U and V.  */
-
-static unsigned int
-lcm (unsigned int u, unsigned int v)
-{
-  unsigned int t = gcd (u, v);
-  if (t == 0)
-    return 0;
-  return u * v / t;
-}
-
 static void
 print_s_char (size_t n_bytes, const char *block, const char *fmt_string)
 {




reply via email to

[Prev in Thread] Current Thread [Next in Thread]