<div dir="ltr"><div><span style="font-family:arial,sans-serif;color:rgb(33,37,41)">Hi,</span><br></div><div><span style="color:rgb(33,37,41)"><font face="arial, sans-serif"><br></font></span></div><div><font face="arial, sans-serif"><span style="color:rgb(33,37,41)">I'm trying to use the FFM API </span></font>(jdk 22)<font face="arial, sans-serif"><span style="color:rgb(33,37,41)"> to call my C++ method and I need to pass a text</span></font><span style="color:rgb(33,37,41);font-family:arial,sans-serif"> </span><span style="color:rgb(33,37,41);font-family:arial,sans-serif">(java String)</span><font face="arial, sans-serif"><span style="color:rgb(33,37,41)"> and receive a text response</span></font><span style="color:rgb(33,37,41);font-family:arial,sans-serif">. While implementing this, I encountered several issues:</span></div><div><div><ol style="box-sizing:border-box;padding-left:2rem;margin-top:0px;margin-bottom:1rem;color:rgb(33,37,41)"><li style="box-sizing:border-box"><p style="box-sizing:border-box;margin-top:0px;margin-bottom:1rem"><font face="arial, sans-serif">What are the best practices for defining <code style="box-sizing:border-box">newSize</code> for use in the <code style="box-sizing:border-box">reinterpret(long newSize)</code> method? Can I use constants like <code style="box-sizing:border-box">Long.MAX_VALUE</code> or <code style="box-sizing:border-box">Integer.MAX_VALUE</code> as <code style="box-sizing:border-box">newSize</code>, or could that cause some problems?</font></p></li><li style="box-sizing:border-box"><p style="box-sizing:border-box;margin-top:0px;margin-bottom:1rem"><font face="arial, sans-serif">When I tried to use in-heap <code style="box-sizing:border-box">MemorySegment</code> with the <code style="box-sizing:border-box">Linker.Option.critical(true)</code> and passed <code style="box-sizing:border-box">MemorySegment.ofArray(text.getBytes())</code>, I started getting extra symbol like SOH in the response. What am I doing wrong? (Sample snippets listed below). Changing </font><span style="font-family:monospace">newSize</span> value in <span style="font-family:monospace">reinterpret(long newSize)</span> doesn't help<br></p></li><li style="box-sizing:border-box"><div>If I inline <span style="color:rgb(0,0,0);font-family:"JetBrains Mono",monospace">MemorySegment.</span><span style="color:rgb(0,0,0);font-family:"JetBrains Mono",monospace;font-style:italic">ofArray</span><span style="color:rgb(0,0,0);font-family:"JetBrains Mono",monospace">(text.getBytes())</span> into <font color="#000000" style=""><font face="JetBrains Mono, monospace">invokeExact, </font><font face="arial, sans-serif">I </font></font><span style="color:rgb(34,34,34)">expected : </span><span style="color:rgb(34,34,34);font-family:"JetBrains Mono",monospace"><font color="#000000">"мое все 123 аи92", but</font></span> got:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">uncaught exception:<br> address -> 0x60000120d710<br> what() -> "util/charset/wide.h:366: failed to decode UTF-8 string at pos 25 in string "\xD0\x9C\xD0\xBE\xD1\x91 \xD0\xB2\xD1\x81\xD1\x91 123 \xD0\x90\xD0\23092\1\xCF\xFD\xBD_""<br> type -> yexception</blockquote></li></ol></div><div>I'm definitely doing something wrong. Please help me figure it out and understand. Thanks! </div><div><br></div><div>Code snippets:</div><div>C++ method:</div><div><div style=""><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt"><font color="#000000" style="background-color:rgb(255,255,255)">char* NormalizeText(const char* text, const char* lang);</font></pre><pre style="font-size:9.8pt"><font color="#000000" style="" face="arial, sans-serif">Java code:</font></pre></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style=""><pre style="font-family:"JetBrains Mono",monospace"><font color="#000000" style="background-color:rgb(255,255,255)">private static class FFNormalizerSettings {<br> private static final SymbolLookup <span style="font-style:italic">SYMBOL_LOOKUP </span>= SymbolLookup.<span style="font-style:italic">loaderLookup</span>();<br> public static final MemorySegment <span style="font-style:italic">ADDR </span>= <span style="font-style:italic">SYMBOL_LOOKUP</span>.find("NormalizeText").orElseThrow();<br> private static final FunctionDescriptor <span style="font-style:italic">FUNCTION_DESCRIPTOR </span>= FunctionDescriptor.<span style="font-style:italic">of</span>(<br> ValueLayout.<span style="font-style:italic">ADDRESS</span>, // Return type<br> ValueLayout.<span style="font-style:italic">ADDRESS</span>, // const char* text<br> ValueLayout.<span style="font-style:italic">ADDRESS </span>// const char* lang<br> );<br> public static final MethodHandle <span style="font-style:italic">HANDLE_CRITICAL</span>;<br><br> static {<br> Linker linker = Linker.<span style="font-style:italic">nativeLinker</span>();<br> <span style="font-style:italic">HANDLE_CRITICAL </span>= linker.downcallHandle(<span style="font-style:italic">ADDR</span>, <span style="font-style:italic">FUNCTION_DESCRIPTOR</span>, Linker.Option.<span style="font-style:italic">critical</span>(true));<br> }<br>}<br></font></pre></div></blockquote><div><br>method</div><div><b>all OK:</b></div><div style=""><blockquote style="font-family:"JetBrains Mono",monospace"><div style=""><pre style="font-family:"JetBrains Mono",monospace"><span style="background-color:rgb(255,255,255)"><font color="#000000"> public static String normalizeFFOfAddress(String text, Lang lang) throws Throwable {<br> String langCode = lang.getCode();<br> try (Arena arena = Arena.<span style="font-style:italic">ofConfined</span>()) {<br> <b>MemorySegment textSegment = arena.allocateFrom(ValueLayout.<span style="font-style:italic">JAVA_BYTE</span>, text.getBytes());</b><br> MemorySegment langSegment = arena.allocateFrom(langCode);<br> MemorySegment segment = (MemorySegment) FFNormalizerSettings.<span style="font-style:italic">HANDLE_CRITICAL</span>.invokeExact(<b>textSegment</b>,<br> langSegment);<br> MemorySegment reinterpret = segment.reinterpret(Long.<span style="font-style:italic">MAX_VALUE</span>);<br> return reinterpret.getString(0);<br> }<br> }</font></span></pre></div></blockquote></div><div>Same method with <span style="color:rgb(0,0,0);font-family:"JetBrains Mono",monospace">MemorySegment textSegment = arena.allocateFrom(text);</span> - <b>all OK</b></div><div><br></div><div><b>extra symbol SOH - NOT OK </b>(expected: <span style="font-family:"JetBrains Mono",monospace"><font color="#000000">"мое все 123 аи92" actual: </font></span><span style="color:rgb(0,0,0);font-family:"JetBrains Mono",monospace">"мое все 123 аи92A"</span>):</div><div><pre style="font-family:"JetBrains Mono",monospace"><font color="#000000"> public static String normalizeFFOfAddress(String text, Lang lang) throws Throwable {<br> String langCode = lang.getCode();
</font> <b>MemorySegment textSegment = MemorySegment.<span style="color:rgb(0,0,0);font-style:italic">ofArray</span><span style="color:rgb(0,0,0)">(text.getBytes());</span></b></pre><pre style="font-family:"JetBrains Mono",monospace"><font color="#000000"> try (Arena arena = Arena.<span style="font-style:italic">ofConfined</span>()) {<br> MemorySegment langSegment = arena.allocateFrom(langCode);<br> MemorySegment segment = (MemorySegment) FFNormalizerSettings.<span style="font-style:italic">HANDLE_CRITICAL</span>.invokeExact(<b>textSegment</b>,<br> langSegment);<br> MemorySegment reinterpret = segment.reinterpret(Long.<span style="font-style:italic">MAX_VALUE</span>);<br> return reinterpret.getString(0);<br> }<br> }</font></pre></div><div><b>uncaught exception - NOT OK</b> (expected: <span style="font-family:"JetBrains Mono",monospace"><font color="#000000">"мое все 123 аи92" </font></span><span style="color:rgb(0,0,0);font-family:"JetBrains Mono",monospace">actual: </span>exception):</div><div><pre style="font-family:"JetBrains Mono",monospace"><font color="#000000"> public static String normalizeFFOfAddress(String text, Lang lang) throws Throwable {<br> String langCode = lang.getCode();<br> try (Arena arena = Arena.<span style="font-style:italic">ofConfined</span>()) {<br> MemorySegment langSegment = arena.allocateFrom(langCode);<br> MemorySegment segment = (MemorySegment) FFNormalizerSettings.<span style="font-style:italic">HANDLE_CRITICAL</span>.invokeExact(</font><b>MemorySegment.<span style="color:rgb(0,0,0);font-style:italic">ofArray</span><span style="color:rgb(0,0,0)">(text.getBytes())</span></b>,<br><font color="#000000"> langSegment);<br> MemorySegment reinterpret = segment.reinterpret(Long.<span style="font-style:italic">MAX_VALUE</span>);<br> return reinterpret.getString(0);<br> }<br> }</font></pre><pre style="font-family:"JetBrains Mono",monospace">uncaught exception:<br style="color:rgb(33,37,41);font-family:Arial,Helvetica,sans-serif;white-space:normal"><span style="color:rgb(33,37,41);font-family:Arial,Helvetica,sans-serif;white-space:normal"> address -> 0x60000120d710</span><br style="color:rgb(33,37,41);font-family:Arial,Helvetica,sans-serif;white-space:normal"><span style="color:rgb(33,37,41);font-family:Arial,Helvetica,sans-serif;white-space:normal"> what() -> "util/charset/wide.h:366: failed to decode UTF-8 string at pos 25 in string "\xD0\x9C\xD0\xBE\xD1\x91 \xD0\xB2\xD1\x81\xD1\x91 123 \xD0\x90\xD0\23092\1\xCF\xFD\xBD_""</span><br style="color:rgb(33,37,41);font-family:Arial,Helvetica,sans-serif;white-space:normal"><span style="color:rgb(33,37,41);font-family:Arial,Helvetica,sans-serif;white-space:normal"> type -> yexception</span><font color="#000000"><br></font></pre></div><div>Thanks!</div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">Lisitskaya Anastasiya</div></div></div>