View Javadoc
1   /*
2    * Copyright 2024 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.uring;
17  
18  import java.nio.ByteBuffer;
19  
20  /**
21   * <pre>{@code
22   * struct iovec {
23   *     void  *iov_base;    // Starting address
24   *     size_t iov_len;     // Number of bytes to transfer
25   * };
26   * }</pre>
27   */
28  final class Iov {
29  
30      private Iov() { }
31  
32      static void set(ByteBuffer buffer, long bufferAddress, int length) {
33          int position = buffer.position();
34          if (Native.SIZEOF_SIZE_T == 4) {
35              buffer.putInt(position + Native.IOVEC_OFFSETOF_IOV_BASE, (int) bufferAddress);
36              buffer.putInt(position + Native.IOVEC_OFFSETOF_IOV_LEN, length);
37          } else {
38              assert Native.SIZEOF_SIZE_T == 8;
39              buffer.putLong(position + Native.IOVEC_OFFSETOF_IOV_BASE, bufferAddress);
40              buffer.putLong(position + Native.IOVEC_OFFSETOF_IOV_LEN, length);
41          }
42      }
43  
44      static long getBufferAddress(ByteBuffer iov) {
45          if (Native.SIZEOF_SIZE_T == 4) {
46              return iov.getInt(iov.position() + Native.IOVEC_OFFSETOF_IOV_BASE);
47          }
48          assert Native.SIZEOF_SIZE_T == 8;
49          return iov.getLong(iov.position() + Native.IOVEC_OFFSETOF_IOV_BASE);
50      }
51  
52      static int getBufferLength(ByteBuffer iov) {
53          if (Native.SIZEOF_SIZE_T == 4) {
54              return iov.getInt(iov.position() + Native.IOVEC_OFFSETOF_IOV_LEN);
55          }
56          assert Native.SIZEOF_SIZE_T == 8;
57          return (int) iov.getLong(iov.position() + Native.IOVEC_OFFSETOF_IOV_LEN);
58      }
59  }