<div dir="ltr"><div dir="ltr">Ah, I was afraid the answer might be something like that<div><br></div><div>My interest was in trying to model the types of Postgres Wire Protocol as MemoryLayout types</div><div>Unfortunately, several of the types have variable-length strength fields in them</div><div><br></div><div>If you ctrl+f here, there are some ~30-ish fields in total which are "String" null-terminated C-string types =/</div><div><a href="https://www.postgresql.org/docs/current/protocol-message-formats.html">PostgreSQL: Documentation: 15: 55.7. Message Formats</a><br></div><div><br></div><div>Of course for these specific fields/types exceptions can be made but it triggers my OCD, ha</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 28, 2022 at 8:40 AM Maurizio Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com">maurizio.cimadamore@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Gavin,<br>
while something like that is possible, that would not be the best way to <br>
use memory layouts. I believe the sweet spot for memory layout is to <br>
capture data types whose size can be determined statically. <br>
Variable-length data structures don't fall in this case (another <br>
relatively common case is C struct which end with a variable-length <br>
array). While it would be possible, with some heroics, to enhance var <br>
handles to take extra dynamic access coordinates at runtimes, the <br>
condition under which this would be possible are very limited.<br>
<br>
Perhaps it would be better to understand what you are trying to achieve <br>
by modelling a C string with a memory layout? Often, passing strings as <br>
opaque pointer just works. Note also that the FFM API provides some <br>
helper functions to allocate and dereference NULL terminated strings - <br>
see MemorySegment::get/setUtf8String, as well as <br>
SegmentAllocator::allocateUtf8String.<br>
<br>
Cheers<br>
Maurizio<br>
<br>
<br>
<br>
On 25/12/2022 19:24, Gavin Ray wrote:<br>
> Is it possible to model C-Strings with MemoryLayouts?<br>
><br>
> I was thinking of using "MemoryLayout.sequenceLayout(0, C_CHAR)" and <br>
> computing the<br>
> length at runtime, plus manually adding the null-terminator.<br>
><br>
> Would this work alright, or is there some better way to do this?<br>
</blockquote></div>