Author: ed
Date: Mon Jan  9 06:36:28 2012
New Revision: 229848
URL: http://svn.freebsd.org/changeset/base/229848

Log:
  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).
  
  Reviewed by:  jasone@

Added:
  head/lib/libc/stdlib/aligned_alloc.3
     - copied, changed from r229786, head/lib/libc/stdlib/posix_memalign.3
Deleted:
  head/lib/libc/stdlib/posix_memalign.3
Modified:
  head/include/stdlib.h
  head/lib/libc/stdlib/Makefile.inc
  head/lib/libc/stdlib/Symbol.map
  head/lib/libc/stdlib/malloc.c

Modified: head/include/stdlib.h
==============================================================================
--- head/include/stdlib.h       Mon Jan  9 05:51:33 2012        (r229847)
+++ head/include/stdlib.h       Mon Jan  9 06:36:28 2012        (r229848)
@@ -152,6 +152,7 @@ _Noreturn void       _Exit(int);
  * If we're in a mode greater than C99, expose C11 functions.
  */
 #if __ISO_C_VISIBLE >= 2011
+void * aligned_alloc(size_t, size_t);
 int    at_quick_exit(void (*)(void));
 _Noreturn void
        quick_exit(int);

Modified: head/lib/libc/stdlib/Makefile.inc
==============================================================================
--- head/lib/libc/stdlib/Makefile.inc   Mon Jan  9 05:51:33 2012        
(r229847)
+++ head/lib/libc/stdlib/Makefile.inc   Mon Jan  9 06:36:28 2012        
(r229848)
@@ -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: head/lib/libc/stdlib/Symbol.map
==============================================================================
--- head/lib/libc/stdlib/Symbol.map     Mon Jan  9 05:51:33 2012        
(r229847)
+++ head/lib/libc/stdlib/Symbol.map     Mon Jan  9 06:36:28 2012        
(r229848)
@@ -93,6 +93,7 @@ FBSD_1.0 {
 };
 
 FBSD_1.3 {
+       aligned_alloc;
        at_quick_exit;
        atof_l;
        atoi_l;

Copied and modified: head/lib/libc/stdlib/aligned_alloc.3 (from r229786, 
head/lib/libc/stdlib/posix_memalign.3)
==============================================================================
--- head/lib/libc/stdlib/posix_memalign.3       Sat Jan  7 16:16:13 2012        
(r229786, copy source)
+++ head/lib/libc/stdlib/aligned_alloc.3        Mon Jan  9 06:36:28 2012        
(r229848)
@@ -27,26 +27,35 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd January 11, 2006
-.Dt POSIX_MEMALIGN 3
+.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
-function allocates
+functions allocate
 .Fa size
 bytes of memory such that the allocation's base address is an even multiple of
-.Fa alignment ,
-and returns the allocation in the value pointed to by
+.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
@@ -55,6 +64,8 @@ must be a power of 2 at least as large a
 .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 ,
@@ -63,12 +74,21 @@ 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
-function will fail if:
+functions will fail if:
 .Bl -tag -width Er
 .It Bq Er EINVAL
 The
@@ -86,6 +106,11 @@ Memory allocation error.
 .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 .
@@ -94,3 +119,8 @@ The
 .Fn posix_memalign
 function first appeared in
 .Fx 7.0 .
+.Pp
+The
+.Fn aligned_alloc
+function first appeared in
+.Fx 10.0 .

Modified: head/lib/libc/stdlib/malloc.c
==============================================================================
--- head/lib/libc/stdlib/malloc.c       Mon Jan  9 05:51:33 2012        
(r229847)
+++ head/lib/libc/stdlib/malloc.c       Mon Jan  9 06:36:28 2012        
(r229848)
@@ -6043,6 +6043,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;
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to