How to test if a string is pointing into read-only memory?

Elronnd elronnd at elronnd.net
Tue Oct 12 09:20:42 UTC 2021


On Tuesday, 12 October 2021 at 08:19:01 UTC, jfondren wrote:
> What's a reliable test that could be used in a toStringz that 
> skips allocation when given a string in read-only memory?

There is no good way.

- You could peek in /proc, but that's not portable

- You could poke the data and catch the resulting fault; but 
that's: 1) horrible, 2) slow, 3) problematic wrt threading, 4) 
sensitive to user code mapping its own memory and then remapping 
as rw (or unmapping)

- You could make a global hash table into which are registered 
the addresses of all rodata; but that is difficult to get right 
across translation units, especially in the face of dynamic 
linking.  This is probably the most feasible, but is really not 
worth the hassle.


More information about the Digitalmars-d-learn mailing list