MCPcopy Index your code
hub / github.com/dcodeIO/webassembly / sys_alloc

Function sys_alloc

lib/dlmalloc/malloc.c:4032–4238  ·  view source on GitHub ↗

Get memory from system using MORECORE or MMAP */

Source from the content-addressed store, hash-verified

4030
4031/* Get memory from system using MORECORE or MMAP */
4032static void* sys_alloc(mstate m, size_t nb) {
4033 char* tbase = CMFAIL;
4034 size_t tsize = 0;
4035 flag_t mmap_flag = 0;
4036 size_t asize; /* allocation size */
4037
4038 ensure_initialization();
4039
4040 /* Directly map large chunks, but only if already initialized */
4041 if (use_mmap(m) && nb >= mparams.mmap_threshold && m->topsize != 0) {
4042 void* mem = mmap_alloc(m, nb);
4043 if (mem != 0)
4044 return mem;
4045 }
4046
4047 asize = granularity_align(nb + SYS_ALLOC_PADDING);
4048 if (asize <= nb)
4049 return 0; /* wraparound */
4050 if (m->footprint_limit != 0) {
4051 size_t fp = m->footprint + asize;
4052 if (fp <= m->footprint || fp > m->footprint_limit)
4053 return 0;
4054 }
4055
4056 /*
4057 Try getting memory in any of three ways (in most-preferred to
4058 least-preferred order):
4059 1. A call to MORECORE that can normally contiguously extend memory.
4060 (disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or
4061 or main space is mmapped or a previous contiguous call failed)
4062 2. A call to MMAP new space (disabled if not HAVE_MMAP).
4063 Note that under the default settings, if MORECORE is unable to
4064 fulfill a request, and HAVE_MMAP is true, then mmap is
4065 used as a noncontiguous system allocator. This is a useful backup
4066 strategy for systems with holes in address spaces -- in this case
4067 sbrk cannot contiguously expand the heap, but mmap may be able to
4068 find space.
4069 3. A call to MORECORE that cannot usually contiguously extend memory.
4070 (disabled if not HAVE_MORECORE)
4071
4072 In all cases, we need to request enough bytes from system to ensure
4073 we can malloc nb bytes upon success, so pad with enough space for
4074 top_foot, plus alignment-pad to make sure we don't lose bytes if
4075 not on boundary, and round this up to a granularity unit.
4076 */
4077
4078 if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) {
4079 char* br = CMFAIL;
4080 size_t ssize = asize; /* sbrk call size */
4081 msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top);
4082 ACQUIRE_MALLOC_GLOBAL_LOCK();
4083
4084 if (ss == 0) { /* First time through or recovery */
4085 char* base = (char*)CALL_MORECORE(0);
4086 if (base != CMFAIL) {
4087 size_t fp;
4088 /* Adjust to end on a page boundary */
4089 if (!is_page_aligned(base))

Callers 2

dlmallocFunction · 0.85
mspace_mallocFunction · 0.85

Calls 6

mmap_allocFunction · 0.85
segment_holdingFunction · 0.85
init_binsFunction · 0.85
init_topFunction · 0.85
prepend_allocFunction · 0.85
add_segmentFunction · 0.85

Tested by

no test coverage detected