malloc.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* malloc() function that is glibc compatible.
  2. Copyright (C) 1997-1998, 2006-2007, 2009-2011 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /* written by Jim Meyering and Bruno Haible */
  15. #define _GL_USE_STDLIB_ALLOC 1
  16. #include <config.h>
  17. /* Only the AC_FUNC_MALLOC macro defines 'malloc' already in config.h. */
  18. #ifdef malloc
  19. # define NEED_MALLOC_GNU 1
  20. # undef malloc
  21. /* Whereas the gnulib module 'malloc-gnu' defines HAVE_MALLOC_GNU. */
  22. #elif GNULIB_MALLOC_GNU && !HAVE_MALLOC_GNU
  23. # define NEED_MALLOC_GNU 1
  24. #endif
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. /* Allocate an N-byte block of memory from the heap.
  28. If N is zero, allocate a 1-byte block. */
  29. void *
  30. rpl_malloc (size_t n)
  31. {
  32. void *result;
  33. #if NEED_MALLOC_GNU
  34. if (n == 0)
  35. n = 1;
  36. #endif
  37. result = malloc (n);
  38. #if !HAVE_MALLOC_POSIX
  39. if (result == NULL)
  40. errno = ENOMEM;
  41. #endif
  42. return result;
  43. }