<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>Hi, some replies below:<br>
    </p>
    <div class="moz-cite-prefix">On 01/10/2024 20:40, Anastasiya
      Lisitskaya wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAD4WG-vb7Kjg3y98AAq0MHUKmo3PTJ=5aUpyV++MYA5AzsWi1Q@mail.gmail.com">
      
      <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>
          <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>
          </ol>
        </div>
      </div>
    </blockquote>
    <p><font face="arial, sans-serif">If the size of the returned string
        (I assume it's a char*) is known, then use that size. Otherwise,
        use Long.MAX_VALUE. MemorySegment::getString will read the
        string bytes up to the null terminator.</font></p>
    <p><font face="arial, sans-serif"><br>
      </font></p>
    <blockquote type="cite" cite="mid:CAD4WG-vb7Kjg3y98AAq0MHUKmo3PTJ=5aUpyV++MYA5AzsWi1Q@mail.gmail.com">
      <div dir="ltr">
        <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">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</p>
              </li>
            </ol>
          </div>
        </div>
      </div>
    </blockquote>
    <blockquote type="cite" cite="mid:CAD4WG-vb7Kjg3y98AAq0MHUKmo3PTJ=5aUpyV++MYA5AzsWi1Q@mail.gmail.com">
      <div dir="ltr">
        <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">
                <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 style="" color="#000000"><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! <br>
          </div>
        </div>
      </div>
    </blockquote>
    <p>I think your problem is that the segment you are creating has no
      NULL terminator in the end?</p>
    <p>E.g. you take a Java string, get its byte array, and turn the
      byte array into a segment.</p>
    <p>To work with string safely, I suggest you use String-accepting
      allocation/accessor methods. Either Arena::allocateFrom(String),
      or MemorySegment::setString. Those will add the required
      terminator.</p>
    <p></p>
    <p>I think even your first example looks incorrect (where you use
      `allocateFrom(JAVA_BYTE, text.getBytes()`), but you are probably
      saved there by the fact that malloc allocated a bigger chunk of
      memory and a zero just happens to be at the end of the string
      bytes?</p>
    <p>You can't pass the byte array of a Java string to a C/C++
      function expecting a null-terminated string w/o performing some
      sort of copy and adding the required trailing terminator. Some
      C/C++ APIs might work with unterminated strings, in which case
      they will probably accept a size - e.g. how many characters are
      expected in the char*. But this doesn't seem to be the case here.</p>
    <p>Hope this helps<br>
      Maurizio<br>
      <br>
    </p>
    <br>
    <p></p>
    <p><br>
    </p>
  </body>
</html>