xref: /onnv-gate/usr/src/lib/libc/port/gen/posix_memalign.c (revision 7088:87e6b40103da)
1*7088Sraf /*
2*7088Sraf  * CDDL HEADER START
3*7088Sraf  *
4*7088Sraf  * The contents of this file are subject to the terms of the
5*7088Sraf  * Common Development and Distribution License (the "License").
6*7088Sraf  * You may not use this file except in compliance with the License.
7*7088Sraf  *
8*7088Sraf  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7088Sraf  * or http://www.opensolaris.org/os/licensing.
10*7088Sraf  * See the License for the specific language governing permissions
11*7088Sraf  * and limitations under the License.
12*7088Sraf  *
13*7088Sraf  * When distributing Covered Code, include this CDDL HEADER in each
14*7088Sraf  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7088Sraf  * If applicable, add the following below this CDDL HEADER, with the
16*7088Sraf  * fields enclosed by brackets "[]" replaced with your own identifying
17*7088Sraf  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7088Sraf  *
19*7088Sraf  * CDDL HEADER END
20*7088Sraf  */
21*7088Sraf 
22*7088Sraf /*
23*7088Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*7088Sraf  * Use is subject to license terms.
25*7088Sraf  */
26*7088Sraf 
27*7088Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7088Sraf 
29*7088Sraf #include "lint.h"
30*7088Sraf #include <stdlib.h>
31*7088Sraf #include <errno.h>
32*7088Sraf 
33*7088Sraf /*
34*7088Sraf  * SUSv3 - aligned memory allocation
35*7088Sraf  *
36*7088Sraf  * From the SUSv3 specification:
37*7088Sraf  *    The value of alignment shall be a power
38*7088Sraf  *    of two multiple of sizeof (void *).
39*7088Sraf  * This is enforced below.
40*7088Sraf  *
41*7088Sraf  * From the SUSv3 specification:
42*7088Sraf  *    If the size of the space requested is 0, the behavior
43*7088Sraf  *    is implementation-defined; the value returned in memptr
44*7088Sraf  *    shall be either a null pointer or a unique pointer.
45*7088Sraf  * We choose always to return a null pointer in this case.
46*7088Sraf  * (Not all implementations of memalign() behave this way.)
47*7088Sraf  */
48*7088Sraf int
posix_memalign(void ** memptr,size_t alignment,size_t size)49*7088Sraf posix_memalign(void **memptr, size_t alignment, size_t size)
50*7088Sraf {
51*7088Sraf 	void *ptr = NULL;
52*7088Sraf 	int error = 0;
53*7088Sraf 
54*7088Sraf 	if (alignment == 0 ||
55*7088Sraf 	    (alignment & (sizeof (void *) - 1)) != 0 ||
56*7088Sraf 	    (alignment & (alignment - 1)) != 0)
57*7088Sraf 		error = EINVAL;
58*7088Sraf 	else if (size != 0 &&
59*7088Sraf 	    (ptr = memalign(alignment, size)) == NULL)
60*7088Sraf 		error = ENOMEM;
61*7088Sraf 
62*7088Sraf 	*memptr = ptr;
63*7088Sraf 	return (error);
64*7088Sraf }
65