Make xmalloc and xrealloc thread-safe
By providing a hook for the routine responsible for trying to free some memory on malloc failure, we can ensure that the called routine is protected by the appropriate locks when threads are in play. The obvious offender here was pack-objects which was calling xmalloc() within threads while release_pack_memory() is not thread safe. Signed-off-by: Nicolas Pitre <nico@fluxnic.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nicolas Pitre committed
Mar 24, 2010 at 16:22 UTC
a9a746364bd26d333c7229c6f7e851b507cd284a
3 files changed
+27
-4
builtin-pack-objects.c
+9
@@ -1549,6 +1549,13 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
1549
1550
#ifndef NO_PTHREADS
1551
1552
+static void try_to_free_from_threads(size_t size)
1553
+{
1554
+ read_lock();
1555
+ release_pack_memory(size, -1);
1556
+ read_unlock();
1557
+}
1558
+
1559
/*
1560
* The main thread waits on the condition that (at least) one of the workers
1561
* has stopped working (which is indicated in the .working member of
@@ -1583,10 +1590,12 @@ static void init_threaded_search(void)
1590
pthread_mutex_init(&cache_mutex, NULL);
1591
pthread_mutex_init(&progress_mutex, NULL);
1592
pthread_cond_init(&progress_cond, NULL);
1593
+ set_try_to_free_routine(try_to_free_from_threads);
1594
}
1595
1596
static void cleanup_threaded_search(void)
1597
{
1598
+ set_try_to_free_routine(NULL);
1599
pthread_cond_destroy(&progress_cond);
1600
pthread_mutex_destroy(&read_mutex);
1601
pthread_mutex_destroy(&cache_mutex);
git-compat-util.h
+2
@@ -346,6 +346,8 @@ static inline char *gitstrchrnul(const char *s, int c)
346
347
extern void release_pack_memory(size_t, int);
348
349
+extern void set_try_to_free_routine(void (*routine)(size_t));
350
+
351
extern char *xstrdup(const char *str);
352
extern void *xmalloc(size_t size);
353
extern void *xmallocz(size_t size);
wrapper.c
+16
-4
@@ -3,11 +3,23 @@
3
*/
4
#include "cache.h"
5
6
+static void try_to_free_builtin(size_t size)
7
+{
8
+ release_pack_memory(size, -1);
9
+}
10
+
11
+static void (*try_to_free_routine)(size_t size) = try_to_free_builtin;
12
+
13
+void set_try_to_free_routine(void (*routine)(size_t))
14
+{
15
+ try_to_free_routine = (routine) ? routine : try_to_free_builtin;
16
+}
17
+
18
char *xstrdup(const char *str)
19
{
20
char *ret = strdup(str);
21
if (!ret) {
10
- release_pack_memory(strlen(str) + 1, -1);
22
+ try_to_free_routine(strlen(str) + 1);
23
ret = strdup(str);
24
if (!ret)
25
die("Out of memory, strdup failed");
@@ -21,7 +33,7 @@ void *xmalloc(size_t size)
33
if (!ret && !size)
34
ret = malloc(1);
35
if (!ret) {
24
- release_pack_memory(size, -1);
36
+ try_to_free_routine(size);
37
ret = malloc(size);
38
if (!ret && !size)
39
ret = malloc(1);
@@ -67,7 +79,7 @@ void *xrealloc(void *ptr, size_t size)
79
if (!ret && !size)
80
ret = realloc(ptr, 1);
81
if (!ret) {
70
- release_pack_memory(size, -1);
82
+ try_to_free_routine(size);
83
ret = realloc(ptr, size);
84
if (!ret && !size)
85
ret = realloc(ptr, 1);
@@ -83,7 +95,7 @@ void *xcalloc(size_t nmemb, size_t size)
95
if (!ret && (!nmemb || !size))
96
ret = calloc(1, 1);
97
if (!ret) {
86
- release_pack_memory(nmemb * size, -1);
98
+ try_to_free_routine(nmemb * size);
99
ret = calloc(nmemb, size);
100
if (!ret && (!nmemb || !size))
101
ret = calloc(1, 1);