1 diff -rup a/malloc/malloc.c b/malloc/malloc.c
2 --- a/malloc/malloc.c 2012-02-13 21:46:11.678847531 -0700
3 +++ b/malloc/malloc.c 2012-02-13 22:43:14.788431976 -0700
4 @@ -3669,8 +3669,9 @@ public_mALLOc(size_t bytes)
7 /* ... or sbrk() has failed and there is still a chance to mmap() */
8 - ar_ptr = arena_get2(ar_ptr->next ? ar_ptr : 0, bytes);
9 - (void)mutex_unlock(&main_arena.mutex);
10 + mstate prev = ar_ptr->next ? ar_ptr : 0;
11 + (void)mutex_unlock(&ar_ptr->mutex);
12 + ar_ptr = arena_get2(prev, bytes);
14 victim = _int_malloc(ar_ptr, bytes);
15 (void)mutex_unlock(&ar_ptr->mutex);
16 @@ -3929,10 +3930,10 @@ public_vALLOc(size_t bytes)
19 p = _int_valloc(ar_ptr, bytes);
20 - (void)mutex_unlock(&ar_ptr->mutex);
22 /* Maybe the failure is due to running out of mmapped areas. */
23 if(ar_ptr != &main_arena) {
24 + (void)mutex_unlock(&ar_ptr->mutex);
26 (void)mutex_lock(&ar_ptr->mutex);
27 p = _int_memalign(ar_ptr, pagesz, bytes);
28 @@ -3940,14 +3941,17 @@ public_vALLOc(size_t bytes)
31 /* ... or sbrk() has failed and there is still a chance to mmap() */
32 - ar_ptr = arena_get2(ar_ptr->next ? ar_ptr : 0, bytes);
33 + mstate prev = ar_ptr->next ? ar_ptr : 0;
34 + (void)mutex_unlock(&ar_ptr->mutex);
35 + ar_ptr = arena_get2(prev, bytes);
37 p = _int_memalign(ar_ptr, pagesz, bytes);
38 (void)mutex_unlock(&ar_ptr->mutex);
44 + (void)mutex_unlock(&ar_ptr->mutex);
45 assert(!p || chunk_is_mmapped(mem2chunk(p)) ||
46 ar_ptr == arena_for_chunk(mem2chunk(p)));
48 @@ -3975,10 +3979,10 @@ public_pVALLOc(size_t bytes)
50 arena_get(ar_ptr, bytes + 2*pagesz + MINSIZE);
51 p = _int_pvalloc(ar_ptr, bytes);
52 - (void)mutex_unlock(&ar_ptr->mutex);
54 /* Maybe the failure is due to running out of mmapped areas. */
55 if(ar_ptr != &main_arena) {
56 + (void)mutex_unlock(&ar_ptr->mutex);
58 (void)mutex_lock(&ar_ptr->mutex);
59 p = _int_memalign(ar_ptr, pagesz, rounded_bytes);
60 @@ -3986,15 +3990,17 @@ public_pVALLOc(size_t bytes)
63 /* ... or sbrk() has failed and there is still a chance to mmap() */
64 - ar_ptr = arena_get2(ar_ptr->next ? ar_ptr : 0,
65 - bytes + 2*pagesz + MINSIZE);
66 + mstate prev = ar_ptr->next ? ar_ptr : 0;
67 + (void)mutex_unlock(&ar_ptr->mutex);
68 + ar_ptr = arena_get2(prev, bytes + 2*pagesz + MINSIZE);
70 p = _int_memalign(ar_ptr, pagesz, rounded_bytes);
71 (void)mutex_unlock(&ar_ptr->mutex);
77 + (void)mutex_unlock(&ar_ptr->mutex);
78 assert(!p || chunk_is_mmapped(mem2chunk(p)) ||
79 ar_ptr == arena_for_chunk(mem2chunk(p)));
81 @@ -4064,8 +4070,6 @@ public_cALLOc(size_t n, size_t elem_size
83 mem = _int_malloc(av, sz);
85 - /* Only clearing follows, so we can unlock early. */
86 - (void)mutex_unlock(&av->mutex);
88 assert(!mem || chunk_is_mmapped(mem2chunk(mem)) ||
89 av == arena_for_chunk(mem2chunk(mem)));
90 @@ -4073,15 +4077,16 @@ public_cALLOc(size_t n, size_t elem_size
92 /* Maybe the failure is due to running out of mmapped areas. */
93 if(av != &main_arena) {
94 + (void)mutex_unlock(&av->mutex);
95 (void)mutex_lock(&main_arena.mutex);
96 mem = _int_malloc(&main_arena, sz);
97 (void)mutex_unlock(&main_arena.mutex);
100 /* ... or sbrk() has failed and there is still a chance to mmap() */
101 - (void)mutex_lock(&main_arena.mutex);
102 - av = arena_get2(av->next ? av : 0, sz);
103 - (void)mutex_unlock(&main_arena.mutex);
104 + mstate prev = av->next ? av : 0;
105 + (void)mutex_unlock(&av->mutex);
106 + av = arena_get2(prev, sz);
108 mem = _int_malloc(av, sz);
109 (void)mutex_unlock(&av->mutex);
110 @@ -4089,7 +4094,8 @@ public_cALLOc(size_t n, size_t elem_size
113 if (mem == 0) return 0;
116 + (void)mutex_unlock(&av->mutex);
119 /* Two optional cases in which clearing not necessary */