xref: /illumos-gate/usr/src/lib/libc/port/gen/posix_memalign.c (revision a547acf91a502e2d79ff67ef86d1b791883ca43a)
1019c3c43Sraf /*
2019c3c43Sraf  * CDDL HEADER START
3019c3c43Sraf  *
4019c3c43Sraf  * The contents of this file are subject to the terms of the
5019c3c43Sraf  * Common Development and Distribution License (the "License").
6019c3c43Sraf  * You may not use this file except in compliance with the License.
7019c3c43Sraf  *
8019c3c43Sraf  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9019c3c43Sraf  * or http://www.opensolaris.org/os/licensing.
10019c3c43Sraf  * See the License for the specific language governing permissions
11019c3c43Sraf  * and limitations under the License.
12019c3c43Sraf  *
13019c3c43Sraf  * When distributing Covered Code, include this CDDL HEADER in each
14019c3c43Sraf  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15019c3c43Sraf  * If applicable, add the following below this CDDL HEADER, with the
16019c3c43Sraf  * fields enclosed by brackets "[]" replaced with your own identifying
17019c3c43Sraf  * information: Portions Copyright [yyyy] [name of copyright owner]
18019c3c43Sraf  *
19019c3c43Sraf  * CDDL HEADER END
20019c3c43Sraf  */
21019c3c43Sraf 
22019c3c43Sraf /*
23019c3c43Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24019c3c43Sraf  * Use is subject to license terms.
25019c3c43Sraf  */
26019c3c43Sraf 
27019c3c43Sraf #include "lint.h"
28*a547acf9SRichard Lowe 
29*a547acf9SRichard Lowe #include <sys/sysmacros.h>
30*a547acf9SRichard Lowe 
31019c3c43Sraf #include <stdlib.h>
32019c3c43Sraf #include <errno.h>
33019c3c43Sraf 
34019c3c43Sraf /*
35019c3c43Sraf  * SUSv3 - aligned memory allocation
36019c3c43Sraf  *
37019c3c43Sraf  * From the SUSv3 specification:
38019c3c43Sraf  *    The value of alignment shall be a power
39019c3c43Sraf  *    of two multiple of sizeof (void *).
40019c3c43Sraf  * This is enforced below.
41019c3c43Sraf  *
42019c3c43Sraf  * From the SUSv3 specification:
43019c3c43Sraf  *    If the size of the space requested is 0, the behavior
44019c3c43Sraf  *    is implementation-defined; the value returned in memptr
45019c3c43Sraf  *    shall be either a null pointer or a unique pointer.
46019c3c43Sraf  * We choose always to return a null pointer in this case.
47019c3c43Sraf  * (Not all implementations of memalign() behave this way.)
48019c3c43Sraf  */
49019c3c43Sraf int
posix_memalign(void ** memptr,size_t alignment,size_t size)50019c3c43Sraf posix_memalign(void **memptr, size_t alignment, size_t size)
51019c3c43Sraf {
52019c3c43Sraf 	void *ptr = NULL;
53019c3c43Sraf 
54*a547acf9SRichard Lowe 	if ((alignment == 0) || !ISP2(alignment) ||
55*a547acf9SRichard Lowe 	    (alignment & (sizeof (void *) - 1)) != 0) {
56*a547acf9SRichard Lowe 		return (EINVAL);
57*a547acf9SRichard Lowe 	} else if (size == 0) {
58*a547acf9SRichard Lowe 		*memptr = NULL;
59*a547acf9SRichard Lowe 		return (0);
60*a547acf9SRichard Lowe 	} else {
61*a547acf9SRichard Lowe 		if ((ptr = memalign(alignment, size)) == NULL) {
62*a547acf9SRichard Lowe 			return (ENOMEM);
63*a547acf9SRichard Lowe 		} else {
64019c3c43Sraf 			*memptr = ptr;
65*a547acf9SRichard Lowe 			return (0);
66*a547acf9SRichard Lowe 		}
67*a547acf9SRichard Lowe 	}
68019c3c43Sraf }
69