Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ See docs/process.md for more on how version tagging works.
are now moved correctly into `-sLEGACY_GL_EMULATION` mode. In GLES and WebGL,
use the correct API functions `glClearDepthf()` and `glDepthRangef()` instead.
(#28379)
- mimalloc was updated to 3.7.1. (#28399)
- mimalloc was updated to 3.7.2. (#28399, #28469)
- The `--emrun` linker flag can again be used together with `-sEXIT_RUNTIME=0`,
which was made an error in 3.1.74. `--emrun` still enables `EXIT_RUNTIME` by
default, but if it is explicitly disabled, the runtime is now kept alive
Expand Down
2 changes: 1 addition & 1 deletion system/lib/mimalloc/README.emscripten
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

This contains mimalloc 1c842769cd1ef35f044f659804806505b58f0b48 (v3.7.1) with
This contains mimalloc d152301e379fd5040408a9a1f60b4b0db9811ab4 (v3.7.2) with
Emscripten-specific changes.

Origin: https://github.com/microsoft/mimalloc
Expand Down
8 changes: 4 additions & 4 deletions system/lib/mimalloc/include/mimalloc-new-delete.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ terms of the MIT license. A copy of the license can be found in the file
mi_decl_new_nothrow(n) void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }

#if (__cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1916))
void operator delete (void* p, std::size_t n) noexcept { mi_free_size(p,n); };
void operator delete[](void* p, std::size_t n) noexcept { mi_free_size(p,n); };
void operator delete (void* p, std::size_t n) noexcept { mi_free_sized(p,n); };
void operator delete[](void* p, std::size_t n) noexcept { mi_free_sized(p,n); };
#endif

#if (__cplusplus > 201402L && defined(__cpp_aligned_new))
void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
void operator delete (void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
void operator delete[](void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
void operator delete (void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_sized_aligned(p, n, static_cast<size_t>(al)); };
void operator delete[](void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_sized_aligned(p, n, static_cast<size_t>(al)); };
void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }

Expand Down
4 changes: 2 additions & 2 deletions system/lib/mimalloc/include/mimalloc-override.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ not accidentally mix pointers from different allocators).
// Microsoft extensions
#define _expand(p,n) mi_expand(p,n)
#define _msize(p) mi_usable_size(p)
#define _recalloc(p,c,n) mi_recalloc(p,c,n)
#define _recalloc(p,c,n) (mi_recalloc)(p,c,n)

#define _strdup(s) mi_strdup(s)
#define _strndup(s,n) mi_strndup(s,n)
Expand Down Expand Up @@ -67,6 +67,6 @@ not accidentally mix pointers from different allocators).
#define _aligned_free(p) mi_free(p)
#define _aligned_offset_malloc(n,a,o) mi_malloc_aligned_at(n,a,o)
#define _aligned_offset_realloc(p,n,a,o) mi_realloc_aligned_at(p,n,a,o)
#define _aligned_offset_recalloc(p,c,n,a,o) mi_recalloc_aligned_at(p,c,n,a,o)
#define _aligned_offset_recalloc(p,c,n,a,o) mi_aligned_offset_recalloc(p,c,n,a,o)

#endif // MIMALLOC_OVERRIDE_H
135 changes: 87 additions & 48 deletions system/lib/mimalloc/include/mimalloc.h

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion system/lib/mimalloc/include/mimalloc/alloc/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ terms of the MIT license. A copy of the license can be found in the file
#include "../types.h"
#include "../util/libc.h"
#include "../util/options.h"
#include "page.h" // mi_page_is_small

mi_decl_restrict void* _mi_page_malloc_zero(mi_theap_t* theap, mi_page_t* page, size_t size, bool zero) mi_attr_noexcept; // called from `_mi_theap_malloc_aligned`
bool _mi_page_can_reset_tail(const mi_page_t* page);
void _mi_page_reset_tail(const mi_page_t* page, void* p, size_t newsize, size_t usable);
mi_decl_restrict void* _mi_theap_malloc_zero(mi_theap_t* theap, size_t size, bool zero, size_t huge_alignment, mi_page_t** ppage) mi_attr_noexcept; // called from `_mi_theap_malloc_aligned`
void* _mi_theap_realloc_zero(mi_theap_t* theap, void* p, size_t newsize, bool zero) mi_attr_noexcept;
void* _mi_theap_realloc_zero(mi_theap_t* theap, void* p, size_t oldsize, size_t newsize, bool zero) mi_attr_noexcept;
void _mi_error_count_size_overflow(size_t count, size_t size); // sets `errno` to `ENOMEM`

// Safe multiply `count*size` into `total`; return `true` on overflow (and set `errno` to `ENOMEM` as required by POSIX).
// Saturating multiply `count*size` (returns `SIZE_MAX` on overflow).
static inline size_t mi_count_size_saturate(size_t count, size_t size) {
size_t total;
return (mi_mul_overflow(count, size, &total) ? SIZE_MAX : total);
}

static inline bool mi_count_size_overflow(size_t count, size_t size, size_t* total) {
if (count==1) { // quick check for the case where count is one (common for C++ allocators)
*total = size;
Expand All @@ -43,6 +50,7 @@ static inline bool mi_count_size_overflow(size_t count, size_t size, size_t* tot
#endif
static inline bool mi_realloc_is_in_place(const mi_page_t* page, size_t size, size_t newsize) {
if mi_likely(newsize > size) return false; // grow
if mi_unlikely(newsize <= MI_SMALL_SIZE_MAX && !mi_page_is_small(page)) return false; // keep `mi_free_sized(p,newsize)` valid
if mi_likely(newsize >= size - (size/MI_REALLOC_SHRINK_FACTOR)) return true; // not too much waste
return (newsize > MI_LARGE_MAX_OBJ_SIZE && newsize >= size/2 && _mi_page_can_reset_tail(page)); // huge singleton pages
}
Expand Down
39 changes: 36 additions & 3 deletions system/lib/mimalloc/include/mimalloc/alloc/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,7 @@ static inline bool mi_page_is_small(const mi_page_t* page) {
}

static inline bool mi_page_is_huge(const mi_page_t* page) {
return (mi_page_is_singleton(page) &&
(page->block_size > MI_LARGE_MAX_OBJ_SIZE ||
(mi_memkind_is_os(page->memid.memkind) && page->memid.mem.os.base < (void*)page)));
return page->memid.is_huge;
}

static inline size_t mi_page_min_commit_size(void) {
Expand Down Expand Up @@ -512,6 +510,41 @@ static inline mi_heap_t* mi_page_heap(const mi_page_t* page) {
return heap;
}

// A page visitor may free or move pages, so we remember the page map entry of a live page
// to check later if it is still live (without accessing the page itself).
typedef struct mi_page_ref_s {
const void* start;
mi_page_t* entry;
} mi_page_ref_t;

static inline mi_page_ref_t mi_page_ref(const mi_page_t* page) {
mi_page_ref_t ref = { NULL, NULL };
if (page != NULL) {
ref.start = mi_page_start(page);
ref.entry = _mi_safe_ptr_page(ref.start);
}
return ref;
}

static inline bool mi_page_ref_is_live(mi_page_ref_t ref) {
return (ref.start == NULL || _mi_safe_ptr_page(ref.start) == ref.entry);
}

// Visit a page list where `fun` may free or move pages: we only continue at the next page if it
// is still live (and may miss pages that are freed or moved). Returns `false` if `fun` did.
typedef bool (mi_page_list_visit_fun)(mi_page_t* page, void* arg);

static inline bool mi_page_list_visit(mi_page_t* page, mi_page_list_visit_fun* fun, void* arg) {
while (page != NULL) {
mi_page_t* const next = page->next;
const mi_page_ref_t next_ref = mi_page_ref(next);
if (!fun(page, arg)) return false;
if (!mi_page_ref_is_live(next_ref)) break;
page = next;
}
return true;
}

static inline mi_subproc_t* mi_page_subproc(const mi_page_t* page) {
mi_heap_t* const heap = mi_page_heap(page);
return heap->subproc;
Expand Down
38 changes: 35 additions & 3 deletions system/lib/mimalloc/include/mimalloc/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ terms of the MIT license. A copy of the license can be found in the file
// (as only the address is an integer value), so we use `__SIZEOF_POINTER__` if available.
// ------------------------------------------------------

#if (INTPTR_MAX > INT64_MAX) || (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 16)
#if defined(__SIZEOF_POINTER__)
#if (__SIZEOF_POINTER__ == 16)
# define MI_INTPTR_SHIFT (4) // 128-bit (as on CHERI for example)
#endif
#endif

#if defined(MI_INTPTR_SHIFT)
// already defined
#elif INTPTR_MAX > INT64_MAX
# define MI_INTPTR_SHIFT (4)
#elif INTPTR_MAX == INT64_MAX
# define MI_INTPTR_SHIFT (3)
#elif INTPTR_MAX == INT32_MAX
Expand Down Expand Up @@ -101,6 +109,21 @@ typedef int32_t mi_ssize_t;
#define MI_MiB (MI_KiB*MI_KiB)
#define MI_GiB (MI_MiB*MI_KiB)

// Byte order of the target (can be overridden with `-DMI_BIG_ENDIAN=1`)
#if !defined(MI_BIG_ENDIAN)
#if defined(__BYTE_ORDER__)
#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#define MI_BIG_ENDIAN 1
#else
#define MI_BIG_ENDIAN 0
#endif
#elif defined(__BIG_ENDIAN__)
#define MI_BIG_ENDIAN 1
#else
#define MI_BIG_ENDIAN 0
#endif
#endif


// ------------------------------------------------------
// Raw addresses: the integer address of a pointer (without provenance or memory tag).
Expand Down Expand Up @@ -293,8 +316,17 @@ static inline void* mi_ptr_unpack(const void* auth, mi_addr_tagged_t addrt) {
// (using ARM pointer authentication or a software keyed encoding); this is a `uintptr_t` so it can be atomic.
// ------------------------------------------------------

typedef uintptr_t mi_ptr_signed_t;
#define mi_ptr_signed(tp) mi_ptr_signed_t // a signed `tp*` (only documents the pointer type)
typedef uintptr_t mi_ptr_signed_t;
#define mi_ptr_signed(tp) mi_ptr_signed_t // a signed `tp*` (only documents the pointer type)

#define mi_ptr_signed_from_ptr(tp,p) ((mi_ptr_signed(tp))(p))
#define mi_ptr_signed_as_ptr(tp,p) ((tp*)(p))
#define mi_ptr_signed_null(tp) mi_ptr_signed_from_ptr(tp,NULL)

static inline bool mi_ptr_signed_is_null(mi_ptr_signed_t p) {
return (p == mi_ptr_signed_null(void));
}


// Pointer signing (see `prim/prim-pac.h`): 0=off, 1=weak (using a software process key), 2=required (using the
// arm64 generic pointer authentication key (`pacga`), which must be supported at runtime).
Expand Down
Loading
Loading