Comment by gpderetta

3 years ago

At some point you need to get the memory for your pool from somewhere, for example from malloc [1], hence you can safely set the type of the raw memory by writing into it. You can also change that type to some other type, by writing other stuff (so you can reuse the memory). You can also write metadata to it between uses. What you cannot do is having overlapping lifetimes for different types.

Making sure that you respect all the underspecified, obscure, and often contradicting rules is not easy, so if you prefer to disable strict-alias, you have my sympathy. For the most part is useful for high performance numerical code, and less advantageous for typical pointer chasing stuff.

From the practical point of view, the safest way to implement a custom allocator is to make sure that the compiler can't see through it, so separate compilation and no LTO and/or launder your pointers through appropriate inline asm.

[1] but other 'anonymous' sources, like mmap, would also work in practice.

> For the most part is useful for high performance numerical code, and less advantageous for typical pointer chasing stuff.

Yeah. I've read that the aliasing rules and features like restrict were introduced to C because Fortran had them.

> the safest way to implement a custom allocator is to make sure that the compiler can't see through it

Makes sense.

> launder your pointers through appropriate inline asm

This is a really neat trick indeed. I learned a lot today.