12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163 |
- #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);
- }
- }
-
- __set_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;
-
- checked_request2size(bytes, nb);
- __MALLOC_LOCK;
- av = get_malloc_state();
-
- 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;
- }
- weak_alias(malloc, __libc_malloc)
|