Skip to content

slab: iterator caches the bitmap word #3

Closed

Description

@aotodev

slab::iterator copies the current 64-bit word (slab.cppm:288) and consumes bits from the copy. Later changes to that word are invisible:

  • Neighbour erased after the iterator was built: still set, ++it yields a destroyed slot.
  • Neighbour inserted after: not set, skipped. pool::erase then returns the wrong next.

multislab and pool iterators wrap it, so both inherit it.

Repro (confirmed)

libmem::pool<int, 64> p;
p.insert(1);
auto b = p.insert(2);
auto it = p.begin();
p.erase(b);
++it;           // != end(), points at destroyed b
libmem::pool<int, 64> p;
auto a = p.insert(1);
p.insert(2);
p.erase(a);     // returns end(), 2 is live

Fix

Store the bit index instead of the word. On increment, re-read slab_->bitmap_[word_idx_] and mask off bits <= bit_. Costs one L1 load per step. operator== compares indices; make_iterator(index) no longer pre-masks.

Tests

Both repros, with an element type that marks itself dead in its destructor.

Activity

  1. self-assigned this
    on Sep 29, 2026
  2. linked a pull request that will close this issue3 slab iterator caches the bitmap word #8on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions