1 /* posix_memalign for uClibc
2  *
3  * Copyright (C) 1996-2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4  * Copyright (C) 2005 by Erik Andersen <andersen@uclibc.org>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Library General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this program; see the file COPYING.LIB.  If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <stdlib.h>
22 #include <malloc.h>
23 #include <sys/types.h>
24 #include <errno.h>
25 #include <sys/param.h>
26 
posix_memalign(void ** memptr,size_t alignment,size_t size)27 int posix_memalign(void **memptr, size_t alignment, size_t size)
28 {
29 	/* Make sure alignment is correct. */
30 	if (alignment % sizeof(void *) != 0)
31 	    /* Skip these checks because the memalign() func does them for us
32 	     || !powerof2(alignment / sizeof(void *)) != 0
33 	     || alignment == 0
34 	     */
35 		return EINVAL;
36 	void *mem = memalign(alignment, size);
37 	if (mem != NULL) {
38 		*memptr = mem;
39 		return 0;
40 	} else
41 		return ENOMEM;
42 }
43