MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / add_segment

Function add_segment

dlmalloc/dlmalloc.c:3358–3408  ·  view source on GitHub ↗

Add a segment to hold a new noncontiguous region */

Source from the content-addressed store, hash-verified

3356
3357/* Add a segment to hold a new noncontiguous region */
3358static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) {
3359 /* Determine locations and sizes of segment, fenceposts, old top */
3360 char* old_top = (char*)m->top;
3361 msegmentptr oldsp = segment_holding(m, old_top);
3362 char* old_end = oldsp->base + oldsp->size;
3363 size_t ssize = pad_request(sizeof(struct malloc_segment));
3364 char* rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
3365 size_t offset = align_offset(chunk2mem(rawsp));
3366 char* asp = rawsp + offset;
3367 char* csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp;
3368 mchunkptr sp = (mchunkptr)csp;
3369 msegmentptr ss = (msegmentptr)(chunk2mem(sp));
3370 mchunkptr tnext = chunk_plus_offset(sp, ssize);
3371 mchunkptr p = tnext;
3372 int nfences = 0;
3373
3374 /* reset top to new space */
3375 init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);
3376
3377 /* Set up segment record */
3378 assert(is_aligned(ss));
3379 set_size_and_pinuse_of_inuse_chunk(m, sp, ssize);
3380 *ss = m->seg; /* Push current record */
3381 m->seg.base = tbase;
3382 m->seg.size = tsize;
3383 m->seg.sflags = mmapped;
3384 m->seg.next = ss;
3385
3386 /* Insert trailing fenceposts */
3387 for (;;) {
3388 mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE);
3389 p->head = FENCEPOST_HEAD;
3390 ++nfences;
3391 if ((char*)(&(nextp->head)) < old_end)
3392 p = nextp;
3393 else
3394 break;
3395 }
3396 assert(nfences >= 2);
3397
3398 /* Insert the rest of old top into a bin as an ordinary free chunk */
3399 if (csp != old_top) {
3400 mchunkptr q = (mchunkptr)old_top;
3401 size_t psize = csp - old_top;
3402 mchunkptr tn = chunk_plus_offset(q, psize);
3403 set_free_with_pinuse(q, psize, tn);
3404 insert_chunk(m, q, psize);
3405 }
3406
3407 check_top_chunk(m, m->top);
3408}
3409
3410/* -------------------------- System allocation -------------------------- */
3411

Callers 2

sys_allocFunction · 0.85
internal_reallocFunction · 0.85

Calls 2

segment_holdingFunction · 0.85
init_topFunction · 0.85

Tested by

no test coverage detected