123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167 |
- #include "malloc.h"
- __UCLIBC_MUTEX_INIT(__malloc_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
- struct malloc_state __malloc_state;
- static int __malloc_largebin_index(unsigned int sz);
- #ifdef __UCLIBC_MALLOC_DEBUGGING__
- void __do_check_chunk(mchunkptr p)
- {
- mstate av = get_malloc_state();
- #ifdef __DOASSERTS__
-
- char* max_address = (char*)(av->top) + chunksize(av->top);
- char* min_address = max_address - av->sbrked_mem;
- unsigned long sz = chunksize(p);
- #endif
- if (!chunk_is_mmapped(p)) {
-
- if (p != av->top) {
- if (contiguous(av)) {
- assert(((char*)p) >= min_address);
- assert(((char*)p + sz) <= ((char*)(av->top)));
- }
- }
- else {
-
- assert((unsigned long)(sz) >= MINSIZE);
-
- assert(prev_inuse(p));
- }
- }
- else {
-
- if (contiguous(av) && av->top != initial_top(av)) {
- assert(((char*)p) < min_address || ((char*)p) > max_address);
- }
-
- assert(((p->prev_size + sz) & (av->pagesize-1)) == 0);
-
- assert(aligned_OK(chunk2mem(p)));
- }
- }
- void __do_check_free_chunk(mchunkptr p)
- {
- size_t sz = p->size & ~PREV_INUSE;
- #ifdef __DOASSERTS__
- mstate av = get_malloc_state();
- mchunkptr next = chunk_at_offset(p, sz);
- #endif
- __do_check_chunk(p);
-
- assert(!inuse(p));
- assert (!chunk_is_mmapped(p));
-
- if ((unsigned long)(sz) >= MINSIZE)
- {
- assert((sz & MALLOC_ALIGN_MASK) == 0);
- assert(aligned_OK(chunk2mem(p)));
-
- assert(next->prev_size == sz);
-
- assert(prev_inuse(p));
- assert (next == av->top || inuse(next));
-
- assert(p->fd->bk == p);
- assert(p->bk->fd == p);
- }
- else
- assert(sz == (sizeof(size_t)));
- }
- void __do_check_inuse_chunk(mchunkptr p)
- {
- mstate av = get_malloc_state();
- mchunkptr next;
- __do_check_chunk(p);
- if (chunk_is_mmapped(p))
- return;
-
- assert(inuse(p));
- next = next_chunk(p);
-
- if (!prev_inuse(p)) {
-
- mchunkptr prv = prev_chunk(p);
- assert(next_chunk(prv) == p);
- __do_check_free_chunk(prv);
- }
- if (next == av->top) {
- assert(prev_inuse(next));
- assert(chunksize(next) >= MINSIZE);
- }
- else if (!inuse(next))
- __do_check_free_chunk(next);
- }
- void __do_check_remalloced_chunk(mchunkptr p, size_t s)
- {
- #ifdef __DOASSERTS__
- size_t sz = p->size & ~PREV_INUSE;
- #endif
- __do_check_inuse_chunk(p);
-
- assert((sz & MALLOC_ALIGN_MASK) == 0);
- assert((unsigned long)(sz) >= MINSIZE);
-
- assert(aligned_OK(chunk2mem(p)));
-
- assert((long)(sz) - (long)(s) >= 0);
- assert((long)(sz) - (long)(s + MINSIZE) < 0);
- }
- void __do_check_malloced_chunk(mchunkptr p, size_t s)
- {
-
- __do_check_remalloced_chunk(p, s);
-
- assert(prev_inuse(p));
- }
- void __do_check_malloc_state(void)
- {
- mstate av = get_malloc_state();
- int i;
- mchunkptr p;
- mchunkptr q;
- mbinptr b;
- unsigned int binbit;
- int empty;
- unsigned int idx;
- size_t size;
- unsigned long total = 0;
- int max_fast_bin;
-
- assert(sizeof(size_t) <= sizeof(char*));
-
- assert((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT-1)) == 0);
-
- if (av->top == 0 || av->top == initial_top(av))
- return;
-
- assert((av->pagesize & (av->pagesize-1)) == 0);
-
-
- assert(get_max_fast(av) <= request2size(MAX_FAST_SIZE));
- max_fast_bin = fastbin_index(av->max_fast);
- for (i = 0; i < NFASTBINS; ++i) {
- p = av->fastbins[i];
-
- if (i > max_fast_bin)
- assert(p == 0);
- while (p != 0) {
-
- __do_check_inuse_chunk(p);
- total += chunksize(p);
-
- assert(fastbin_index(chunksize(p)) == i);
- p = p->fd;
- }
- }
- if (total != 0)
- assert(have_fastchunks(av));
- else if (!have_fastchunks(av))
- assert(total == 0);
-
- for (i = 1; i < NBINS; ++i) {
- b = bin_at(av,i);
-
- if (i >= 2) {
- binbit = get_binmap(av,i);
- empty = last(b) == b;
- if (!binbit)
- assert(empty);
- else if (!empty)
- assert(binbit);
- }
- for (p = last(b); p != b; p = p->bk) {
-
- __do_check_free_chunk(p);
- size = chunksize(p);
- total += size;
- if (i >= 2) {
-
- idx = bin_index(size);
- assert(idx == i);
-
- if ((unsigned long) size >= (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
- assert(p->bk == b ||
- (unsigned long)chunksize(p->bk) >=
- (unsigned long)chunksize(p));
- }
- }
-
- for (q = next_chunk(p);
- (q != av->top && inuse(q) &&
- (unsigned long)(chunksize(q)) >= MINSIZE);
- q = next_chunk(q))
- __do_check_inuse_chunk(q);
- }
- }
-
- __do_check_chunk(av->top);
-
- assert(total <= (unsigned long)(av->max_total_mem));
- assert(av->n_mmaps >= 0);
- assert(av->n_mmaps <= av->max_n_mmaps);
- assert((unsigned long)(av->sbrked_mem) <=
- (unsigned long)(av->max_sbrked_mem));
- assert((unsigned long)(av->mmapped_mem) <=
- (unsigned long)(av->max_mmapped_mem));
- assert((unsigned long)(av->max_total_mem) >=
- (unsigned long)(av->mmapped_mem) + (unsigned long)(av->sbrked_mem));
- }
- #endif
- static void* __malloc_alloc(size_t nb, mstate av)
- {
- mchunkptr old_top;
- size_t old_size;
- char* old_end;
- long size;
- char* fst_brk;
- long correction;
- char* snd_brk;
- size_t front_misalign;
- size_t end_misalign;
- char* aligned_brk;
- mchunkptr p;
- mchunkptr remainder;
- unsigned long remainder_size;
- unsigned long sum;
- size_t pagemask = av->pagesize - 1;
-
- if (have_fastchunks(av)) {
- assert(in_smallbin_range(nb));
- __malloc_consolidate(av);
- return malloc(nb - MALLOC_ALIGN_MASK);
- }
-
- if ((unsigned long)(nb) >= (unsigned long)(av->mmap_threshold) &&
- (av->n_mmaps < av->n_mmaps_max)) {
- char* mm;
-
- size = (nb + (sizeof(size_t)) + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
-
- if ((unsigned long)(size) > (unsigned long)(nb)) {
- mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
- if (mm != (char*)(MORECORE_FAILURE)) {
-
- front_misalign = (size_t)chunk2mem(mm) & MALLOC_ALIGN_MASK;
- if (front_misalign > 0) {
- correction = MALLOC_ALIGNMENT - front_misalign;
- p = (mchunkptr)(mm + correction);
- p->prev_size = correction;
- set_head(p, (size - correction) |IS_MMAPPED);
- }
- else {
- p = (mchunkptr)mm;
- p->prev_size = 0;
- set_head(p, size|IS_MMAPPED);
- }
-
- if (++av->n_mmaps > av->max_n_mmaps)
- av->max_n_mmaps = av->n_mmaps;
- sum = av->mmapped_mem += size;
- if (sum > (unsigned long)(av->max_mmapped_mem))
- av->max_mmapped_mem = sum;
- sum += av->sbrked_mem;
- if (sum > (unsigned long)(av->max_total_mem))
- av->max_total_mem = sum;
- check_chunk(p);
- return chunk2mem(p);
- }
- }
- }
-
- old_top = av->top;
- old_size = chunksize(old_top);
- old_end = (char*)(chunk_at_offset(old_top, old_size));
- fst_brk = snd_brk = (char*)(MORECORE_FAILURE);
-
- assert((old_top == initial_top(av) && old_size == 0) ||
- ((unsigned long) (old_size) >= MINSIZE &&
- prev_inuse(old_top)));
-
- assert((unsigned long)(old_size) < (unsigned long)(nb + MINSIZE));
-
- assert(!have_fastchunks(av));
-
- size = nb + av->top_pad + MINSIZE;
-
- if (contiguous(av))
- size -= old_size;
-
- size = (size + pagemask) & ~pagemask;
-
- if (size > 0)
- fst_brk = (char*)(MORECORE(size));
-
- if (fst_brk == (char*)(MORECORE_FAILURE)) {
-
- if (contiguous(av))
- size = (size + old_size + pagemask) & ~pagemask;
-
- if ((unsigned long)(size) < (unsigned long)(MMAP_AS_MORECORE_SIZE))
- size = MMAP_AS_MORECORE_SIZE;
-
- if ((unsigned long)(size) > (unsigned long)(nb)) {
- fst_brk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
- if (fst_brk != (char*)(MORECORE_FAILURE)) {
-
- snd_brk = fst_brk + size;
-
- set_noncontiguous(av);
- }
- }
- }
- if (fst_brk != (char*)(MORECORE_FAILURE)) {
- av->sbrked_mem += size;
-
- if (fst_brk == old_end && snd_brk == (char*)(MORECORE_FAILURE)) {
- set_head(old_top, (size + old_size) | PREV_INUSE);
- }
-
- else {
- front_misalign = 0;
- end_misalign = 0;
- correction = 0;
- aligned_brk = fst_brk;
-
- if (contiguous(av) && old_size != 0 && fst_brk < old_end) {
- set_noncontiguous(av);
- }
-
- if (contiguous(av)) {
-
- if (old_size != 0)
- av->sbrked_mem += fst_brk - old_end;
-
- front_misalign = (size_t)chunk2mem(fst_brk) & MALLOC_ALIGN_MASK;
- if (front_misalign > 0) {
-
- correction = MALLOC_ALIGNMENT - front_misalign;
- aligned_brk += correction;
- }
-
- correction += old_size;
-
- end_misalign = (size_t)(fst_brk + size + correction);
- correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign;
- assert(correction >= 0);
- snd_brk = (char*)(MORECORE(correction));
- if (snd_brk == (char*)(MORECORE_FAILURE)) {
-
- correction = 0;
- snd_brk = (char*)(MORECORE(0));
- }
- else if (snd_brk < fst_brk) {
-
- snd_brk = fst_brk + size;
- correction = 0;
- set_noncontiguous(av);
- }
- }
-
- else {
-
- assert(aligned_OK(chunk2mem(fst_brk)));
-
- if (snd_brk == (char*)(MORECORE_FAILURE)) {
- snd_brk = (char*)(MORECORE(0));
- av->sbrked_mem += snd_brk - fst_brk - size;
- }
- }
-
- if (snd_brk != (char*)(MORECORE_FAILURE)) {
- av->top = (mchunkptr)aligned_brk;
- set_head(av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
- av->sbrked_mem += correction;
-
- if (old_size != 0) {
-
- old_size = (old_size - 3*(sizeof(size_t))) & ~MALLOC_ALIGN_MASK;
- set_head(old_top, old_size | PREV_INUSE);
-
- chunk_at_offset(old_top, old_size )->size =
- (sizeof(size_t))|PREV_INUSE;
- chunk_at_offset(old_top, old_size + (sizeof(size_t)))->size =
- (sizeof(size_t))|PREV_INUSE;
-
- if (old_size >= MINSIZE) {
- size_t tt = av->trim_threshold;
- av->trim_threshold = (size_t)(-1);
- free(chunk2mem(old_top));
- av->trim_threshold = tt;
- }
- }
- }
- }
-
- sum = av->sbrked_mem;
- if (sum > (unsigned long)(av->max_sbrked_mem))
- av->max_sbrked_mem = sum;
- sum += av->mmapped_mem;
- if (sum > (unsigned long)(av->max_total_mem))
- av->max_total_mem = sum;
- check_malloc_state();
-
- p = av->top;
- size = chunksize(p);
-
- if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
- remainder_size = size - nb;
- remainder = chunk_at_offset(p, nb);
- av->top = remainder;
- set_head(p, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
- check_malloced_chunk(p, nb);
- return chunk2mem(p);
- }
- }
-
- errno = ENOMEM;
- return 0;
- }
- static int __malloc_largebin_index(unsigned int sz)
- {
- unsigned int x = sz >> SMALLBIN_WIDTH;
- unsigned int m;
- if (x >= 0x10000) return NBINS-1;
-
- #if defined(__GNUC__) && defined(i386)
- __asm__("bsrl %1,%0\n\t"
- : "=r" (m)
- : "g" (x));
- #else
- {
-
- unsigned int n = ((x - 0x100) >> 16) & 8;
- x <<= n;
- m = ((x - 0x1000) >> 16) & 4;
- n += m;
- x <<= m;
- m = ((x - 0x4000) >> 16) & 2;
- n += m;
- x = (x << m) >> 14;
- m = 13 - n + (x & ~(x>>1));
- }
- #endif
-
- return NSMALLBINS + (m << 2) + ((sz >> (m + 6)) & 3);
- }
- void* malloc(size_t bytes)
- {
- mstate av;
- size_t nb;
- unsigned int idx;
- mbinptr bin;
- mfastbinptr* fb;
- mchunkptr victim;
- size_t size;
- int victim_index;
- mchunkptr remainder;
- unsigned long remainder_size;
- unsigned int block;
- unsigned int bit;
- unsigned int map;
- mchunkptr fwd;
- mchunkptr bck;
- void * sysmem;
- void * retval;
- #if !defined(__MALLOC_GLIBC_COMPAT__)
- if (!bytes) {
- __set_errno(ENOMEM);
- return NULL;
- }
- #endif
- __MALLOC_LOCK;
- av = get_malloc_state();
-
- checked_request2size(bytes, nb);
-
- if (!have_anychunks(av)) {
- if (av->max_fast == 0)
- __malloc_consolidate(av);
- goto use_top;
- }
-
- if ((unsigned long)(nb) <= (unsigned long)(av->max_fast)) {
- fb = &(av->fastbins[(fastbin_index(nb))]);
- if ( (victim = *fb) != 0) {
- *fb = victim->fd;
- check_remalloced_chunk(victim, nb);
- retval = chunk2mem(victim);
- goto DONE;
- }
- }
-
- if (in_smallbin_range(nb)) {
- idx = smallbin_index(nb);
- bin = bin_at(av,idx);
- if ( (victim = last(bin)) != bin) {
- bck = victim->bk;
- set_inuse_bit_at_offset(victim, nb);
- bin->bk = bck;
- bck->fd = bin;
- check_malloced_chunk(victim, nb);
- retval = chunk2mem(victim);
- goto DONE;
- }
- }
-
- else {
- idx = __malloc_largebin_index(nb);
- if (have_fastchunks(av))
- __malloc_consolidate(av);
- }
-
- while ( (victim = unsorted_chunks(av)->bk) != unsorted_chunks(av)) {
- bck = victim->bk;
- size = chunksize(victim);
-
- if (in_smallbin_range(nb) &&
- bck == unsorted_chunks(av) &&
- victim == av->last_remainder &&
- (unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
-
- remainder_size = size - nb;
- remainder = chunk_at_offset(victim, nb);
- unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
- av->last_remainder = remainder;
- remainder->bk = remainder->fd = unsorted_chunks(av);
- set_head(victim, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
- set_foot(remainder, remainder_size);
- check_malloced_chunk(victim, nb);
- retval = chunk2mem(victim);
- goto DONE;
- }
-
- unsorted_chunks(av)->bk = bck;
- bck->fd = unsorted_chunks(av);
-
- if (size == nb) {
- set_inuse_bit_at_offset(victim, size);
- check_malloced_chunk(victim, nb);
- retval = chunk2mem(victim);
- goto DONE;
- }
-
- if (in_smallbin_range(size)) {
- victim_index = smallbin_index(size);
- bck = bin_at(av, victim_index);
- fwd = bck->fd;
- }
- else {
- victim_index = __malloc_largebin_index(size);
- bck = bin_at(av, victim_index);
- fwd = bck->fd;
- if (fwd != bck) {
-
- if ((unsigned long)(size) < (unsigned long)(bck->bk->size)) {
- fwd = bck;
- bck = bck->bk;
- }
- else if ((unsigned long)(size) >=
- (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
-
- size |= PREV_INUSE;
- while ((unsigned long)(size) < (unsigned long)(fwd->size))
- fwd = fwd->fd;
- bck = fwd->bk;
- }
- }
- }
- mark_bin(av, victim_index);
- victim->bk = bck;
- victim->fd = fwd;
- fwd->bk = victim;
- bck->fd = victim;
- }
-
- if (!in_smallbin_range(nb)) {
- bin = bin_at(av, idx);
- for (victim = last(bin); victim != bin; victim = victim->bk) {
- size = chunksize(victim);
- if ((unsigned long)(size) >= (unsigned long)(nb)) {
- remainder_size = size - nb;
- unlink(victim, bck, fwd);
-
- if (remainder_size < MINSIZE) {
- set_inuse_bit_at_offset(victim, size);
- check_malloced_chunk(victim, nb);
- retval = chunk2mem(victim);
- goto DONE;
- }
-
- else {
- remainder = chunk_at_offset(victim, nb);
- unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
- remainder->bk = remainder->fd = unsorted_chunks(av);
- set_head(victim, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
- set_foot(remainder, remainder_size);
- check_malloced_chunk(victim, nb);
- retval = chunk2mem(victim);
- goto DONE;
- }
- }
- }
- }
-
- ++idx;
- bin = bin_at(av,idx);
- block = idx2block(idx);
- map = av->binmap[block];
- bit = idx2bit(idx);
- for (;;) {
-
- if (bit > map || bit == 0) {
- do {
- if (++block >= BINMAPSIZE)
- goto use_top;
- } while ( (map = av->binmap[block]) == 0);
- bin = bin_at(av, (block << BINMAPSHIFT));
- bit = 1;
- }
-
- while ((bit & map) == 0) {
- bin = next_bin(bin);
- bit <<= 1;
- assert(bit != 0);
- }
-
- victim = last(bin);
-
- if (victim == bin) {
- av->binmap[block] = map &= ~bit;
- bin = next_bin(bin);
- bit <<= 1;
- }
- else {
- size = chunksize(victim);
-
- assert((unsigned long)(size) >= (unsigned long)(nb));
- remainder_size = size - nb;
-
- bck = victim->bk;
- bin->bk = bck;
- bck->fd = bin;
-
- if (remainder_size < MINSIZE) {
- set_inuse_bit_at_offset(victim, size);
- check_malloced_chunk(victim, nb);
- retval = chunk2mem(victim);
- goto DONE;
- }
-
- else {
- remainder = chunk_at_offset(victim, nb);
- unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
- remainder->bk = remainder->fd = unsorted_chunks(av);
-
- if (in_smallbin_range(nb))
- av->last_remainder = remainder;
- set_head(victim, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
- set_foot(remainder, remainder_size);
- check_malloced_chunk(victim, nb);
- retval = chunk2mem(victim);
- goto DONE;
- }
- }
- }
- use_top:
-
- victim = av->top;
- size = chunksize(victim);
- if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
- remainder_size = size - nb;
- remainder = chunk_at_offset(victim, nb);
- av->top = remainder;
- set_head(victim, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
- check_malloced_chunk(victim, nb);
- retval = chunk2mem(victim);
- goto DONE;
- }
-
- sysmem = __malloc_alloc(nb, av);
- retval = sysmem;
- DONE:
- __MALLOC_UNLOCK;
- return retval;
- }
|