Thread overview | |||||
---|---|---|---|---|---|
|
September 12, 2019 How to force an array literal into a read-only data segment? | ||||
---|---|---|---|---|
| ||||
test.d: __gshared t = "text".ptr; As expected, the "text" literal ends up in a read-only data segment, with a pointer to it stored in a writable data segment (_TMP0 pointing into .rodata.str1.1): .data segment _D4test1tPya: dd offset FLAT:_TMP0@64 db 000h,000h,000h,000h ;.... .data ends Hex dump of section '.rodata.str1.1': 0x00000000 74657874 00 text. How to achieve the same for an array literal? The closest I could come: enum immutable(int[3]) _tmp = [1, 2, 3]; __gshared a = _tmp.ptr; But the array is still placed into the writable segment: .data segment internal: db 001h,000h,000h,000h,002h,000h,000h,000h ;........ db 003h,000h,000h,000h,000h,000h,000h,000h ;........ _D4test1aPyi: dd offset FLAT:internal@64 db 000h,000h,000h,000h ;.... .data ends Is it possible to force the array into rodata? |
September 12, 2019 Re: How to force an array literal into a read-only data segment? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | On Thursday, 12 September 2019 at 07:04:19 UTC, Max Samukha wrote: > > How to achieve the same for an array literal? The closest I could come: > > enum immutable(int[3]) _tmp = [1, 2, 3]; > __gshared a = _tmp.ptr; > > Is it possible to force the array into rodata? https://p0nce.github.io/d-idioms/#Precomputed-tables-at-compile-time-through-CTFE static immutable(int[3]) _tmp = [1, 2, 3]; ? |
September 12, 2019 Re: How to force an array literal into a read-only data segment? | ||||
---|---|---|---|---|
| ||||
Posted in reply to a11e99z | On Thursday, 12 September 2019 at 08:54:09 UTC, a11e99z wrote:
> On Thursday, 12 September 2019 at 07:04:19 UTC, Max Samukha wrote:
>>
>> How to achieve the same for an array literal? The closest I could come:
>>
>> enum immutable(int[3]) _tmp = [1, 2, 3];
>> __gshared a = _tmp.ptr;
>>
>> Is it possible to force the array into rodata?
>
> https://p0nce.github.io/d-idioms/#Precomputed-tables-at-compile-time-through-CTFE
>
> static immutable(int[3]) _tmp = [1, 2, 3];
> ?
That looks the same as my example, where 'static' is redundant at the module level, and enum just removes the unneeded reference to the temporary from the object file. However, __gshared in my example does seem to be redundant - 'immutable' implies thread-shared in this case.
|
Copyright © 1999-2021 by the D Language Foundation