Counting an initialised array, and segments
Cecil Ward
cecil at cecilward.com
Sun Jun 25 22:08:19 UTC 2023
I recently had some problems
dchar[] arr = [ ‘ ‘, TAB, CR, LF … ];
and I got errors from the compiler which led to me having to
count the elements in the initialiser and declare the array with
an explicit size. I don’t want the array to be mutable so I later
added immutable to it, but that didn’t help matters. At one
point, because the array was quite long, I got the arr[
n_elements ] number wrong, it was too small and the remainder of
the array was full of 0xffs (or something), which was good,
helped me spot the bug.
Is there any way to get the compiler to count the number of
elements in the initialiser and set the array to that size ? And
it’s immutable.
The only reason that I’m giving it a name is that I want the
object to be used in several places and I don’t want multiple
copies of it in the code/readonly initialised data segment.
Another couple of unrelated questions: is there such a thing as a
no-execute initialised readonly data segment? I’m seeing
immutables going into the code segment, I think, with x86 LDC at
least, can’t remember about GDC. Anyway on x86-64 immutables are
addressed as [rip + displ] which is very pleasing as it’s vastly
more efficient than accessing statics in TLS which seems to be a
nightmare in Linux at least.
In MS Windows, isn’t TLS dealt with using FS: ( or GS: ?)
prefixes? Shame this doesn’t seem to be exploited in Linux, or am
I wrong?
I’d like to deal with the overhead of retrieving the static base
address all the time in the Linux situation (if I have got the
right end of the stick) but having an ‘application object’ which
contains all the statics in a struct in an alloc cell or
something, and passing a pointer to this static base app object
everywhere seems a nightmare too as it eats a register and worse
eats one of the limited number of precious function argument
registers which are in short supply in eg x86-64, where there are
less than half a dozen argument registers allowed. I realise that
one can deal with that limited number by rolling some passed
arguments up into a passed struct, but that’s introducing a level
of indirection and other overhead, that or just live with the
fact that the extra args are going into the stack, which isn’t
the worst thing in the world. I wonder what others do about
statics in TLS?
More information about the Digitalmars-d-learn
mailing list