Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Libc2.23 malloc partial source code analysis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

< (unsigned long) (bck->

Bk- > size) {fwd = bck; bck = bck- > bk; victim- > fd_nextsize = fwd- > fd; victim- > bk_nextsize = fwd- > fd- > bk_nextsize; fwd- > fd- > bk_nextsize = victim- > bk_nextsize- > fd_nextsize = victim } else {assert ((fwd- > size & NON_MAIN_ARENA) = = 0); while ((unsigned long) size

< fwd->

Size) {fwd = fwd- > fd_nextsize; assert ((fwd- > size & NON_MAIN_ARENA) = = 0);} if ((unsigned long) size = = (unsigned long) fwd- > size) / * Always insert in the second position. * / fwd = fwd- > fd; else {victim- > fd_nextsize = fwd; victim- > bk_nextsize = fwd- > bk_nextsize; fwd- > bk_nextsize = victim Victim- > bk_nextsize- > fd_nextsize = victim;} bck = fwd- > bk;}} else victim- > fd_nextsize = victim- > bk_nextsize = victim;} mark_bin (av, victim_index) Victim- > bk = bck; victim- > fd = fwd; fwd- > bk = victim; bck- > fd = victim;#define MAX_ITERS 10000 if (+ + iters > = MAX_ITERS) break;} / * If a large request, scan through the chunks of current bin in sorted order to find smallest that fits. Use the skip list for this. * / if (! in_smallbin_range (nb)) {bin = bin_at (av, idx); / * skip scan if empty or largest chunk is too small * / if ((victim = first (bin))! = bin & & (unsigned long) (victim- > size) > = (unsigned long) (nb)) {victim = victim- > bk_nextsize While ((unsigned long) (size = chunksize (victim)

< (unsigned long) (nb))) victim = victim->

Bk_nextsize; / * Avoid removing the first entry for a size so that the skip list does not have to be rerouted. * / if (victim! = last (bin) & & victim- > size = = victim- > fd- > size) victim = victim- > fd; remainder_size = size-nb; unlink (av, victim, bck, fwd); / * Exhaust * / if (remainder_size

< MINSIZE) { set_inuse_bit_at_offset (victim, size); if (av != &main_arena) victim->

Size | = NON_MAIN_ARENA;} / * Split * / else {remainder = chunk_at_offset (victim, nb); / * We cannot assume the unsorted list is empty and therefore have to perform a complete insert here. * / bck = unsorted_chunks (av); fwd = bck- > fd; if (_ _ glibc_unlikely (fwd- > bk! = bck)) {errstr = "malloc (): corrupted unsorted chunks"; goto errout;} remainder- > bk = bck Remainder- > fd = fwd; bck- > fd = remainder; fwd- > bk = remainder; if (! in_smallbin_range (remainder_size)) {remainder- > fd_nextsize = NULL; remainder- > bk_nextsize = NULL } set_head (victim, nb | PREV_INUSE | (av! = & main_arena? NON_MAIN_ARENA: 0)); set_head (remainder, remainder_size | PREV_INUSE); set_foot (remainder, remainder_size);} check_malloced_chunk (av, victim, nb); void * p = chunk2mem (victim); alloc_perturb (p, bytes); return p } / * Search for a chunk by scanning bins, starting with next largest bin. This search is strictly by best-fit; i.e., the smallest (with ties going to approximately the least recently used) chunk that fits is selected. The bitmap avoids needing to check that most blocks are nonempty. The particular case of skipping all bins during warm-up phases when no chunks have been returned yet is faster than it might look. * / + + idx; bin = bin_at (av, idx); block = idx2block (idx); map = av- > binmap [block]; bit = idx2bit (idx); for (;) {/ * Skip rest of block if there are no more set bits in this block. * / if (bit > map | | bit = = 0) {do {if (+ + block > = BINMAPSIZE) / * out of bins * / goto use_top;} while ((map = av- > binmap [block]) = = 0) Bin = bin_at (av, (block bk! = bck)) {errstr = "malloc (): corrupted unsorted chunks 2"; goto errout;} remainder- > bk = bck; remainder- > fd = fwd; bck- > fd = remainder Fwd- > bk = remainder; / * advertise as last remainder * / if (in_smallbin_range (nb)) av- > last_remainder = remainder; if (! in_smallbin_range (remainder_size)) {remainder- > fd_nextsize = NULL Remainder- > bk_nextsize = NULL;} set_head (victim, nb | PREV_INUSE | (av! = & main_arena? NON_MAIN_ARENA: 0)); set_head (remainder, remainder_size | PREV_INUSE); set_foot (remainder, remainder_size);} check_malloced_chunk (av, victim, nb); void * p = chunk2mem (victim); alloc_perturb (p, bytes); return p }} use_top: / * If large enough, split off the chunk bordering the end of memory (held in av- > top). Note that this is in accord with the best-fit search rule. In effect, av- > top is treated as larger (and thus less well fitting) than any other available chunk since it can be extended to be as large as necessary (up to system limitations). We require that av- > top always exists (i.e., has size > = MINSIZE) after initialization, so if it would otherwise be exhausted by current request, it is replenished. (The main reason for ensuring it exists is that we may need MINSIZE space to put in fenceposts in sysmalloc.) * / 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 | (av! = & main_arena? NON_MAIN_ARENA: 0); set_head (remainder, remainder_size | PREV_INUSE); check_malloced_chunk (av, victim, nb); void * p = chunk2mem (victim); alloc_perturb (p, bytes); return p;} / * When we are using atomic ops to free fast chunks we can get here for all block sizes. * / else if (have_fastchunks (av)) {malloc_consolidate (av); / * restore original bin index * / if (in_smallbin_range (nb)) idx = smallbin_index (nb); else idx = largebin_index (nb) } / * Otherwise, relay to handle system-dependent cases * / else {void * p = sysmalloc (nb, av); if (p! = NULL) alloc_perturb (p, bytes); return p At this point, the study of "libc2.23 malloc partial source code analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report