malloc.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* malloc() function that is glibc compatible.
  2. Copyright (C) 1997-1998, 2006-2007, 2009-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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, see <https://www.gnu.org/licenses/>. */
  13. /* written by Jim Meyering and Bruno Haible */
  14. #define _GL_USE_STDLIB_ALLOC 1
  15. #include <config.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include "xalloc-oversized.h"
  19. /* Allocate an N-byte block of memory from the heap, even if N is 0. */
  20. void *
  21. rpl_malloc (size_t n)
  22. {
  23. if (n == 0)
  24. n = 1;
  25. if (xalloc_oversized (n, 1))
  26. {
  27. errno = ENOMEM;
  28. return NULL;
  29. }
  30. void *result = malloc (n);
  31. #if !HAVE_MALLOC_POSIX
  32. if (result == NULL)
  33. errno = ENOMEM;
  34. #endif
  35. return result;
  36. }