Source-Changes-HG archive

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

[src/trunk]: src Add the C11 function aligned_alloc to libc.



details:   https://anonhg.NetBSD.org/src/rev/eb4e90c2a854
branches:  trunk
changeset: 811605:eb4e90c2a854
user:      nros <nros%NetBSD.org@localhost>
date:      Sat Nov 07 16:21:42 2015 +0000

description:
Add the C11 function aligned_alloc to libc.

diffstat:

 distrib/sets/lists/base/shl.mi           |   6 +-
 distrib/sets/lists/comp/mi               |   4 +-
 include/stdlib.h                         |   3 +-
 lib/libc/shlib_version                   |   4 +-
 lib/libc/stdlib/Makefile.inc             |   5 +-
 lib/libc/stdlib/aligned_alloc.c          |  70 ++++++++++++++++++++++++++++++++
 lib/libc/stdlib/malloc.3                 |   5 +-
 lib/libc/stdlib/posix_memalign.3         |  66 ++++++++++++++++++++++++++---
 lib/librumpuser/rumpuser_port.h          |   3 +-
 tests/lib/libc/stdlib/t_posix_memalign.c |  59 +++++++++++++++++++++++++-
 10 files changed, 202 insertions(+), 23 deletions(-)

diffs (truncated from 431 to 300 lines):

diff -r 98a295cc450f -r eb4e90c2a854 distrib/sets/lists/base/shl.mi
--- a/distrib/sets/lists/base/shl.mi    Sat Nov 07 14:29:10 2015 +0000
+++ b/distrib/sets/lists/base/shl.mi    Sat Nov 07 16:21:42 2015 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: shl.mi,v 1.753 2015/11/04 23:57:05 knakahara Exp $
+# $NetBSD: shl.mi,v 1.754 2015/11/07 16:21:42 nros Exp $
 #
 # Note:        Don't delete entries from here - mark them as "obsolete" instead,
 #      unless otherwise stated below.
@@ -18,7 +18,7 @@
 ./lib/libblacklist.so.0.0                      base-sys-shlib          dynamicroot
 ./lib/libc.so                                  base-sys-shlib          dynamicroot
 ./lib/libc.so.12                               base-sys-shlib          dynamicroot
-./lib/libc.so.12.198                           base-sys-shlib          dynamicroot
+./lib/libc.so.12.199                           base-sys-shlib          dynamicroot
 ./lib/libcrypt.so                              base-sys-shlib          dynamicroot
 ./lib/libcrypt.so.1                            base-sys-shlib          dynamicroot
 ./lib/libcrypt.so.1.0                          base-sys-shlib          dynamicroot
@@ -209,7 +209,7 @@
 ./usr/lib/libc++.so.1.0                                base-sys-shlib          compatfile,libcxx
 ./usr/lib/libc.so                              base-sys-shlib          compatfile
 ./usr/lib/libc.so.12                           base-sys-shlib          compatfile
-./usr/lib/libc.so.12.198                       base-sys-shlib          compatfile
+./usr/lib/libc.so.12.199                       base-sys-shlib          compatfile
 ./usr/lib/libcdk.so                            base-obsolete           compatfile,obsolete
 ./usr/lib/libcom_err.so                                base-krb5-shlib         compatfile,kerberos
 ./usr/lib/libcom_err.so.7                      base-krb5-shlib         compatfile,kerberos
diff -r 98a295cc450f -r eb4e90c2a854 distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Sat Nov 07 14:29:10 2015 +0000
+++ b/distrib/sets/lists/comp/mi        Sat Nov 07 16:21:42 2015 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: mi,v 1.2003 2015/11/07 13:28:05 joerg Exp $
+#      $NetBSD: mi,v 1.2004 2015/11/07 16:21:42 nros Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp                           comp-sys-root
@@ -11894,6 +11894,7 @@
 ./usr/share/man/html3/aio_suspend.html         comp-c-htmlman          html
 ./usr/share/man/html3/aio_write.html           comp-c-htmlman          html
 ./usr/share/man/html3/alarm.html               comp-c-htmlman          html
+./usr/share/man/html3/aligned_alloc.html       comp-c-htmlman          html
 ./usr/share/man/html3/alloca.html              comp-c-htmlman          html
 ./usr/share/man/html3/allocaddrinfo.html       comp-c-htmlman          html
 ./usr/share/man/html3/alphasort.html           comp-c-htmlman          html
@@ -18697,6 +18698,7 @@
 ./usr/share/man/man3/aio_suspend.3             comp-c-man              .man
 ./usr/share/man/man3/aio_write.3               comp-c-man              .man
 ./usr/share/man/man3/alarm.3                   comp-c-man              .man
+./usr/share/man/man3/aligned_alloc.3           comp-c-man              .man
 ./usr/share/man/man3/alloca.3                  comp-c-man              .man
 ./usr/share/man/man3/allocaddrinfo.3           comp-c-man              .man
 ./usr/share/man/man3/alphasort.3               comp-c-man              .man
diff -r 98a295cc450f -r eb4e90c2a854 include/stdlib.h
--- a/include/stdlib.h  Sat Nov 07 14:29:10 2015 +0000
+++ b/include/stdlib.h  Sat Nov 07 16:21:42 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: stdlib.h,v 1.115 2015/02/17 20:33:40 joerg Exp $       */
+/*     $NetBSD: stdlib.h,v 1.116 2015/11/07 16:21:42 nros Exp $        */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -234,6 +234,7 @@
 
 #if defined(_ISOC11_SOURCE) || (__STDC_VERSION__ - 0) >= 201101L || \
     defined(_NETBSD_SOURCE) || (__cplusplus - 0) >= 201103L
+void   *aligned_alloc(size_t, size_t);
 int    at_quick_exit(void (*)(void));
 __dead void quick_exit(int);
 #endif
diff -r 98a295cc450f -r eb4e90c2a854 lib/libc/shlib_version
--- a/lib/libc/shlib_version    Sat Nov 07 14:29:10 2015 +0000
+++ b/lib/libc/shlib_version    Sat Nov 07 16:21:42 2015 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: shlib_version,v 1.259 2015/10/29 17:55:09 christos Exp $
+#      $NetBSD: shlib_version,v 1.260 2015/11/07 16:21:42 nros Exp $
 #      Remember to update distrib/sets/lists/base/shl.* when changing
 #
 # things we wish to do on next major version bump:
@@ -42,4 +42,4 @@
 # - move gethostbyname to a compat library
 # - remove arc4random(3) API
 major=12
-minor=198
+minor=199
diff -r 98a295cc450f -r eb4e90c2a854 lib/libc/stdlib/Makefile.inc
--- a/lib/libc/stdlib/Makefile.inc      Sat Nov 07 14:29:10 2015 +0000
+++ b/lib/libc/stdlib/Makefile.inc      Sat Nov 07 16:21:42 2015 +0000
@@ -1,11 +1,11 @@
-#      $NetBSD: Makefile.inc,v 1.90 2015/05/01 14:17:56 christos Exp $
+#      $NetBSD: Makefile.inc,v 1.91 2015/11/07 16:21:42 nros Exp $
 #      from: @(#)Makefile.inc  8.3 (Berkeley) 2/4/95
 
 # stdlib sources
 .PATH: ${ARCHDIR}/stdlib ${.CURDIR}/stdlib
 
 SRCS+= _env.c _rand48.c \
-       a64l.c abort.c atexit.c atof.c atoi.c atol.c atoll.c \
+       a64l.c abort.c aligned_alloc.c atexit.c atof.c atoi.c atol.c atoll.c \
        bsearch.c drand48.c exit.c \
        getenv.c getopt.c getopt_long.c getsubopt.c \
        hcreate.c heapsort.c imaxdiv.c insque.c jrand48.c l64a.c lldiv.c \
@@ -74,6 +74,7 @@
 MLINKS+=insque.3 remque.3
 MLINKS+=lsearch.3 lfind.3
 MLINKS+=malloc.3 calloc.3 malloc.3 realloc.3 malloc.3 free.3
+MLINKS+=posix_memalign.3 aligned_alloc.3
 MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3
 MLINKS+=ptsname.3 ptsname_r.3
 MLINKS+=rand.3 rand_r.3
diff -r 98a295cc450f -r eb4e90c2a854 lib/libc/stdlib/aligned_alloc.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/stdlib/aligned_alloc.c   Sat Nov 07 16:21:42 2015 +0000
@@ -0,0 +1,70 @@
+/* $NetBSD: aligned_alloc.c,v 1.1 2015/11/07 16:21:42 nros Exp $ */
+
+/*-
+ * Copyright (C) 2015 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Niclas Rosenvik.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice(s), this list of conditions and the following disclaimer as
+ *    the first lines of this file unmodified other than the possible
+ *    addition of one or more copyright notices.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice(s), this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+
+void *
+aligned_alloc(size_t alignment, size_t size)
+{
+        void *memptr;
+        int err;
+       
+        /*
+         * Check that alignment is a power of 2
+         * and that size is an integer multiple of alignment.
+         */
+        if (alignment == 0 || ((alignment - 1) & alignment) != 0 ||
+            (size & (alignment-1)) != 0) {
+                errno = EINVAL;
+                return NULL;
+        }
+       
+        /*
+         * Adjust alignment to satisfy posix_memalign,
+         * larger alignments satisfy smaller alignments.
+         */
+        while (alignment < sizeof(void *)) {
+                alignment <<= 1;
+        }
+       
+        err = posix_memalign(&memptr, alignment, size);
+       
+        if (err) {
+                errno = err;
+                return NULL;
+        }
+       
+        return memptr;
+}
diff -r 98a295cc450f -r eb4e90c2a854 lib/libc/stdlib/malloc.3
--- a/lib/libc/stdlib/malloc.3  Sat Nov 07 14:29:10 2015 +0000
+++ b/lib/libc/stdlib/malloc.3  Sat Nov 07 16:21:42 2015 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: malloc.3,v 1.44 2015/07/26 22:32:03 wiz Exp $
+.\" $NetBSD: malloc.3,v 1.45 2015/11/07 16:21:42 nros Exp $
 .\"
 .\" Copyright (c) 1980, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -252,7 +252,8 @@
 .Xr getpagesize 3 ,
 .Xr memory 3 ,
 .Xr posix_memalign 3 ,
-.Xr reallocarr 3
+.Xr reallocarr 3 ,
+.Xr aligned_alloc 3
 .Pp
 For the implementation details, see
 .Xr jemalloc 3 .
diff -r 98a295cc450f -r eb4e90c2a854 lib/libc/stdlib/posix_memalign.3
--- a/lib/libc/stdlib/posix_memalign.3  Sat Nov 07 14:29:10 2015 +0000
+++ b/lib/libc/stdlib/posix_memalign.3  Sat Nov 07 16:21:42 2015 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: posix_memalign.3,v 1.1 2007/11/19 14:48:43 ad Exp $
+.\"    $NetBSD: posix_memalign.3,v 1.2 2015/11/07 16:21:42 nros Exp $
 .\"
 .\" Copyright (C) 2006 Jason Evans <jasone%FreeBSD.org@localhost>.
 .\" All rights reserved.
@@ -29,11 +29,11 @@
 .\"
 .\" FreeBSD: src/lib/libc/stdlib/posix_memalign.3,v 1.3 2007/03/28 04:32:51 jasone Exp
 .\"
-.Dd November 19, 2007
+.Dd October 30, 2015
 .Dt POSIX_MEMALIGN 3
 .Os
 .Sh NAME
-.Nm posix_memalign
+.Nm posix_memalign , aligned_alloc
 .Nd aligned memory allocation
 .Sh LIBRARY
 .Lb libc
@@ -41,23 +41,39 @@
 .In stdlib.h
 .Ft int
 .Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
+.Ft void *
+.Fn aligned_alloc "size_t alignment" "size_t size"
 .Sh DESCRIPTION
 The
 .Fn posix_memalign
 function allocates
 .Fa size
 bytes of memory such that the allocation's base address is an even multiple of
-.Fa alignment ,
+.Fa alignment , 
 and returns the allocation in the value pointed to by
 .Fa ptr .
-.Pp
 The requested
 .Fa alignment
 must be a power of 2 at least as large as
 .Fn sizeof "void *" .
 .Pp
+The
+.Fn aligned_alloc
+function allocates
+.Fa size
+bytes of memory such that the allocation's base address is an even multiple of
+.Fa alignment .
+The requested
+.Fa alignment
+must be a power of 2 and
+.Fa size
+must be a integer multiple of
+.Fa alignment .
+.Pp
 Memory that is allocated via
 .Fn posix_memalign
+or
+.Fn aligned_alloc
 can be used as an argument in subsequent calls to
 .Xr realloc 3
 and
@@ -66,19 +82,49 @@
 The
 .Fn posix_memalign
 function returns the value 0 if successful; otherwise it returns an error value.
+.Pp
+The
+.Fn aligned_alloc
+function returns a pointer to the allocated memory if successful; on failure it
+returns NULL and sets
+.Fa errno
+to indicate the error.
 .Sh ERRORS
 The
 .Fn posix_memalign
-function will fail if:
+and
+.Fn aligned_alloc
+functions will fail if:
 .Bl -tag -width Er
 .It Bq Er EINVAL
 The
 .Fa alignment
-parameter is not a power of 2 at least as large as
-.Fn sizeof "void *" .
+parameter is not a power of 2.
 .It Bq Er ENOMEM
 Memory allocation error.
 .El
+.Pp
+The
+.Fn posix_memalign
+function will also fail if
+.Bl -tag -width Er



Home | Main Index | Thread Index | Old Index