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