From 7e721c881b01c96137c8b246bececff47a5dde20 Mon Sep 17 00:00:00 2001 From: daanx Date: Thu, 6 Mar 2025 16:50:56 -0800 Subject: [PATCH] add comments --- src/bitmap.c | 11 ++++++++--- src/bitmap.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/bitmap.c b/src/bitmap.c index a2e29645..f7f94ddb 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -1557,7 +1557,7 @@ static inline bool mi_bbitmap_try_find_and_clear_generic(mi_bbitmap_t* bbitmap, mi_assert_internal(MI_BFIELD_BITS >= MI_BCHUNK_FIELDS); const mi_bfield_t cmap_mask = mi_bfield_mask(cmap_max_count,0); const size_t cmap_cycle = cmap_acc+1; - const mi_bbin_t bbin = mi_bbin_of(n); + const mi_bbin_t bbin = mi_bbin_of(n); // visit each cmap entry size_t cmap_idx = 0; mi_bfield_cycle_iterate(cmap_mask, tseq, cmap_cycle, cmap_idx, X) @@ -1576,11 +1576,15 @@ static inline bool mi_bbitmap_try_find_and_clear_generic(mi_bbitmap_t* bbitmap, cmap_bins[MI_BBIN_NONE] &= ~cmap_bin; // clear bits that are in an assigned size bin } - // consider only chunks for a particular size bin at a time + // consider only chunks for a particular size bin at a time + // this picks the best bin only within a cmap entry (~ 1GiB address space), but avoids multiple + // iterations through all entries. + mi_assert_internal(bbin < MI_BBIN_NONE); for (mi_bbin_t ibin = MI_BBIN_SMALL; ibin <= MI_BBIN_NONE; // skip from bbin to NONE (so, say, a SMALL will never be placed in a OTHER, MEDIUM, or LARGE chunk to reduce fragmentation) ibin = (ibin == bbin ? MI_BBIN_NONE : mi_bbin_inc(ibin))) { + mi_assert_internal(ibin < MI_BBIN_COUNT); const mi_bfield_t cmap_bin = cmap_bins[ibin]; size_t eidx = 0; mi_bfield_cycle_iterate(cmap_bin, tseq, cmap_entry_cycle, eidx, Y) @@ -1603,7 +1607,8 @@ static inline bool mi_bbitmap_try_find_and_clear_generic(mi_bbitmap_t* bbitmap, return true; } else { - /* we may find that all are cleared only on a second iteration but that is ok as the chunkmap is a conservative approximation. */ + // todo: should _on_find_ return a boolen if there is a chance all are clear to avoid calling `try_clear?` + // we may find that all are cleared only on a second iteration but that is ok as the chunkmap is a conservative approximation. mi_bbitmap_chunkmap_try_clear(bbitmap, chunk_idx); } } diff --git a/src/bitmap.h b/src/bitmap.h index e797bd8e..0237d005 100644 --- a/src/bitmap.h +++ b/src/bitmap.h @@ -248,7 +248,7 @@ typedef mi_decl_align(MI_BCHUNK_SIZE) struct mi_bbitmap_s { _Atomic(size_t) chunk_max_accessed; // max chunk index that was once cleared or set size_t _padding[MI_BCHUNK_SIZE/MI_SIZE_SIZE - 2]; // suppress warning on msvc mi_bchunkmap_t chunkmap; - mi_bchunkmap_t chunkmap_bins[MI_BBIN_COUNT - 1]; // chunkmaps with bit set if the chunk is in that size class (except MI_BBIN_NONE) + mi_bchunkmap_t chunkmap_bins[MI_BBIN_COUNT - 1]; // chunkmaps with bit set if the chunk is in that size class (excluding MI_BBIN_NONE) mi_bchunk_t chunks[MI_BITMAP_DEFAULT_CHUNK_COUNT]; // usually dynamic MI_BITMAP_MAX_CHUNK_COUNT } mi_bbitmap_t;