With position independent data structures, I have to use an offset.
Maximum convenience is when the offset is from the data structure state structure – but this implies negative offsets can occur, which means I have a problem becasuse I can’t address the entire address range.
Well – if we say all pointers used will be to 4 or 8 bytes offsets, then we have two or three bits free at the LS bit end of things.
When we do pointer math we get a ptrdiff_t, like it or not.
What we could do is cast the pointers to int long long unsigned (or anyway the unsigned int same length as a pointer), figure out the difference and if it’s negative or positive and use one of those free LS bits as the sign bit.
That way the *normal* data structure can support the full address range, using offsets, and so is used in the normal case and is also sound for use with single segment relocations.
(Multi-segment still needs all the extra work with storing multiple base addresses.)
This is all fly-by-night, of course. Totally outside of the Standard.
The problem with this – user data is a void pointer for key and a void pointer for value.
These are both converted to an offset.
They could point to chars.
They could also be set directly. Being converted to an offset doesn’t hurt, you’re converted back afterwards, but having the LS bit used for something else will.
Ack. Just realised, direct setting is a problem for the normal signed offset variant. If the user sets a value directly, and the value is far enough away from the address of the data structure state, signed overflow will occur. Implementation defined is not a place I want to be. Need a different macro for setting direct values.