From owner-svn-src-stable@FreeBSD.ORG Thu Nov 22 15:19:54 2012 Return-Path: Delivered-To: svn-src-stable@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 4D4185E8; Thu, 22 Nov 2012 15:19:54 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 311098FC18; Thu, 22 Nov 2012 15:19:54 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.5/8.14.5) with ESMTP id qAMFJs0o007468; Thu, 22 Nov 2012 15:19:54 GMT (envelope-from ed@svn.freebsd.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.5/8.14.5/Submit) id qAMFJroe007462; Thu, 22 Nov 2012 15:19:53 GMT (envelope-from ed@svn.freebsd.org) Message-Id: <201211221519.qAMFJroe007462@svn.freebsd.org> From: Ed Schouten Date: Thu, 22 Nov 2012 15:19:53 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org Subject: svn commit: r243405 - in stable/9: include lib/libc/stdlib X-SVN-Group: stable-9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 22 Nov 2012 15:19:54 -0000 Author: ed Date: Thu Nov 22 15:19:53 2012 New Revision: 243405 URL: http://svnweb.freebsd.org/changeset/base/243405 Log: MFC r229848: Add aligned_alloc(3). The C11 folks reinvented the wheel by introducing an aligned version of malloc(3) called aligned_alloc(3), instead of posix_memalign(3). Instead of returning the allocation by reference, it returns the address, just like malloc(3). I'm MFCing this now, as it seems aligned_alloc(3) is needed to make the new version of libc++ work, which was merged back to FreeBSD 9 in r243376. Requested by: dim Added: stable/9/lib/libc/stdlib/aligned_alloc.3 - copied unchanged from r229848, head/lib/libc/stdlib/aligned_alloc.3 Deleted: stable/9/lib/libc/stdlib/posix_memalign.3 Modified: stable/9/include/stdlib.h stable/9/lib/libc/stdlib/Makefile.inc stable/9/lib/libc/stdlib/Symbol.map stable/9/lib/libc/stdlib/malloc.c Directory Properties: stable/9/include/ (props changed) stable/9/lib/libc/ (props changed) Modified: stable/9/include/stdlib.h ============================================================================== --- stable/9/include/stdlib.h Thu Nov 22 14:40:26 2012 (r243404) +++ stable/9/include/stdlib.h Thu Nov 22 15:19:53 2012 (r243405) @@ -155,6 +155,7 @@ _Noreturn void _Exit(int); * If we're in a mode greater than C99, expose C1x functions. */ #if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L +void * aligned_alloc(size_t, size_t); _Noreturn void quick_exit(int); int at_quick_exit(void (*)(void)); Modified: stable/9/lib/libc/stdlib/Makefile.inc ============================================================================== --- stable/9/lib/libc/stdlib/Makefile.inc Thu Nov 22 14:40:26 2012 (r243404) +++ stable/9/lib/libc/stdlib/Makefile.inc Thu Nov 22 15:19:53 2012 (r243405) @@ -18,17 +18,18 @@ SYM_MAPS+= ${.CURDIR}/stdlib/Symbol.map # machine-dependent stdlib sources .sinclude "${.CURDIR}/${LIBC_ARCH}/stdlib/Makefile.inc" -MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 \ - at_quick_exit.3 bsearch.3 \ +MAN+= a64l.3 abort.3 abs.3 aligned_alloc.3 alloca.3 atexit.3 atof.3 \ + atoi.3 atol.3 at_quick_exit.3 bsearch.3 \ div.3 exit.3 getenv.3 getopt.3 getopt_long.3 getsubopt.3 \ hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 llabs.3 lldiv.3 \ - lsearch.3 malloc.3 memory.3 posix_memalign.3 ptsname.3 qsort.3 \ + lsearch.3 malloc.3 memory.3 ptsname.3 qsort.3 \ quick_exit.3 \ radixsort.3 rand.3 random.3 \ realpath.3 strfmon.3 strtod.3 strtol.3 strtonum.3 strtoul.3 system.3 \ tsearch.3 MLINKS+=a64l.3 l64a.3 a64l.3 l64a_r.3 +MLINKS+=aligned_alloc.3 posix_memalign.3 MLINKS+=atol.3 atoll.3 MLINKS+=exit.3 _Exit.3 MLINKS+=getenv.3 putenv.3 getenv.3 setenv.3 getenv.3 unsetenv.3 Modified: stable/9/lib/libc/stdlib/Symbol.map ============================================================================== --- stable/9/lib/libc/stdlib/Symbol.map Thu Nov 22 14:40:26 2012 (r243404) +++ stable/9/lib/libc/stdlib/Symbol.map Thu Nov 22 15:19:53 2012 (r243405) @@ -93,6 +93,7 @@ FBSD_1.0 { }; FBSD_1.3 { + aligned_alloc; atof_l; atoi_l; atol_l; Copied: stable/9/lib/libc/stdlib/aligned_alloc.3 (from r229848, head/lib/libc/stdlib/aligned_alloc.3) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/9/lib/libc/stdlib/aligned_alloc.3 Thu Nov 22 15:19:53 2012 (r243405, copy of r229848, head/lib/libc/stdlib/aligned_alloc.3) @@ -0,0 +1,126 @@ +.\" Copyright (C) 2006 Jason Evans . +.\" All rights reserved. +.\" +.\" 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. +.\" +.\" $FreeBSD$ +.\" +.Dd January 7, 2011 +.Dt ALIGNED_ALLOC 3 +.Os +.Sh NAME +.Nm aligned_alloc , +.Nm posix_memalign +.Nd aligned memory allocation +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdlib.h +.Ft void * +.Fn aligned_alloc "size_t alignment" "size_t size" +.Ft int +.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size" +.Sh DESCRIPTION +The +.Fn aligned_alloc +and +.Fn posix_memalign +functions allocate +.Fa size +bytes of memory such that the allocation's base address is an even multiple of +.Fa alignment . +The +.Fn aligned_alloc +function returns the allocation, while the +.Fn posix_memalign +function stores 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 +Memory that is allocated via +.Fn aligned_alloc +and +.Fn posix_memalign +can be used as an argument in subsequent calls to +.Xr realloc 3 , +.Xr reallocf 3 , +and +.Xr free 3 . +.Sh RETURN VALUES +The +.Fn aligned_alloc +function returns a pointer to the allocation if successful; otherwise a +NULL pointer is returned and +.Va errno +is set to an error value. +.Pp +The +.Fn posix_memalign +function returns the value 0 if successful; otherwise it returns an error value. +.Sh ERRORS +The +.Fn aligned_alloc +and +.Fn posix_memalign +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 *" . +.It Bq Er ENOMEM +Memory allocation error. +.El +.Sh SEE ALSO +.Xr free 3 , +.Xr malloc 3 , +.Xr realloc 3 , +.Xr reallocf 3 , +.Xr valloc 3 +.Sh STANDARDS +The +.Fn aligned_alloc +function conforms to +.St -isoC-2011 . +.Pp +The +.Fn posix_memalign +function conforms to +.St -p1003.1-2001 . +.Sh HISTORY +The +.Fn posix_memalign +function first appeared in +.Fx 7.0 . +.Pp +The +.Fn aligned_alloc +function first appeared in +.Fx 10.0 . Modified: stable/9/lib/libc/stdlib/malloc.c ============================================================================== --- stable/9/lib/libc/stdlib/malloc.c Thu Nov 22 14:40:26 2012 (r243404) +++ stable/9/lib/libc/stdlib/malloc.c Thu Nov 22 15:19:53 2012 (r243405) @@ -6046,6 +6046,20 @@ RETURN: } void * +aligned_alloc(size_t alignment, size_t size) +{ + void *memptr; + int ret; + + ret = posix_memalign(&memptr, alignment, size); + if (ret != 0) { + errno = ret; + return (NULL); + } + return (memptr); +} + +void * calloc(size_t num, size_t size) { void *ret;