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

Function _mi_mem_alloc_aligned

3rd/mimalloc-2.0.9/src/region.c:349–384  ·  view source on GitHub ↗

Allocate `size` memory aligned at `alignment`. Return non NULL on success, with a given memory `id`. (`id` is abstract, but `id = idx*MI_REGION_MAP_BITS + bitidx`)

Source from the content-addressed store, hash-verified

347// Allocate `size` memory aligned at `alignment`. Return non NULL on success, with a given memory `id`.
348// (`id` is abstract, but `id = idx*MI_REGION_MAP_BITS + bitidx`)
349void* _mi_mem_alloc_aligned(size_t size, size_t alignment, size_t align_offset, bool* commit, bool* large, bool* is_pinned, bool* is_zero, size_t* memid, mi_os_tld_t* tld)
350{
351 mi_assert_internal(memid != NULL && tld != NULL);
352 mi_assert_internal(size > 0);
353 *memid = 0;
354 *is_zero = false;
355 *is_pinned = false;
356 bool default_large = false;
357 if (large==NULL) large = &default_large; // ensure `large != NULL`
358 if (size == 0) return NULL;
359 size = _mi_align_up(size, _mi_os_page_size());
360
361 // allocate from regions if possible
362 void* p = NULL;
363 size_t arena_memid;
364 const size_t blocks = mi_region_block_count(size);
365 if (blocks <= MI_REGION_MAX_OBJ_BLOCKS && alignment <= MI_SEGMENT_ALIGN && align_offset == 0) {
366 p = mi_region_try_alloc(blocks, commit, large, is_pinned, is_zero, memid, tld);
367 if (p == NULL) {
368 _mi_warning_message("unable to allocate from region: size %zu\n", size);
369 }
370 }
371 if (p == NULL) {
372 // and otherwise fall back to the OS
373 p = _mi_arena_alloc_aligned(size, alignment, align_offset, commit, large, is_pinned, is_zero, _mi_arena_id_none(), & arena_memid, tld);
374 *memid = mi_memid_create_from_arena(arena_memid);
375 }
376
377 if (p != NULL) {
378 mi_assert_internal(((uintptr_t)p + align_offset) % alignment == 0);
379 #if (MI_DEBUG>=2) && !MI_TRACK_ENABLED
380 if (*commit) { ((uint8_t*)p)[0] = 0; } // ensure the memory is committed
381 #endif
382 }
383 return p;
384}
385
386
387

Callers

nothing calls this directly

Calls 8

_mi_align_upFunction · 0.85
_mi_os_page_sizeFunction · 0.85
mi_region_block_countFunction · 0.85
mi_region_try_allocFunction · 0.85
_mi_warning_messageFunction · 0.85
_mi_arena_alloc_alignedFunction · 0.85
_mi_arena_id_noneFunction · 0.85

Tested by

no test coverage detected