Skip to main content

Module generation

Module generation 

Source
Expand description

Generation counter with embedded element state.

This module provides type-safe encapsulation of the generation/version system used for ABA protection in the pool allocator.

§Encoding

A Generation is a 32-bit value that packs two pieces of information:

┌────────────────────────────────────┬───────────────────┐
│     Generation Counter (30 bits)   │   State (2 bits)  │
│     0 - 1,073,741,823              │   00 01 10 11     │
└────────────────────────────────────┴───────────────────┘

§States

Each element transitions through a well-defined state machine:

    ┌───────────────────────────────────────────┐
    │                                           │
    ▼                                           │
 ┌──────┐  index_new()    ┌────────┐            │
 │ Free │ ──────────────► │ Zombie │            │
 └──────┘                 └────────┘            │
    ▲                          │                │
    │                          │ make_used()    │
    │ make_free()              ▼                │
    │                     ┌────────┐            │
    └──────────────────── │  Used  │ ───────────┘
                          └────────┘   make_zombie()
                               │
                               │ make_sentinel() (from Zombie)
                               ▼
                          ┌──────────┐
                          │ Sentinel │
                          └──────────┘

§Size Guarantee

The Generation type is guaranteed to be exactly 32 bits:

assert_eq!(core::mem::size_of::<Generation>(), 4);

Structs§

Generation
A 32-bit value encoding a generation counter (30 bits) and element state (2 bits).

Enums§

ElemState
The lifecycle state of a pool element.