__uc_malloc.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA.
  15. */
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <malloc.h>
  19. void (*__uc_malloc_failed)(size_t size) = NULL;
  20. /* Seemingly superfluous assigment of NULL above prevents gas error
  21. * ("__uc_malloc_failed can't be equated to common symbol
  22. * __GI___uc_malloc_failed") in libc_hidden_data_def: */
  23. libc_hidden_data_def(__uc_malloc_failed)
  24. void *__uc_malloc(size_t size)
  25. {
  26. void *p;
  27. while (1) {
  28. p = malloc(size);
  29. if (!size || p)
  30. return p;
  31. if (!__uc_malloc_failed)
  32. _exit(1);
  33. free(p);
  34. __uc_malloc_failed(size);
  35. }
  36. }
  37. libc_hidden_def(__uc_malloc)