1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
| static void _int_free(mstate av, mchunkptr p, int have_lock) { INTERNAL_SIZE_T size; mfastbinptr* fb; mchunkptr nextchunk; INTERNAL_SIZE_T nextsize; int nextinuse; INTERNAL_SIZE_T prevsize; mchunkptr bck; mchunkptr fwd;
const char* errstr = NULL; int locked = 0;
size = chunksize(p);
if (__builtin_expect((uintptr_t)p > (uintptr_t)-size, 0) || __builtin_expect(misaligned_chunk(p), 0)) { errstr = "free(): invalid pointer"; errout: if (!have_lock && locked) (void)mutex_unlock(&av->mutex); malloc_printerr(check_action, errstr, chunk2mem(p), av); return; }
if (__glibc_unlikely(size < MINSIZE || !aligned_OK(size))) { errstr = "free(): invalid size"; goto errout; }
check_inuse_chunk(av, p);
if ((unsigned long)(size) <= (unsigned long)(get_max_fast())
#if TRIM_FASTBINS && (chunk_at_offset(p, size) != av->top) #endif ) { if (__builtin_expect(chunk_at_offset(p, size)->size <= 2 * SIZE_SZ, 0) || __builtin_expect(chunksize(chunk_at_offset(p, size)) >= av->system_mem, 0)) { if (have_lock || ({ assert(locked == 0); mutex_lock(&av->mutex); locked = 1; chunk_at_offset(p, size)->size <= 2 * SIZE_SZ || chunksize(chunk_at_offset(p, size)) >= av->system_mem; })) { errstr = "free(): invalid next size (fast)"; goto errout; } if (!have_lock) { (void)mutex_unlock(&av->mutex); locked = 0; } }
free_perturb(chunk2mem(p), size - 2 * SIZE_SZ);
set_fastchunks(av); unsigned int idx = fastbin_index(size); fb = &fastbin(av, idx);
mchunkptr old = *fb, old2; unsigned int old_idx = ~0u; do { if (__builtin_expect(old == p, 0)) { errstr = "double free or corruption (fasttop)"; goto errout; } if (have_lock && old != NULL) old_idx = fastbin_index(chunksize(old)); p->fd = old2 = old; } while ((old = catomic_compare_and_exchange_val_rel(fb, p, old2)) != old2);
if (have_lock && old != NULL && __builtin_expect(old_idx != idx, 0)) { errstr = "invalid fastbin entry (free)"; goto errout; } }
else if (!chunk_is_mmapped(p)) { if (!have_lock) { (void)mutex_lock(&av->mutex); locked = 1; }
nextchunk = chunk_at_offset(p, size);
if (__glibc_unlikely(p == av->top)) { errstr = "double free or corruption (top)"; goto errout; } if (__builtin_expect(contiguous(av) && (char*)nextchunk >= ((char*)av->top + chunksize(av->top)), 0)) { errstr = "double free or corruption (out)"; goto errout; } if (__glibc_unlikely(!prev_inuse(nextchunk))) { errstr = "double free or corruption (!prev)"; goto errout; }
nextsize = chunksize(nextchunk); if (__builtin_expect(nextchunk->size <= 2 * SIZE_SZ, 0) || __builtin_expect(nextsize >= av->system_mem, 0)) { errstr = "free(): invalid next size (normal)"; goto errout; }
free_perturb(chunk2mem(p), size - 2 * SIZE_SZ);
if (!prev_inuse(p)) { prevsize = p->prev_size; size += prevsize; p = chunk_at_offset(p, -((long)prevsize)); unlink(av, p, bck, fwd); }
if (nextchunk != av->top) { nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
if (!nextinuse) { unlink(av, nextchunk, bck, fwd); size += nextsize; } else clear_inuse_bit_at_offset(nextchunk, 0);
bck = unsorted_chunks(av); fwd = bck->fd; if (__glibc_unlikely(fwd->bk != bck)) { errstr = "free(): corrupted unsorted chunks"; goto errout; } p->fd = fwd; p->bk = bck; if (!in_smallbin_range(size)) { p->fd_nextsize = NULL; p->bk_nextsize = NULL; } bck->fd = p; fwd->bk = p;
set_head(p, size | PREV_INUSE); set_foot(p, size);
check_free_chunk(av, p); }
else { size += nextsize; set_head(p, size | PREV_INUSE); av->top = p; check_chunk(av, p); }
if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) { if (have_fastchunks(av)) malloc_consolidate(av);
if (av == &main_arena) { #ifndef MORECORE_CANNOT_TRIM if ((unsigned long)(chunksize(av->top)) >= (unsigned long)(mp_.trim_threshold)) systrim(mp_.top_pad, av); #endif } else { heap_info* heap = heap_for_ptr(top(av));
assert(heap->ar_ptr == av); heap_trim(heap, mp_.top_pad); } }
if (!have_lock) { assert(locked); (void)mutex_unlock(&av->mutex); } }
else { munmap_chunk(p); } }
|