▲ | lentil_soup 2 days ago | ||||||||||||||||
One cool technique is to use the packed bits to check if a raw pointer is still valid. Say you have a pool of objects, when you allocate one you give out a pointer, but in the packed bits you add a 'generation' counter. Every time you free that object from the pool you increment the counter. When you dereference a pointer you can check the generation in the packed pointer and the one in the pool. If they don't match you're using a pointer to an invalid object. | |||||||||||||||||
▲ | simonask a day ago | parent | next [-] | ||||||||||||||||
It's a cool technique, but only useful in very limited scenarios. In particular, it is unsuitable when the number of reallocations is unbounded, because the generation counter can easily overflow. Even if you can squeeze in 32 tag bits (you would need to leverage the virtual memory system in some platform-specific way), counting to 4-billion-and-change takes no time on modern CPUs, so you would realistically overflow the tag in milliseconds. But if your system is aware of virtual memory, you can still do interesting things, like remapping a whole arena with a different "tag" and use the information in GC write barriers. | |||||||||||||||||
| |||||||||||||||||
▲ | RossBencina 2 days ago | parent | prev [-] | ||||||||||||||||
A similar technique is sometimes used to mitigate the ABA problem in lock-free data structures. | |||||||||||||||||
|