MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / add_segment

Function add_segment

system/lib/dlmalloc.c:4063–4113  ·  view source on GitHub ↗

Add a segment to hold a new noncontiguous region */

Source from the content-addressed store, hash-verified

4061
4062/* Add a segment to hold a new noncontiguous region */
4063static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) {
4064 /* Determine locations and sizes of segment, fenceposts, old top */
4065 char* old_top = (char*)m->top;
4066 msegmentptr oldsp = segment_holding(m, old_top);
4067 char* old_end = oldsp->base + oldsp->size;
4068 size_t ssize = pad_request(sizeof(struct malloc_segment));
4069 char* rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
4070 size_t offset = align_offset(chunk2mem(rawsp));
4071 char* asp = rawsp + offset;
4072 char* csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp;
4073 mchunkptr sp = (mchunkptr)csp;
4074 msegmentptr ss = (msegmentptr)(chunk2mem(sp));
4075 mchunkptr tnext = chunk_plus_offset(sp, ssize);
4076 mchunkptr p = tnext;
4077 int nfences = 0;
4078
4079 /* reset top to new space */
4080 init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);
4081
4082 /* Set up segment record */
4083 assert(is_aligned(ss));
4084 set_size_and_pinuse_of_inuse_chunk(m, sp, ssize);
4085 *ss = m->seg; /* Push current record */
4086 m->seg.base = tbase;
4087 m->seg.size = tsize;
4088 m->seg.sflags = mmapped;
4089 m->seg.next = ss;
4090
4091 /* Insert trailing fenceposts */
4092 for (;;) {
4093 mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE);
4094 p->head = FENCEPOST_HEAD;
4095 ++nfences;
4096 if ((char*)(&(nextp->head)) < old_end)
4097 p = nextp;
4098 else
4099 break;
4100 }
4101 assert(nfences >= 2);
4102
4103 /* Insert the rest of old top into a bin as an ordinary free chunk */
4104 if (csp != old_top) {
4105 mchunkptr q = (mchunkptr)old_top;
4106 size_t psize = csp - old_top;
4107 mchunkptr tn = chunk_plus_offset(q, psize);
4108 set_free_with_pinuse(q, psize, tn);
4109 insert_chunk(m, q, psize);
4110 }
4111
4112 check_top_chunk(m, m->top);
4113}
4114
4115/* -------------------------- System allocation -------------------------- */
4116

Callers 1

sys_allocFunction · 0.85

Calls 3

segment_holdingFunction · 0.85
init_topFunction · 0.85
assertFunction · 0.50

Tested by

no test coverage detected