xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* uClibc internal malloc. |
| 3 | Copyright (C) 2007 Denys Vlasenko |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Library General Public License |
| 7 | version 2 as published by the Free Software Foundation. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Library General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Library General Public |
| 15 | License along with this library; see the file COPYING.LIB. If |
| 16 | not, write to the Free Software Foundation, Inc., 675 Mass Ave, |
| 17 | Cambridge, MA 02139, USA. |
| 18 | |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <malloc.h> |
| 24 | |
| 25 | |
| 26 | void (*__uc_malloc_failed)(size_t size) = NULL; |
| 27 | /* Seemingly superfluous assigment of NULL above prevents gas error |
| 28 | * ("__uc_malloc_failed can't be equated to common symbol |
| 29 | * __GI___uc_malloc_failed") in libc_hidden_data_def: */ |
| 30 | libc_hidden_data_def(__uc_malloc_failed) |
| 31 | |
| 32 | void *__uc_malloc(size_t size) |
| 33 | { |
| 34 | void *p; |
| 35 | |
| 36 | while (1) { |
| 37 | p = malloc(size); |
| 38 | if (!size || p) |
| 39 | return p; |
| 40 | if (!__uc_malloc_failed) |
| 41 | _exit(1); |
| 42 | free(p); |
| 43 | __uc_malloc_failed(size); |
| 44 | } |
| 45 | } |
| 46 | libc_hidden_def(__uc_malloc) |