__uc_malloc.c 1.2 KB

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