So...

I have a question, about just how bad or not bad someting that I am doing is.

I have a data structure - simple one, just a freelist.

The freelist has a state structure, one per instance, and then freelist element structures, and they contain a void pointer to the user data.

I am working to make it position independent, so it can be used in shared memory segments which have differing virtual addresses.

If I think there is only a single segment, then it's easy : I know all data is in that one segment, and so all pointers become offsets from the freelist state structure.

This is fine - all pointers are inside the same allocation.

However, when I think about multi-segment support, things are more complicated.

What I need to do is keep an array of segment base addresses, and then recruit a few bits from the MSBs of the offsets to indicate which segment an offset is in.

When I need to convert an offset to a pointer, I simply add the base address of its segment.

However, when I need to convert a pointer to an offset, I have a problem - how do I figure out which segment the pointer is in - *whilst using and only using C specification compliant code?*

You see, it's easy enough to assume that there is one address range, and that all segments are in that range, and so all I need to do is iterate over the segment base addresses and the correct base address must be larger than the pointer, and the base address which is the *smallest* base which is larger than the pointer.

This however is not I think valid C, because in valid C, I can only compare pointers *in the same allocation*. Here I would be comparing allocations in *different* allocations.

So, my question is - how bad do people think this is? are there particular use cases where people know these assumptions are wrong?