add mi_unsafe_free_with_threadid and mi_get_current_threadid()

This commit is contained in:
daan 2021-11-23 17:59:27 -08:00
parent 1c22650719
commit 8b60a5ab70
3 changed files with 26 additions and 4 deletions

View File

@ -20,14 +20,17 @@ terms of the MIT license. A copy of the license can be found in the file
#if defined(_MSC_VER) #if defined(_MSC_VER)
#pragma warning(disable:4127) // suppress constant conditional warning (due to MI_SECURE paths) #pragma warning(disable:4127) // suppress constant conditional warning (due to MI_SECURE paths)
#define mi_decl_noinline __declspec(noinline) #define mi_decl_noinline __declspec(noinline)
#define mi_decl_always_inline __forceinline
#define mi_decl_thread __declspec(thread) #define mi_decl_thread __declspec(thread)
#define mi_decl_cache_align __declspec(align(MI_CACHE_LINE)) #define mi_decl_cache_align __declspec(align(MI_CACHE_LINE))
#elif (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__) // includes clang and icc #elif (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__) // includes clang and icc
#define mi_decl_noinline __attribute__((noinline)) #define mi_decl_noinline __attribute__((noinline))
#define mi_decl_always_inline __attribute__((always_inline))
#define mi_decl_thread __thread #define mi_decl_thread __thread
#define mi_decl_cache_align __attribute__((aligned(MI_CACHE_LINE))) #define mi_decl_cache_align __attribute__((aligned(MI_CACHE_LINE)))
#else #else
#define mi_decl_noinline #define mi_decl_noinline
#define mi_decl_always_inline inline
#define mi_decl_thread __thread // hope for the best :-) #define mi_decl_thread __thread // hope for the best :-)
#define mi_decl_cache_align #define mi_decl_cache_align
#endif #endif

View File

@ -271,6 +271,8 @@ mi_decl_export int mi_reserve_huge_os_pages_at(size_t pages, int numa_node, size
mi_decl_export int mi_reserve_os_memory(size_t size, bool commit, bool allow_large) mi_attr_noexcept; mi_decl_export int mi_reserve_os_memory(size_t size, bool commit, bool allow_large) mi_attr_noexcept;
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; 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;
mi_decl_export size_t mi_get_current_threadid(void) mi_attr_noexcept;
mi_decl_export void mi_unsafe_free_with_threadid(void* p, size_t current_tid ) mi_attr_noexcept;
// deprecated // deprecated
mi_decl_export int mi_reserve_huge_os_pages(size_t pages, double max_secs, size_t* pages_reserved) mi_attr_noexcept; mi_decl_export int mi_reserve_huge_os_pages(size_t pages, double max_secs, size_t* pages_reserved) mi_attr_noexcept;

View File

@ -475,14 +475,12 @@ static inline mi_segment_t* mi_checked_ptr_segment(const void* p, const char* ms
return segment; return segment;
} }
// Free a block with a known threadid
// Free a block static mi_decl_always_inline void _mi_free_with_threadid(void* p, mi_threadid_t tid) mi_attr_noexcept
void mi_free(void* p) mi_attr_noexcept
{ {
const mi_segment_t* const segment = mi_checked_ptr_segment(p,"mi_free"); const mi_segment_t* const segment = mi_checked_ptr_segment(p,"mi_free");
if (mi_unlikely(segment == NULL)) return; if (mi_unlikely(segment == NULL)) return;
const mi_threadid_t tid = _mi_thread_id();
mi_page_t* const page = _mi_segment_page_of(segment, p); mi_page_t* const page = _mi_segment_page_of(segment, p);
mi_block_t* const block = (mi_block_t*)p; mi_block_t* const block = (mi_block_t*)p;
@ -507,6 +505,25 @@ void mi_free(void* p) mi_attr_noexcept
} }
} }
// Get the current thread id
size_t mi_get_current_threadid(void) mi_attr_noexcept {
return _mi_thread_id();
}
// Free a block passing the current thread id explicitly
void mi_unsafe_free_with_threadid(void* p, size_t current_tid ) mi_attr_noexcept
{
mi_assert(current_tid == _mi_thread_id());
_mi_free_with_threadid(p,current_tid);
}
// Free a block
void mi_free(void* p) mi_attr_noexcept {
_mi_free_with_threadid(p, _mi_thread_id());
}
bool _mi_free_delayed_block(mi_block_t* block) { bool _mi_free_delayed_block(mi_block_t* block) {
// get segment and page // get segment and page
const mi_segment_t* const segment = _mi_ptr_segment(block); const mi_segment_t* const segment = _mi_ptr_segment(block);