From bcce4d52bf1494746f4dd7f5569e542cbfdf5d36 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 18 Oct 2021 20:39:39 -0700 Subject: [PATCH 1/3] fix bug in determination of block size in pre-reserved arena memory --- src/arena.c | 2 +- src/init.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/arena.c b/src/arena.c index 0e6615a4..a4e473a3 100644 --- a/src/arena.c +++ b/src/arena.c @@ -287,7 +287,7 @@ bool mi_manage_os_memory(void* start, size_t size, bool is_committed, bool is_la is_committed = true; } - const size_t bcount = mi_block_count_of_size(size); + const size_t bcount = size / MI_ARENA_BLOCK_SIZE; const size_t fields = _mi_divide_up(bcount, MI_BITMAP_FIELD_BITS); const size_t bitmaps = (is_committed ? 2 : 3); const size_t asize = sizeof(mi_arena_t) + (bitmaps*fields*sizeof(mi_bitmap_field_t)); diff --git a/src/init.c b/src/init.c index c0f09b5e..d899f6ad 100644 --- a/src/init.c +++ b/src/init.c @@ -498,7 +498,9 @@ void mi_process_init(void) mi_attr_noexcept { } if (mi_option_is_enabled(mi_option_reserve_os_memory)) { long ksize = mi_option_get(mi_option_reserve_os_memory); - if (ksize > 0) mi_reserve_os_memory((size_t)ksize*KiB, true, true); + if (ksize > 0) { + mi_reserve_os_memory(_mi_divide_up((size_t)ksize*KiB, MI_SEGMENT_SIZE), true, true); + } } } From 22c2fd82cc6e339ecdf34bf28accf1f6658f5509 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 18 Oct 2021 20:44:19 -0700 Subject: [PATCH 2/3] ensure managed os memory is at least one arena block in size --- src/arena.c | 4 +++- src/init.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/arena.c b/src/arena.c index a4e473a3..b786ffb4 100644 --- a/src/arena.c +++ b/src/arena.c @@ -282,6 +282,8 @@ static bool mi_arena_add(mi_arena_t* arena) { 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 { + if (size < MI_ARENA_BLOCK_SIZE) return false; + if (is_large) { mi_assert_internal(is_committed); is_committed = true; @@ -321,7 +323,7 @@ bool mi_manage_os_memory(void* start, size_t size, bool is_committed, bool is_la // Reserve a range of regular OS memory int mi_reserve_os_memory(size_t size, bool commit, bool allow_large) mi_attr_noexcept { - size = _mi_os_good_alloc_size(size); + size = _mi_align_up(size, MI_ARENA_BLOCK_SIZE); // at least one block bool large = allow_large; void* start = _mi_os_alloc_aligned(size, MI_SEGMENT_ALIGN, commit, &large, &_mi_stats_main); if (start==NULL) return ENOMEM; diff --git a/src/init.c b/src/init.c index d899f6ad..c088cada 100644 --- a/src/init.c +++ b/src/init.c @@ -499,7 +499,7 @@ void mi_process_init(void) mi_attr_noexcept { if (mi_option_is_enabled(mi_option_reserve_os_memory)) { long ksize = mi_option_get(mi_option_reserve_os_memory); if (ksize > 0) { - mi_reserve_os_memory(_mi_divide_up((size_t)ksize*KiB, MI_SEGMENT_SIZE), true, true); + mi_reserve_os_memory((size_t)ksize*KiB, true, true); } } } From 10c31f9b41f5559e459dc30cf463cc750bfa67ac Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Tue, 19 Oct 2021 15:13:01 -0700 Subject: [PATCH 3/3] fix warnings --- src/stats.c | 4 ++-- test/test-api.c | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/stats.c b/src/stats.c index dda55cda..115d938e 100644 --- a/src/stats.c +++ b/src/stats.c @@ -153,8 +153,8 @@ static void mi_printf_amount(int64_t n, int64_t unit, mi_output_fun* out, void* const int64_t tens = (n / (divider/10)); const long whole = (long)(tens/10); const long frac1 = (long)(tens%10); - char unitdesc[16]; - snprintf(unitdesc, 16, "%s%s%s", magnitude, (base==1024 ? "i" : ""), suffix); + char unitdesc[8]; + snprintf(unitdesc, 8, "%s%s%s", magnitude, (base==1024 ? "i" : ""), suffix); snprintf(buf, len, "%ld.%ld %-3s", whole, (frac1 < 0 ? -frac1 : frac1), unitdesc); } _mi_fprintf(out, arg, (fmt==NULL ? "%11s" : fmt), buf); diff --git a/test/test-api.c b/test/test-api.c index 55c80431..f72cfaf5 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -4,6 +4,7 @@ This is free software; you can redistribute it and/or modify it under the terms of the MIT license. A copy of the license can be found in the file "LICENSE" at the root of this distribution. -----------------------------------------------------------------------------*/ +#pragma GCC diagnostic ignored "-Walloc-size-larger-than=" /* Testing allocators is difficult as bugs may only surface after particular