Source-Changes-HG archive

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

[src/trunk]: src/bin/dd When buffers are at least page sized, explicitely req...



details:   https://anonhg.NetBSD.org/src/rev/acb833e9f479
branches:  trunk
changeset: 449783:acb833e9f479
user:      mlelstv <mlelstv%NetBSD.org@localhost>
date:      Sat Mar 23 09:33:16 2019 +0000

description:
When buffers are at least page sized, explicitely request page alignment.

diffstat:

 bin/dd/dd.c |  25 ++++++++++++++++++++-----
 1 files changed, 20 insertions(+), 5 deletions(-)

diffs (65 lines):

diff -r 7aea547ae4c5 -r acb833e9f479 bin/dd/dd.c
--- a/bin/dd/dd.c       Sat Mar 23 03:04:57 2019 +0000
+++ b/bin/dd/dd.c       Sat Mar 23 09:33:16 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dd.c,v 1.51 2016/09/05 01:00:07 sevan Exp $    */
+/*     $NetBSD: dd.c,v 1.52 2019/03/23 09:33:16 mlelstv Exp $  */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -43,7 +43,7 @@
 #if 0
 static char sccsid[] = "@(#)dd.c       8.5 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: dd.c,v 1.51 2016/09/05 01:00:07 sevan Exp $");
+__RCSID("$NetBSD: dd.c,v 1.52 2019/03/23 09:33:16 mlelstv Exp $");
 #endif
 #endif /* not lint */
 
@@ -72,6 +72,7 @@
 static void getfdtype(IO *);
 static void redup_clean_fd(IO *);
 static void setup(void);
+static void *buffer_alloc(size_t);
 
 IO             in, out;                /* input/output state */
 STAT           st;                     /* statistics */
@@ -150,6 +151,20 @@
        /* NOTREACHED */
 }
 
+static void *
+buffer_alloc(size_t sz)
+{
+       size_t align = getpagesize();
+       void *res;
+
+       if (sz < align)
+               res = malloc(sz);
+       else if (posix_memalign(&res, align, sz) != 0)
+               res = NULL;
+
+       return res;
+}
+
 static void
 setup(void)
 {
@@ -233,14 +248,14 @@
                size_t dbsz = out.dbsz;
                if (!(ddflags & C_BS))
                        dbsz += in.dbsz - 1;
-               if ((in.db = malloc(dbsz)) == NULL) {
+               if ((in.db = buffer_alloc(dbsz)) == NULL) {
                        err(EXIT_FAILURE, NULL);
                        /* NOTREACHED */
                }
                out.db = in.db;
        } else if ((in.db =
-           malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
-           (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
+           buffer_alloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
+           (out.db = buffer_alloc((u_int)(out.dbsz + cbsz))) == NULL) {
                err(EXIT_FAILURE, NULL);
                /* NOTREACHED */
        }



Home | Main Index | Thread Index | Old Index