Get memory from system using MORECORE or MMAP */
| 4116 | |
| 4117 | /* Get memory from system using MORECORE or MMAP */ |
| 4118 | static void* sys_alloc(mstate m, size_t nb) { |
| 4119 | char* tbase = CMFAIL; |
| 4120 | size_t tsize = 0; |
| 4121 | flag_t mmap_flag = 0; |
| 4122 | size_t asize; /* allocation size */ |
| 4123 | |
| 4124 | ensure_initialization(); |
| 4125 | |
| 4126 | /* Directly map large chunks, but only if already initialized */ |
| 4127 | if (use_mmap(m) && nb >= mparams.mmap_threshold && m->topsize != 0) { |
| 4128 | void* mem = mmap_alloc(m, nb); |
| 4129 | if (mem != 0) |
| 4130 | return mem; |
| 4131 | } |
| 4132 | |
| 4133 | asize = granularity_align(nb + SYS_ALLOC_PADDING); |
| 4134 | if (asize <= nb) |
| 4135 | return 0; /* wraparound */ |
| 4136 | if (m->footprint_limit != 0) { |
| 4137 | size_t fp = m->footprint + asize; |
| 4138 | if (fp <= m->footprint || fp > m->footprint_limit) |
| 4139 | return 0; |
| 4140 | } |
| 4141 | |
| 4142 | /* |
| 4143 | Try getting memory in any of three ways (in most-preferred to |
| 4144 | least-preferred order): |
| 4145 | 1. A call to MORECORE that can normally contiguously extend memory. |
| 4146 | (disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or |
| 4147 | or main space is mmapped or a previous contiguous call failed) |
| 4148 | 2. A call to MMAP new space (disabled if not HAVE_MMAP). |
| 4149 | Note that under the default settings, if MORECORE is unable to |
| 4150 | fulfill a request, and HAVE_MMAP is true, then mmap is |
| 4151 | used as a noncontiguous system allocator. This is a useful backup |
| 4152 | strategy for systems with holes in address spaces -- in this case |
| 4153 | sbrk cannot contiguously expand the heap, but mmap may be able to |
| 4154 | find space. |
| 4155 | 3. A call to MORECORE that cannot usually contiguously extend memory. |
| 4156 | (disabled if not HAVE_MORECORE) |
| 4157 | |
| 4158 | In all cases, we need to request enough bytes from system to ensure |
| 4159 | we can malloc nb bytes upon success, so pad with enough space for |
| 4160 | top_foot, plus alignment-pad to make sure we don't lose bytes if |
| 4161 | not on boundary, and round this up to a granularity unit. |
| 4162 | */ |
| 4163 | |
| 4164 | if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) { |
| 4165 | char* br = CMFAIL; |
| 4166 | size_t ssize = asize; /* sbrk call size */ |
| 4167 | msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top); |
| 4168 | ACQUIRE_MALLOC_GLOBAL_LOCK(); |
| 4169 | |
| 4170 | if (ss == 0) { /* First time through or recovery */ |
| 4171 | char* base = (char*)CALL_MORECORE(0); |
| 4172 | if (base != CMFAIL) { |
| 4173 | size_t fp; |
| 4174 | /* Adjust to end on a page boundary */ |
| 4175 | if (!is_page_aligned(base)) |
no test coverage detected