mirror of https://github.com/microsoft/mimalloc
fix statistics to include padding correctly (issue #301)
This commit is contained in:
parent
5d2b925f3e
commit
7114d5424a
42
src/alloc.c
42
src/alloc.c
|
@ -282,6 +282,35 @@ static void mi_padding_shrink(const mi_page_t* page, const mi_block_t* block, co
|
|||
}
|
||||
#endif
|
||||
|
||||
// only maintain stats for smaller objects if requested
|
||||
#if (MI_STAT>1)
|
||||
static void mi_stat_free(const mi_page_t* page, const mi_block_t* block) {
|
||||
mi_heap_t* const heap = mi_heap_get_default();
|
||||
const size_t usize = mi_page_usable_size_of(page, block);
|
||||
const size_t bsize = mi_page_usable_block_size(page);
|
||||
mi_heap_stat_decrease(heap, malloc, usize);
|
||||
if (bsize <= MI_LARGE_OBJ_SIZE_MAX) {
|
||||
mi_heap_stat_decrease(heap, normal[_mi_bin(bsize)], 1);
|
||||
}
|
||||
}
|
||||
#else
|
||||
static void mi_stat_free(const mi_page_t* page, const mi_block_t* block) {
|
||||
UNUSED(page); UNUSED(block);
|
||||
}
|
||||
#endif
|
||||
|
||||
// always maintain stats for huge objects
|
||||
static void mi_stat_huge_free(const mi_page_t* page) {
|
||||
mi_heap_t* const heap = mi_heap_get_default();
|
||||
const size_t bsize = mi_page_block_size(page); // to match stats in `page.c:mi_page_huge_alloc`
|
||||
if (bsize <= MI_HUGE_OBJ_SIZE_MAX) {
|
||||
mi_heap_stat_decrease(heap, huge, bsize);
|
||||
}
|
||||
else {
|
||||
mi_heap_stat_decrease(heap, giant, bsize);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// Free
|
||||
// ------------------------------------------------------
|
||||
|
@ -300,6 +329,7 @@ static mi_decl_noinline void _mi_free_block_mt(mi_page_t* page, mi_block_t* bloc
|
|||
// huge page segments are always abandoned and can be freed immediately
|
||||
mi_segment_t* const segment = _mi_page_segment(page);
|
||||
if (segment->page_kind==MI_PAGE_HUGE) {
|
||||
mi_stat_huge_free(page);
|
||||
_mi_segment_huge_page_free(segment, page, block);
|
||||
return;
|
||||
}
|
||||
|
@ -343,7 +373,6 @@ static mi_decl_noinline void _mi_free_block_mt(mi_page_t* page, mi_block_t* bloc
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// regular free
|
||||
static inline void _mi_free_block(mi_page_t* page, bool local, mi_block_t* block)
|
||||
{
|
||||
|
@ -383,6 +412,7 @@ mi_block_t* _mi_page_ptr_unalign(const mi_segment_t* segment, const mi_page_t* p
|
|||
static void mi_decl_noinline mi_free_generic(const mi_segment_t* segment, bool local, void* p) {
|
||||
mi_page_t* const page = _mi_segment_page_of(segment, p);
|
||||
mi_block_t* const block = (mi_page_has_aligned(page) ? _mi_page_ptr_unalign(segment, page, p) : (mi_block_t*)p);
|
||||
mi_stat_free(page, block);
|
||||
_mi_free_block(page, local, block);
|
||||
}
|
||||
|
||||
|
@ -430,19 +460,11 @@ void mi_free(void* p) mi_attr_noexcept
|
|||
mi_page_t* const page = _mi_segment_page_of(segment, p);
|
||||
mi_block_t* const block = (mi_block_t*)p;
|
||||
|
||||
#if (MI_STAT>1)
|
||||
mi_heap_t* const heap = mi_heap_get_default();
|
||||
const size_t bsize = mi_page_usable_block_size(page);
|
||||
mi_heap_stat_decrease(heap, malloc, bsize);
|
||||
if (bsize <= MI_LARGE_OBJ_SIZE_MAX) { // huge page stats are accounted for in `_mi_page_retire`
|
||||
mi_heap_stat_decrease(heap, normal[_mi_bin(bsize)], 1);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (mi_likely(tid == segment->thread_id && page->flags.full_aligned == 0)) { // the thread id matches and it is not a full page, nor has aligned blocks
|
||||
// local, and not full or aligned
|
||||
if (mi_unlikely(mi_check_is_double_free(page,block))) return;
|
||||
mi_check_padding(page, block);
|
||||
mi_stat_free(page, block);
|
||||
#if (MI_DEBUG!=0)
|
||||
memset(block, MI_DEBUG_FREED, mi_page_block_size(page));
|
||||
#endif
|
||||
|
|
|
@ -1334,13 +1334,6 @@ void _mi_segment_huge_page_free(mi_segment_t* segment, mi_page_t* page, mi_block
|
|||
page->is_zero = false;
|
||||
mi_assert(page->used == 0);
|
||||
mi_tld_t* tld = heap->tld;
|
||||
const size_t bsize = mi_page_usable_block_size(page);
|
||||
if (bsize > MI_HUGE_OBJ_SIZE_MAX) {
|
||||
_mi_stat_decrease(&tld->stats.giant, bsize);
|
||||
}
|
||||
else {
|
||||
_mi_stat_decrease(&tld->stats.huge, bsize);
|
||||
}
|
||||
mi_segments_track_size((long)segment->segment_size, &tld->segments);
|
||||
_mi_segment_page_free(page, true, &tld->segments);
|
||||
}
|
||||
|
|
|
@ -15,10 +15,11 @@ static void invalid_free();
|
|||
static void test_aslr(void);
|
||||
static void test_process_info(void);
|
||||
static void test_reserved(void);
|
||||
static void negative_stat(void);
|
||||
|
||||
int main() {
|
||||
mi_version();
|
||||
|
||||
mi_stats_reset();
|
||||
// detect double frees and heap corruption
|
||||
// double_free1();
|
||||
// double_free2();
|
||||
|
@ -27,27 +28,29 @@ int main() {
|
|||
// test_aslr();
|
||||
// invalid_free();
|
||||
// test_reserved();
|
||||
|
||||
// negative_stat();
|
||||
|
||||
void* p1 = malloc(78);
|
||||
void* p2 = malloc(24);
|
||||
free(p1);
|
||||
p1 = mi_malloc(8);
|
||||
//char* s = strdup("hello\n");
|
||||
char* s = strdup("hello\n");
|
||||
free(p2);
|
||||
|
||||
p2 = malloc(16);
|
||||
p1 = realloc(p1, 32);
|
||||
free(p1);
|
||||
free(p2);
|
||||
//free(s);
|
||||
//mi_collect(true);
|
||||
|
||||
free(s);
|
||||
|
||||
/* now test if override worked by allocating/freeing across the api's*/
|
||||
//p1 = mi_malloc(32);
|
||||
//free(p1);
|
||||
//p2 = malloc(32);
|
||||
//mi_free(p2);
|
||||
mi_collect(true);
|
||||
mi_stats_print(NULL);
|
||||
test_process_info();
|
||||
// test_process_info();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -167,3 +170,13 @@ static void test_reserved(void) {
|
|||
p3 = malloc(1*GiB);
|
||||
free(p4);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void negative_stat(void) {
|
||||
int* p = mi_malloc(60000);
|
||||
mi_stats_print_out(NULL, NULL);
|
||||
*p = 100;
|
||||
mi_free(p);
|
||||
mi_stats_print_out(NULL, NULL);
|
||||
}
|
|
@ -123,7 +123,7 @@ static void free_items(void* p) {
|
|||
|
||||
static void stress(intptr_t tid) {
|
||||
//bench_start_thread();
|
||||
uintptr_t r = (tid * 43); // rand();
|
||||
uintptr_t r = ((tid + 1) * 43); // rand();
|
||||
const size_t max_item_shift = 5; // 128
|
||||
const size_t max_item_retained_shift = max_item_shift + 2;
|
||||
size_t allocs = 100 * ((size_t)SCALE) * (tid % 8 + 1); // some threads do more
|
||||
|
|
Loading…
Reference in New Issue