__uc_malloc.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* vi: set sw=4 ts=4: */
  2. /* uClibc internal malloc.
  3. Copyright (C) 2007 Denys Vlasenko
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License
  6. version 2 as published by the Free Software Foundation.
  7. This library 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 GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this library; see the file COPYING.LIB. If
  13. not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <malloc.h>
  18. void (*__uc_malloc_failed)(size_t size) = NULL;
  19. /* Seemingly superfluous assigment of NULL above prevents gas error
  20. * ("__uc_malloc_failed can't be equated to common symbol
  21. * __GI___uc_malloc_failed") in libc_hidden_data_def: */
  22. libc_hidden_data_def(__uc_malloc_failed)
  23. void *__uc_malloc(size_t size)
  24. {
  25. void *p;
  26. while (1) {
  27. p = malloc(size);
  28. if (!size || p)
  29. return p;
  30. if (!__uc_malloc_failed)
  31. _exit(1);
  32. free(p);
  33. __uc_malloc_failed(size);
  34. }
  35. }
  36. libc_hidden_def(__uc_malloc)