check better for valid pointers in free in debug mode

This commit is contained in:
daan 2019-07-19 09:26:51 -07:00
parent 81a7ae33e7
commit dd5fa88c45

View file

@ -198,28 +198,29 @@ static void mi_decl_noinline mi_free_generic(const mi_segment_t* segment, mi_pag
// Free a block // Free a block
void mi_free(void* p) mi_attr_noexcept void mi_free(void* p) mi_attr_noexcept
{ {
// optimize: merge null check with the segment masking (below)
//if (p == NULL) return;
#if (MI_DEBUG>0) #if (MI_DEBUG>0)
if (mi_unlikely(((uintptr_t)p & (MI_INTPTR_SIZE - 1)) != 0)) { if (mi_unlikely(((uintptr_t)p & (MI_INTPTR_SIZE - 1)) != 0)) {
_mi_error_message("trying to free an invalid (unaligned) pointer: %p\n", p); _mi_error_message("trying to free an invalid (unaligned) pointer: %p\n", p);
return; return;
} }
#endif #endif
const mi_segment_t* const segment = _mi_ptr_segment(p); const mi_segment_t* const segment = _mi_ptr_segment(p);
if (segment == NULL) return; // checks for (p==NULL) if (segment == NULL) return; // checks for (p==NULL)
bool local = (_mi_thread_id() == segment->thread_id); // preload, note: putting the thread_id in the page->flags does not improve performance
#if (MI_DEBUG>0) #if (MI_DEBUG>0)
if (mi_unlikely(!mi_is_in_heap_region(p))) {
_mi_warning_message("possibly trying to mi_free a pointer that does not point to a valid heap region: %p\n"
"(this may still be a valid very large allocation (over 64MiB))\n", p);
}
if (mi_unlikely(_mi_ptr_cookie(segment) != segment->cookie)) { if (mi_unlikely(_mi_ptr_cookie(segment) != segment->cookie)) {
_mi_error_message("trying to mi_free a pointer that does not point to a valid heap space: %p\n", p); _mi_error_message("trying to mi_free a pointer that does not point to a valid heap space: %p\n", p);
return; return;
} }
#endif #endif
bool local = (_mi_thread_id() == segment->thread_id); // preload, note: putting the thread_id in the page->flags does not improve performance
mi_page_t* page = _mi_segment_page_of(segment, p); mi_page_t* page = _mi_segment_page_of(segment, p);
#if (MI_STAT>1) #if (MI_STAT>1)