Add a segment to hold a new noncontiguous region
| 2483 | |
| 2484 | // Add a segment to hold a new noncontiguous region |
| 2485 | void malloc_state::add_segment(char* tbase, size_t tsize, flag_t mmapped) |
| 2486 | { |
| 2487 | // Determine locations and sizes of segment, fenceposts, old top |
| 2488 | char* old_top = (char*)_top; |
| 2489 | msegmentptr oldsp = segment_holding(old_top); |
| 2490 | char* old_end = oldsp->_base + oldsp->_size; |
| 2491 | size_t ssize = pad_request(sizeof(struct malloc_segment)); |
| 2492 | char* rawsp = old_end - (ssize + 4 * sizeof(size_t) + spp_chunk_align_mask); |
| 2493 | size_t offset = align_offset(chunk2mem(rawsp)); |
| 2494 | char* asp = rawsp + offset; |
| 2495 | char* csp = (asp < (old_top + MIN_CHUNK_SIZE)) ? old_top : asp; |
| 2496 | mchunkptr sp = (mchunkptr)csp; |
| 2497 | msegmentptr ss = (msegmentptr)(chunk2mem(sp)); |
| 2498 | mchunkptr tnext = (mchunkptr)sp->chunk_plus_offset(ssize); |
| 2499 | mchunkptr p = tnext; |
| 2500 | int nfences = 0; |
| 2501 | |
| 2502 | // reset top to new space |
| 2503 | init_top((mchunkptr)tbase, tsize - top_foot_size()); |
| 2504 | |
| 2505 | // Set up segment record |
| 2506 | assert(spp_is_aligned(ss)); |
| 2507 | set_size_and_pinuse_of_inuse_chunk(sp, ssize); |
| 2508 | *ss = _seg; // Push current record |
| 2509 | _seg._base = tbase; |
| 2510 | _seg._size = tsize; |
| 2511 | _seg._sflags = mmapped; |
| 2512 | _seg._next = ss; |
| 2513 | |
| 2514 | // Insert trailing fenceposts |
| 2515 | for (;;) |
| 2516 | { |
| 2517 | mchunkptr nextp = (mchunkptr)p->chunk_plus_offset(sizeof(size_t)); |
| 2518 | p->_head = FENCEPOST_HEAD; |
| 2519 | ++nfences; |
| 2520 | if ((char*)(&(nextp->_head)) < old_end) |
| 2521 | p = nextp; |
| 2522 | else |
| 2523 | break; |
| 2524 | } |
| 2525 | assert(nfences >= 2); |
| 2526 | |
| 2527 | // Insert the rest of old top into a bin as an ordinary free chunk |
| 2528 | if (csp != old_top) |
| 2529 | { |
| 2530 | mchunkptr q = (mchunkptr)old_top; |
| 2531 | size_t psize = csp - old_top; |
| 2532 | mchunkptr tn = (mchunkptr)q->chunk_plus_offset(psize); |
| 2533 | q->set_free_with_pinuse(psize, tn); |
| 2534 | insert_chunk(q, psize); |
| 2535 | } |
| 2536 | |
| 2537 | check_top_chunk(_top); |
| 2538 | } |
| 2539 | |
| 2540 | /* -------------------------- System allocation -------------------------- */ |
| 2541 |
nothing calls this directly
no test coverage detected