Path: blob/main/system/lib/mimalloc/src/bitmap.h
6175 views
/* ----------------------------------------------------------------------------1Copyright (c) 2019-2023 Microsoft Research, Daan Leijen2This is free software; you can redistribute it and/or modify it under the3terms of the MIT license. A copy of the license can be found in the file4"LICENSE" at the root of this distribution.5-----------------------------------------------------------------------------*/67/* ----------------------------------------------------------------------------8Concurrent bitmap that can set/reset sequences of bits atomically,9represented as an array of fields where each field is a machine word (`size_t`)1011There are two api's; the standard one cannot have sequences that cross12between the bitmap fields (and a sequence must be <= MI_BITMAP_FIELD_BITS).13(this is used in region allocation)1415The `_across` postfixed functions do allow sequences that can cross over16between the fields. (This is used in arena allocation)17---------------------------------------------------------------------------- */18#pragma once19#ifndef MI_BITMAP_H20#define MI_BITMAP_H2122/* -----------------------------------------------------------23Bitmap definition24----------------------------------------------------------- */2526#define MI_BITMAP_FIELD_BITS (8*MI_SIZE_SIZE)27#define MI_BITMAP_FIELD_FULL (~((size_t)0)) // all bits set2829// An atomic bitmap of `size_t` fields30typedef _Atomic(size_t) mi_bitmap_field_t;31typedef mi_bitmap_field_t* mi_bitmap_t;3233// A bitmap index is the index of the bit in a bitmap.34typedef size_t mi_bitmap_index_t;3536// Create a bit index.37static inline mi_bitmap_index_t mi_bitmap_index_create(size_t idx, size_t bitidx) {38mi_assert_internal(bitidx < MI_BITMAP_FIELD_BITS);39return (idx*MI_BITMAP_FIELD_BITS) + bitidx;40}4142// Create a bit index.43static inline mi_bitmap_index_t mi_bitmap_index_create_from_bit(size_t full_bitidx) {44return mi_bitmap_index_create(full_bitidx / MI_BITMAP_FIELD_BITS, full_bitidx % MI_BITMAP_FIELD_BITS);45}4647// Get the field index from a bit index.48static inline size_t mi_bitmap_index_field(mi_bitmap_index_t bitmap_idx) {49return (bitmap_idx / MI_BITMAP_FIELD_BITS);50}5152// Get the bit index in a bitmap field53static inline size_t mi_bitmap_index_bit_in_field(mi_bitmap_index_t bitmap_idx) {54return (bitmap_idx % MI_BITMAP_FIELD_BITS);55}5657// Get the full bit index58static inline size_t mi_bitmap_index_bit(mi_bitmap_index_t bitmap_idx) {59return bitmap_idx;60}6162/* -----------------------------------------------------------63Claim a bit sequence atomically64----------------------------------------------------------- */6566// Try to atomically claim a sequence of `count` bits in a single67// field at `idx` in `bitmap`. Returns `true` on success.68bool _mi_bitmap_try_find_claim_field(mi_bitmap_t bitmap, size_t idx, const size_t count, mi_bitmap_index_t* bitmap_idx);6970// Starts at idx, and wraps around to search in all `bitmap_fields` fields.71// For now, `count` can be at most MI_BITMAP_FIELD_BITS and will never cross fields.72bool _mi_bitmap_try_find_from_claim(mi_bitmap_t bitmap, const size_t bitmap_fields, const size_t start_field_idx, const size_t count, mi_bitmap_index_t* bitmap_idx);7374// Like _mi_bitmap_try_find_from_claim but with an extra predicate that must be fullfilled75typedef bool (mi_cdecl *mi_bitmap_pred_fun_t)(mi_bitmap_index_t bitmap_idx, void* pred_arg);76bool _mi_bitmap_try_find_from_claim_pred(mi_bitmap_t bitmap, const size_t bitmap_fields, const size_t start_field_idx, const size_t count, mi_bitmap_pred_fun_t pred_fun, void* pred_arg, mi_bitmap_index_t* bitmap_idx);7778// Set `count` bits at `bitmap_idx` to 0 atomically79// Returns `true` if all `count` bits were 1 previously.80bool _mi_bitmap_unclaim(mi_bitmap_t bitmap, size_t bitmap_fields, size_t count, mi_bitmap_index_t bitmap_idx);8182// Try to set `count` bits at `bitmap_idx` from 0 to 1 atomically.83// Returns `true` if successful when all previous `count` bits were 0.84bool _mi_bitmap_try_claim(mi_bitmap_t bitmap, size_t bitmap_fields, size_t count, mi_bitmap_index_t bitmap_idx);8586// Set `count` bits at `bitmap_idx` to 1 atomically87// Returns `true` if all `count` bits were 0 previously. `any_zero` is `true` if there was at least one zero bit.88bool _mi_bitmap_claim(mi_bitmap_t bitmap, size_t bitmap_fields, size_t count, mi_bitmap_index_t bitmap_idx, bool* any_zero);8990bool _mi_bitmap_is_claimed(mi_bitmap_t bitmap, size_t bitmap_fields, size_t count, mi_bitmap_index_t bitmap_idx);91bool _mi_bitmap_is_any_claimed(mi_bitmap_t bitmap, size_t bitmap_fields, size_t count, mi_bitmap_index_t bitmap_idx);929394//--------------------------------------------------------------------------95// the `_across` functions work on bitmaps where sequences can cross over96// between the fields. This is used in arena allocation97//--------------------------------------------------------------------------9899// Find `count` bits of zeros and set them to 1 atomically; returns `true` on success.100// Starts at idx, and wraps around to search in all `bitmap_fields` fields.101bool _mi_bitmap_try_find_from_claim_across(mi_bitmap_t bitmap, const size_t bitmap_fields, const size_t start_field_idx, const size_t count, mi_bitmap_index_t* bitmap_idx, mi_stats_t* stats);102103// Set `count` bits at `bitmap_idx` to 0 atomically104// Returns `true` if all `count` bits were 1 previously.105bool _mi_bitmap_unclaim_across(mi_bitmap_t bitmap, size_t bitmap_fields, size_t count, mi_bitmap_index_t bitmap_idx);106107// Set `count` bits at `bitmap_idx` to 1 atomically108// Returns `true` if all `count` bits were 0 previously. `any_zero` is `true` if there was at least one zero bit.109bool _mi_bitmap_claim_across(mi_bitmap_t bitmap, size_t bitmap_fields, size_t count, mi_bitmap_index_t bitmap_idx, bool* pany_zero);110111bool _mi_bitmap_is_claimed_across(mi_bitmap_t bitmap, size_t bitmap_fields, size_t count, mi_bitmap_index_t bitmap_idx);112bool _mi_bitmap_is_any_claimed_across(mi_bitmap_t bitmap, size_t bitmap_fields, size_t count, mi_bitmap_index_t bitmap_idx);113114#endif115116117