microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
9.74k stars 791 forks source link

[suggestion] add a comment at the declaration of `mi_arena_t` for easier custom modify #896

Closed LXYan2333 closed 1 month ago

LXYan2333 commented 1 month ago

Dear maintainer:

Hello, I suggest to add a comment right after

https://github.com/microsoft/mimalloc/blob/81a771161e37c8559c442fff099115cd1977db1e/src/arena.c#L59

// A memory arena descriptor
typedef struct mi_arena_s {
  //…………
  mi_bitmap_field_t  blocks_inuse[1];     // in-place bitmap of in-use blocks (of size `field_count`)
  // add a comment here: do not add custom field after `blocks_inuse` field because the bitmat array bounds will overflow and override its later fields. check https://github.com/microsoft/mimalloc/blob/81a771161e37c8559c442fff099115cd1977db1e/src/arena.c#L903C3-L903C8
} mi_arena_t;

Array out of bounds is not a well-defined behaviour and should be documented explicitly, or people (like me) may waste some time on it……

Thanks for this awesome project anyway.

LXYan2333 commented 16 hours ago

I just learned that this is something resemble to "flexible array" https://en.wikipedia.org/wiki/Flexible_array_member, but according to https://stackoverflow.com/a/46251908 specify the length of blocks_inuse array is actually an UB.