mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-08-25 16:54:47 +03:00
merge from dev
This commit is contained in:
commit
6527819eaf
8 changed files with 767 additions and 25 deletions
|
@ -21,7 +21,6 @@ static mi_decl_restrict void* mi_base_malloc_zero_aligned_at(mi_heap_t* const he
|
|||
|
||||
if (mi_unlikely(size > PTRDIFF_MAX)) return NULL; // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
|
||||
if (mi_unlikely(alignment==0 || !_mi_is_power_of_two(alignment))) return NULL; // require power-of-two (see <https://en.cppreference.com/w/c/memory/aligned_alloc>)
|
||||
if (alignment <= MI_MAX_ALIGN_SIZE && offset==0) return _mi_base_malloc_zero(heap, size, zero MI_SOURCE_XARG);
|
||||
const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
|
||||
|
||||
// try if there is a small block available with just the right alignment
|
||||
|
|
|
@ -61,7 +61,7 @@ MI_SOURCE_API3(void*, reallocarray, void*, p, size_t, count, size_t, size)
|
|||
MI_SOURCE_API2(mi_decl_restrict void*, memalign, size_t, alignment, size_t, size)
|
||||
{
|
||||
void* p;
|
||||
if (alignment <= MI_MAX_ALIGN_SIZE) {
|
||||
if (mi_malloc_satisfies_alignment(alignment, size)) {
|
||||
p = MI_SOURCE_ARG(mi_malloc, size);
|
||||
}
|
||||
else {
|
||||
|
@ -73,7 +73,7 @@ MI_SOURCE_API2(mi_decl_restrict void*, memalign, size_t, alignment, size_t, size
|
|||
|
||||
MI_SOURCE_API1(mi_decl_restrict void*, valloc, size_t, size)
|
||||
{
|
||||
return MI_SOURCE_ARG(mi_malloc_aligned, size, _mi_os_page_size());
|
||||
return MI_SOURCE_ARG(mi_memalign, _mi_os_page_size(), size);
|
||||
}
|
||||
|
||||
MI_SOURCE_API1(mi_decl_restrict void*, pvalloc, size_t, size)
|
||||
|
@ -89,7 +89,7 @@ MI_SOURCE_API2(mi_decl_restrict void*, aligned_alloc, size_t, alignment, size_t,
|
|||
if (alignment==0 || !_mi_is_power_of_two(alignment)) return NULL;
|
||||
if ((size&(alignment-1)) != 0) return NULL; // C11 requires integral multiple, see <https://en.cppreference.com/w/c/memory/aligned_alloc>
|
||||
void* p;
|
||||
if (alignment <= MI_MAX_ALIGN_SIZE) {
|
||||
if (mi_malloc_satisfies_alignment(alignment, size)) {
|
||||
p = MI_SOURCE_ARG(mi_malloc, size);
|
||||
}
|
||||
else {
|
||||
|
@ -107,7 +107,7 @@ MI_SOURCE_API3(int, posix_memalign, void**, p, size_t, alignment, size_t, size)
|
|||
if (alignment % sizeof(void*) != 0) return EINVAL; // natural alignment
|
||||
if (!_mi_is_power_of_two(alignment)) return EINVAL; // not a power of 2
|
||||
void* q;
|
||||
if (alignment <= MI_MAX_ALIGN_SIZE) {
|
||||
if (mi_malloc_satisfies_alignment(alignment, size)) {
|
||||
q = MI_SOURCE_ARG(mi_malloc, size);
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -89,7 +89,7 @@ MI_ALLOC_API1(inline mi_decl_restrict void*, malloc, mi_heap_t*, heap, size_t, s
|
|||
else {
|
||||
mi_assert(heap!=NULL);
|
||||
mi_assert(heap->thread_id == 0 || heap->thread_id == _mi_thread_id()); // heaps are thread local
|
||||
void* const p = _mi_malloc_generic(heap, size + MI_PADDING_SIZE MI_SOURCE_XARG);
|
||||
void* const p = _mi_malloc_generic(heap, size + MI_PADDING_SIZE MI_SOURCE_XARG); // note: size can overflow but it is detected in malloc_generic
|
||||
mi_assert_internal(p == NULL || mi_usable_size(p) >= size);
|
||||
#if MI_STAT>1
|
||||
if (p != NULL) {
|
||||
|
|
11
src/page.c
11
src/page.c
|
@ -7,7 +7,7 @@ terms of the MIT license. A copy of the license can be found in the file
|
|||
|
||||
/* -----------------------------------------------------------
|
||||
The core of the allocator. Every segment contains
|
||||
pages of a certain block size. The main function
|
||||
pages of a {certain block size. The main function
|
||||
exported is `mi_malloc_generic`.
|
||||
----------------------------------------------------------- */
|
||||
|
||||
|
@ -774,6 +774,7 @@ static mi_page_t* mi_huge_page_alloc(mi_heap_t* heap, size_t size) {
|
|||
|
||||
|
||||
// Generic allocation routine if the fast path (`alloc.c:mi_page_malloc`) does not succeed.
|
||||
// Note: in debug mode the size includes MI_PADDING_SIZE and might have overflowed.
|
||||
void* _mi_malloc_generic(mi_heap_t* heap, size_t size MI_SOURCE_XPARAM) mi_attr_noexcept
|
||||
{
|
||||
mi_assert_internal(heap != NULL);
|
||||
|
@ -793,9 +794,10 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size MI_SOURCE_XPARAM) mi_attr_
|
|||
|
||||
// huge allocation?
|
||||
mi_page_t* page;
|
||||
if (mi_unlikely(size > MI_LARGE_OBJ_SIZE_MAX)) {
|
||||
if (mi_unlikely(size > PTRDIFF_MAX)) { // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
|
||||
_mi_error_message(EOVERFLOW, "allocation request is too large (%zu b requested)\n", size);
|
||||
const size_t req_size = size - MI_PADDING_SIZE; // correct for padding_size in case of an overflow on `size`
|
||||
if (mi_unlikely(req_size > (MI_LARGE_OBJ_SIZE_MAX - MI_PADDING_SIZE) )) {
|
||||
if (mi_unlikely(req_size > PTRDIFF_MAX)) { // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
|
||||
_mi_error_message(EOVERFLOW, "allocation request is too large (%zu b requested)\n", req_size);
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
|
@ -804,6 +806,7 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size MI_SOURCE_XPARAM) mi_attr_
|
|||
}
|
||||
else {
|
||||
// otherwise find a page with free blocks in our size segregated queues
|
||||
mi_assert_internal(size >= MI_PADDING_SIZE);
|
||||
page = mi_find_free_page(heap,size);
|
||||
}
|
||||
if (mi_unlikely(page == NULL)) { // out of memory
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue