mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-06 07:29:30 +03:00
further progress on removing aligned limit
This commit is contained in:
parent
4b91ff760d
commit
a200291ae5
3 changed files with 28 additions and 11 deletions
|
@ -41,10 +41,9 @@ static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t*
|
||||||
#endif
|
#endif
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
oversize = size + MI_SEGMENT_SIZE - 1;
|
oversize = (size <= MI_SMALL_SIZE_MAX ? MI_SMALL_SIZE_MAX + 1 /* ensure we use generic malloc path */ : size);
|
||||||
p = _mi_heap_malloc_zero_ex(heap, oversize, zero, alignment);
|
p = _mi_heap_malloc_zero_ex(heap, oversize, zero, alignment); // the page block size should be large enough to align in the single huge page block
|
||||||
if (p == NULL) return NULL;
|
if (p == NULL) return NULL;
|
||||||
//mi_assert_internal(_mi_is_aligned(p, alignment));
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// otherwise over-allocate
|
// otherwise over-allocate
|
||||||
|
@ -57,7 +56,8 @@ static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t*
|
||||||
uintptr_t adjust = alignment - (((uintptr_t)p + offset) & align_mask);
|
uintptr_t adjust = alignment - (((uintptr_t)p + offset) & align_mask);
|
||||||
mi_assert_internal(adjust <= alignment);
|
mi_assert_internal(adjust <= alignment);
|
||||||
void* aligned_p = (adjust == alignment ? p : (void*)((uintptr_t)p + adjust));
|
void* aligned_p = (adjust == alignment ? p : (void*)((uintptr_t)p + adjust));
|
||||||
if (aligned_p != p) mi_page_set_has_aligned(_mi_ptr_page(p), true);
|
if (aligned_p != p) { mi_page_set_has_aligned(_mi_ptr_page(p), true); }
|
||||||
|
mi_assert_internal(mi_page_usable_block_size(_mi_ptr_page(p)) >= adjust + size);
|
||||||
mi_assert_internal(p == _mi_page_ptr_unalign(_mi_ptr_segment(aligned_p), _mi_ptr_page(aligned_p), aligned_p));
|
mi_assert_internal(p == _mi_page_ptr_unalign(_mi_ptr_segment(aligned_p), _mi_ptr_page(aligned_p), aligned_p));
|
||||||
mi_assert_internal(((uintptr_t)aligned_p + offset) % alignment == 0);
|
mi_assert_internal(((uintptr_t)aligned_p + offset) % alignment == 0);
|
||||||
|
|
||||||
|
|
|
@ -262,7 +262,7 @@ static mi_page_t* mi_page_fresh_alloc(mi_heap_t* heap, mi_page_queue_t* pq, size
|
||||||
}
|
}
|
||||||
// a fresh page was found, initialize it
|
// a fresh page was found, initialize it
|
||||||
mi_assert_internal(pq==NULL || _mi_page_segment(page)->page_kind != MI_PAGE_HUGE);
|
mi_assert_internal(pq==NULL || _mi_page_segment(page)->page_kind != MI_PAGE_HUGE);
|
||||||
mi_page_init(heap, page, block_size, heap->tld);
|
mi_page_init(heap, page, (pq == NULL ? MI_HUGE_BLOCK_SIZE : block_size), heap->tld);
|
||||||
mi_heap_stat_increase(heap, pages, 1);
|
mi_heap_stat_increase(heap, pages, 1);
|
||||||
if (pq!=NULL) mi_page_queue_push(heap, pq, page); // huge pages use pq==NULL
|
if (pq!=NULL) mi_page_queue_push(heap, pq, page); // huge pages use pq==NULL
|
||||||
mi_assert_expensive(_mi_page_is_valid(page));
|
mi_assert_expensive(_mi_page_is_valid(page));
|
||||||
|
@ -643,7 +643,7 @@ static void mi_page_init(mi_heap_t* heap, mi_page_t* page, size_t block_size, mi
|
||||||
mi_track_mem_noaccess(page_start,page_size);
|
mi_track_mem_noaccess(page_start,page_size);
|
||||||
page->xblock_size = (block_size < MI_HUGE_BLOCK_SIZE ? (uint32_t)block_size : MI_HUGE_BLOCK_SIZE);
|
page->xblock_size = (block_size < MI_HUGE_BLOCK_SIZE ? (uint32_t)block_size : MI_HUGE_BLOCK_SIZE);
|
||||||
mi_assert_internal(page_size / block_size < (1L<<16));
|
mi_assert_internal(page_size / block_size < (1L<<16));
|
||||||
page->reserved = (uint16_t)(page_size / block_size);
|
page->reserved = (block_size < MI_HUGE_BLOCK_SIZE ? (uint16_t)(page_size / block_size) : 1);
|
||||||
#ifdef MI_ENCODE_FREELIST
|
#ifdef MI_ENCODE_FREELIST
|
||||||
page->keys[0] = _mi_heap_random_next(heap);
|
page->keys[0] = _mi_heap_random_next(heap);
|
||||||
page->keys[1] = _mi_heap_random_next(heap);
|
page->keys[1] = _mi_heap_random_next(heap);
|
||||||
|
|
|
@ -163,10 +163,27 @@ int main(void) {
|
||||||
CHECK_BODY("malloc-aligned7") {
|
CHECK_BODY("malloc-aligned7") {
|
||||||
void* p = mi_malloc_aligned(1024,MI_ALIGNMENT_MAX);
|
void* p = mi_malloc_aligned(1024,MI_ALIGNMENT_MAX);
|
||||||
mi_free(p);
|
mi_free(p);
|
||||||
|
result = ((uintptr_t)p % MI_ALIGNMENT_MAX) == 0;
|
||||||
};
|
};
|
||||||
CHECK_BODY("malloc-aligned8") {
|
CHECK_BODY("malloc-aligned8") {
|
||||||
void* p = mi_malloc_aligned(1024,2*MI_ALIGNMENT_MAX);
|
bool ok = true;
|
||||||
|
for (int i = 0; i < 5 && ok; i++) {
|
||||||
|
int n = (1 << i);
|
||||||
|
void* p = mi_malloc_aligned(1024, n * MI_ALIGNMENT_MAX);
|
||||||
|
ok = ((uintptr_t)p % (n*MI_ALIGNMENT_MAX)) == 0;
|
||||||
mi_free(p);
|
mi_free(p);
|
||||||
|
}
|
||||||
|
result = ok;
|
||||||
|
};
|
||||||
|
CHECK_BODY("malloc-aligned9") {
|
||||||
|
bool ok = true;
|
||||||
|
for (int i = 0; i < 5 && ok; i++) {
|
||||||
|
int n = (1 << i);
|
||||||
|
void* p = mi_malloc_aligned( 2*n*MI_ALIGNMENT_MAX, n*MI_ALIGNMENT_MAX);
|
||||||
|
ok = ((uintptr_t)p % (n*MI_ALIGNMENT_MAX)) == 0;
|
||||||
|
mi_free(p);
|
||||||
|
}
|
||||||
|
result = ok;
|
||||||
};
|
};
|
||||||
CHECK_BODY("malloc-aligned-at1") {
|
CHECK_BODY("malloc-aligned-at1") {
|
||||||
void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);
|
void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);
|
||||||
|
|
Loading…
Add table
Reference in a new issue