Skip to content
Merged
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
14 changes: 7 additions & 7 deletions Include/internal/mimalloc/mimalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ terms of the MIT license. A copy of the license can be found in the file
// ------------------------------------------------------

#ifdef __cplusplus
#if (__cplusplus >= 201103L) || (_MSC_VER > 1900) // C++11
#if (__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900) // C++11
#define mi_attr_noexcept noexcept
#else
#define mi_attr_noexcept throw()
Expand All @@ -30,7 +30,7 @@ terms of the MIT license. A copy of the license can be found in the file
#define mi_decl_nodiscard __attribute__((warn_unused_result))
#elif defined(_HAS_NODISCARD)
#define mi_decl_nodiscard _NODISCARD
#elif (_MSC_VER >= 1700)
#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
#define mi_decl_nodiscard _Check_return_
#else
#define mi_decl_nodiscard
Expand Down Expand Up @@ -513,7 +513,7 @@ mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_heap_alloc_new_n(mi_h

#include <cstddef> // std::size_t
#include <cstdint> // PTRDIFF_MAX
#if (__cplusplus >= 201103L) || (_MSC_VER > 1900) // C++11
#if (__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900) // C++11
#include <type_traits> // std::true_type
#include <utility> // std::forward
#endif
Expand All @@ -527,7 +527,7 @@ template<class T> struct _mi_stl_allocator_common {
typedef value_type* pointer;
typedef value_type const* const_pointer;

#if ((__cplusplus >= 201103L) || (_MSC_VER > 1900)) // C++11
#if ((__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900)) // C++11
using propagate_on_container_copy_assignment = std::true_type;
using propagate_on_container_move_assignment = std::true_type;
using propagate_on_container_swap = std::true_type;
Expand Down Expand Up @@ -562,7 +562,7 @@ template<class T> struct mi_stl_allocator : public _mi_stl_allocator_common<T> {
mi_decl_nodiscard pointer allocate(size_type count, const void* = 0) { return static_cast<pointer>(mi_new_n(count, sizeof(value_type))); }
#endif

#if ((__cplusplus >= 201103L) || (_MSC_VER > 1900)) // C++11
#if ((__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900)) // C++11
using is_always_equal = std::true_type;
#endif
};
Expand All @@ -571,7 +571,7 @@ template<class T1,class T2> bool operator==(const mi_stl_allocator<T1>& , const
template<class T1,class T2> bool operator!=(const mi_stl_allocator<T1>& , const mi_stl_allocator<T2>& ) mi_attr_noexcept { return false; }


#if (__cplusplus >= 201103L) || (_MSC_VER >= 1900) // C++11
#if (__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1900) // C++11
#define MI_HAS_HEAP_STL_ALLOCATOR 1

#include <memory> // std::shared_ptr
Expand All @@ -591,7 +591,7 @@ template<class T, bool _mi_destroy> struct _mi_heap_stl_allocator_common : publi
mi_decl_nodiscard pointer allocate(size_type count, const void* = 0) { return static_cast<pointer>(mi_heap_alloc_new_n(this->heap.get(), count, sizeof(value_type))); }
#endif

#if ((__cplusplus >= 201103L) || (_MSC_VER > 1900)) // C++11
#if ((__cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER > 1900)) // C++11
using is_always_equal = std::false_type;
#endif

Expand Down
15 changes: 11 additions & 4 deletions Include/internal/mimalloc/mimalloc/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ terms of the MIT license. A copy of the license can be found in the file
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#elif !defined(__wasi__) && (!defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__))
#define MI_USE_PTHREADS
#endif
#if !defined(MI_USE_PTHREADS)
#if !defined(_WIN32) && !defined(__wasi__) && (!defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__))
#define MI_USE_PTHREADS 1
#else
#define MI_USE_PTHREADS 0
#endif
#endif
#if MI_USE_PTHREADS
#include <pthread.h>
#endif

Expand Down Expand Up @@ -511,7 +518,7 @@ static inline void mi_atomic_yield_sleep( size_t* ticks, const size_t ticks_unti
// These should be light-weight in-process only locks.
// Only used for reserving arena's and to maintain the abandoned list.
// ----------------------------------------------------------------------
#if _MSC_VER
#if defined(_MSC_VER)
#pragma warning(disable:26110) // unlock with holding lock
#endif

Expand Down Expand Up @@ -543,7 +550,7 @@ static inline void mi_lock_done(mi_lock_t* lock) {
(void)(lock);
}

#elif defined(MI_USE_PTHREADS)
#elif MI_USE_PTHREADS

#include <string.h> // memcpy
void _mi_error_message(int err, const char* fmt, ...);
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/mimalloc/mimalloc/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void _mi_segment_page_abandon(mi_page_t* page, mi_segments_tld_t* tld);
bool _mi_segment_try_reclaim_abandoned( mi_heap_t* heap, bool try_all, mi_segments_tld_t* tld);
void _mi_segment_collect(mi_segment_t* segment, bool force);

#if MI_HUGE_PAGE_ABANDON
#if defined(MI_HUGE_PAGE_ABANDON) && MI_HUGE_PAGE_ABANDON
void _mi_segment_huge_page_free(mi_segment_t* segment, mi_page_t* page, mi_block_t* block);
#else
void _mi_segment_huge_page_reset(mi_segment_t* segment, mi_page_t* page, mi_block_t* block);
Expand Down
11 changes: 11 additions & 0 deletions Include/internal/mimalloc/mimalloc/prim.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ static inline void* mi_prim_tls_slot(size_t slot) mi_attr_noexcept {
__asm__ volatile ("mv %0, tp" : "=r" (tcb));
res = tcb[slot];
#elif defined(__APPLE__) && defined(__POWERPC__) // ppc, issue #781
#if !MI_USE_PTHREADS
#error PowerPC TLS slots require MI_USE_PTHREADS
#endif
MI_UNUSED(ofs);
res = pthread_getspecific(slot);
#endif
Expand Down Expand Up @@ -405,6 +408,10 @@ static inline mi_heap_t* mi_prim_get_default_heap(void) {

#elif defined(MI_TLS_PTHREAD_SLOT_OFS)

#if !MI_USE_PTHREADS
#error MI_TLS_PTHREAD_SLOT_OFS requires MI_USE_PTHREADS
#endif

static inline mi_heap_t** mi_prim_tls_pthread_heap_slot(void) {
pthread_t self = pthread_self();
#if defined(__DragonFly__)
Expand All @@ -423,6 +430,10 @@ static inline mi_heap_t* mi_prim_get_default_heap(void) {

#elif defined(MI_TLS_PTHREAD)

#if !MI_USE_PTHREADS
#error MI_TLS_PTHREAD requires MI_USE_PTHREADS
#endif

extern mi_decl_hidden pthread_key_t _mi_heap_default_key;
static inline mi_heap_t* mi_prim_get_default_heap(void) {
mi_heap_t* heap = (mi_unlikely(_mi_heap_default_key == (pthread_key_t)(-1)) ? _mi_heap_main_get() : (mi_heap_t*)pthread_getspecific(_mi_heap_default_key));
Expand Down
6 changes: 3 additions & 3 deletions Include/internal/mimalloc/mimalloc/track.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defined, undefined, or not accessible at all:
-------------------------------------------------------------------------------------------------------*/

#if MI_TRACK_VALGRIND
#if defined(MI_TRACK_VALGRIND) && MI_TRACK_VALGRIND
// valgrind tool

#define MI_TRACK_ENABLED 1
Expand All @@ -60,7 +60,7 @@ defined, undefined, or not accessible at all:
#define mi_track_mem_undefined(p,size) VALGRIND_MAKE_MEM_UNDEFINED(p,size)
#define mi_track_mem_noaccess(p,size) VALGRIND_MAKE_MEM_NOACCESS(p,size)

#elif MI_TRACK_ASAN
#elif defined(MI_TRACK_ASAN) && MI_TRACK_ASAN
// address sanitizer

#define MI_TRACK_ENABLED 1
Expand All @@ -75,7 +75,7 @@ defined, undefined, or not accessible at all:
#define mi_track_mem_undefined(p,size) ASAN_UNPOISON_MEMORY_REGION(p,size)
#define mi_track_mem_noaccess(p,size) ASAN_POISON_MEMORY_REGION(p,size)

#elif MI_TRACK_ETW
#elif defined(MI_TRACK_ETW) && MI_TRACK_ETW
// windows event tracing

#define MI_TRACK_ENABLED 1
Expand Down
14 changes: 9 additions & 5 deletions Include/internal/mimalloc/mimalloc/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ terms of the MIT license. A copy of the license can be found in the file
#include <stddef.h> // ptrdiff_t
#include <stdint.h> // uintptr_t, uint16_t, etc
#include <stdbool.h> // bool
#include <limits.h> // LONG_MAX
#include "atomic.h" // _Atomic

#ifdef _MSC_VER
Expand Down Expand Up @@ -83,9 +84,12 @@ terms of the MIT license. A copy of the license can be found in the file

// Reserve extra padding at the end of each block to be more resilient against heap block overflows.
// The padding can detect buffer overflow on free.
#if !defined(MI_PADDING) && (MI_SECURE>=3 || MI_DEBUG>=1 || (MI_TRACK_VALGRIND || MI_TRACK_ASAN || MI_TRACK_ETW))
#if !defined(MI_PADDING) && (MI_SECURE>=3 || MI_DEBUG>=1 || (defined(MI_TRACK_VALGRIND) && MI_TRACK_VALGRIND) || (defined(MI_TRACK_ASAN) && MI_TRACK_ASAN) || (defined(MI_TRACK_ETW) && MI_TRACK_ETW))
#define MI_PADDING 1
#endif
#if !defined(MI_PADDING)
#define MI_PADDING 0
#endif

// Check padding bytes; allows byte-precise buffer overflow detection
#if !defined(MI_PADDING_CHECK) && MI_PADDING && (MI_SECURE>=3 || MI_DEBUG>=1)
Expand All @@ -99,7 +103,7 @@ terms of the MIT license. A copy of the license can be found in the file
#define MI_ENCODE_FREELIST 1
#endif

#if (MI_ENCODE_FREELIST && (MI_SECURE>=4 || MI_DEBUG!=0))
#if defined(MI_ENCODE_FREELIST) && (MI_SECURE>=4 || MI_DEBUG!=0)
#define MI_CHECK_DOUBLE_FREE 1
#endif

Expand Down Expand Up @@ -228,7 +232,7 @@ typedef int32_t mi_ssize_t;

// we never allocate more than PTRDIFF_MAX (see also <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
// on 64-bit+ systems we also limit the maximum allocation size such that the slice count fits in 32-bits. (issue #877)
#if (PTRDIFF_MAX > INT32_MAX) && (PTRDIFF_MAX >= (MI_SEGMENT_SLIZE_SIZE * UINT32_MAX))
#if (PTRDIFF_MAX > INT32_MAX) && (PTRDIFF_MAX >= (MI_SEGMENT_SLICE_SIZE * UINT32_MAX))
#define MI_MAX_ALLOC_SIZE (MI_SEGMENT_SLICE_SIZE * (UINT32_MAX-1))
#else
#define MI_MAX_ALLOC_SIZE PTRDIFF_MAX
Expand Down Expand Up @@ -270,7 +274,7 @@ typedef enum mi_delayed_e {

// The `in_full` and `has_aligned` page flags are put in a union to efficiently
// test if both are false (`full_aligned == 0`) in the `mi_free` routine.
#if !MI_TSAN
#if !(defined(MI_TSAN) && MI_TSAN)
typedef union mi_page_flags_s {
uint8_t full_aligned;
struct {
Expand Down Expand Up @@ -347,7 +351,7 @@ typedef struct mi_page_s {
size_t block_size; // size available in each block (always `>0`)
uint8_t* page_start; // start of the page area containing the blocks

#if (MI_ENCODE_FREELIST || MI_PADDING)
#if (defined(MI_ENCODE_FREELIST) || MI_PADDING)
uintptr_t keys[2]; // two random keys to encode the free lists (see `_mi_block_next`) or padding canary
#endif

Expand Down
2 changes: 1 addition & 1 deletion Objects/mimalloc/prim/emscripten/prim.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
// Thread init/done
//----------------------------------------------------------------

#if defined(MI_USE_PTHREADS)
#if MI_USE_PTHREADS

// use pthread local storage keys to detect thread ending
// (and used with MI_TLS_PTHREADS for the default heap)
Expand Down
9 changes: 7 additions & 2 deletions Objects/mimalloc/prim/unix/prim.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,13 @@ void _mi_prim_out_stderr( const char* msg ) {
#if !defined(MI_USE_ENVIRON) || (MI_USE_ENVIRON!=0)
// On Posix systemsr use `environ` to access environment variables
// even before the C runtime is initialized.
#if defined(__APPLE__) && defined(__has_include) && __has_include(<crt_externs.h>)
#if defined(__APPLE__) && defined(__has_include)
#if __has_include(<crt_externs.h>)
#include <crt_externs.h>
#define MI_UNIX_HAS_CRT_EXTERNS_H 1
#endif
#endif
#if MI_UNIX_HAS_CRT_EXTERNS_H
static char** mi_get_environ(void) {
return (*_NSGetEnviron());
}
Expand Down Expand Up @@ -939,7 +944,7 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
// Thread init/done
//----------------------------------------------------------------

#if defined(MI_USE_PTHREADS)
#if MI_USE_PTHREADS

// use pthread local storage keys to detect thread ending
// (and used with MI_TLS_PTHREADS for the default heap)
Expand Down
10 changes: 5 additions & 5 deletions Objects/mimalloc/prim/wasi/prim.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ int _mi_prim_free(void* addr, size_t size ) {
}
#endif

#if defined(MI_USE_PTHREADS)
#if MI_USE_PTHREADS
static pthread_mutex_t mi_heap_grow_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif

static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
void* p = NULL;
if (try_alignment <= 1) {
// `sbrk` is not thread safe in general so try to protect it (we could skip this on WASM but leave it in for now)
#if defined(MI_USE_PTHREADS)
#if MI_USE_PTHREADS
pthread_mutex_lock(&mi_heap_grow_mutex);
#endif
p = mi_memory_grow(size);
#if defined(MI_USE_PTHREADS)
#if MI_USE_PTHREADS
pthread_mutex_unlock(&mi_heap_grow_mutex);
#endif
}
Expand All @@ -83,7 +83,7 @@ static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
// to allocate aligned use a lock to try to avoid thread interaction
// between getting the current size and actual allocation
// (also, `sbrk` is not thread safe in general)
#if defined(MI_USE_PTHREADS)
#if MI_USE_PTHREADS
pthread_mutex_lock(&mi_heap_grow_mutex);
#endif
{
Expand All @@ -94,7 +94,7 @@ static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
base = mi_memory_grow(alloc_size);
}
}
#if defined(MI_USE_PTHREADS)
#if MI_USE_PTHREADS
pthread_mutex_unlock(&mi_heap_grow_mutex);
#endif
if (base != NULL) {
Expand Down
4 changes: 3 additions & 1 deletion Objects/mimalloc/prim/windows/prim.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ terms of the MIT license. A copy of the license can be found in the file
#include <stdlib.h> // atexit

// xbox has no console IO and cannot use LoadLibrary or GetModuleHandle
#if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
#if !defined(WINAPI_FAMILY_PARTITION)
#define MI_WIN_DESKTOP 1
#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
#define MI_WIN_DESKTOP 1
#endif

Expand Down
11 changes: 11 additions & 0 deletions Objects/mimalloc/static.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ terms of the MIT license. A copy of the license can be found in the file
#endif

#include "mimalloc.h"

// Internal configuration treats undefined feature macros as zero by design.
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wundef"
#endif

#include "mimalloc/internal.h"

// For a static override we create a single object file
Expand All @@ -39,3 +46,7 @@ terms of the MIT license. A copy of the license can be found in the file
#if MI_OSX_ZONE
#include "prim/osx/alloc-override-zone.c"
#endif

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
Loading