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: Wed, 5 Nov 2003 17:13:18 -0600
User-agent: Mutt/1.5.4i

On Tue, Nov 04, 2003 at 05:50:31PM -0800, Paul Eggert wrote:
> Surely it should use the least common multiple of the two block sizes?
> 
> (Checking for overflow, of course.)

Hello Paul,

Thanks for your feedback.  From an organizational standpoint, is it
appropriate to re-use the lcm routine residing in od.c in the
following manner?


Best regards,
-Neill.


diff -N -P -r -u 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-05 16:53:51.000000000 -0600
@@ -189,6 +189,33 @@
   return -ret;
 }
 
+/* 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) || (u > (UINT_MAX / v)))
+    return 0;
+  return u * v / t;
+}
+
 /* Copy a regular file from SRC_PATH to DST_PATH.
    If the source file contains holes, copies holes and blocks of zeros
    in the source file as holes in the destination file.
@@ -216,6 +243,8 @@
   off_t n_read_total = 0;
   int last_write_made_hole = 0;
   int make_holes = (x->sparse_mode == SPARSE_ALWAYS);
+  unsigned int max_block_size = 0;
+  unsigned int min_block_size = 0;
 
   source_desc = open (src_path, O_RDONLY);
   if (source_desc < 0)
@@ -285,7 +314,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 < 1)
+    {
+      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))




reply via email to

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