Get memory from system using MMAP
| 2541 | |
| 2542 | // Get memory from system using MMAP |
| 2543 | void* malloc_state::sys_alloc(size_t nb) |
| 2544 | { |
| 2545 | char* tbase = cmfail; |
| 2546 | size_t tsize = 0; |
| 2547 | flag_t mmap_flag = 0; |
| 2548 | size_t asize; // allocation size |
| 2549 | |
| 2550 | mparams.ensure_initialization(); |
| 2551 | |
| 2552 | // Directly map large chunks, but only if already initialized |
| 2553 | if (use_mmap() && nb >= mparams._mmap_threshold && _topsize != 0) |
| 2554 | { |
| 2555 | void* mem = mmap_alloc(nb); |
| 2556 | if (mem != 0) |
| 2557 | return mem; |
| 2558 | } |
| 2559 | |
| 2560 | asize = mparams.granularity_align(nb + sys_alloc_padding()); |
| 2561 | if (asize <= nb) |
| 2562 | return 0; // wraparound |
| 2563 | if (_footprint_limit != 0) |
| 2564 | { |
| 2565 | size_t fp = _footprint + asize; |
| 2566 | if (fp <= _footprint || fp > _footprint_limit) |
| 2567 | return 0; |
| 2568 | } |
| 2569 | |
| 2570 | /* |
| 2571 | Try getting memory with a call to MMAP new space (disabled if not SPP_HAVE_MMAP). |
| 2572 | We need to request enough bytes from system to ensure |
| 2573 | we can malloc nb bytes upon success, so pad with enough space for |
| 2574 | top_foot, plus alignment-pad to make sure we don't lose bytes if |
| 2575 | not on boundary, and round this up to a granularity unit. |
| 2576 | */ |
| 2577 | |
| 2578 | if (SPP_HAVE_MMAP && tbase == cmfail) |
| 2579 | { |
| 2580 | // Try MMAP |
| 2581 | char* mp = (char*)(SPP_CALL_MMAP(asize)); |
| 2582 | if (mp != cmfail) |
| 2583 | { |
| 2584 | tbase = mp; |
| 2585 | tsize = asize; |
| 2586 | mmap_flag = USE_MMAP_BIT; |
| 2587 | } |
| 2588 | } |
| 2589 | |
| 2590 | if (tbase != cmfail) |
| 2591 | { |
| 2592 | |
| 2593 | if ((_footprint += tsize) > _max_footprint) |
| 2594 | _max_footprint = _footprint; |
| 2595 | |
| 2596 | if (!is_initialized()) |
| 2597 | { |
| 2598 | // first-time initialization |
| 2599 | if (_least_addr == 0 || tbase < _least_addr) |
| 2600 | _least_addr = tbase; |
nothing calls this directly
no test coverage detected