Skip to content

Commit 8b60a5a

Browse files
committed
add mi_unsafe_free_with_threadid and mi_get_current_threadid()
1 parent 1c22650 commit 8b60a5a

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

include/mimalloc-internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ terms of the MIT license. A copy of the license can be found in the file
2020
#if defined(_MSC_VER)
2121
#pragma warning(disable:4127) // suppress constant conditional warning (due to MI_SECURE paths)
2222
#define mi_decl_noinline __declspec(noinline)
23+
#define mi_decl_always_inline __forceinline
2324
#define mi_decl_thread __declspec(thread)
2425
#define mi_decl_cache_align __declspec(align(MI_CACHE_LINE))
2526
#elif (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__) // includes clang and icc
2627
#define mi_decl_noinline __attribute__((noinline))
28+
#define mi_decl_always_inline __attribute__((always_inline))
2729
#define mi_decl_thread __thread
2830
#define mi_decl_cache_align __attribute__((aligned(MI_CACHE_LINE)))
2931
#else
3032
#define mi_decl_noinline
33+
#define mi_decl_always_inline inline
3134
#define mi_decl_thread __thread // hope for the best :-)
3235
#define mi_decl_cache_align
3336
#endif

include/mimalloc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ mi_decl_export int mi_reserve_huge_os_pages_at(size_t pages, int numa_node, size
271271
mi_decl_export int mi_reserve_os_memory(size_t size, bool commit, bool allow_large) mi_attr_noexcept;
272272
mi_decl_export bool mi_manage_os_memory(void* start, size_t size, bool is_committed, bool is_large, bool is_zero, int numa_node) mi_attr_noexcept;
273273

274+
mi_decl_export size_t mi_get_current_threadid(void) mi_attr_noexcept;
275+
mi_decl_export void mi_unsafe_free_with_threadid(void* p, size_t current_tid ) mi_attr_noexcept;
274276

275277
// deprecated
276278
mi_decl_export int mi_reserve_huge_os_pages(size_t pages, double max_secs, size_t* pages_reserved) mi_attr_noexcept;

src/alloc.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,12 @@ static inline mi_segment_t* mi_checked_ptr_segment(const void* p, const char* ms
475475
return segment;
476476
}
477477

478-
479-
// Free a block
480-
void mi_free(void* p) mi_attr_noexcept
478+
// Free a block with a known threadid
479+
static mi_decl_always_inline void _mi_free_with_threadid(void* p, mi_threadid_t tid) mi_attr_noexcept
481480
{
482481
const mi_segment_t* const segment = mi_checked_ptr_segment(p,"mi_free");
483482
if (mi_unlikely(segment == NULL)) return;
484483

485-
const mi_threadid_t tid = _mi_thread_id();
486484
mi_page_t* const page = _mi_segment_page_of(segment, p);
487485
mi_block_t* const block = (mi_block_t*)p;
488486

@@ -507,6 +505,25 @@ void mi_free(void* p) mi_attr_noexcept
507505
}
508506
}
509507

508+
// Get the current thread id
509+
size_t mi_get_current_threadid(void) mi_attr_noexcept {
510+
return _mi_thread_id();
511+
}
512+
513+
// Free a block passing the current thread id explicitly
514+
void mi_unsafe_free_with_threadid(void* p, size_t current_tid ) mi_attr_noexcept
515+
{
516+
mi_assert(current_tid == _mi_thread_id());
517+
_mi_free_with_threadid(p,current_tid);
518+
}
519+
520+
521+
// Free a block
522+
void mi_free(void* p) mi_attr_noexcept {
523+
_mi_free_with_threadid(p, _mi_thread_id());
524+
}
525+
526+
510527
bool _mi_free_delayed_block(mi_block_t* block) {
511528
// get segment and page
512529
const mi_segment_t* const segment = _mi_ptr_segment(block);

0 commit comments

Comments
 (0)