MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / mi_os_mem_alloc

Function mi_os_mem_alloc

3rd/mimalloc-2.0.9/src/os.c:708–743  ·  view source on GitHub ↗

Note: the `try_alignment` is just a hint and the returned pointer is not guaranteed to be aligned.

Source from the content-addressed store, hash-verified

706
707// Note: the `try_alignment` is just a hint and the returned pointer is not guaranteed to be aligned.
708static void* mi_os_mem_alloc(size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large, mi_stats_t* stats) {
709 mi_assert_internal(size > 0 && (size % _mi_os_page_size()) == 0);
710 if (size == 0) return NULL;
711 if (!commit) allow_large = false;
712 if (try_alignment == 0) try_alignment = 1; // avoid 0 to ensure there will be no divide by zero when aligning
713
714 void* p = NULL;
715 /*
716 if (commit && allow_large) {
717 p = _mi_os_try_alloc_from_huge_reserved(size, try_alignment);
718 if (p != NULL) {
719 *is_large = true;
720 return p;
721 }
722 }
723 */
724
725 #if defined(_WIN32)
726 int flags = MEM_RESERVE;
727 if (commit) { flags |= MEM_COMMIT; }
728 p = mi_win_virtual_alloc(NULL, size, try_alignment, flags, false, allow_large, is_large);
729 #elif defined(MI_USE_SBRK) || defined(__wasi__)
730 MI_UNUSED(allow_large);
731 *is_large = false;
732 p = mi_heap_grow(size, try_alignment);
733 #else
734 int protect_flags = (commit ? (PROT_WRITE | PROT_READ) : PROT_NONE);
735 p = mi_unix_mmap(NULL, size, try_alignment, protect_flags, false, allow_large, is_large);
736 #endif
737 mi_stat_counter_increase(stats->mmap_calls, 1);
738 if (p != NULL) {
739 _mi_stat_increase(&stats->reserved, size);
740 if (commit) { _mi_stat_increase(&stats->committed, size); }
741 }
742 return p;
743}
744
745
746// Primitive aligned allocation from the OS.

Callers 2

mi_os_mem_alloc_alignedFunction · 0.85
_mi_os_allocFunction · 0.85

Calls 5

_mi_os_page_sizeFunction · 0.85
mi_win_virtual_allocFunction · 0.85
mi_heap_growFunction · 0.85
mi_unix_mmapFunction · 0.85
_mi_stat_increaseFunction · 0.85

Tested by

no test coverage detected