posix_memalign.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. #include <stdlib.h>
  21. #include <malloc.h>
  22. #include <sys/types.h>
  23. #include <errno.h>
  24. #include <sys/param.h>
  25. int posix_memalign(void **memptr, size_t alignment, size_t size)
  26. {
  27. /* Make sure alignment is correct. */
  28. if (alignment % sizeof(void *) != 0)
  29. /* Skip these checks because the memalign() func does them for us
  30. || !powerof2(alignment / sizeof(void *)) != 0
  31. || alignment == 0
  32. */
  33. return EINVAL;
  34. void *mem = memalign(alignment, size);
  35. if (mem != NULL) {
  36. *memptr = mem;
  37. return 0;
  38. } else
  39. return ENOMEM;
  40. }