@@ -9,16 +9,23 @@ static void do_nothing(size_t size)
9
9
10
10
static void (* try_to_free_routine )(size_t size ) = do_nothing ;
11
11
12
- static void memory_limit_check (size_t size )
12
+ static int memory_limit_check (size_t size , int gentle )
13
13
{
14
14
static int limit = -1 ;
15
15
if (limit == -1 ) {
16
16
const char * env = getenv ("GIT_ALLOC_LIMIT" );
17
17
limit = env ? atoi (env ) * 1024 : 0 ;
18
18
}
19
- if (limit && size > limit )
20
- die ("attempting to allocate %" PRIuMAX " over limit %d" ,
21
- (intmax_t )size , limit );
19
+ if (limit && size > limit ) {
20
+ if (gentle ) {
21
+ error ("attempting to allocate %" PRIuMAX " over limit %d" ,
22
+ (intmax_t )size , limit );
23
+ return -1 ;
24
+ } else
25
+ die ("attempting to allocate %" PRIuMAX " over limit %d" ,
26
+ (intmax_t )size , limit );
27
+ }
28
+ return 0 ;
22
29
}
23
30
24
31
try_to_free_t set_try_to_free_routine (try_to_free_t routine )
@@ -42,11 +49,12 @@ char *xstrdup(const char *str)
42
49
return ret ;
43
50
}
44
51
45
- void * xmalloc (size_t size )
52
+ static void * do_xmalloc (size_t size , int gentle )
46
53
{
47
54
void * ret ;
48
55
49
- memory_limit_check (size );
56
+ if (memory_limit_check (size , gentle ))
57
+ return NULL ;
50
58
ret = malloc (size );
51
59
if (!ret && !size )
52
60
ret = malloc (1 );
@@ -55,26 +63,54 @@ void *xmalloc(size_t size)
55
63
ret = malloc (size );
56
64
if (!ret && !size )
57
65
ret = malloc (1 );
58
- if (!ret )
59
- die ("Out of memory, malloc failed (tried to allocate %lu bytes)" ,
60
- (unsigned long )size );
66
+ if (!ret ) {
67
+ if (!gentle )
68
+ die ("Out of memory, malloc failed (tried to allocate %lu bytes)" ,
69
+ (unsigned long )size );
70
+ else {
71
+ error ("Out of memory, malloc failed (tried to allocate %lu bytes)" ,
72
+ (unsigned long )size );
73
+ return NULL ;
74
+ }
75
+ }
61
76
}
62
77
#ifdef XMALLOC_POISON
63
78
memset (ret , 0xA5 , size );
64
79
#endif
65
80
return ret ;
66
81
}
67
82
68
- void * xmallocz (size_t size )
83
+ void * xmalloc (size_t size )
84
+ {
85
+ return do_xmalloc (size , 0 );
86
+ }
87
+
88
+ static void * do_xmallocz (size_t size , int gentle )
69
89
{
70
90
void * ret ;
71
- if (unsigned_add_overflows (size , 1 ))
72
- die ("Data too large to fit into virtual memory space." );
73
- ret = xmalloc (size + 1 );
74
- ((char * )ret )[size ] = 0 ;
91
+ if (unsigned_add_overflows (size , 1 )) {
92
+ if (gentle ) {
93
+ error ("Data too large to fit into virtual memory space." );
94
+ return NULL ;
95
+ } else
96
+ die ("Data too large to fit into virtual memory space." );
97
+ }
98
+ ret = do_xmalloc (size + 1 , gentle );
99
+ if (ret )
100
+ ((char * )ret )[size ] = 0 ;
75
101
return ret ;
76
102
}
77
103
104
+ void * xmallocz (size_t size )
105
+ {
106
+ return do_xmallocz (size , 0 );
107
+ }
108
+
109
+ void * xmallocz_gently (size_t size )
110
+ {
111
+ return do_xmallocz (size , 1 );
112
+ }
113
+
78
114
/*
79
115
* xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
80
116
* "data" to the allocated memory, zero terminates the allocated memory,
@@ -96,7 +132,7 @@ void *xrealloc(void *ptr, size_t size)
96
132
{
97
133
void * ret ;
98
134
99
- memory_limit_check (size );
135
+ memory_limit_check (size , 0 );
100
136
ret = realloc (ptr , size );
101
137
if (!ret && !size )
102
138
ret = realloc (ptr , 1 );
@@ -115,7 +151,7 @@ void *xcalloc(size_t nmemb, size_t size)
115
151
{
116
152
void * ret ;
117
153
118
- memory_limit_check (size * nmemb );
154
+ memory_limit_check (size * nmemb , 0 );
119
155
ret = calloc (nmemb , size );
120
156
if (!ret && (!nmemb || !size ))
121
157
ret = calloc (1 , 1 );
0 commit comments