📄 malloc.c
字号:
If have mmap, and the request size meets the mmap threshold, and the system supports mmap, and there are few enough currently allocated mmapped regions, try to directly map this request rather than expanding top. */ if ((unsigned long)(nb) >= (unsigned long)(av->mmap_threshold) && (av->n_mmaps < av->n_mmaps_max)) { char* mm; /* return value from mmap call*/ /* Round up size to nearest page. For mmapped chunks, the overhead is one (sizeof(size_t)) unit larger than for normal chunks, because there is no following chunk whose prev_size field could be used. */ size = (nb + (sizeof(size_t)) + MALLOC_ALIGN_MASK + pagemask) & ~pagemask; /* Don't try if size wraps around 0 */ if ((unsigned long)(size) > (unsigned long)(nb)) { mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE)); if (mm != (char*)(MORECORE_FAILURE)) { /* The offset to the start of the mmapped region is stored in the prev_size field of the chunk. This allows us to adjust returned start address to meet alignment requirements here and in memalign(), and still be able to compute proper address argument for later munmap in free() and realloc(). */ 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); } /* update statistics */ 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); } } } /* Record incoming configuration of top */ old_top = av->top; old_size = chunksize(old_top); old_end = (char*)(chunk_at_offset(old_top, old_size)); brk = snd_brk = (char*)(MORECORE_FAILURE); /* If not the first time through, we require old_size to * be at least MINSIZE and to have prev_inuse set. */ assert((old_top == initial_top(av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse(old_top))); /* Precondition: not enough current space to satisfy nb request */ assert((unsigned long)(old_size) < (unsigned long)(nb + MINSIZE)); /* Precondition: all fastbins are consolidated */ assert(!have_fastchunks(av)); /* Request enough space for nb + pad + overhead */ size = nb + av->top_pad + MINSIZE; /* If contiguous, we can subtract out existing space that we hope to combine with new space. We add it back later only if we don't actually get contiguous space. */ if (contiguous(av)) size -= old_size; /* Round to a multiple of page size. If MORECORE is not contiguous, this ensures that we only call it with whole-page arguments. And if MORECORE is contiguous and this is not first time through, this preserves page-alignment of previous calls. Otherwise, we correct to page-align below. */ size = (size + pagemask) & ~pagemask; /* Don't try to call MORECORE if argument is so big as to appear negative. Note that since mmap takes size_t arg, it may succeed below even if we cannot call MORECORE. */ if (size > 0) brk = (char*)(MORECORE(size)); /* If have mmap, try using it as a backup when MORECORE fails or cannot be used. This is worth doing on systems that have "holes" in address space, so sbrk cannot extend to give contiguous space, but space is available elsewhere. Note that we ignore mmap max count and threshold limits, since the space will not be used as a segregated mmap region. */ if (brk == (char*)(MORECORE_FAILURE)) { /* Cannot merge with old top, so add its size back in */ if (contiguous(av)) size = (size + old_size + pagemask) & ~pagemask; /* If we are relying on mmap as backup, then use larger units */ if ((unsigned long)(size) < (unsigned long)(MMAP_AS_MORECORE_SIZE)) size = MMAP_AS_MORECORE_SIZE; /* Don't try if size wraps around 0 */ if ((unsigned long)(size) > (unsigned long)(nb)) { brk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE)); if (brk != (char*)(MORECORE_FAILURE)) { /* We do not need, and cannot use, another sbrk call to find end */ snd_brk = brk + size; /* Record that we no longer have a contiguous sbrk region. After the first time mmap is used as backup, we do not ever rely on contiguous space since this could incorrectly bridge regions. */ set_noncontiguous(av); } } } if (brk != (char*)(MORECORE_FAILURE)) { av->sbrked_mem += size; /* If MORECORE extends previous space, we can likewise extend top size. */ if (brk == old_end && snd_brk == (char*)(MORECORE_FAILURE)) { set_head(old_top, (size + old_size) | PREV_INUSE); } /* Otherwise, make adjustments: * If the first time through or noncontiguous, we need to call sbrk just to find out where the end of memory lies. * We need to ensure that all returned chunks from malloc will meet MALLOC_ALIGNMENT * If there was an intervening foreign sbrk, we need to adjust sbrk request size to account for fact that we will not be able to combine new space with existing space in old_top. * Almost all systems internally allocate whole pages at a time, in which case we might as well use the whole last page of request. So we allocate enough more memory to hit a page boundary now, which in turn causes future contiguous calls to page-align. */ else { front_misalign = 0; end_misalign = 0; correction = 0; aligned_brk = brk; /* If MORECORE returns an address lower than we have seen before, we know it isn't really contiguous. This and some subsequent checks help cope with non-conforming MORECORE functions and the presence of "foreign" calls to MORECORE from outside of malloc or by other threads. We cannot guarantee to detect these in all cases, but cope with the ones we do detect. */ if (contiguous(av) && old_size != 0 && brk < old_end) { set_noncontiguous(av); } /* handle contiguous cases */ if (contiguous(av)) { /* We can tolerate forward non-contiguities here (usually due to foreign calls) but treat them as part of our space for stats reporting. */ if (old_size != 0) av->sbrked_mem += brk - old_end; /* Guarantee alignment of first new chunk made from this space */ front_misalign = (size_t)chunk2mem(brk) & MALLOC_ALIGN_MASK; if (front_misalign > 0) { /* Skip over some bytes to arrive at an aligned position. We don't need to specially mark these wasted front bytes. They will never be accessed anyway because prev_inuse of av->top (and any chunk created from its start) is always true after initialization. */ correction = MALLOC_ALIGNMENT - front_misalign; aligned_brk += correction; } /* If this isn't adjacent to existing space, then we will not be able to merge with old_top space, so must add to 2nd request. */ correction += old_size; /* Extend the end address to hit a page boundary */ end_misalign = (size_t)(brk + size + correction); correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign; assert(correction >= 0); snd_brk = (char*)(MORECORE(correction)); if (snd_brk == (char*)(MORECORE_FAILURE)) { /* If can't allocate correction, try to at least find out current brk. It might be enough to proceed without failing. */ correction = 0; snd_brk = (char*)(MORECORE(0)); } else if (snd_brk < brk) { /* If the second call gives noncontiguous space even though it says it won't, the only course of action is to ignore results of second call, and conservatively estimate where the first call left us. Also set noncontiguous, so this won't happen again, leaving at most one hole. Note that this check is intrinsically incomplete. Because MORECORE is allowed to give more space than we ask for, there is no reliable way to detect a noncontiguity producing a forward gap for the second call. */ snd_brk = brk + size; correction = 0; set_noncontiguous(av); } } /* handle non-contiguous cases */ else { /* MORECORE/mmap must correctly align */ assert(aligned_OK(chunk2mem(brk))); /* Find out current end of memory */ if (snd_brk == (char*)(MORECORE_FAILURE)) { snd_brk = (char*)(MORECORE(0)); av->sbrked_mem += snd_brk - brk - size; } } /* Adjust top based on results of second sbrk */ 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 not the first time through, we either have a gap due to foreign sbrk or a non-contiguous region. Insert a double fencepost at old_top to prevent consolidation with space we don't own. These fenceposts are artificial chunks that are marked as inuse and are in any case too small to use. We need two to make sizes and alignments work out. */ if (old_size != 0) { /* Shrink old_top to insert fenceposts, keeping size a multiple of MALLOC_ALIGNMENT. We know there is at least enough space in old_top to do this. */ old_size = (old_size - 3*(sizeof(size_t))) & ~MALLOC_ALIGN_MASK; set_head(old_top, old_size | PREV_INUSE); /* Note that the following assignments completely overwrite old_top when old_size was previously MINSIZE. This is intentional. We need the fencepost, even if old_top otherwise gets lost. */ 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 possible, release the rest, suppressing trimming. */ if (old_size >= MINSIZE) { size_t tt = av->trim_threshold; av->trim_threshold = (size_t)(-1); free(chunk2mem(old_top)); av->trim_threshold = tt; } } } } /* Update statistics */ 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(); /* finally, do the allocation */ p = av->top; size = chunksize(p); /* check that one of the above allocation paths succeeded */ 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); } } /* catch all failure paths */ errno = ENOMEM; return 0;}/* Compute index for size. We expect this to be inlined when compiled with optimization, else not, which works out well.*/static int __malloc_largebin_index(unsigned int sz){ unsigned int x = sz >> SMALLBIN_WIDTH; unsigned int m; /* bit position of highest set bit of m */ if (x >= 0x10000) return NBINS-1; /* On intel, use BSRL instruction to find highest bit */#if defined(__GNUC__) && defined(i386) __asm__("bsrl %1,%0\n\t" : "=r" (m) : "g" (x));#else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -