1303bf60bSsdebnath /*
2303bf60bSsdebnath  * CDDL HEADER START
3303bf60bSsdebnath  *
4303bf60bSsdebnath  * The contents of this file are subject to the terms of the
5f841f6adSraf  * Common Development and Distribution License (the "License").
6f841f6adSraf  * You may not use this file except in compliance with the License.
7303bf60bSsdebnath  *
8303bf60bSsdebnath  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9303bf60bSsdebnath  * or http://www.opensolaris.org/os/licensing.
10303bf60bSsdebnath  * See the License for the specific language governing permissions
11303bf60bSsdebnath  * and limitations under the License.
12303bf60bSsdebnath  *
13303bf60bSsdebnath  * When distributing Covered Code, include this CDDL HEADER in each
14303bf60bSsdebnath  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15303bf60bSsdebnath  * If applicable, add the following below this CDDL HEADER, with the
16303bf60bSsdebnath  * fields enclosed by brackets "[]" replaced with your own identifying
17303bf60bSsdebnath  * information: Portions Copyright [yyyy] [name of copyright owner]
18303bf60bSsdebnath  *
19303bf60bSsdebnath  * CDDL HEADER END
20303bf60bSsdebnath  */
21e8031f0aSraf 
22303bf60bSsdebnath /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24303bf60bSsdebnath  * Use is subject to license terms.
25303bf60bSsdebnath  */
26303bf60bSsdebnath 
277257d1b4Sraf #include "lint.h"
28*a547acf9SRichard Lowe 
29*a547acf9SRichard Lowe #include <sys/sysmacros.h>
30*a547acf9SRichard Lowe 
31019c3c43Sraf #include <stdlib.h>
32303bf60bSsdebnath #include <errno.h>
33303bf60bSsdebnath 
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  */
49303bf60bSsdebnath int
posix_memalign(void ** memptr,size_t alignment,size_t size)50019c3c43Sraf posix_memalign(void **memptr, size_t alignment, size_t size)
51303bf60bSsdebnath {
52019c3c43Sraf 	void *ptr = NULL;
53303bf60bSsdebnath 
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 {
64*a547acf9SRichard Lowe 			*memptr = ptr;
65*a547acf9SRichard Lowe 			return (0);
66*a547acf9SRichard Lowe 		}
67*a547acf9SRichard Lowe 	}
68303bf60bSsdebnath }
69